xref: /openbmc/linux/security/selinux/hooks.c (revision 0b1f24e6db9a60c1f68117ad158ea29faa7c3a7f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  NSA Security-Enhanced Linux (SELinux) security module
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This file contains the SELinux hook function implementations.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
71da177e4SLinus Torvalds  *	      Chris Vance, <cvance@nai.com>
81da177e4SLinus Torvalds  *	      Wayne Salamon, <wsalamon@nai.com>
91da177e4SLinus Torvalds  *	      James Morris <jmorris@redhat.com>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
122069f457SEric Paris  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
132069f457SEric Paris  *					   Eric Paris <eparis@redhat.com>
141da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
151da177e4SLinus Torvalds  *			    <dgoeddel@trustedcs.com>
16ed6d76e4SPaul Moore  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
1782c21bfaSPaul Moore  *	Paul Moore <paul@paul-moore.com>
18788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19788e7dd4SYuichi Nakamura  *		       Yuichi Nakamura <ynakam@hitachisoft.jp>
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
221da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
231da177e4SLinus Torvalds  *	as published by the Free Software Foundation.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/init.h>
270b24dcb7SEric Paris #include <linux/kd.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
290d094efeSRoland McGrath #include <linux/tracehook.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
311da177e4SLinus Torvalds #include <linux/sched.h>
321da177e4SLinus Torvalds #include <linux/security.h>
331da177e4SLinus Torvalds #include <linux/xattr.h>
341da177e4SLinus Torvalds #include <linux/capability.h>
351da177e4SLinus Torvalds #include <linux/unistd.h>
361da177e4SLinus Torvalds #include <linux/mm.h>
371da177e4SLinus Torvalds #include <linux/mman.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds #include <linux/pagemap.h>
400b24dcb7SEric Paris #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/swap.h>
421da177e4SLinus Torvalds #include <linux/spinlock.h>
431da177e4SLinus Torvalds #include <linux/syscalls.h>
442a7dba39SEric Paris #include <linux/dcache.h>
451da177e4SLinus Torvalds #include <linux/file.h>
469f3acc31SAl Viro #include <linux/fdtable.h>
471da177e4SLinus Torvalds #include <linux/namei.h>
481da177e4SLinus Torvalds #include <linux/mount.h>
491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
501da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
511da177e4SLinus Torvalds #include <linux/tty.h>
521da177e4SLinus Torvalds #include <net/icmp.h>
53227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
54ca10b9e9SEric Dumazet #include <net/sock.h>
551da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
56da2ea0d0SPaul Moore #include <net/inet_connection_sock.h>
57220deb96SPaul Moore #include <net/net_namespace.h>
58d621d35eSPaul Moore #include <net/netlabel.h>
59f5269710SEric Paris #include <linux/uaccess.h>
601da177e4SLinus Torvalds #include <asm/ioctls.h>
6160063497SArun Sharma #include <linux/atomic.h>
621da177e4SLinus Torvalds #include <linux/bitops.h>
631da177e4SLinus Torvalds #include <linux/interrupt.h>
641da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6577954983SHong zhi guo #include <net/netlink.h>
661da177e4SLinus Torvalds #include <linux/tcp.h>
671da177e4SLinus Torvalds #include <linux/udp.h>
682ee92d46SJames Morris #include <linux/dccp.h>
691da177e4SLinus Torvalds #include <linux/quota.h>
701da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
711da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
721da177e4SLinus Torvalds #include <linux/parser.h>
731da177e4SLinus Torvalds #include <linux/nfs_mount.h>
741da177e4SLinus Torvalds #include <net/ipv6.h>
751da177e4SLinus Torvalds #include <linux/hugetlb.h>
761da177e4SLinus Torvalds #include <linux/personality.h>
771da177e4SLinus Torvalds #include <linux/audit.h>
786931dfc9SEric Paris #include <linux/string.h>
79877ce7c1SCatherine Zhang #include <linux/selinux.h>
8023970741SEric Paris #include <linux/mutex.h>
81f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8200234592SKees Cook #include <linux/syslog.h>
833486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8444fc7ea0SPaul Gortmaker #include <linux/export.h>
85eb9ae686SDavid Quigley #include <linux/security.h>
8640401530SAl Viro #include <linux/msg.h>
8740401530SAl Viro #include <linux/shm.h>
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #include "avc.h"
901da177e4SLinus Torvalds #include "objsec.h"
911da177e4SLinus Torvalds #include "netif.h"
92224dfbd8SPaul Moore #include "netnode.h"
933e112172SPaul Moore #include "netport.h"
94d28d1e08STrent Jaeger #include "xfrm.h"
95c60475bfSPaul Moore #include "netlabel.h"
969d57a7f9SAhmed S. Darwish #include "audit.h"
977b98a585SJames Morris #include "avc_ss.h"
981da177e4SLinus Torvalds 
99102aefddSAnand Avati #define SB_TYPE_FMT "%s%s%s"
100102aefddSAnand Avati #define SB_SUBTYPE(sb) (sb->s_subtype && sb->s_subtype[0])
101102aefddSAnand Avati #define SB_TYPE_ARGS(sb) sb->s_type->name, SB_SUBTYPE(sb) ? "." : "", SB_SUBTYPE(sb) ? sb->s_subtype : ""
102c9180a57SEric Paris 
10320510f2fSJames Morris extern struct security_operations *security_ops;
1041da177e4SLinus Torvalds 
105d621d35eSPaul Moore /* SECMARK reference count */
10656a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
107d621d35eSPaul Moore 
1081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
109828dfe1dSEric Paris int selinux_enforcing;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1121da177e4SLinus Torvalds {
113f5269710SEric Paris 	unsigned long enforcing;
114f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enforcing))
115f5269710SEric Paris 		selinux_enforcing = enforcing ? 1 : 0;
1161da177e4SLinus Torvalds 	return 1;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1191da177e4SLinus Torvalds #endif
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1221da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1251da177e4SLinus Torvalds {
126f5269710SEric Paris 	unsigned long enabled;
127f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enabled))
128f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1291da177e4SLinus Torvalds 	return 1;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
13230d55280SStephen Smalley #else
13330d55280SStephen Smalley int selinux_enabled = 1;
1341da177e4SLinus Torvalds #endif
1351da177e4SLinus Torvalds 
136e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1377cae7e26SJames Morris 
138d621d35eSPaul Moore /**
139d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
140d621d35eSPaul Moore  *
141d621d35eSPaul Moore  * Description:
142d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
143d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
144d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1452be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1462be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
147d621d35eSPaul Moore  *
148d621d35eSPaul Moore  */
149d621d35eSPaul Moore static int selinux_secmark_enabled(void)
150d621d35eSPaul Moore {
1512be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
1522be4d74fSChris PeBenito }
1532be4d74fSChris PeBenito 
1542be4d74fSChris PeBenito /**
1552be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1562be4d74fSChris PeBenito  *
1572be4d74fSChris PeBenito  * Description:
1582be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1592be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1602be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1612be4d74fSChris PeBenito  * is always considered enabled.
1622be4d74fSChris PeBenito  *
1632be4d74fSChris PeBenito  */
1642be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1652be4d74fSChris PeBenito {
1662be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
167d621d35eSPaul Moore }
168d621d35eSPaul Moore 
169d84f4f99SDavid Howells /*
170d84f4f99SDavid Howells  * initialise the security for the init task
171d84f4f99SDavid Howells  */
172d84f4f99SDavid Howells static void cred_init_security(void)
1731da177e4SLinus Torvalds {
1743b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1751da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1761da177e4SLinus Torvalds 
17789d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1781da177e4SLinus Torvalds 	if (!tsec)
179d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1801da177e4SLinus Torvalds 
181d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
182f1752eecSDavid Howells 	cred->security = tsec;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185275bb41eSDavid Howells /*
18688e67f3bSDavid Howells  * get the security ID of a set of credentials
18788e67f3bSDavid Howells  */
18888e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
18988e67f3bSDavid Howells {
19088e67f3bSDavid Howells 	const struct task_security_struct *tsec;
19188e67f3bSDavid Howells 
19288e67f3bSDavid Howells 	tsec = cred->security;
19388e67f3bSDavid Howells 	return tsec->sid;
19488e67f3bSDavid Howells }
19588e67f3bSDavid Howells 
19688e67f3bSDavid Howells /*
1973b11a1deSDavid Howells  * get the objective security ID of a task
198275bb41eSDavid Howells  */
199275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
200275bb41eSDavid Howells {
201275bb41eSDavid Howells 	u32 sid;
202275bb41eSDavid Howells 
203275bb41eSDavid Howells 	rcu_read_lock();
20488e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
205275bb41eSDavid Howells 	rcu_read_unlock();
206275bb41eSDavid Howells 	return sid;
207275bb41eSDavid Howells }
208275bb41eSDavid Howells 
209275bb41eSDavid Howells /*
2103b11a1deSDavid Howells  * get the subjective security ID of the current task
211275bb41eSDavid Howells  */
212275bb41eSDavid Howells static inline u32 current_sid(void)
213275bb41eSDavid Howells {
2145fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
215275bb41eSDavid Howells 
216275bb41eSDavid Howells 	return tsec->sid;
217275bb41eSDavid Howells }
218275bb41eSDavid Howells 
21988e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
22088e67f3bSDavid Howells 
2211da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct inode_security_struct *isec;
224275bb41eSDavid Howells 	u32 sid = current_sid();
2251da177e4SLinus Torvalds 
226a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2271da177e4SLinus Torvalds 	if (!isec)
2281da177e4SLinus Torvalds 		return -ENOMEM;
2291da177e4SLinus Torvalds 
23023970741SEric Paris 	mutex_init(&isec->lock);
2311da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2321da177e4SLinus Torvalds 	isec->inode = inode;
2331da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2341da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
235275bb41eSDavid Howells 	isec->task_sid = sid;
2361da177e4SLinus Torvalds 	inode->i_security = isec;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	return 0;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2441da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2471da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2481da177e4SLinus Torvalds 		list_del_init(&isec->list);
2491da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	inode->i_security = NULL;
2527cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	struct file_security_struct *fsec;
258275bb41eSDavid Howells 	u32 sid = current_sid();
2591da177e4SLinus Torvalds 
26026d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2611da177e4SLinus Torvalds 	if (!fsec)
2621da177e4SLinus Torvalds 		return -ENOMEM;
2631da177e4SLinus Torvalds 
264275bb41eSDavid Howells 	fsec->sid = sid;
265275bb41eSDavid Howells 	fsec->fown_sid = sid;
2661da177e4SLinus Torvalds 	file->f_security = fsec;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	return 0;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds static void file_free_security(struct file *file)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2741da177e4SLinus Torvalds 	file->f_security = NULL;
2751da177e4SLinus Torvalds 	kfree(fsec);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2811da177e4SLinus Torvalds 
28289d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2831da177e4SLinus Torvalds 	if (!sbsec)
2841da177e4SLinus Torvalds 		return -ENOMEM;
2851da177e4SLinus Torvalds 
286bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2871da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2881da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2891da177e4SLinus Torvalds 	sbsec->sb = sb;
2901da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2911da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
292c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2931da177e4SLinus Torvalds 	sb->s_security = sbsec;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3011da177e4SLinus Torvalds 	sb->s_security = NULL;
3021da177e4SLinus Torvalds 	kfree(sbsec);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3061da177e4SLinus Torvalds 
307eb9ae686SDavid Quigley static const char *labeling_behaviors[7] = {
3081da177e4SLinus Torvalds 	"uses xattr",
3091da177e4SLinus Torvalds 	"uses transition SIDs",
3101da177e4SLinus Torvalds 	"uses task SIDs",
3111da177e4SLinus Torvalds 	"uses genfs_contexts",
3121da177e4SLinus Torvalds 	"not configured for labeling",
3131da177e4SLinus Torvalds 	"uses mountpoint labeling",
314eb9ae686SDavid Quigley 	"uses native labeling",
3151da177e4SLinus Torvalds };
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds enum {
32531e87930SEric Paris 	Opt_error = -1,
3261da177e4SLinus Torvalds 	Opt_context = 1,
3271da177e4SLinus Torvalds 	Opt_fscontext = 2,
328c9180a57SEric Paris 	Opt_defcontext = 3,
329c9180a57SEric Paris 	Opt_rootcontext = 4,
33011689d47SDavid P. Quigley 	Opt_labelsupport = 5,
331d355987fSEric Paris 	Opt_nextmntopt = 6,
3321da177e4SLinus Torvalds };
3331da177e4SLinus Torvalds 
334d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
335d355987fSEric Paris 
336a447c093SSteven Whitehouse static const match_table_t tokens = {
337832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
338832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
339832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
340832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
34111689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
34231e87930SEric Paris 	{Opt_error, NULL},
3431da177e4SLinus Torvalds };
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3461da177e4SLinus Torvalds 
347c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
348c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
349275bb41eSDavid Howells 			const struct cred *cred)
350c312feb2SEric Paris {
351275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
352c312feb2SEric Paris 	int rc;
353c312feb2SEric Paris 
354c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
355c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
356c312feb2SEric Paris 	if (rc)
357c312feb2SEric Paris 		return rc;
358c312feb2SEric Paris 
359c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
360c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
361c312feb2SEric Paris 	return rc;
362c312feb2SEric Paris }
363c312feb2SEric Paris 
3640808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3650808925eSEric Paris 			struct superblock_security_struct *sbsec,
366275bb41eSDavid Howells 			const struct cred *cred)
3670808925eSEric Paris {
368275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3690808925eSEric Paris 	int rc;
3700808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3710808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3720808925eSEric Paris 	if (rc)
3730808925eSEric Paris 		return rc;
3740808925eSEric Paris 
3750808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3760808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3770808925eSEric Paris 	return rc;
3780808925eSEric Paris }
3790808925eSEric Paris 
380b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
381b43e725dSEric Paris {
382b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
383b43e725dSEric Paris 
384b43e725dSEric Paris 	if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
385b43e725dSEric Paris 	    sbsec->behavior == SECURITY_FS_USE_TRANS ||
386b43e725dSEric Paris 	    sbsec->behavior == SECURITY_FS_USE_TASK)
387b43e725dSEric Paris 		return 1;
388b43e725dSEric Paris 
389b43e725dSEric Paris 	/* Special handling for sysfs. Is genfs but also has setxattr handler*/
390b43e725dSEric Paris 	if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
391b43e725dSEric Paris 		return 1;
392b43e725dSEric Paris 
393b43e725dSEric Paris 	/*
394b43e725dSEric Paris 	 * Special handling for rootfs. Is genfs but supports
395b43e725dSEric Paris 	 * setting SELinux context on in-core inodes.
396b43e725dSEric Paris 	 */
397b43e725dSEric Paris 	if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
398b43e725dSEric Paris 		return 1;
399b43e725dSEric Paris 
400b43e725dSEric Paris 	return 0;
401b43e725dSEric Paris }
402b43e725dSEric Paris 
403c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4061da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
407c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
4081da177e4SLinus Torvalds 	int rc = 0;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
4111da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
4121da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
4131da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
4141da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
4151da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
416c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
417102aefddSAnand Avati 			printk(KERN_WARNING "SELinux: (dev %s, type "SB_TYPE_FMT") has no "
418102aefddSAnand Avati 			       "xattr support\n", sb->s_id, SB_TYPE_ARGS(sb));
4191da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
4201da177e4SLinus Torvalds 			goto out;
4211da177e4SLinus Torvalds 		}
422c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
4231da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
4241da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
4251da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
426102aefddSAnand Avati 				       SB_TYPE_FMT") has no security xattr handler\n",
427102aefddSAnand Avati 				       sb->s_id, SB_TYPE_ARGS(sb));
4281da177e4SLinus Torvalds 			else
4291da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
430102aefddSAnand Avati 				       SB_TYPE_FMT") getxattr errno %d\n", sb->s_id,
431102aefddSAnand Avati 				       SB_TYPE_ARGS(sb), -rc);
4321da177e4SLinus Torvalds 			goto out;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
436c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
437102aefddSAnand Avati 		printk(KERN_ERR "SELinux: initialized (dev %s, type "SB_TYPE_FMT"), unknown behavior\n",
438102aefddSAnand Avati 		       sb->s_id, SB_TYPE_ARGS(sb));
439c9180a57SEric Paris 	else
440102aefddSAnand Avati 		printk(KERN_DEBUG "SELinux: initialized (dev %s, type "SB_TYPE_FMT"), %s\n",
441102aefddSAnand Avati 		       sb->s_id, SB_TYPE_ARGS(sb),
4421da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
4431da177e4SLinus Torvalds 
444eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
445b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
44612f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
447ddd29ec6SDavid P. Quigley 
4481da177e4SLinus Torvalds 	/* Initialize the root inode. */
449c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4521da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4531da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4541da177e4SLinus Torvalds 	   populates itself. */
4551da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4561da177e4SLinus Torvalds next_inode:
4571da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4581da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4591da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4601da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4611da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
4621da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4631da177e4SLinus Torvalds 		inode = igrab(inode);
4641da177e4SLinus Torvalds 		if (inode) {
4651da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4661da177e4SLinus Torvalds 				inode_doinit(inode);
4671da177e4SLinus Torvalds 			iput(inode);
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4701da177e4SLinus Torvalds 		list_del_init(&isec->list);
4711da177e4SLinus Torvalds 		goto next_inode;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4741da177e4SLinus Torvalds out:
475c9180a57SEric Paris 	return rc;
476c9180a57SEric Paris }
477c9180a57SEric Paris 
478c9180a57SEric Paris /*
479c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
480c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
481c9180a57SEric Paris  * mount options, or whatever.
482c9180a57SEric Paris  */
483c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
484e0007529SEric Paris 				struct security_mnt_opts *opts)
485c9180a57SEric Paris {
486c9180a57SEric Paris 	int rc = 0, i;
487c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
488c9180a57SEric Paris 	char *context = NULL;
489c9180a57SEric Paris 	u32 len;
490c9180a57SEric Paris 	char tmp;
491c9180a57SEric Paris 
492e0007529SEric Paris 	security_init_mnt_opts(opts);
493c9180a57SEric Paris 
4940d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
495c9180a57SEric Paris 		return -EINVAL;
496c9180a57SEric Paris 
497c9180a57SEric Paris 	if (!ss_initialized)
498c9180a57SEric Paris 		return -EINVAL;
499c9180a57SEric Paris 
500af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
501af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
502af8e50ccSEric Paris 
5030d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
504c9180a57SEric Paris 	/* count the number of mount options for this sb */
505af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
506c9180a57SEric Paris 		if (tmp & 0x01)
507e0007529SEric Paris 			opts->num_mnt_opts++;
508c9180a57SEric Paris 		tmp >>= 1;
509c9180a57SEric Paris 	}
51011689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
5110b4bdb35SEric Paris 	if (sbsec->flags & SBLABEL_MNT)
51211689d47SDavid P. Quigley 		opts->num_mnt_opts++;
513c9180a57SEric Paris 
514e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
515e0007529SEric Paris 	if (!opts->mnt_opts) {
516c9180a57SEric Paris 		rc = -ENOMEM;
517c9180a57SEric Paris 		goto out_free;
518c9180a57SEric Paris 	}
519c9180a57SEric Paris 
520e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
521e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
522c9180a57SEric Paris 		rc = -ENOMEM;
523c9180a57SEric Paris 		goto out_free;
524c9180a57SEric Paris 	}
525c9180a57SEric Paris 
526c9180a57SEric Paris 	i = 0;
527c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
528c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
529c9180a57SEric Paris 		if (rc)
530c9180a57SEric Paris 			goto out_free;
531e0007529SEric Paris 		opts->mnt_opts[i] = context;
532e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
533c9180a57SEric Paris 	}
534c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
535c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
536c9180a57SEric Paris 		if (rc)
537c9180a57SEric Paris 			goto out_free;
538e0007529SEric Paris 		opts->mnt_opts[i] = context;
539e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
540c9180a57SEric Paris 	}
541c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
542c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
543c9180a57SEric Paris 		if (rc)
544c9180a57SEric Paris 			goto out_free;
545e0007529SEric Paris 		opts->mnt_opts[i] = context;
546e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
547c9180a57SEric Paris 	}
548c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
549c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
550c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
551c9180a57SEric Paris 
552c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
553c9180a57SEric Paris 		if (rc)
554c9180a57SEric Paris 			goto out_free;
555e0007529SEric Paris 		opts->mnt_opts[i] = context;
556e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
557c9180a57SEric Paris 	}
55812f348b9SEric Paris 	if (sbsec->flags & SBLABEL_MNT) {
55911689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
56012f348b9SEric Paris 		opts->mnt_opts_flags[i++] = SBLABEL_MNT;
56111689d47SDavid P. Quigley 	}
562c9180a57SEric Paris 
563e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
564c9180a57SEric Paris 
565c9180a57SEric Paris 	return 0;
566c9180a57SEric Paris 
567c9180a57SEric Paris out_free:
568e0007529SEric Paris 	security_free_mnt_opts(opts);
569c9180a57SEric Paris 	return rc;
570c9180a57SEric Paris }
571c9180a57SEric Paris 
572c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
573c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
574c9180a57SEric Paris {
5750d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5760d90a7ecSDavid P. Quigley 
577c9180a57SEric Paris 	/* check if the old mount command had the same options */
5780d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
579c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
580c9180a57SEric Paris 		    (old_sid != new_sid))
581c9180a57SEric Paris 			return 1;
582c9180a57SEric Paris 
583c9180a57SEric Paris 	/* check if we were passed the same options twice,
584c9180a57SEric Paris 	 * aka someone passed context=a,context=b
585c9180a57SEric Paris 	 */
5860d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5870d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
588c9180a57SEric Paris 			return 1;
589c9180a57SEric Paris 	return 0;
590c9180a57SEric Paris }
591e0007529SEric Paris 
592c9180a57SEric Paris /*
593c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
594c9180a57SEric Paris  * labeling information.
595c9180a57SEric Paris  */
596e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
597649f6e77SDavid Quigley 				struct security_mnt_opts *opts,
598649f6e77SDavid Quigley 				unsigned long kern_flags,
599649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
600c9180a57SEric Paris {
601275bb41eSDavid Howells 	const struct cred *cred = current_cred();
602c9180a57SEric Paris 	int rc = 0, i;
603c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
604089be43eSJames Morris 	struct inode *inode = sbsec->sb->s_root->d_inode;
605089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
606c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
607c9180a57SEric Paris 	u32 defcontext_sid = 0;
608e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
609e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
610e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
611c9180a57SEric Paris 
612c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
613c9180a57SEric Paris 
614c9180a57SEric Paris 	if (!ss_initialized) {
615c9180a57SEric Paris 		if (!num_opts) {
616c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
617c9180a57SEric Paris 			   after the initial policy is loaded and the security
618c9180a57SEric Paris 			   server is ready to handle calls. */
619c9180a57SEric Paris 			goto out;
620c9180a57SEric Paris 		}
621c9180a57SEric Paris 		rc = -EINVAL;
622744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
623744ba35eSEric Paris 			"before the security server is initialized\n");
624c9180a57SEric Paris 		goto out;
625c9180a57SEric Paris 	}
626649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
627649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
628649f6e77SDavid Quigley 		 * place the results is not allowed */
629649f6e77SDavid Quigley 		rc = -EINVAL;
630649f6e77SDavid Quigley 		goto out;
631649f6e77SDavid Quigley 	}
632c9180a57SEric Paris 
633c9180a57SEric Paris 	/*
634e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
635e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
636e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
637e0007529SEric Paris 	 * we need to skip the double mount verification.
638e0007529SEric Paris 	 *
639e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
640e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
641e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
642e0007529SEric Paris 	 * will be used for both mounts)
643e0007529SEric Paris 	 */
6440d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
645e0007529SEric Paris 	    && (num_opts == 0))
646e0007529SEric Paris 		goto out;
647e0007529SEric Paris 
648e0007529SEric Paris 	/*
649c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
650c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
651c9180a57SEric Paris 	 * than once with different security options.
652c9180a57SEric Paris 	 */
653c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
654c9180a57SEric Paris 		u32 sid;
65511689d47SDavid P. Quigley 
65612f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
65711689d47SDavid P. Quigley 			continue;
658c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
659c9180a57SEric Paris 					     strlen(mount_options[i]), &sid);
660c9180a57SEric Paris 		if (rc) {
661c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
662102aefddSAnand Avati 			       "(%s) failed for (dev %s, type "SB_TYPE_FMT") errno=%d\n",
663102aefddSAnand Avati 			       mount_options[i], sb->s_id, SB_TYPE_ARGS(sb), rc);
664c9180a57SEric Paris 			goto out;
665c9180a57SEric Paris 		}
666c9180a57SEric Paris 		switch (flags[i]) {
667c9180a57SEric Paris 		case FSCONTEXT_MNT:
668c9180a57SEric Paris 			fscontext_sid = sid;
669c9180a57SEric Paris 
670c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
671c9180a57SEric Paris 					fscontext_sid))
672c9180a57SEric Paris 				goto out_double_mount;
673c9180a57SEric Paris 
674c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
675c9180a57SEric Paris 			break;
676c9180a57SEric Paris 		case CONTEXT_MNT:
677c9180a57SEric Paris 			context_sid = sid;
678c9180a57SEric Paris 
679c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
680c9180a57SEric Paris 					context_sid))
681c9180a57SEric Paris 				goto out_double_mount;
682c9180a57SEric Paris 
683c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
684c9180a57SEric Paris 			break;
685c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
686c9180a57SEric Paris 			rootcontext_sid = sid;
687c9180a57SEric Paris 
688c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
689c9180a57SEric Paris 					rootcontext_sid))
690c9180a57SEric Paris 				goto out_double_mount;
691c9180a57SEric Paris 
692c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
693c9180a57SEric Paris 
694c9180a57SEric Paris 			break;
695c9180a57SEric Paris 		case DEFCONTEXT_MNT:
696c9180a57SEric Paris 			defcontext_sid = sid;
697c9180a57SEric Paris 
698c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
699c9180a57SEric Paris 					defcontext_sid))
700c9180a57SEric Paris 				goto out_double_mount;
701c9180a57SEric Paris 
702c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
703c9180a57SEric Paris 
704c9180a57SEric Paris 			break;
705c9180a57SEric Paris 		default:
706c9180a57SEric Paris 			rc = -EINVAL;
707c9180a57SEric Paris 			goto out;
708c9180a57SEric Paris 		}
709c9180a57SEric Paris 	}
710c9180a57SEric Paris 
7110d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
712c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
7130d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
714c9180a57SEric Paris 			goto out_double_mount;
715c9180a57SEric Paris 		rc = 0;
716c9180a57SEric Paris 		goto out;
717c9180a57SEric Paris 	}
718c9180a57SEric Paris 
719089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
7200d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
721c9180a57SEric Paris 
722eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
723eb9ae686SDavid Quigley 		/*
724eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
725eb9ae686SDavid Quigley 		 * filesystem type.
726eb9ae686SDavid Quigley 		 */
727a64c54cfSEric Paris 		rc = security_fs_use(sb);
728c9180a57SEric Paris 		if (rc) {
729eb9ae686SDavid Quigley 			printk(KERN_WARNING
730eb9ae686SDavid Quigley 				"%s: security_fs_use(%s) returned %d\n",
731089be43eSJames Morris 					__func__, sb->s_type->name, rc);
732c9180a57SEric Paris 			goto out;
733c9180a57SEric Paris 		}
734eb9ae686SDavid Quigley 	}
735c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
736c9180a57SEric Paris 	if (fscontext_sid) {
737275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
738c9180a57SEric Paris 		if (rc)
739c9180a57SEric Paris 			goto out;
740c9180a57SEric Paris 
741c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
742c9180a57SEric Paris 	}
743c9180a57SEric Paris 
744c9180a57SEric Paris 	/*
745c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
746c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
747c9180a57SEric Paris 	 * the superblock context if not already set.
748c9180a57SEric Paris 	 */
749eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
750eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
751eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
752eb9ae686SDavid Quigley 	}
753eb9ae686SDavid Quigley 
754c9180a57SEric Paris 	if (context_sid) {
755c9180a57SEric Paris 		if (!fscontext_sid) {
756275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
757275bb41eSDavid Howells 							  cred);
758c9180a57SEric Paris 			if (rc)
759c9180a57SEric Paris 				goto out;
760c9180a57SEric Paris 			sbsec->sid = context_sid;
761c9180a57SEric Paris 		} else {
762275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
763275bb41eSDavid Howells 							     cred);
764c9180a57SEric Paris 			if (rc)
765c9180a57SEric Paris 				goto out;
766c9180a57SEric Paris 		}
767c9180a57SEric Paris 		if (!rootcontext_sid)
768c9180a57SEric Paris 			rootcontext_sid = context_sid;
769c9180a57SEric Paris 
770c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
771c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
772c9180a57SEric Paris 	}
773c9180a57SEric Paris 
774c9180a57SEric Paris 	if (rootcontext_sid) {
775275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
776275bb41eSDavid Howells 						     cred);
777c9180a57SEric Paris 		if (rc)
778c9180a57SEric Paris 			goto out;
779c9180a57SEric Paris 
780c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
781c9180a57SEric Paris 		root_isec->initialized = 1;
782c9180a57SEric Paris 	}
783c9180a57SEric Paris 
784c9180a57SEric Paris 	if (defcontext_sid) {
785eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
786eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
787c9180a57SEric Paris 			rc = -EINVAL;
788c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
789c9180a57SEric Paris 			       "invalid for this filesystem type\n");
790c9180a57SEric Paris 			goto out;
791c9180a57SEric Paris 		}
792c9180a57SEric Paris 
793c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
794c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
795275bb41eSDavid Howells 							     sbsec, cred);
796c9180a57SEric Paris 			if (rc)
797c9180a57SEric Paris 				goto out;
798c9180a57SEric Paris 		}
799c9180a57SEric Paris 
800c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
801c9180a57SEric Paris 	}
802c9180a57SEric Paris 
803c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
804c9180a57SEric Paris out:
805bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8061da177e4SLinus Torvalds 	return rc;
807c9180a57SEric Paris out_double_mount:
808c9180a57SEric Paris 	rc = -EINVAL;
809c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
810102aefddSAnand Avati 	       "security settings for (dev %s, type "SB_TYPE_FMT")\n", sb->s_id,
811102aefddSAnand Avati 	       SB_TYPE_ARGS(sb));
812c9180a57SEric Paris 	goto out;
813c9180a57SEric Paris }
814c9180a57SEric Paris 
815094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
816094f7b69SJeff Layton 				    const struct super_block *newsb)
817094f7b69SJeff Layton {
818094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
819094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
820094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
821094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
822094f7b69SJeff Layton 
823094f7b69SJeff Layton 	if (oldflags != newflags)
824094f7b69SJeff Layton 		goto mismatch;
825094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
826094f7b69SJeff Layton 		goto mismatch;
827094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
828094f7b69SJeff Layton 		goto mismatch;
829094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
830094f7b69SJeff Layton 		goto mismatch;
831094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
832094f7b69SJeff Layton 		struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
833094f7b69SJeff Layton 		struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
834094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
835094f7b69SJeff Layton 			goto mismatch;
836094f7b69SJeff Layton 	}
837094f7b69SJeff Layton 	return 0;
838094f7b69SJeff Layton mismatch:
839094f7b69SJeff Layton 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
840094f7b69SJeff Layton 			    "different security settings for (dev %s, "
841094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
842094f7b69SJeff Layton 	return -EBUSY;
843094f7b69SJeff Layton }
844094f7b69SJeff Layton 
845094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
846c9180a57SEric Paris 					struct super_block *newsb)
847c9180a57SEric Paris {
848c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
849c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
850c9180a57SEric Paris 
851c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
852c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
853c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
854c9180a57SEric Paris 
8550f5e6420SEric Paris 	/*
8560f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
857e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
8580f5e6420SEric Paris 	 */
859e8c26255SAl Viro 	if (!ss_initialized)
860094f7b69SJeff Layton 		return 0;
861c9180a57SEric Paris 
862c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8630d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
864c9180a57SEric Paris 
865094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
8660d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
867094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
8685a552617SEric Paris 
869c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
870c9180a57SEric Paris 
871c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
872c9180a57SEric Paris 
873c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
874c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
875c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
876c9180a57SEric Paris 
877c9180a57SEric Paris 	if (set_context) {
878c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
879c9180a57SEric Paris 
880c9180a57SEric Paris 		if (!set_fscontext)
881c9180a57SEric Paris 			newsbsec->sid = sid;
882c9180a57SEric Paris 		if (!set_rootcontext) {
883c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
884c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
885c9180a57SEric Paris 			newisec->sid = sid;
886c9180a57SEric Paris 		}
887c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
888c9180a57SEric Paris 	}
889c9180a57SEric Paris 	if (set_rootcontext) {
890c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
891c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
892c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
893c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
894c9180a57SEric Paris 
895c9180a57SEric Paris 		newisec->sid = oldisec->sid;
896c9180a57SEric Paris 	}
897c9180a57SEric Paris 
898c9180a57SEric Paris 	sb_finish_set_opts(newsb);
899c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
900094f7b69SJeff Layton 	return 0;
901c9180a57SEric Paris }
902c9180a57SEric Paris 
9032e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
9042e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
905c9180a57SEric Paris {
906e0007529SEric Paris 	char *p;
907c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
908c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
909e0007529SEric Paris 	int rc, num_mnt_opts = 0;
910c9180a57SEric Paris 
911e0007529SEric Paris 	opts->num_mnt_opts = 0;
912c9180a57SEric Paris 
913c9180a57SEric Paris 	/* Standard string-based options. */
914c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
915c9180a57SEric Paris 		int token;
916c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
917c9180a57SEric Paris 
918c9180a57SEric Paris 		if (!*p)
919c9180a57SEric Paris 			continue;
920c9180a57SEric Paris 
921c9180a57SEric Paris 		token = match_token(p, tokens, args);
922c9180a57SEric Paris 
923c9180a57SEric Paris 		switch (token) {
924c9180a57SEric Paris 		case Opt_context:
925c9180a57SEric Paris 			if (context || defcontext) {
926c9180a57SEric Paris 				rc = -EINVAL;
927c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
928c9180a57SEric Paris 				goto out_err;
929c9180a57SEric Paris 			}
930c9180a57SEric Paris 			context = match_strdup(&args[0]);
931c9180a57SEric Paris 			if (!context) {
932c9180a57SEric Paris 				rc = -ENOMEM;
933c9180a57SEric Paris 				goto out_err;
934c9180a57SEric Paris 			}
935c9180a57SEric Paris 			break;
936c9180a57SEric Paris 
937c9180a57SEric Paris 		case Opt_fscontext:
938c9180a57SEric Paris 			if (fscontext) {
939c9180a57SEric Paris 				rc = -EINVAL;
940c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
941c9180a57SEric Paris 				goto out_err;
942c9180a57SEric Paris 			}
943c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
944c9180a57SEric Paris 			if (!fscontext) {
945c9180a57SEric Paris 				rc = -ENOMEM;
946c9180a57SEric Paris 				goto out_err;
947c9180a57SEric Paris 			}
948c9180a57SEric Paris 			break;
949c9180a57SEric Paris 
950c9180a57SEric Paris 		case Opt_rootcontext:
951c9180a57SEric Paris 			if (rootcontext) {
952c9180a57SEric Paris 				rc = -EINVAL;
953c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
954c9180a57SEric Paris 				goto out_err;
955c9180a57SEric Paris 			}
956c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
957c9180a57SEric Paris 			if (!rootcontext) {
958c9180a57SEric Paris 				rc = -ENOMEM;
959c9180a57SEric Paris 				goto out_err;
960c9180a57SEric Paris 			}
961c9180a57SEric Paris 			break;
962c9180a57SEric Paris 
963c9180a57SEric Paris 		case Opt_defcontext:
964c9180a57SEric Paris 			if (context || defcontext) {
965c9180a57SEric Paris 				rc = -EINVAL;
966c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
967c9180a57SEric Paris 				goto out_err;
968c9180a57SEric Paris 			}
969c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
970c9180a57SEric Paris 			if (!defcontext) {
971c9180a57SEric Paris 				rc = -ENOMEM;
972c9180a57SEric Paris 				goto out_err;
973c9180a57SEric Paris 			}
974c9180a57SEric Paris 			break;
97511689d47SDavid P. Quigley 		case Opt_labelsupport:
97611689d47SDavid P. Quigley 			break;
977c9180a57SEric Paris 		default:
978c9180a57SEric Paris 			rc = -EINVAL;
979c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
980c9180a57SEric Paris 			goto out_err;
981c9180a57SEric Paris 
982c9180a57SEric Paris 		}
983c9180a57SEric Paris 	}
984c9180a57SEric Paris 
985e0007529SEric Paris 	rc = -ENOMEM;
986e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
987e0007529SEric Paris 	if (!opts->mnt_opts)
988e0007529SEric Paris 		goto out_err;
989e0007529SEric Paris 
990e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
991e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
992e0007529SEric Paris 		kfree(opts->mnt_opts);
993e0007529SEric Paris 		goto out_err;
994c9180a57SEric Paris 	}
995c9180a57SEric Paris 
996e0007529SEric Paris 	if (fscontext) {
997e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
998e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
999e0007529SEric Paris 	}
1000e0007529SEric Paris 	if (context) {
1001e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
1002e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1003e0007529SEric Paris 	}
1004e0007529SEric Paris 	if (rootcontext) {
1005e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
1006e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1007e0007529SEric Paris 	}
1008e0007529SEric Paris 	if (defcontext) {
1009e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
1010e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1011e0007529SEric Paris 	}
1012e0007529SEric Paris 
1013e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
1014e0007529SEric Paris 	return 0;
1015e0007529SEric Paris 
1016c9180a57SEric Paris out_err:
1017c9180a57SEric Paris 	kfree(context);
1018c9180a57SEric Paris 	kfree(defcontext);
1019c9180a57SEric Paris 	kfree(fscontext);
1020c9180a57SEric Paris 	kfree(rootcontext);
1021c9180a57SEric Paris 	return rc;
10221da177e4SLinus Torvalds }
1023e0007529SEric Paris /*
1024e0007529SEric Paris  * string mount options parsing and call set the sbsec
1025e0007529SEric Paris  */
1026e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
1027e0007529SEric Paris {
1028e0007529SEric Paris 	int rc = 0;
1029e0007529SEric Paris 	char *options = data;
1030e0007529SEric Paris 	struct security_mnt_opts opts;
1031e0007529SEric Paris 
1032e0007529SEric Paris 	security_init_mnt_opts(&opts);
1033e0007529SEric Paris 
1034e0007529SEric Paris 	if (!data)
1035e0007529SEric Paris 		goto out;
1036e0007529SEric Paris 
1037e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1038e0007529SEric Paris 
1039e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1040e0007529SEric Paris 	if (rc)
1041e0007529SEric Paris 		goto out_err;
1042e0007529SEric Paris 
1043e0007529SEric Paris out:
1044649f6e77SDavid Quigley 	rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1045e0007529SEric Paris 
1046e0007529SEric Paris out_err:
1047e0007529SEric Paris 	security_free_mnt_opts(&opts);
1048e0007529SEric Paris 	return rc;
1049e0007529SEric Paris }
10501da177e4SLinus Torvalds 
10513583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
10523583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
10532069f457SEric Paris {
10542069f457SEric Paris 	int i;
10552069f457SEric Paris 	char *prefix;
10562069f457SEric Paris 
10572069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
105811689d47SDavid P. Quigley 		char *has_comma;
105911689d47SDavid P. Quigley 
106011689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
106111689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
106211689d47SDavid P. Quigley 		else
106311689d47SDavid P. Quigley 			has_comma = NULL;
10642069f457SEric Paris 
10652069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
10662069f457SEric Paris 		case CONTEXT_MNT:
10672069f457SEric Paris 			prefix = CONTEXT_STR;
10682069f457SEric Paris 			break;
10692069f457SEric Paris 		case FSCONTEXT_MNT:
10702069f457SEric Paris 			prefix = FSCONTEXT_STR;
10712069f457SEric Paris 			break;
10722069f457SEric Paris 		case ROOTCONTEXT_MNT:
10732069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
10742069f457SEric Paris 			break;
10752069f457SEric Paris 		case DEFCONTEXT_MNT:
10762069f457SEric Paris 			prefix = DEFCONTEXT_STR;
10772069f457SEric Paris 			break;
107812f348b9SEric Paris 		case SBLABEL_MNT:
107911689d47SDavid P. Quigley 			seq_putc(m, ',');
108011689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
108111689d47SDavid P. Quigley 			continue;
10822069f457SEric Paris 		default:
10832069f457SEric Paris 			BUG();
1084a35c6c83SEric Paris 			return;
10852069f457SEric Paris 		};
10862069f457SEric Paris 		/* we need a comma before each option */
10872069f457SEric Paris 		seq_putc(m, ',');
10882069f457SEric Paris 		seq_puts(m, prefix);
10892069f457SEric Paris 		if (has_comma)
10902069f457SEric Paris 			seq_putc(m, '\"');
10912069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
10922069f457SEric Paris 		if (has_comma)
10932069f457SEric Paris 			seq_putc(m, '\"');
10942069f457SEric Paris 	}
10952069f457SEric Paris }
10962069f457SEric Paris 
10972069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10982069f457SEric Paris {
10992069f457SEric Paris 	struct security_mnt_opts opts;
11002069f457SEric Paris 	int rc;
11012069f457SEric Paris 
11022069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1103383795c2SEric Paris 	if (rc) {
1104383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1105383795c2SEric Paris 		if (rc == -EINVAL)
1106383795c2SEric Paris 			rc = 0;
11072069f457SEric Paris 		return rc;
1108383795c2SEric Paris 	}
11092069f457SEric Paris 
11102069f457SEric Paris 	selinux_write_opts(m, &opts);
11112069f457SEric Paris 
11122069f457SEric Paris 	security_free_mnt_opts(&opts);
11132069f457SEric Paris 
11142069f457SEric Paris 	return rc;
11152069f457SEric Paris }
11162069f457SEric Paris 
11171da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11181da177e4SLinus Torvalds {
11191da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11201da177e4SLinus Torvalds 	case S_IFSOCK:
11211da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11221da177e4SLinus Torvalds 	case S_IFLNK:
11231da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11241da177e4SLinus Torvalds 	case S_IFREG:
11251da177e4SLinus Torvalds 		return SECCLASS_FILE;
11261da177e4SLinus Torvalds 	case S_IFBLK:
11271da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11281da177e4SLinus Torvalds 	case S_IFDIR:
11291da177e4SLinus Torvalds 		return SECCLASS_DIR;
11301da177e4SLinus Torvalds 	case S_IFCHR:
11311da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11321da177e4SLinus Torvalds 	case S_IFIFO:
11331da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	return SECCLASS_FILE;
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
114013402580SJames Morris static inline int default_protocol_stream(int protocol)
114113402580SJames Morris {
114213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
114313402580SJames Morris }
114413402580SJames Morris 
114513402580SJames Morris static inline int default_protocol_dgram(int protocol)
114613402580SJames Morris {
114713402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
114813402580SJames Morris }
114913402580SJames Morris 
11501da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	switch (family) {
11531da177e4SLinus Torvalds 	case PF_UNIX:
11541da177e4SLinus Torvalds 		switch (type) {
11551da177e4SLinus Torvalds 		case SOCK_STREAM:
11561da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11571da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11581da177e4SLinus Torvalds 		case SOCK_DGRAM:
11591da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11601da177e4SLinus Torvalds 		}
11611da177e4SLinus Torvalds 		break;
11621da177e4SLinus Torvalds 	case PF_INET:
11631da177e4SLinus Torvalds 	case PF_INET6:
11641da177e4SLinus Torvalds 		switch (type) {
11651da177e4SLinus Torvalds 		case SOCK_STREAM:
116613402580SJames Morris 			if (default_protocol_stream(protocol))
11671da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
116813402580SJames Morris 			else
116913402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11701da177e4SLinus Torvalds 		case SOCK_DGRAM:
117113402580SJames Morris 			if (default_protocol_dgram(protocol))
11721da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
117313402580SJames Morris 			else
117413402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11752ee92d46SJames Morris 		case SOCK_DCCP:
11762ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
117713402580SJames Morris 		default:
11781da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11791da177e4SLinus Torvalds 		}
11801da177e4SLinus Torvalds 		break;
11811da177e4SLinus Torvalds 	case PF_NETLINK:
11821da177e4SLinus Torvalds 		switch (protocol) {
11831da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11841da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11851da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
11861da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
11877f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11881da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11891da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11901da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11911da177e4SLinus Torvalds 		case NETLINK_XFRM:
11921da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11931da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11941da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11951da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11961da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11971da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
11981da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
11991da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12001da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12010c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12020c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12031da177e4SLinus Torvalds 		default:
12041da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds 	case PF_PACKET:
12071da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12081da177e4SLinus Torvalds 	case PF_KEY:
12091da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12103e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12113e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12188e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12191da177e4SLinus Torvalds 				u16 tclass,
12201da177e4SLinus Torvalds 				u32 *sid)
12211da177e4SLinus Torvalds {
12228e6c9693SLucian Adrian Grijincu 	int rc;
12238e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
12261da177e4SLinus Torvalds 	if (!buffer)
12271da177e4SLinus Torvalds 		return -ENOMEM;
12281da177e4SLinus Torvalds 
12298e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
12308e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
12318e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
12328e6c9693SLucian Adrian Grijincu 	else {
12338e6c9693SLucian Adrian Grijincu 		/* each process gets a /proc/PID/ entry. Strip off the
12348e6c9693SLucian Adrian Grijincu 		 * PID part to get a valid selinux labeling.
12358e6c9693SLucian Adrian Grijincu 		 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
12368e6c9693SLucian Adrian Grijincu 		while (path[1] >= '0' && path[1] <= '9') {
12378e6c9693SLucian Adrian Grijincu 			path[1] = '/';
12388e6c9693SLucian Adrian Grijincu 			path++;
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 		rc = security_genfs_sid("proc", path, tclass, sid);
12418e6c9693SLucian Adrian Grijincu 	}
12421da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
12431da177e4SLinus Torvalds 	return rc;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds #else
12468e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12471da177e4SLinus Torvalds 				u16 tclass,
12481da177e4SLinus Torvalds 				u32 *sid)
12491da177e4SLinus Torvalds {
12501da177e4SLinus Torvalds 	return -EINVAL;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds #endif
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
12551da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
12581da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12591da177e4SLinus Torvalds 	u32 sid;
12601da177e4SLinus Torvalds 	struct dentry *dentry;
12611da177e4SLinus Torvalds #define INITCONTEXTLEN 255
12621da177e4SLinus Torvalds 	char *context = NULL;
12631da177e4SLinus Torvalds 	unsigned len = 0;
12641da177e4SLinus Torvalds 	int rc = 0;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (isec->initialized)
12671da177e4SLinus Torvalds 		goto out;
12681da177e4SLinus Torvalds 
126923970741SEric Paris 	mutex_lock(&isec->lock);
12701da177e4SLinus Torvalds 	if (isec->initialized)
127123970741SEric Paris 		goto out_unlock;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
12740d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
12751da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
12761da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
12771da177e4SLinus Torvalds 		   server is ready to handle calls. */
12781da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
12791da177e4SLinus Torvalds 		if (list_empty(&isec->list))
12801da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
12811da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
128223970741SEric Paris 		goto out_unlock;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1286eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1287eb9ae686SDavid Quigley 		break;
12881da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
12891da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
12901da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
12911da177e4SLinus Torvalds 			break;
12921da177e4SLinus Torvalds 		}
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
12951da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
12961da177e4SLinus Torvalds 		if (opt_dentry) {
12971da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
12981da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
12991da177e4SLinus Torvalds 		} else {
13001da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
13011da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 		if (!dentry) {
1304df7f54c0SEric Paris 			/*
1305df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1306df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1307df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1308df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1309df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1310df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1311df7f54c0SEric Paris 			 * be used again by userspace.
1312df7f54c0SEric Paris 			 */
131323970741SEric Paris 			goto out_unlock;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
13174cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
13181da177e4SLinus Torvalds 		if (!context) {
13191da177e4SLinus Torvalds 			rc = -ENOMEM;
13201da177e4SLinus Torvalds 			dput(dentry);
132123970741SEric Paris 			goto out_unlock;
13221da177e4SLinus Torvalds 		}
13234cb912f1SEric Paris 		context[len] = '\0';
13241da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13251da177e4SLinus Torvalds 					   context, len);
13261da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1327314dabb8SJames Morris 			kfree(context);
1328314dabb8SJames Morris 
13291da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
13301da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13311da177e4SLinus Torvalds 						   NULL, 0);
13321da177e4SLinus Torvalds 			if (rc < 0) {
13331da177e4SLinus Torvalds 				dput(dentry);
133423970741SEric Paris 				goto out_unlock;
13351da177e4SLinus Torvalds 			}
13361da177e4SLinus Torvalds 			len = rc;
13374cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
13381da177e4SLinus Torvalds 			if (!context) {
13391da177e4SLinus Torvalds 				rc = -ENOMEM;
13401da177e4SLinus Torvalds 				dput(dentry);
134123970741SEric Paris 				goto out_unlock;
13421da177e4SLinus Torvalds 			}
13434cb912f1SEric Paris 			context[len] = '\0';
13441da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
13451da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
13461da177e4SLinus Torvalds 						   context, len);
13471da177e4SLinus Torvalds 		}
13481da177e4SLinus Torvalds 		dput(dentry);
13491da177e4SLinus Torvalds 		if (rc < 0) {
13501da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1351744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1352dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
13531da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
13541da177e4SLinus Torvalds 				kfree(context);
135523970741SEric Paris 				goto out_unlock;
13561da177e4SLinus Torvalds 			}
13571da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
13581da177e4SLinus Torvalds 			sid = sbsec->def_sid;
13591da177e4SLinus Torvalds 			rc = 0;
13601da177e4SLinus Torvalds 		} else {
1361f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1362869ab514SStephen Smalley 							     sbsec->def_sid,
1363869ab514SStephen Smalley 							     GFP_NOFS);
13641da177e4SLinus Torvalds 			if (rc) {
13654ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
13664ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
13674ba0a8adSEric Paris 
13684ba0a8adSEric Paris 				if (rc == -EINVAL) {
13694ba0a8adSEric Paris 					if (printk_ratelimit())
13704ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
13714ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
13724ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
13734ba0a8adSEric Paris 				} else {
1374744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
13751da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
13764ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
13774ba0a8adSEric Paris 				}
13781da177e4SLinus Torvalds 				kfree(context);
13791da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
13801da177e4SLinus Torvalds 				rc = 0;
13811da177e4SLinus Torvalds 				break;
13821da177e4SLinus Torvalds 			}
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 		kfree(context);
13851da177e4SLinus Torvalds 		isec->sid = sid;
13861da177e4SLinus Torvalds 		break;
13871da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
13881da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
13891da177e4SLinus Torvalds 		break;
13901da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
13911da177e4SLinus Torvalds 		/* Default to the fs SID. */
13921da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
13951da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
1396652bb9b0SEric Paris 		rc = security_transition_sid(isec->task_sid, sbsec->sid,
1397652bb9b0SEric Paris 					     isec->sclass, NULL, &sid);
13981da177e4SLinus Torvalds 		if (rc)
139923970741SEric Paris 			goto out_unlock;
14001da177e4SLinus Torvalds 		isec->sid = sid;
14011da177e4SLinus Torvalds 		break;
1402c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1403c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1404c312feb2SEric Paris 		break;
14051da177e4SLinus Torvalds 	default:
1406c312feb2SEric Paris 		/* Default to the fs superblock SID. */
14071da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
14081da177e4SLinus Torvalds 
14090d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
14108e6c9693SLucian Adrian Grijincu 			if (opt_dentry) {
14111da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
14128e6c9693SLucian Adrian Grijincu 				rc = selinux_proc_get_sid(opt_dentry,
14131da177e4SLinus Torvalds 							  isec->sclass,
14141da177e4SLinus Torvalds 							  &sid);
14151da177e4SLinus Torvalds 				if (rc)
141623970741SEric Paris 					goto out_unlock;
14171da177e4SLinus Torvalds 				isec->sid = sid;
14181da177e4SLinus Torvalds 			}
14191da177e4SLinus Torvalds 		}
14201da177e4SLinus Torvalds 		break;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	isec->initialized = 1;
14241da177e4SLinus Torvalds 
142523970741SEric Paris out_unlock:
142623970741SEric Paris 	mutex_unlock(&isec->lock);
14271da177e4SLinus Torvalds out:
14281da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
14291da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
14301da177e4SLinus Torvalds 	return rc;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
14341da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	u32 perm = 0;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	switch (sig) {
14391da177e4SLinus Torvalds 	case SIGCHLD:
14401da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
14411da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
14421da177e4SLinus Torvalds 		break;
14431da177e4SLinus Torvalds 	case SIGKILL:
14441da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14451da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
14461da177e4SLinus Torvalds 		break;
14471da177e4SLinus Torvalds 	case SIGSTOP:
14481da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14491da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
14501da177e4SLinus Torvalds 		break;
14511da177e4SLinus Torvalds 	default:
14521da177e4SLinus Torvalds 		/* All other signals. */
14531da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
14541da177e4SLinus Torvalds 		break;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	return perm;
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
1460275bb41eSDavid Howells /*
1461d84f4f99SDavid Howells  * Check permission between a pair of credentials
1462d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1463d84f4f99SDavid Howells  */
1464d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1465d84f4f99SDavid Howells 			 const struct cred *target,
1466d84f4f99SDavid Howells 			 u32 perms)
1467d84f4f99SDavid Howells {
1468d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1469d84f4f99SDavid Howells 
1470d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1471d84f4f99SDavid Howells }
1472d84f4f99SDavid Howells 
1473d84f4f99SDavid Howells /*
147488e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1475275bb41eSDavid Howells  * fork check, ptrace check, etc.
1476275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
14773b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1478275bb41eSDavid Howells  */
1479275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1480275bb41eSDavid Howells 			 const struct task_struct *tsk2,
14811da177e4SLinus Torvalds 			 u32 perms)
14821da177e4SLinus Torvalds {
1483275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1484275bb41eSDavid Howells 	u32 sid1, sid2;
14851da177e4SLinus Torvalds 
1486275bb41eSDavid Howells 	rcu_read_lock();
1487275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1488275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1489275bb41eSDavid Howells 	rcu_read_unlock();
1490275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
14933b11a1deSDavid Howells /*
14943b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
14953b11a1deSDavid Howells  * fork check, ptrace check, etc.
14963b11a1deSDavid Howells  * current is the actor and tsk2 is the target
14973b11a1deSDavid Howells  * - this uses current's subjective creds
14983b11a1deSDavid Howells  */
14993b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
15003b11a1deSDavid Howells 			    u32 perms)
15013b11a1deSDavid Howells {
15023b11a1deSDavid Howells 	u32 sid, tsid;
15033b11a1deSDavid Howells 
15043b11a1deSDavid Howells 	sid = current_sid();
15053b11a1deSDavid Howells 	tsid = task_sid(tsk);
15063b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
15073b11a1deSDavid Howells }
15083b11a1deSDavid Howells 
1509b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1510b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1511b68e418cSStephen Smalley #endif
1512b68e418cSStephen Smalley 
15131da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
15146a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
151506112163SEric Paris 			       int cap, int audit)
15161da177e4SLinus Torvalds {
15172bf49690SThomas Liu 	struct common_audit_data ad;
151806112163SEric Paris 	struct av_decision avd;
1519b68e418cSStephen Smalley 	u16 sclass;
15203699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1521b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
152206112163SEric Paris 	int rc;
15231da177e4SLinus Torvalds 
152450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
15251da177e4SLinus Torvalds 	ad.u.cap = cap;
15261da177e4SLinus Torvalds 
1527b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1528b68e418cSStephen Smalley 	case 0:
1529b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1530b68e418cSStephen Smalley 		break;
1531b68e418cSStephen Smalley 	case 1:
1532b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1533b68e418cSStephen Smalley 		break;
1534b68e418cSStephen Smalley 	default:
1535b68e418cSStephen Smalley 		printk(KERN_ERR
1536b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1537b68e418cSStephen Smalley 		BUG();
1538a35c6c83SEric Paris 		return -EINVAL;
1539b68e418cSStephen Smalley 	}
154006112163SEric Paris 
1541275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
15429ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
1543ab354062SLinus Torvalds 		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
15449ade0cf4SEric Paris 		if (rc2)
15459ade0cf4SEric Paris 			return rc2;
15469ade0cf4SEric Paris 	}
154706112163SEric Paris 	return rc;
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
15511da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
15521da177e4SLinus Torvalds 			   u32 perms)
15531da177e4SLinus Torvalds {
1554275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
15551da177e4SLinus Torvalds 
1556275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
15571da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
15611da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
15621da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
156388e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
15641da177e4SLinus Torvalds 			  struct inode *inode,
15651da177e4SLinus Torvalds 			  u32 perms,
156619e49834SLinus Torvalds 			  struct common_audit_data *adp)
15671da177e4SLinus Torvalds {
15681da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1569275bb41eSDavid Howells 	u32 sid;
15701da177e4SLinus Torvalds 
1571e0e81739SDavid Howells 	validate_creds(cred);
1572e0e81739SDavid Howells 
1573bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1574bbaca6c2SStephen Smalley 		return 0;
1575bbaca6c2SStephen Smalley 
157688e67f3bSDavid Howells 	sid = cred_sid(cred);
15771da177e4SLinus Torvalds 	isec = inode->i_security;
15781da177e4SLinus Torvalds 
157919e49834SLinus Torvalds 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
15801da177e4SLinus Torvalds }
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
15831da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
15841da177e4SLinus Torvalds    pathname if needed. */
158588e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
15861da177e4SLinus Torvalds 				  struct dentry *dentry,
15871da177e4SLinus Torvalds 				  u32 av)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15902bf49690SThomas Liu 	struct common_audit_data ad;
159188e67f3bSDavid Howells 
159250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
15932875fa00SEric Paris 	ad.u.dentry = dentry;
159419e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
15952875fa00SEric Paris }
15962875fa00SEric Paris 
15972875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
15982875fa00SEric Paris    the path to help the auditing code to more easily generate the
15992875fa00SEric Paris    pathname if needed. */
16002875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
16012875fa00SEric Paris 				struct path *path,
16022875fa00SEric Paris 				u32 av)
16032875fa00SEric Paris {
16042875fa00SEric Paris 	struct inode *inode = path->dentry->d_inode;
16052875fa00SEric Paris 	struct common_audit_data ad;
16062875fa00SEric Paris 
160750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
16082875fa00SEric Paris 	ad.u.path = *path;
160919e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
161213f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
161313f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
161413f8e981SDavid Howells 				     struct file *file,
161513f8e981SDavid Howells 				     u32 av)
161613f8e981SDavid Howells {
161713f8e981SDavid Howells 	struct common_audit_data ad;
161813f8e981SDavid Howells 
161913f8e981SDavid Howells 	ad.type = LSM_AUDIT_DATA_PATH;
162013f8e981SDavid Howells 	ad.u.path = file->f_path;
162119e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
162213f8e981SDavid Howells }
162313f8e981SDavid Howells 
16241da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
16251da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
16261da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
16271da177e4SLinus Torvalds    check a particular permission to the file.
16281da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
16291da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
16301da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
16311da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
163288e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
16331da177e4SLinus Torvalds 			 struct file *file,
16341da177e4SLinus Torvalds 			 u32 av)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1637496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16382bf49690SThomas Liu 	struct common_audit_data ad;
163988e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
16401da177e4SLinus Torvalds 	int rc;
16411da177e4SLinus Torvalds 
164250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
1643f48b7399SEric Paris 	ad.u.path = file->f_path;
16441da177e4SLinus Torvalds 
1645275bb41eSDavid Howells 	if (sid != fsec->sid) {
1646275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
16471da177e4SLinus Torvalds 				  SECCLASS_FD,
16481da177e4SLinus Torvalds 				  FD__USE,
16491da177e4SLinus Torvalds 				  &ad);
16501da177e4SLinus Torvalds 		if (rc)
165188e67f3bSDavid Howells 			goto out;
16521da177e4SLinus Torvalds 	}
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
165588e67f3bSDavid Howells 	rc = 0;
16561da177e4SLinus Torvalds 	if (av)
165719e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
16581da177e4SLinus Torvalds 
165988e67f3bSDavid Howells out:
166088e67f3bSDavid Howells 	return rc;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds /* Check whether a task can create a file. */
16641da177e4SLinus Torvalds static int may_create(struct inode *dir,
16651da177e4SLinus Torvalds 		      struct dentry *dentry,
16661da177e4SLinus Torvalds 		      u16 tclass)
16671da177e4SLinus Torvalds {
16685fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
16691da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
16701da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1671275bb41eSDavid Howells 	u32 sid, newsid;
16722bf49690SThomas Liu 	struct common_audit_data ad;
16731da177e4SLinus Torvalds 	int rc;
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	dsec = dir->i_security;
16761da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
16771da177e4SLinus Torvalds 
1678275bb41eSDavid Howells 	sid = tsec->sid;
1679275bb41eSDavid Howells 	newsid = tsec->create_sid;
1680275bb41eSDavid Howells 
168150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1682a269434dSEric Paris 	ad.u.dentry = dentry;
16831da177e4SLinus Torvalds 
1684275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
16851da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
16861da177e4SLinus Torvalds 			  &ad);
16871da177e4SLinus Torvalds 	if (rc)
16881da177e4SLinus Torvalds 		return rc;
16891da177e4SLinus Torvalds 
169012f348b9SEric Paris 	if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
1691cb1e922fSEric Paris 		rc = security_transition_sid(sid, dsec->sid, tclass,
1692cb1e922fSEric Paris 					     &dentry->d_name, &newsid);
16931da177e4SLinus Torvalds 		if (rc)
16941da177e4SLinus Torvalds 			return rc;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 
1697275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
16981da177e4SLinus Torvalds 	if (rc)
16991da177e4SLinus Torvalds 		return rc;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
17021da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
17031da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
17064eb582cfSMichael LeMay /* Check whether a task can create a key. */
17074eb582cfSMichael LeMay static int may_create_key(u32 ksid,
17084eb582cfSMichael LeMay 			  struct task_struct *ctx)
17094eb582cfSMichael LeMay {
1710275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
17114eb582cfSMichael LeMay 
1712275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
17134eb582cfSMichael LeMay }
17144eb582cfSMichael LeMay 
17151da177e4SLinus Torvalds #define MAY_LINK	0
17161da177e4SLinus Torvalds #define MAY_UNLINK	1
17171da177e4SLinus Torvalds #define MAY_RMDIR	2
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
17201da177e4SLinus Torvalds static int may_link(struct inode *dir,
17211da177e4SLinus Torvalds 		    struct dentry *dentry,
17221da177e4SLinus Torvalds 		    int kind)
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds {
17251da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
17262bf49690SThomas Liu 	struct common_audit_data ad;
1727275bb41eSDavid Howells 	u32 sid = current_sid();
17281da177e4SLinus Torvalds 	u32 av;
17291da177e4SLinus Torvalds 	int rc;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	dsec = dir->i_security;
17321da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
17331da177e4SLinus Torvalds 
173450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1735a269434dSEric Paris 	ad.u.dentry = dentry;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	av = DIR__SEARCH;
17381da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1739275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
17401da177e4SLinus Torvalds 	if (rc)
17411da177e4SLinus Torvalds 		return rc;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	switch (kind) {
17441da177e4SLinus Torvalds 	case MAY_LINK:
17451da177e4SLinus Torvalds 		av = FILE__LINK;
17461da177e4SLinus Torvalds 		break;
17471da177e4SLinus Torvalds 	case MAY_UNLINK:
17481da177e4SLinus Torvalds 		av = FILE__UNLINK;
17491da177e4SLinus Torvalds 		break;
17501da177e4SLinus Torvalds 	case MAY_RMDIR:
17511da177e4SLinus Torvalds 		av = DIR__RMDIR;
17521da177e4SLinus Torvalds 		break;
17531da177e4SLinus Torvalds 	default:
1754744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1755744ba35eSEric Paris 			__func__, kind);
17561da177e4SLinus Torvalds 		return 0;
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 
1759275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
17601da177e4SLinus Torvalds 	return rc;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
17641da177e4SLinus Torvalds 			     struct dentry *old_dentry,
17651da177e4SLinus Torvalds 			     struct inode *new_dir,
17661da177e4SLinus Torvalds 			     struct dentry *new_dentry)
17671da177e4SLinus Torvalds {
17681da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
17692bf49690SThomas Liu 	struct common_audit_data ad;
1770275bb41eSDavid Howells 	u32 sid = current_sid();
17711da177e4SLinus Torvalds 	u32 av;
17721da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
17731da177e4SLinus Torvalds 	int rc;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
17761da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
17771da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
17781da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
17791da177e4SLinus Torvalds 
178050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
17811da177e4SLinus Torvalds 
1782a269434dSEric Paris 	ad.u.dentry = old_dentry;
1783275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
17841da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
17851da177e4SLinus Torvalds 	if (rc)
17861da177e4SLinus Torvalds 		return rc;
1787275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
17881da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
17891da177e4SLinus Torvalds 	if (rc)
17901da177e4SLinus Torvalds 		return rc;
17911da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1792275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
17931da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
17941da177e4SLinus Torvalds 		if (rc)
17951da177e4SLinus Torvalds 			return rc;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
1798a269434dSEric Paris 	ad.u.dentry = new_dentry;
17991da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
18001da177e4SLinus Torvalds 	if (new_dentry->d_inode)
18011da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1802275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
18031da177e4SLinus Torvalds 	if (rc)
18041da177e4SLinus Torvalds 		return rc;
18051da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
18061da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
18071da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1808275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
18091da177e4SLinus Torvalds 				  new_isec->sclass,
18101da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
18111da177e4SLinus Torvalds 		if (rc)
18121da177e4SLinus Torvalds 			return rc;
18131da177e4SLinus Torvalds 	}
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	return 0;
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
181988e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
18201da177e4SLinus Torvalds 			       struct super_block *sb,
18211da177e4SLinus Torvalds 			       u32 perms,
18222bf49690SThomas Liu 			       struct common_audit_data *ad)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
182588e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	sbsec = sb->s_security;
1828275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
18321da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
18331da177e4SLinus Torvalds {
18341da177e4SLinus Torvalds 	u32 av = 0;
18351da177e4SLinus Torvalds 
1836dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
18371da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18381da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
18391da177e4SLinus Torvalds 		if (mask & MAY_READ)
18401da177e4SLinus Torvalds 			av |= FILE__READ;
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
18431da177e4SLinus Torvalds 			av |= FILE__APPEND;
18441da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
18451da177e4SLinus Torvalds 			av |= FILE__WRITE;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	} else {
18481da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18491da177e4SLinus Torvalds 			av |= DIR__SEARCH;
18501da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
18511da177e4SLinus Torvalds 			av |= DIR__WRITE;
18521da177e4SLinus Torvalds 		if (mask & MAY_READ)
18531da177e4SLinus Torvalds 			av |= DIR__READ;
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	return av;
18571da177e4SLinus Torvalds }
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
18601da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
18611da177e4SLinus Torvalds {
18621da177e4SLinus Torvalds 	u32 av = 0;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
18651da177e4SLinus Torvalds 		av |= FILE__READ;
18661da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
18671da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
18681da177e4SLinus Torvalds 			av |= FILE__APPEND;
18691da177e4SLinus Torvalds 		else
18701da177e4SLinus Torvalds 			av |= FILE__WRITE;
18711da177e4SLinus Torvalds 	}
18720794c66dSStephen Smalley 	if (!av) {
18730794c66dSStephen Smalley 		/*
18740794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
18750794c66dSStephen Smalley 		 */
18760794c66dSStephen Smalley 		av = FILE__IOCTL;
18770794c66dSStephen Smalley 	}
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	return av;
18801da177e4SLinus Torvalds }
18811da177e4SLinus Torvalds 
18828b6a5a37SEric Paris /*
18838b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
18848b6a5a37SEric Paris  * open permission.
18858b6a5a37SEric Paris  */
18868b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
18878b6a5a37SEric Paris {
18888b6a5a37SEric Paris 	u32 av = file_to_av(file);
18898b6a5a37SEric Paris 
189049b7b8deSEric Paris 	if (selinux_policycap_openperm)
18918b6a5a37SEric Paris 		av |= FILE__OPEN;
189249b7b8deSEric Paris 
18938b6a5a37SEric Paris 	return av;
18948b6a5a37SEric Paris }
18958b6a5a37SEric Paris 
18961da177e4SLinus Torvalds /* Hook functions begin here. */
18971da177e4SLinus Torvalds 
18989e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
1899006ebb40SStephen Smalley 				     unsigned int mode)
19001da177e4SLinus Torvalds {
19011da177e4SLinus Torvalds 	int rc;
19021da177e4SLinus Torvalds 
19039e48858fSIngo Molnar 	rc = cap_ptrace_access_check(child, mode);
19041da177e4SLinus Torvalds 	if (rc)
19051da177e4SLinus Torvalds 		return rc;
19061da177e4SLinus Torvalds 
190769f594a3SEric Paris 	if (mode & PTRACE_MODE_READ) {
1908275bb41eSDavid Howells 		u32 sid = current_sid();
1909275bb41eSDavid Howells 		u32 csid = task_sid(child);
1910275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1911006ebb40SStephen Smalley 	}
1912006ebb40SStephen Smalley 
19133b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
19145cd9c58fSDavid Howells }
19155cd9c58fSDavid Howells 
19165cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
19175cd9c58fSDavid Howells {
19185cd9c58fSDavid Howells 	int rc;
19195cd9c58fSDavid Howells 
1920200ac532SEric Paris 	rc = cap_ptrace_traceme(parent);
19215cd9c58fSDavid Howells 	if (rc)
19225cd9c58fSDavid Howells 		return rc;
19235cd9c58fSDavid Howells 
19245cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
19281da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	int error;
19311da177e4SLinus Torvalds 
19323b11a1deSDavid Howells 	error = current_has_perm(target, PROCESS__GETCAP);
19331da177e4SLinus Torvalds 	if (error)
19341da177e4SLinus Torvalds 		return error;
19351da177e4SLinus Torvalds 
1936200ac532SEric Paris 	return cap_capget(target, effective, inheritable, permitted);
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
1939d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
1940d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
194115a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
194215a2460eSDavid Howells 			  const kernel_cap_t *permitted)
19431da177e4SLinus Torvalds {
19441da177e4SLinus Torvalds 	int error;
19451da177e4SLinus Torvalds 
1946200ac532SEric Paris 	error = cap_capset(new, old,
1947d84f4f99SDavid Howells 				      effective, inheritable, permitted);
19481da177e4SLinus Torvalds 	if (error)
19491da177e4SLinus Torvalds 		return error;
19501da177e4SLinus Torvalds 
1951d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
19545626d3e8SJames Morris /*
19555626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
19565626d3e8SJames Morris  * which was removed).
19575626d3e8SJames Morris  *
19585626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
19595626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
19605626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
19615626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
19625626d3e8SJames Morris  */
19635626d3e8SJames Morris 
19646a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
19656a9de491SEric Paris 			   int cap, int audit)
19661da177e4SLinus Torvalds {
19671da177e4SLinus Torvalds 	int rc;
19681da177e4SLinus Torvalds 
19696a9de491SEric Paris 	rc = cap_capable(cred, ns, cap, audit);
19701da177e4SLinus Torvalds 	if (rc)
19711da177e4SLinus Torvalds 		return rc;
19721da177e4SLinus Torvalds 
19736a9de491SEric Paris 	return cred_has_capability(cred, cap, audit);
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
19771da177e4SLinus Torvalds {
197888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
19791da177e4SLinus Torvalds 	int rc = 0;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	if (!sb)
19821da177e4SLinus Torvalds 		return 0;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	switch (cmds) {
19851da177e4SLinus Torvalds 	case Q_SYNC:
19861da177e4SLinus Torvalds 	case Q_QUOTAON:
19871da177e4SLinus Torvalds 	case Q_QUOTAOFF:
19881da177e4SLinus Torvalds 	case Q_SETINFO:
19891da177e4SLinus Torvalds 	case Q_SETQUOTA:
199088e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
19911da177e4SLinus Torvalds 		break;
19921da177e4SLinus Torvalds 	case Q_GETFMT:
19931da177e4SLinus Torvalds 	case Q_GETINFO:
19941da177e4SLinus Torvalds 	case Q_GETQUOTA:
199588e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
19961da177e4SLinus Torvalds 		break;
19971da177e4SLinus Torvalds 	default:
19981da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
19991da177e4SLinus Torvalds 		break;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds 	return rc;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
20051da177e4SLinus Torvalds {
200688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
200788e67f3bSDavid Howells 
20082875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
201112b3052cSEric Paris static int selinux_syslog(int type)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	int rc;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	switch (type) {
2016d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2017d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
20181da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
20191da177e4SLinus Torvalds 		break;
2020d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2021d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2022d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2023d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
20241da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
20251da177e4SLinus Torvalds 		break;
2026d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLOSE:	/* Close log */
2027d78ca3cdSKees Cook 	case SYSLOG_ACTION_OPEN:	/* Open log */
2028d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ:	/* Read from log */
2029d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_CLEAR:	/* Read/clear last kernel messages */
2030d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLEAR:	/* Clear ring buffer */
20311da177e4SLinus Torvalds 	default:
20321da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
20331da177e4SLinus Torvalds 		break;
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 	return rc;
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds /*
20391da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
20401da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
20411da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
20421da177e4SLinus Torvalds  *
20431da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
20441da177e4SLinus Torvalds  * processes that allocate mappings.
20451da177e4SLinus Torvalds  */
204634b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
20491da177e4SLinus Torvalds 
20506a9de491SEric Paris 	rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
20513699c53cSDavid Howells 			     SECURITY_CAP_NOAUDIT);
20521da177e4SLinus Torvalds 	if (rc == 0)
20531da177e4SLinus Torvalds 		cap_sys_admin = 1;
20541da177e4SLinus Torvalds 
205534b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds /* binprm security operations */
20591da177e4SLinus Torvalds 
2060a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
20611da177e4SLinus Torvalds {
2062a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2063a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
20641da177e4SLinus Torvalds 	struct inode_security_struct *isec;
20652bf49690SThomas Liu 	struct common_audit_data ad;
2066496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
20671da177e4SLinus Torvalds 	int rc;
20681da177e4SLinus Torvalds 
2069200ac532SEric Paris 	rc = cap_bprm_set_creds(bprm);
20701da177e4SLinus Torvalds 	if (rc)
20711da177e4SLinus Torvalds 		return rc;
20721da177e4SLinus Torvalds 
2073a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2074a6f76f23SDavid Howells 	 * the script interpreter */
2075a6f76f23SDavid Howells 	if (bprm->cred_prepared)
20761da177e4SLinus Torvalds 		return 0;
20771da177e4SLinus Torvalds 
2078a6f76f23SDavid Howells 	old_tsec = current_security();
2079a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
20801da177e4SLinus Torvalds 	isec = inode->i_security;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	/* Default to the current task SID. */
2083a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2084a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
20851da177e4SLinus Torvalds 
208628eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2087a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2088a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2089a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
20901da177e4SLinus Torvalds 
2091a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2092a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
20931da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2094a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2095259e5e6cSAndy Lutomirski 
2096259e5e6cSAndy Lutomirski 		/*
2097259e5e6cSAndy Lutomirski 		 * Minimize confusion: if no_new_privs and a transition is
2098259e5e6cSAndy Lutomirski 		 * explicitly requested, then fail the exec.
2099259e5e6cSAndy Lutomirski 		 */
2100259e5e6cSAndy Lutomirski 		if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2101259e5e6cSAndy Lutomirski 			return -EPERM;
21021da177e4SLinus Torvalds 	} else {
21031da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2104a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2105652bb9b0SEric Paris 					     SECCLASS_PROCESS, NULL,
2106652bb9b0SEric Paris 					     &new_tsec->sid);
21071da177e4SLinus Torvalds 		if (rc)
21081da177e4SLinus Torvalds 			return rc;
21091da177e4SLinus Torvalds 	}
21101da177e4SLinus Torvalds 
211150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
2112f48b7399SEric Paris 	ad.u.path = bprm->file->f_path;
21131da177e4SLinus Torvalds 
2114259e5e6cSAndy Lutomirski 	if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2115259e5e6cSAndy Lutomirski 	    (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
2116a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->sid;
21171da177e4SLinus Torvalds 
2118a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2119a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
21201da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
21211da177e4SLinus Torvalds 		if (rc)
21221da177e4SLinus Torvalds 			return rc;
21231da177e4SLinus Torvalds 	} else {
21241da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2125a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
21261da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
21271da177e4SLinus Torvalds 		if (rc)
21281da177e4SLinus Torvalds 			return rc;
21291da177e4SLinus Torvalds 
2130a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
21311da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
21321da177e4SLinus Torvalds 		if (rc)
21331da177e4SLinus Torvalds 			return rc;
21341da177e4SLinus Torvalds 
2135a6f76f23SDavid Howells 		/* Check for shared state */
2136a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2137a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2138a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2139a6f76f23SDavid Howells 					  NULL);
2140a6f76f23SDavid Howells 			if (rc)
2141a6f76f23SDavid Howells 				return -EPERM;
21421da177e4SLinus Torvalds 		}
21431da177e4SLinus Torvalds 
2144a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2145a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2146a6f76f23SDavid Howells 		if (bprm->unsafe &
2147a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2148a6f76f23SDavid Howells 			struct task_struct *tracer;
2149a6f76f23SDavid Howells 			struct task_security_struct *sec;
2150a6f76f23SDavid Howells 			u32 ptsid = 0;
2151a6f76f23SDavid Howells 
2152a6f76f23SDavid Howells 			rcu_read_lock();
215306d98473STejun Heo 			tracer = ptrace_parent(current);
2154a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2155a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2156a6f76f23SDavid Howells 				ptsid = sec->sid;
2157a6f76f23SDavid Howells 			}
2158a6f76f23SDavid Howells 			rcu_read_unlock();
2159a6f76f23SDavid Howells 
2160a6f76f23SDavid Howells 			if (ptsid != 0) {
2161a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2162a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2163a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2164a6f76f23SDavid Howells 				if (rc)
2165a6f76f23SDavid Howells 					return -EPERM;
2166a6f76f23SDavid Howells 			}
2167a6f76f23SDavid Howells 		}
2168a6f76f23SDavid Howells 
2169a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2170a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2171a6f76f23SDavid Howells 	}
2172a6f76f23SDavid Howells 
21731da177e4SLinus Torvalds 	return 0;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
21771da177e4SLinus Torvalds {
21785fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
2179275bb41eSDavid Howells 	u32 sid, osid;
21801da177e4SLinus Torvalds 	int atsecure = 0;
21811da177e4SLinus Torvalds 
2182275bb41eSDavid Howells 	sid = tsec->sid;
2183275bb41eSDavid Howells 	osid = tsec->osid;
2184275bb41eSDavid Howells 
2185275bb41eSDavid Howells 	if (osid != sid) {
21861da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
21871da177e4SLinus Torvalds 		   the noatsecure permission is granted between
21881da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2189275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
21901da177e4SLinus Torvalds 					SECCLASS_PROCESS,
21911da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
21921da177e4SLinus Torvalds 	}
21931da177e4SLinus Torvalds 
2194200ac532SEric Paris 	return (atsecure || cap_bprm_secureexec(bprm));
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
2197c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2198c3c073f8SAl Viro {
2199c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2200c3c073f8SAl Viro }
2201c3c073f8SAl Viro 
22021da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2203745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2204745ca247SDavid Howells 					    struct files_struct *files)
22051da177e4SLinus Torvalds {
22061da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2207b20c8122SStephen Smalley 	struct tty_struct *tty;
220824ec839cSPeter Zijlstra 	int drop_tty = 0;
2209c3c073f8SAl Viro 	unsigned n;
22101da177e4SLinus Torvalds 
221124ec839cSPeter Zijlstra 	tty = get_current_tty();
22121da177e4SLinus Torvalds 	if (tty) {
2213ee2ffa0dSNick Piggin 		spin_lock(&tty_files_lock);
221437dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2215d996b62aSNick Piggin 			struct tty_file_private *file_priv;
221637dd0bd0SEric Paris 
22171da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
221813f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
221913f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
222013f8e981SDavid Howells 			   open file may belong to another process and we are
222113f8e981SDavid Howells 			   only interested in the inode-based check here. */
2222d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2223d996b62aSNick Piggin 						struct tty_file_private, list);
2224d996b62aSNick Piggin 			file = file_priv->file;
222513f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
222624ec839cSPeter Zijlstra 				drop_tty = 1;
22271da177e4SLinus Torvalds 		}
2228ee2ffa0dSNick Piggin 		spin_unlock(&tty_files_lock);
2229452a00d2SAlan Cox 		tty_kref_put(tty);
22301da177e4SLinus Torvalds 	}
223198a27ba4SEric W. Biederman 	/* Reset controlling tty. */
223298a27ba4SEric W. Biederman 	if (drop_tty)
223398a27ba4SEric W. Biederman 		no_tty();
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2236c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2237c3c073f8SAl Viro 	if (!n) /* none found? */
2238c3c073f8SAl Viro 		return;
22391da177e4SLinus Torvalds 
2240c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
224145525b26SAl Viro 	if (IS_ERR(devnull))
224245525b26SAl Viro 		devnull = NULL;
2243c3c073f8SAl Viro 	/* replace all the matching ones with this */
2244c3c073f8SAl Viro 	do {
224545525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2246c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
224745525b26SAl Viro 	if (devnull)
2248c3c073f8SAl Viro 		fput(devnull);
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds /*
2252a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
22531da177e4SLinus Torvalds  */
2254a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
22551da177e4SLinus Torvalds {
2256a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
22571da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
22581da177e4SLinus Torvalds 	int rc, i;
22591da177e4SLinus Torvalds 
2260a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2261a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
22621da177e4SLinus Torvalds 		return;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2265a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
22661da177e4SLinus Torvalds 
2267a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2268a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2269a6f76f23SDavid Howells 
2270a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2271a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2272a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2273a6f76f23SDavid Howells 	 *
2274a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2275a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2276a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2277a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2278a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2279a6f76f23SDavid Howells 	 */
2280a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2281a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2282a6f76f23SDavid Howells 	if (rc) {
2283eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2284eb2d55a3SOleg Nesterov 		task_lock(current);
2285a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2286a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2287a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2288a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2289a6f76f23SDavid Howells 		}
2290eb2d55a3SOleg Nesterov 		task_unlock(current);
2291eb2d55a3SOleg Nesterov 		update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2292a6f76f23SDavid Howells 	}
2293a6f76f23SDavid Howells }
2294a6f76f23SDavid Howells 
2295a6f76f23SDavid Howells /*
2296a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2297a6f76f23SDavid Howells  * due to exec
2298a6f76f23SDavid Howells  */
2299a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2300a6f76f23SDavid Howells {
2301a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2302a6f76f23SDavid Howells 	struct itimerval itimer;
2303a6f76f23SDavid Howells 	u32 osid, sid;
2304a6f76f23SDavid Howells 	int rc, i;
2305a6f76f23SDavid Howells 
2306a6f76f23SDavid Howells 	osid = tsec->osid;
2307a6f76f23SDavid Howells 	sid = tsec->sid;
2308a6f76f23SDavid Howells 
2309a6f76f23SDavid Howells 	if (sid == osid)
2310a6f76f23SDavid Howells 		return;
2311a6f76f23SDavid Howells 
2312a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2313a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2314a6f76f23SDavid Howells 	 * flush and unblock signals.
2315a6f76f23SDavid Howells 	 *
2316a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2317a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2318a6f76f23SDavid Howells 	 */
2319a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
23201da177e4SLinus Torvalds 	if (rc) {
23211da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
23221da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
23231da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
23241da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
23253bcac026SDavid Howells 		if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
23263bcac026SDavid Howells 			__flush_signals(current);
23271da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
23281da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
23293bcac026SDavid Howells 		}
23301da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
23311da177e4SLinus Torvalds 	}
23321da177e4SLinus Torvalds 
2333a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2334a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2335ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
23360b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2337ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds /* superblock security operations */
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
23481da177e4SLinus Torvalds {
23491da177e4SLinus Torvalds 	superblock_free_security(sb);
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
23531da177e4SLinus Torvalds {
23541da177e4SLinus Torvalds 	if (plen > olen)
23551da177e4SLinus Torvalds 		return 0;
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
23611da177e4SLinus Torvalds {
2362832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2363832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2364832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
236511689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
236611689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
23701da177e4SLinus Torvalds {
23711da177e4SLinus Torvalds 	if (!*first) {
23721da177e4SLinus Torvalds 		**to = ',';
23731da177e4SLinus Torvalds 		*to += 1;
23743528a953SCory Olmo 	} else
23751da177e4SLinus Torvalds 		*first = 0;
23761da177e4SLinus Torvalds 	memcpy(*to, from, len);
23771da177e4SLinus Torvalds 	*to += len;
23781da177e4SLinus Torvalds }
23791da177e4SLinus Torvalds 
23803528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
23813528a953SCory Olmo 				       int len)
23823528a953SCory Olmo {
23833528a953SCory Olmo 	int current_size = 0;
23843528a953SCory Olmo 
23853528a953SCory Olmo 	if (!*first) {
23863528a953SCory Olmo 		**to = '|';
23873528a953SCory Olmo 		*to += 1;
2388828dfe1dSEric Paris 	} else
23893528a953SCory Olmo 		*first = 0;
23903528a953SCory Olmo 
23913528a953SCory Olmo 	while (current_size < len) {
23923528a953SCory Olmo 		if (*from != '"') {
23933528a953SCory Olmo 			**to = *from;
23943528a953SCory Olmo 			*to += 1;
23953528a953SCory Olmo 		}
23963528a953SCory Olmo 		from += 1;
23973528a953SCory Olmo 		current_size += 1;
23983528a953SCory Olmo 	}
23993528a953SCory Olmo }
24003528a953SCory Olmo 
2401e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
24041da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
24051da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
24063528a953SCory Olmo 	int open_quote = 0;
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	in_curr = orig;
24091da177e4SLinus Torvalds 	sec_curr = copy;
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
24121da177e4SLinus Torvalds 	if (!nosec) {
24131da177e4SLinus Torvalds 		rc = -ENOMEM;
24141da177e4SLinus Torvalds 		goto out;
24151da177e4SLinus Torvalds 	}
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	nosec_save = nosec;
24181da177e4SLinus Torvalds 	fnosec = fsec = 1;
24191da177e4SLinus Torvalds 	in_save = in_end = orig;
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	do {
24223528a953SCory Olmo 		if (*in_end == '"')
24233528a953SCory Olmo 			open_quote = !open_quote;
24243528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
24253528a953SCory Olmo 				*in_end == '\0') {
24261da177e4SLinus Torvalds 			int len = in_end - in_curr;
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
24293528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
24301da177e4SLinus Torvalds 			else
24311da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 			in_curr = in_end + 1;
24341da177e4SLinus Torvalds 		}
24351da177e4SLinus Torvalds 	} while (*in_end++);
24361da177e4SLinus Torvalds 
24376931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2438da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
24391da177e4SLinus Torvalds out:
24401da177e4SLinus Torvalds 	return rc;
24411da177e4SLinus Torvalds }
24421da177e4SLinus Torvalds 
2443026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2444026eb167SEric Paris {
2445026eb167SEric Paris 	int rc, i, *flags;
2446026eb167SEric Paris 	struct security_mnt_opts opts;
2447026eb167SEric Paris 	char *secdata, **mount_options;
2448026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2449026eb167SEric Paris 
2450026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2451026eb167SEric Paris 		return 0;
2452026eb167SEric Paris 
2453026eb167SEric Paris 	if (!data)
2454026eb167SEric Paris 		return 0;
2455026eb167SEric Paris 
2456026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2457026eb167SEric Paris 		return 0;
2458026eb167SEric Paris 
2459026eb167SEric Paris 	security_init_mnt_opts(&opts);
2460026eb167SEric Paris 	secdata = alloc_secdata();
2461026eb167SEric Paris 	if (!secdata)
2462026eb167SEric Paris 		return -ENOMEM;
2463026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2464026eb167SEric Paris 	if (rc)
2465026eb167SEric Paris 		goto out_free_secdata;
2466026eb167SEric Paris 
2467026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2468026eb167SEric Paris 	if (rc)
2469026eb167SEric Paris 		goto out_free_secdata;
2470026eb167SEric Paris 
2471026eb167SEric Paris 	mount_options = opts.mnt_opts;
2472026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2473026eb167SEric Paris 
2474026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2475026eb167SEric Paris 		u32 sid;
2476026eb167SEric Paris 		size_t len;
2477026eb167SEric Paris 
247812f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
2479026eb167SEric Paris 			continue;
2480026eb167SEric Paris 		len = strlen(mount_options[i]);
2481026eb167SEric Paris 		rc = security_context_to_sid(mount_options[i], len, &sid);
2482026eb167SEric Paris 		if (rc) {
2483026eb167SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
2484102aefddSAnand Avati 			       "(%s) failed for (dev %s, type "SB_TYPE_FMT") errno=%d\n",
2485102aefddSAnand Avati 			       mount_options[i], sb->s_id, SB_TYPE_ARGS(sb), rc);
2486026eb167SEric Paris 			goto out_free_opts;
2487026eb167SEric Paris 		}
2488026eb167SEric Paris 		rc = -EINVAL;
2489026eb167SEric Paris 		switch (flags[i]) {
2490026eb167SEric Paris 		case FSCONTEXT_MNT:
2491026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2492026eb167SEric Paris 				goto out_bad_option;
2493026eb167SEric Paris 			break;
2494026eb167SEric Paris 		case CONTEXT_MNT:
2495026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2496026eb167SEric Paris 				goto out_bad_option;
2497026eb167SEric Paris 			break;
2498026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2499026eb167SEric Paris 			struct inode_security_struct *root_isec;
2500026eb167SEric Paris 			root_isec = sb->s_root->d_inode->i_security;
2501026eb167SEric Paris 
2502026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2503026eb167SEric Paris 				goto out_bad_option;
2504026eb167SEric Paris 			break;
2505026eb167SEric Paris 		}
2506026eb167SEric Paris 		case DEFCONTEXT_MNT:
2507026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2508026eb167SEric Paris 				goto out_bad_option;
2509026eb167SEric Paris 			break;
2510026eb167SEric Paris 		default:
2511026eb167SEric Paris 			goto out_free_opts;
2512026eb167SEric Paris 		}
2513026eb167SEric Paris 	}
2514026eb167SEric Paris 
2515026eb167SEric Paris 	rc = 0;
2516026eb167SEric Paris out_free_opts:
2517026eb167SEric Paris 	security_free_mnt_opts(&opts);
2518026eb167SEric Paris out_free_secdata:
2519026eb167SEric Paris 	free_secdata(secdata);
2520026eb167SEric Paris 	return rc;
2521026eb167SEric Paris out_bad_option:
2522026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
2523102aefddSAnand Avati 	       "during remount (dev %s, type "SB_TYPE_FMT")\n", sb->s_id,
2524102aefddSAnand Avati 	       SB_TYPE_ARGS(sb));
2525026eb167SEric Paris 	goto out_free_opts;
2526026eb167SEric Paris }
2527026eb167SEric Paris 
252812204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
25291da177e4SLinus Torvalds {
253088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25312bf49690SThomas Liu 	struct common_audit_data ad;
25321da177e4SLinus Torvalds 	int rc;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
25351da177e4SLinus Torvalds 	if (rc)
25361da177e4SLinus Torvalds 		return rc;
25371da177e4SLinus Torvalds 
253874192246SJames Morris 	/* Allow all mounts performed by the kernel */
253974192246SJames Morris 	if (flags & MS_KERNMOUNT)
254074192246SJames Morris 		return 0;
254174192246SJames Morris 
254250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2543a269434dSEric Paris 	ad.u.dentry = sb->s_root;
254488e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
2547726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
25481da177e4SLinus Torvalds {
254988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25502bf49690SThomas Liu 	struct common_audit_data ad;
25511da177e4SLinus Torvalds 
255250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2553a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
255488e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
2557808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
2558b5266eb4SAl Viro 			 struct path *path,
2559808d4e3cSAl Viro 			 const char *type,
25601da177e4SLinus Torvalds 			 unsigned long flags,
25611da177e4SLinus Torvalds 			 void *data)
25621da177e4SLinus Torvalds {
256388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2566d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
25671da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
25681da177e4SLinus Torvalds 	else
25692875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
25701da177e4SLinus Torvalds }
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
25731da177e4SLinus Torvalds {
257488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25751da177e4SLinus Torvalds 
257688e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
25771da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds /* inode security operations */
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
25831da177e4SLinus Torvalds {
25841da177e4SLinus Torvalds 	return inode_alloc_security(inode);
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
25881da177e4SLinus Torvalds {
25891da177e4SLinus Torvalds 	inode_free_security(inode);
25901da177e4SLinus Torvalds }
25911da177e4SLinus Torvalds 
2592d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2593d47be3dfSDavid Quigley 					struct qstr *name, void **ctx,
2594d47be3dfSDavid Quigley 					u32 *ctxlen)
2595d47be3dfSDavid Quigley {
2596d47be3dfSDavid Quigley 	const struct cred *cred = current_cred();
2597d47be3dfSDavid Quigley 	struct task_security_struct *tsec;
2598d47be3dfSDavid Quigley 	struct inode_security_struct *dsec;
2599d47be3dfSDavid Quigley 	struct superblock_security_struct *sbsec;
2600d47be3dfSDavid Quigley 	struct inode *dir = dentry->d_parent->d_inode;
2601d47be3dfSDavid Quigley 	u32 newsid;
2602d47be3dfSDavid Quigley 	int rc;
2603d47be3dfSDavid Quigley 
2604d47be3dfSDavid Quigley 	tsec = cred->security;
2605d47be3dfSDavid Quigley 	dsec = dir->i_security;
2606d47be3dfSDavid Quigley 	sbsec = dir->i_sb->s_security;
2607d47be3dfSDavid Quigley 
2608d47be3dfSDavid Quigley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2609d47be3dfSDavid Quigley 		newsid = tsec->create_sid;
2610d47be3dfSDavid Quigley 	} else {
2611d47be3dfSDavid Quigley 		rc = security_transition_sid(tsec->sid, dsec->sid,
2612d47be3dfSDavid Quigley 					     inode_mode_to_security_class(mode),
2613d47be3dfSDavid Quigley 					     name,
2614d47be3dfSDavid Quigley 					     &newsid);
2615d47be3dfSDavid Quigley 		if (rc) {
2616d47be3dfSDavid Quigley 			printk(KERN_WARNING
2617d47be3dfSDavid Quigley 				"%s: security_transition_sid failed, rc=%d\n",
2618d47be3dfSDavid Quigley 			       __func__, -rc);
2619d47be3dfSDavid Quigley 			return rc;
2620d47be3dfSDavid Quigley 		}
2621d47be3dfSDavid Quigley 	}
2622d47be3dfSDavid Quigley 
2623d47be3dfSDavid Quigley 	return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2624d47be3dfSDavid Quigley }
2625d47be3dfSDavid Quigley 
26265e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
26279548906bSTetsuo Handa 				       const struct qstr *qstr,
26289548906bSTetsuo Handa 				       const char **name,
26292a7dba39SEric Paris 				       void **value, size_t *len)
26305e41ff9eSStephen Smalley {
26315fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
26325e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
26335e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2634275bb41eSDavid Howells 	u32 sid, newsid, clen;
26355e41ff9eSStephen Smalley 	int rc;
26369548906bSTetsuo Handa 	char *context;
26375e41ff9eSStephen Smalley 
26385e41ff9eSStephen Smalley 	dsec = dir->i_security;
26395e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
26405e41ff9eSStephen Smalley 
2641275bb41eSDavid Howells 	sid = tsec->sid;
26425e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2643275bb41eSDavid Howells 
2644415103f9SEric Paris 	if ((sbsec->flags & SE_SBINITIALIZED) &&
2645415103f9SEric Paris 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2646415103f9SEric Paris 		newsid = sbsec->mntpoint_sid;
264712f348b9SEric Paris 	else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
2648275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
26495e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
2650652bb9b0SEric Paris 					     qstr, &newsid);
26515e41ff9eSStephen Smalley 		if (rc) {
26525e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
26535e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
26545e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2655dd6f953aSHarvey Harrison 			       __func__,
26565e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
26575e41ff9eSStephen Smalley 			return rc;
26585e41ff9eSStephen Smalley 		}
26595e41ff9eSStephen Smalley 	}
26605e41ff9eSStephen Smalley 
2661296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
26620d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2663296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2664296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2665296fddf7SEric Paris 		isec->sid = newsid;
2666296fddf7SEric Paris 		isec->initialized = 1;
2667296fddf7SEric Paris 	}
26685e41ff9eSStephen Smalley 
266912f348b9SEric Paris 	if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
267025a74f3bSStephen Smalley 		return -EOPNOTSUPP;
267125a74f3bSStephen Smalley 
26729548906bSTetsuo Handa 	if (name)
26739548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
26745e41ff9eSStephen Smalley 
2675570bc1c2SStephen Smalley 	if (value && len) {
267612b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
26779548906bSTetsuo Handa 		if (rc)
26785e41ff9eSStephen Smalley 			return rc;
26795e41ff9eSStephen Smalley 		*value = context;
2680570bc1c2SStephen Smalley 		*len = clen;
2681570bc1c2SStephen Smalley 	}
26825e41ff9eSStephen Smalley 
26835e41ff9eSStephen Smalley 	return 0;
26845e41ff9eSStephen Smalley }
26855e41ff9eSStephen Smalley 
26864acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
26871da177e4SLinus Torvalds {
26881da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
26891da177e4SLinus Torvalds }
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
26921da177e4SLinus Torvalds {
26931da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
26971da177e4SLinus Torvalds {
26981da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
27021da177e4SLinus Torvalds {
27031da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
270618bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
27071da177e4SLinus Torvalds {
27081da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
27121da177e4SLinus Torvalds {
27131da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
27161a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
27221da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
27231da177e4SLinus Torvalds {
27241da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
27281da177e4SLinus Torvalds {
272988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
273088e67f3bSDavid Howells 
27312875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
27351da177e4SLinus Torvalds {
273688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27371da177e4SLinus Torvalds 
27382875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
2741d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2742d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2743d4cf970dSEric Paris 					   unsigned flags)
2744d4cf970dSEric Paris {
2745d4cf970dSEric Paris 	struct common_audit_data ad;
2746d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2747d4cf970dSEric Paris 	int rc;
2748d4cf970dSEric Paris 
274950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2750d4cf970dSEric Paris 	ad.u.inode = inode;
2751d4cf970dSEric Paris 
2752d4cf970dSEric Paris 	rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2753d4cf970dSEric Paris 			    audited, denied, &ad, flags);
2754d4cf970dSEric Paris 	if (rc)
2755d4cf970dSEric Paris 		return rc;
2756d4cf970dSEric Paris 	return 0;
2757d4cf970dSEric Paris }
2758d4cf970dSEric Paris 
2759e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
27601da177e4SLinus Torvalds {
276188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2762b782e0a6SEric Paris 	u32 perms;
2763b782e0a6SEric Paris 	bool from_access;
2764cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
27652e334057SEric Paris 	struct inode_security_struct *isec;
27662e334057SEric Paris 	u32 sid;
27672e334057SEric Paris 	struct av_decision avd;
27682e334057SEric Paris 	int rc, rc2;
27692e334057SEric Paris 	u32 audited, denied;
27701da177e4SLinus Torvalds 
2771b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2772d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2773d09ca739SEric Paris 
27741da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2775b782e0a6SEric Paris 	if (!mask)
27761da177e4SLinus Torvalds 		return 0;
27771da177e4SLinus Torvalds 
27782e334057SEric Paris 	validate_creds(cred);
2779b782e0a6SEric Paris 
27802e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
27812e334057SEric Paris 		return 0;
2782b782e0a6SEric Paris 
2783b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2784b782e0a6SEric Paris 
27852e334057SEric Paris 	sid = cred_sid(cred);
27862e334057SEric Paris 	isec = inode->i_security;
27872e334057SEric Paris 
27882e334057SEric Paris 	rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
27892e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
27902e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
27912e334057SEric Paris 				     &denied);
27922e334057SEric Paris 	if (likely(!audited))
27932e334057SEric Paris 		return rc;
27942e334057SEric Paris 
2795d4cf970dSEric Paris 	rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
27962e334057SEric Paris 	if (rc2)
27972e334057SEric Paris 		return rc2;
27982e334057SEric Paris 	return rc;
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
28021da177e4SLinus Torvalds {
280388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2804bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
280595dbf739SEric Paris 	__u32 av = FILE__WRITE;
28061da177e4SLinus Torvalds 
2807bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2808bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
2809bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2810bc6a6008SAmerigo Wang 			      ATTR_FORCE);
2811bc6a6008SAmerigo Wang 		if (!ia_valid)
28121da177e4SLinus Torvalds 			return 0;
2813bc6a6008SAmerigo Wang 	}
28141da177e4SLinus Torvalds 
2815bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2816bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
28172875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
28181da177e4SLinus Torvalds 
28193d2195c3SEric Paris 	if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
282095dbf739SEric Paris 		av |= FILE__OPEN;
282195dbf739SEric Paris 
282295dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
28231da177e4SLinus Torvalds }
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
28261da177e4SLinus Torvalds {
282788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
28282875fa00SEric Paris 	struct path path;
282988e67f3bSDavid Howells 
28302875fa00SEric Paris 	path.dentry = dentry;
28312875fa00SEric Paris 	path.mnt = mnt;
28322875fa00SEric Paris 
28332875fa00SEric Paris 	return path_has_perm(cred, &path, FILE__GETATTR);
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
28368f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2837b5376771SSerge E. Hallyn {
283888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
283988e67f3bSDavid Howells 
2840b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2841b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2842b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2843b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2844b5376771SSerge E. Hallyn 				return -EPERM;
2845b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2846b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2847b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2848b5376771SSerge E. Hallyn 			return -EPERM;
2849b5376771SSerge E. Hallyn 		}
2850b5376771SSerge E. Hallyn 	}
2851b5376771SSerge E. Hallyn 
2852b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2853b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
28542875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__SETATTR);
2855b5376771SSerge E. Hallyn }
2856b5376771SSerge E. Hallyn 
28578f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
28588f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
28591da177e4SLinus Torvalds {
28601da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28611da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28621da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
28632bf49690SThomas Liu 	struct common_audit_data ad;
2864275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
28651da177e4SLinus Torvalds 	int rc = 0;
28661da177e4SLinus Torvalds 
2867b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2868b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
287112f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
28721da177e4SLinus Torvalds 		return -EOPNOTSUPP;
28731da177e4SLinus Torvalds 
28742e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
28751da177e4SLinus Torvalds 		return -EPERM;
28761da177e4SLinus Torvalds 
287750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2878a269434dSEric Paris 	ad.u.dentry = dentry;
28791da177e4SLinus Torvalds 
2880275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
28811da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
28821da177e4SLinus Torvalds 	if (rc)
28831da177e4SLinus Torvalds 		return rc;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
288612b29f34SStephen Smalley 	if (rc == -EINVAL) {
2887d6ea83ecSEric Paris 		if (!capable(CAP_MAC_ADMIN)) {
2888d6ea83ecSEric Paris 			struct audit_buffer *ab;
2889d6ea83ecSEric Paris 			size_t audit_size;
2890d6ea83ecSEric Paris 			const char *str;
2891d6ea83ecSEric Paris 
2892d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
2893d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
2894e3fea3f7SAl Viro 			if (value) {
2895d6ea83ecSEric Paris 				str = value;
2896d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
2897d6ea83ecSEric Paris 					audit_size = size - 1;
2898d6ea83ecSEric Paris 				else
2899d6ea83ecSEric Paris 					audit_size = size;
2900e3fea3f7SAl Viro 			} else {
2901e3fea3f7SAl Viro 				str = "";
2902e3fea3f7SAl Viro 				audit_size = 0;
2903e3fea3f7SAl Viro 			}
2904d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2905d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
2906d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
2907d6ea83ecSEric Paris 			audit_log_end(ab);
2908d6ea83ecSEric Paris 
290912b29f34SStephen Smalley 			return rc;
2910d6ea83ecSEric Paris 		}
291112b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
291212b29f34SStephen Smalley 	}
29131da177e4SLinus Torvalds 	if (rc)
29141da177e4SLinus Torvalds 		return rc;
29151da177e4SLinus Torvalds 
2916275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
29171da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
29181da177e4SLinus Torvalds 	if (rc)
29191da177e4SLinus Torvalds 		return rc;
29201da177e4SLinus Torvalds 
2921275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
29221da177e4SLinus Torvalds 					  isec->sclass);
29231da177e4SLinus Torvalds 	if (rc)
29241da177e4SLinus Torvalds 		return rc;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	return avc_has_perm(newsid,
29271da177e4SLinus Torvalds 			    sbsec->sid,
29281da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
29291da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
29301da177e4SLinus Torvalds 			    &ad);
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
29338f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
29348f0cfa52SDavid Howells 					const void *value, size_t size,
29358f0cfa52SDavid Howells 					int flags)
29361da177e4SLinus Torvalds {
29371da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
29381da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29391da177e4SLinus Torvalds 	u32 newsid;
29401da177e4SLinus Torvalds 	int rc;
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
29431da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
29441da177e4SLinus Torvalds 		return;
29451da177e4SLinus Torvalds 	}
29461da177e4SLinus Torvalds 
294712b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
29481da177e4SLinus Torvalds 	if (rc) {
294912b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
295012b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
295112b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
29521da177e4SLinus Torvalds 		return;
29531da177e4SLinus Torvalds 	}
29541da177e4SLinus Torvalds 
2955aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
29561da177e4SLinus Torvalds 	isec->sid = newsid;
2957aa9c2669SDavid Quigley 	isec->initialized = 1;
2958aa9c2669SDavid Quigley 
29591da177e4SLinus Torvalds 	return;
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
29628f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
29631da177e4SLinus Torvalds {
296488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
296588e67f3bSDavid Howells 
29662875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
29701da177e4SLinus Torvalds {
297188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
297288e67f3bSDavid Howells 
29732875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
29741da177e4SLinus Torvalds }
29751da177e4SLinus Torvalds 
29768f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
29771da177e4SLinus Torvalds {
2978b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2979b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
29821da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
29831da177e4SLinus Torvalds 	return -EACCES;
29841da177e4SLinus Torvalds }
29851da177e4SLinus Torvalds 
2986d381d8a9SJames Morris /*
2987abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
2988d381d8a9SJames Morris  *
2989d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2990d381d8a9SJames Morris  */
299142492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
29921da177e4SLinus Torvalds {
299342492594SDavid P. Quigley 	u32 size;
299442492594SDavid P. Quigley 	int error;
299542492594SDavid P. Quigley 	char *context = NULL;
29961da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29971da177e4SLinus Torvalds 
29988c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29998c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
30001da177e4SLinus Torvalds 
3001abc69bb6SStephen Smalley 	/*
3002abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3003abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3004abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3005abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3006abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3007abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3008abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3009abc69bb6SStephen Smalley 	 */
30106a9de491SEric Paris 	error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
30113699c53cSDavid Howells 				SECURITY_CAP_NOAUDIT);
3012abc69bb6SStephen Smalley 	if (!error)
3013abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
3014abc69bb6SStephen Smalley 						      &size);
3015abc69bb6SStephen Smalley 	else
301642492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
301742492594SDavid P. Quigley 	if (error)
301842492594SDavid P. Quigley 		return error;
301942492594SDavid P. Quigley 	error = size;
302042492594SDavid P. Quigley 	if (alloc) {
302142492594SDavid P. Quigley 		*buffer = context;
302242492594SDavid P. Quigley 		goto out_nofree;
302342492594SDavid P. Quigley 	}
302442492594SDavid P. Quigley 	kfree(context);
302542492594SDavid P. Quigley out_nofree:
302642492594SDavid P. Quigley 	return error;
30271da177e4SLinus Torvalds }
30281da177e4SLinus Torvalds 
30291da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
30301da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
30311da177e4SLinus Torvalds {
30321da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
30331da177e4SLinus Torvalds 	u32 newsid;
30341da177e4SLinus Torvalds 	int rc;
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
30371da177e4SLinus Torvalds 		return -EOPNOTSUPP;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	if (!value || !size)
30401da177e4SLinus Torvalds 		return -EACCES;
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 	rc = security_context_to_sid((void *)value, size, &newsid);
30431da177e4SLinus Torvalds 	if (rc)
30441da177e4SLinus Torvalds 		return rc;
30451da177e4SLinus Torvalds 
3046aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
30471da177e4SLinus Torvalds 	isec->sid = newsid;
3048ddd29ec6SDavid P. Quigley 	isec->initialized = 1;
30491da177e4SLinus Torvalds 	return 0;
30501da177e4SLinus Torvalds }
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
30531da177e4SLinus Torvalds {
30541da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
30551da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
30561da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
30571da177e4SLinus Torvalds 	return len;
30581da177e4SLinus Torvalds }
30591da177e4SLinus Torvalds 
3060713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3061713a04aeSAhmed S. Darwish {
3062713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
3063713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3064713a04aeSAhmed S. Darwish }
3065713a04aeSAhmed S. Darwish 
30661da177e4SLinus Torvalds /* file security operations */
30671da177e4SLinus Torvalds 
3068788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
30691da177e4SLinus Torvalds {
307088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3071496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
30741da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
30751da177e4SLinus Torvalds 		mask |= MAY_APPEND;
30761da177e4SLinus Torvalds 
3077389fb800SPaul Moore 	return file_has_perm(cred, file,
30781da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
30791da177e4SLinus Torvalds }
30801da177e4SLinus Torvalds 
3081788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3082788e7dd4SYuichi Nakamura {
3083496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
308420dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
308520dda18bSStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
308620dda18bSStephen Smalley 	u32 sid = current_sid();
308720dda18bSStephen Smalley 
3088389fb800SPaul Moore 	if (!mask)
3089788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3090788e7dd4SYuichi Nakamura 		return 0;
3091788e7dd4SYuichi Nakamura 
309220dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
309320dda18bSStephen Smalley 	    fsec->pseqno == avc_policy_seqno())
309483d49856SEric Paris 		/* No change since file_open check. */
309520dda18bSStephen Smalley 		return 0;
309620dda18bSStephen Smalley 
3097788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3098788e7dd4SYuichi Nakamura }
3099788e7dd4SYuichi Nakamura 
31001da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
31011da177e4SLinus Torvalds {
31021da177e4SLinus Torvalds 	return file_alloc_security(file);
31031da177e4SLinus Torvalds }
31041da177e4SLinus Torvalds 
31051da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
31061da177e4SLinus Torvalds {
31071da177e4SLinus Torvalds 	file_free_security(file);
31081da177e4SLinus Torvalds }
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
31111da177e4SLinus Torvalds 			      unsigned long arg)
31121da177e4SLinus Torvalds {
311388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31140b24dcb7SEric Paris 	int error = 0;
31151da177e4SLinus Torvalds 
31160b24dcb7SEric Paris 	switch (cmd) {
31170b24dcb7SEric Paris 	case FIONREAD:
31180b24dcb7SEric Paris 	/* fall through */
31190b24dcb7SEric Paris 	case FIBMAP:
31200b24dcb7SEric Paris 	/* fall through */
31210b24dcb7SEric Paris 	case FIGETBSZ:
31220b24dcb7SEric Paris 	/* fall through */
31232f99c369SAl Viro 	case FS_IOC_GETFLAGS:
31240b24dcb7SEric Paris 	/* fall through */
31252f99c369SAl Viro 	case FS_IOC_GETVERSION:
31260b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
31270b24dcb7SEric Paris 		break;
31281da177e4SLinus Torvalds 
31292f99c369SAl Viro 	case FS_IOC_SETFLAGS:
31300b24dcb7SEric Paris 	/* fall through */
31312f99c369SAl Viro 	case FS_IOC_SETVERSION:
31320b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
31330b24dcb7SEric Paris 		break;
31340b24dcb7SEric Paris 
31350b24dcb7SEric Paris 	/* sys_ioctl() checks */
31360b24dcb7SEric Paris 	case FIONBIO:
31370b24dcb7SEric Paris 	/* fall through */
31380b24dcb7SEric Paris 	case FIOASYNC:
31390b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
31400b24dcb7SEric Paris 		break;
31410b24dcb7SEric Paris 
31420b24dcb7SEric Paris 	case KDSKBENT:
31430b24dcb7SEric Paris 	case KDSKBSENT:
31446a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
31450b24dcb7SEric Paris 					    SECURITY_CAP_AUDIT);
31460b24dcb7SEric Paris 		break;
31470b24dcb7SEric Paris 
31480b24dcb7SEric Paris 	/* default case assumes that the command will go
31490b24dcb7SEric Paris 	 * to the file's ioctl() function.
31500b24dcb7SEric Paris 	 */
31510b24dcb7SEric Paris 	default:
31520b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__IOCTL);
31530b24dcb7SEric Paris 	}
31540b24dcb7SEric Paris 	return error;
31551da177e4SLinus Torvalds }
31561da177e4SLinus Torvalds 
3157fcaaade1SStephen Smalley static int default_noexec;
3158fcaaade1SStephen Smalley 
31591da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
31601da177e4SLinus Torvalds {
316188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3162d84f4f99SDavid Howells 	int rc = 0;
316388e67f3bSDavid Howells 
3164fcaaade1SStephen Smalley 	if (default_noexec &&
3165fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
31661da177e4SLinus Torvalds 		/*
31671da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
31681da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
31691da177e4SLinus Torvalds 		 * This has an additional check.
31701da177e4SLinus Torvalds 		 */
3171d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
31721da177e4SLinus Torvalds 		if (rc)
3173d84f4f99SDavid Howells 			goto error;
31741da177e4SLinus Torvalds 	}
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 	if (file) {
31771da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
31781da177e4SLinus Torvalds 		u32 av = FILE__READ;
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
31811da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
31821da177e4SLinus Torvalds 			av |= FILE__WRITE;
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
31851da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
31861da177e4SLinus Torvalds 
318788e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
31881da177e4SLinus Torvalds 	}
3189d84f4f99SDavid Howells 
3190d84f4f99SDavid Howells error:
3191d84f4f99SDavid Howells 	return rc;
31921da177e4SLinus Torvalds }
31931da177e4SLinus Torvalds 
3194e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
31951da177e4SLinus Torvalds {
3196ed032189SEric Paris 	int rc = 0;
3197275bb41eSDavid Howells 	u32 sid = current_sid();
31981da177e4SLinus Torvalds 
319984336d1aSEric Paris 	/*
320084336d1aSEric Paris 	 * notice that we are intentionally putting the SELinux check before
320184336d1aSEric Paris 	 * the secondary cap_file_mmap check.  This is such a likely attempt
320284336d1aSEric Paris 	 * at bad behaviour/exploit that we always want to get the AVC, even
320384336d1aSEric Paris 	 * if DAC would have also denied the operation.
320484336d1aSEric Paris 	 */
3205a2551df7SEric Paris 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3206ed032189SEric Paris 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3207ed032189SEric Paris 				  MEMPROTECT__MMAP_ZERO, NULL);
320884336d1aSEric Paris 		if (rc)
320984336d1aSEric Paris 			return rc;
321084336d1aSEric Paris 	}
321184336d1aSEric Paris 
321284336d1aSEric Paris 	/* do DAC check on address space usage */
3213e5467859SAl Viro 	return cap_mmap_addr(addr);
3214e5467859SAl Viro }
32151da177e4SLinus Torvalds 
3216e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3217e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3218e5467859SAl Viro {
32191da177e4SLinus Torvalds 	if (selinux_checkreqprot)
32201da177e4SLinus Torvalds 		prot = reqprot;
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
32231da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
32241da177e4SLinus Torvalds }
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
32271da177e4SLinus Torvalds 				 unsigned long reqprot,
32281da177e4SLinus Torvalds 				 unsigned long prot)
32291da177e4SLinus Torvalds {
323088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 	if (selinux_checkreqprot)
32331da177e4SLinus Torvalds 		prot = reqprot;
32341da177e4SLinus Torvalds 
3235fcaaade1SStephen Smalley 	if (default_noexec &&
3236fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3237d541bbeeSJames Morris 		int rc = 0;
3238db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3239db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3240d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3241db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
32426b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
32436b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
32443b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3245db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3246db4c9641SStephen Smalley 			/*
3247db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3248db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3249db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3250db4c9641SStephen Smalley 			 * modified content.  This typically should only
3251db4c9641SStephen Smalley 			 * occur for text relocations.
3252db4c9641SStephen Smalley 			 */
3253d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3254db4c9641SStephen Smalley 		}
32556b992197SLorenzo Hernandez García-Hierro 		if (rc)
32566b992197SLorenzo Hernandez García-Hierro 			return rc;
32576b992197SLorenzo Hernandez García-Hierro 	}
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
32601da177e4SLinus Torvalds }
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
32631da177e4SLinus Torvalds {
326488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
326588e67f3bSDavid Howells 
326688e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
32701da177e4SLinus Torvalds 			      unsigned long arg)
32711da177e4SLinus Torvalds {
327288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
32731da177e4SLinus Torvalds 	int err = 0;
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds 	switch (cmd) {
32761da177e4SLinus Torvalds 	case F_SETFL:
32771da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
327888e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
32791da177e4SLinus Torvalds 			break;
32801da177e4SLinus Torvalds 		}
32811da177e4SLinus Torvalds 		/* fall through */
32821da177e4SLinus Torvalds 	case F_SETOWN:
32831da177e4SLinus Torvalds 	case F_SETSIG:
32841da177e4SLinus Torvalds 	case F_GETFL:
32851da177e4SLinus Torvalds 	case F_GETOWN:
32861da177e4SLinus Torvalds 	case F_GETSIG:
32871d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
32881da177e4SLinus Torvalds 		/* Just check FD__USE permission */
328988e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
32901da177e4SLinus Torvalds 		break;
32911da177e4SLinus Torvalds 	case F_GETLK:
32921da177e4SLinus Torvalds 	case F_SETLK:
32931da177e4SLinus Torvalds 	case F_SETLKW:
32941da177e4SLinus Torvalds #if BITS_PER_LONG == 32
32951da177e4SLinus Torvalds 	case F_GETLK64:
32961da177e4SLinus Torvalds 	case F_SETLK64:
32971da177e4SLinus Torvalds 	case F_SETLKW64:
32981da177e4SLinus Torvalds #endif
329988e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
33001da177e4SLinus Torvalds 		break;
33011da177e4SLinus Torvalds 	}
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds 	return err;
33041da177e4SLinus Torvalds }
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
33071da177e4SLinus Torvalds {
33081da177e4SLinus Torvalds 	struct file_security_struct *fsec;
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 	fsec = file->f_security;
3311275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 	return 0;
33141da177e4SLinus Torvalds }
33151da177e4SLinus Torvalds 
33161da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
33171da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
33181da177e4SLinus Torvalds {
33191da177e4SLinus Torvalds 	struct file *file;
332065c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
33211da177e4SLinus Torvalds 	u32 perm;
33221da177e4SLinus Torvalds 	struct file_security_struct *fsec;
33231da177e4SLinus Torvalds 
33241da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3325b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
33261da177e4SLinus Torvalds 
33271da177e4SLinus Torvalds 	fsec = file->f_security;
33281da177e4SLinus Torvalds 
33291da177e4SLinus Torvalds 	if (!signum)
33301da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
33311da177e4SLinus Torvalds 	else
33321da177e4SLinus Torvalds 		perm = signal_to_av(signum);
33331da177e4SLinus Torvalds 
3334275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
33351da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
33361da177e4SLinus Torvalds }
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
33391da177e4SLinus Torvalds {
334088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
334188e67f3bSDavid Howells 
334288e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
33431da177e4SLinus Torvalds }
33441da177e4SLinus Torvalds 
334583d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3346788e7dd4SYuichi Nakamura {
3347788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3348788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3349d84f4f99SDavid Howells 
3350788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3351496ad9aaSAl Viro 	isec = file_inode(file)->i_security;
3352788e7dd4SYuichi Nakamura 	/*
3353788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3354788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3355788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3356788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3357788e7dd4SYuichi Nakamura 	 * struct as its SID.
3358788e7dd4SYuichi Nakamura 	 */
3359788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3360788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3361788e7dd4SYuichi Nakamura 	/*
3362788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3363788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3364788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3365788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3366788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3367788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3368788e7dd4SYuichi Nakamura 	 */
336913f8e981SDavid Howells 	return file_path_has_perm(cred, file, open_file_to_av(file));
3370788e7dd4SYuichi Nakamura }
3371788e7dd4SYuichi Nakamura 
33721da177e4SLinus Torvalds /* task security operations */
33731da177e4SLinus Torvalds 
33741da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
33751da177e4SLinus Torvalds {
33763b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
33771da177e4SLinus Torvalds }
33781da177e4SLinus Torvalds 
3379f1752eecSDavid Howells /*
3380ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3381ee18d64cSDavid Howells  */
3382ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3383ee18d64cSDavid Howells {
3384ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3385ee18d64cSDavid Howells 
3386ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3387ee18d64cSDavid Howells 	if (!tsec)
3388ee18d64cSDavid Howells 		return -ENOMEM;
3389ee18d64cSDavid Howells 
3390ee18d64cSDavid Howells 	cred->security = tsec;
3391ee18d64cSDavid Howells 	return 0;
3392ee18d64cSDavid Howells }
3393ee18d64cSDavid Howells 
3394ee18d64cSDavid Howells /*
3395f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3396f1752eecSDavid Howells  */
3397f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
33981da177e4SLinus Torvalds {
3399f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3400e0e81739SDavid Howells 
34012edeaa34STetsuo Handa 	/*
34022edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
34032edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
34042edeaa34STetsuo Handa 	 */
34052edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3406e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3407f1752eecSDavid Howells 	kfree(tsec);
34081da177e4SLinus Torvalds }
34091da177e4SLinus Torvalds 
3410d84f4f99SDavid Howells /*
3411d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3412d84f4f99SDavid Howells  */
3413d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3414d84f4f99SDavid Howells 				gfp_t gfp)
3415d84f4f99SDavid Howells {
3416d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3417d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3418d84f4f99SDavid Howells 
3419d84f4f99SDavid Howells 	old_tsec = old->security;
3420d84f4f99SDavid Howells 
3421d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3422d84f4f99SDavid Howells 	if (!tsec)
3423d84f4f99SDavid Howells 		return -ENOMEM;
3424d84f4f99SDavid Howells 
3425d84f4f99SDavid Howells 	new->security = tsec;
3426d84f4f99SDavid Howells 	return 0;
3427d84f4f99SDavid Howells }
3428d84f4f99SDavid Howells 
3429d84f4f99SDavid Howells /*
3430ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3431ee18d64cSDavid Howells  */
3432ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3433ee18d64cSDavid Howells {
3434ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3435ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3436ee18d64cSDavid Howells 
3437ee18d64cSDavid Howells 	*tsec = *old_tsec;
3438ee18d64cSDavid Howells }
3439ee18d64cSDavid Howells 
3440ee18d64cSDavid Howells /*
34413a3b7ce9SDavid Howells  * set the security data for a kernel service
34423a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
34433a3b7ce9SDavid Howells  */
34443a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
34453a3b7ce9SDavid Howells {
34463a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
34473a3b7ce9SDavid Howells 	u32 sid = current_sid();
34483a3b7ce9SDavid Howells 	int ret;
34493a3b7ce9SDavid Howells 
34503a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
34513a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
34523a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
34533a3b7ce9SDavid Howells 			   NULL);
34543a3b7ce9SDavid Howells 	if (ret == 0) {
34553a3b7ce9SDavid Howells 		tsec->sid = secid;
34563a3b7ce9SDavid Howells 		tsec->create_sid = 0;
34573a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
34583a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
34593a3b7ce9SDavid Howells 	}
34603a3b7ce9SDavid Howells 	return ret;
34613a3b7ce9SDavid Howells }
34623a3b7ce9SDavid Howells 
34633a3b7ce9SDavid Howells /*
34643a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
34653a3b7ce9SDavid Howells  * objective context of the specified inode
34663a3b7ce9SDavid Howells  */
34673a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
34683a3b7ce9SDavid Howells {
34693a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
34703a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
34713a3b7ce9SDavid Howells 	u32 sid = current_sid();
34723a3b7ce9SDavid Howells 	int ret;
34733a3b7ce9SDavid Howells 
34743a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
34753a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
34763a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
34773a3b7ce9SDavid Howells 			   NULL);
34783a3b7ce9SDavid Howells 
34793a3b7ce9SDavid Howells 	if (ret == 0)
34803a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3481ef57471aSDavid Howells 	return ret;
34823a3b7ce9SDavid Howells }
34833a3b7ce9SDavid Howells 
3484dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
348525354c4fSEric Paris {
3486dd8dbf2eSEric Paris 	u32 sid;
3487dd8dbf2eSEric Paris 	struct common_audit_data ad;
3488dd8dbf2eSEric Paris 
3489dd8dbf2eSEric Paris 	sid = task_sid(current);
3490dd8dbf2eSEric Paris 
349150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3492dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3493dd8dbf2eSEric Paris 
3494dd8dbf2eSEric Paris 	return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3495dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
349625354c4fSEric Paris }
349725354c4fSEric Paris 
34981da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
34991da177e4SLinus Torvalds {
35003b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
35041da177e4SLinus Torvalds {
35053b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
35091da177e4SLinus Torvalds {
35103b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
35111da177e4SLinus Torvalds }
35121da177e4SLinus Torvalds 
3513f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3514f9008e4cSDavid Quigley {
3515275bb41eSDavid Howells 	*secid = task_sid(p);
3516f9008e4cSDavid Quigley }
3517f9008e4cSDavid Quigley 
35181da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
35191da177e4SLinus Torvalds {
35201da177e4SLinus Torvalds 	int rc;
35211da177e4SLinus Torvalds 
3522200ac532SEric Paris 	rc = cap_task_setnice(p, nice);
35231da177e4SLinus Torvalds 	if (rc)
35241da177e4SLinus Torvalds 		return rc;
35251da177e4SLinus Torvalds 
35263b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
35271da177e4SLinus Torvalds }
35281da177e4SLinus Torvalds 
352903e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
353003e68060SJames Morris {
3531b5376771SSerge E. Hallyn 	int rc;
3532b5376771SSerge E. Hallyn 
3533200ac532SEric Paris 	rc = cap_task_setioprio(p, ioprio);
3534b5376771SSerge E. Hallyn 	if (rc)
3535b5376771SSerge E. Hallyn 		return rc;
3536b5376771SSerge E. Hallyn 
35373b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
353803e68060SJames Morris }
353903e68060SJames Morris 
3540a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3541a1836a42SDavid Quigley {
35423b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3543a1836a42SDavid Quigley }
3544a1836a42SDavid Quigley 
35458fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
35468fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
35471da177e4SLinus Torvalds {
35488fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
35491da177e4SLinus Torvalds 
35501da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
35511da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
35521da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3553d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
35541da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
35558fd00b4dSJiri Slaby 		return current_has_perm(p, PROCESS__SETRLIMIT);
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds 	return 0;
35581da177e4SLinus Torvalds }
35591da177e4SLinus Torvalds 
3560b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
35611da177e4SLinus Torvalds {
3562b5376771SSerge E. Hallyn 	int rc;
3563b5376771SSerge E. Hallyn 
3564b0ae1981SKOSAKI Motohiro 	rc = cap_task_setscheduler(p);
3565b5376771SSerge E. Hallyn 	if (rc)
3566b5376771SSerge E. Hallyn 		return rc;
3567b5376771SSerge E. Hallyn 
35683b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
35691da177e4SLinus Torvalds }
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
35721da177e4SLinus Torvalds {
35733b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
35741da177e4SLinus Torvalds }
35751da177e4SLinus Torvalds 
357635601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
357735601547SDavid Quigley {
35783b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
357935601547SDavid Quigley }
358035601547SDavid Quigley 
3581f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3582f9008e4cSDavid Quigley 				int sig, u32 secid)
35831da177e4SLinus Torvalds {
35841da177e4SLinus Torvalds 	u32 perm;
35851da177e4SLinus Torvalds 	int rc;
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 	if (!sig)
35881da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
35891da177e4SLinus Torvalds 	else
35901da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3591f9008e4cSDavid Quigley 	if (secid)
3592275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3593275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3594f9008e4cSDavid Quigley 	else
35953b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3596f9008e4cSDavid Quigley 	return rc;
35971da177e4SLinus Torvalds }
35981da177e4SLinus Torvalds 
35991da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
36001da177e4SLinus Torvalds {
36018a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
36021da177e4SLinus Torvalds }
36031da177e4SLinus Torvalds 
36041da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
36051da177e4SLinus Torvalds 				  struct inode *inode)
36061da177e4SLinus Torvalds {
36071da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3608275bb41eSDavid Howells 	u32 sid = task_sid(p);
36091da177e4SLinus Torvalds 
3610275bb41eSDavid Howells 	isec->sid = sid;
36111da177e4SLinus Torvalds 	isec->initialized = 1;
36121da177e4SLinus Torvalds }
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
361567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
36162bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
36171da177e4SLinus Torvalds {
36181da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
36191da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
36201da177e4SLinus Torvalds 
3621bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
36221da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
36231da177e4SLinus Torvalds 	if (ih == NULL)
36241da177e4SLinus Torvalds 		goto out;
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
36271da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
36281da177e4SLinus Torvalds 		goto out;
36291da177e4SLinus Torvalds 
363048c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
363148c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
36321da177e4SLinus Torvalds 	ret = 0;
36331da177e4SLinus Torvalds 
363467f83cbfSVenkat Yekkirala 	if (proto)
363567f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
363667f83cbfSVenkat Yekkirala 
36371da177e4SLinus Torvalds 	switch (ih->protocol) {
36381da177e4SLinus Torvalds 	case IPPROTO_TCP: {
36391da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
36421da177e4SLinus Torvalds 			break;
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 		offset += ihlen;
36451da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
36461da177e4SLinus Torvalds 		if (th == NULL)
36471da177e4SLinus Torvalds 			break;
36481da177e4SLinus Torvalds 
364948c62af6SEric Paris 		ad->u.net->sport = th->source;
365048c62af6SEric Paris 		ad->u.net->dport = th->dest;
36511da177e4SLinus Torvalds 		break;
36521da177e4SLinus Torvalds 	}
36531da177e4SLinus Torvalds 
36541da177e4SLinus Torvalds 	case IPPROTO_UDP: {
36551da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
36561da177e4SLinus Torvalds 
36571da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
36581da177e4SLinus Torvalds 			break;
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 		offset += ihlen;
36611da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
36621da177e4SLinus Torvalds 		if (uh == NULL)
36631da177e4SLinus Torvalds 			break;
36641da177e4SLinus Torvalds 
366548c62af6SEric Paris 		ad->u.net->sport = uh->source;
366648c62af6SEric Paris 		ad->u.net->dport = uh->dest;
36671da177e4SLinus Torvalds 		break;
36681da177e4SLinus Torvalds 	}
36691da177e4SLinus Torvalds 
36702ee92d46SJames Morris 	case IPPROTO_DCCP: {
36712ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
36722ee92d46SJames Morris 
36732ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
36742ee92d46SJames Morris 			break;
36752ee92d46SJames Morris 
36762ee92d46SJames Morris 		offset += ihlen;
36772ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
36782ee92d46SJames Morris 		if (dh == NULL)
36792ee92d46SJames Morris 			break;
36802ee92d46SJames Morris 
368148c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
368248c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
36832ee92d46SJames Morris 		break;
36842ee92d46SJames Morris 	}
36852ee92d46SJames Morris 
36861da177e4SLinus Torvalds 	default:
36871da177e4SLinus Torvalds 		break;
36881da177e4SLinus Torvalds 	}
36891da177e4SLinus Torvalds out:
36901da177e4SLinus Torvalds 	return ret;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
36931da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
369667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
36972bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
36981da177e4SLinus Torvalds {
36991da177e4SLinus Torvalds 	u8 nexthdr;
37001da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
37011da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
370275f2811cSJesse Gross 	__be16 frag_off;
37031da177e4SLinus Torvalds 
3704bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
37051da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
37061da177e4SLinus Torvalds 	if (ip6 == NULL)
37071da177e4SLinus Torvalds 		goto out;
37081da177e4SLinus Torvalds 
370948c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
371048c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
37111da177e4SLinus Torvalds 	ret = 0;
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
37141da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
371575f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
37161da177e4SLinus Torvalds 	if (offset < 0)
37171da177e4SLinus Torvalds 		goto out;
37181da177e4SLinus Torvalds 
371967f83cbfSVenkat Yekkirala 	if (proto)
372067f83cbfSVenkat Yekkirala 		*proto = nexthdr;
372167f83cbfSVenkat Yekkirala 
37221da177e4SLinus Torvalds 	switch (nexthdr) {
37231da177e4SLinus Torvalds 	case IPPROTO_TCP: {
37241da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
37271da177e4SLinus Torvalds 		if (th == NULL)
37281da177e4SLinus Torvalds 			break;
37291da177e4SLinus Torvalds 
373048c62af6SEric Paris 		ad->u.net->sport = th->source;
373148c62af6SEric Paris 		ad->u.net->dport = th->dest;
37321da177e4SLinus Torvalds 		break;
37331da177e4SLinus Torvalds 	}
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 	case IPPROTO_UDP: {
37361da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
37391da177e4SLinus Torvalds 		if (uh == NULL)
37401da177e4SLinus Torvalds 			break;
37411da177e4SLinus Torvalds 
374248c62af6SEric Paris 		ad->u.net->sport = uh->source;
374348c62af6SEric Paris 		ad->u.net->dport = uh->dest;
37441da177e4SLinus Torvalds 		break;
37451da177e4SLinus Torvalds 	}
37461da177e4SLinus Torvalds 
37472ee92d46SJames Morris 	case IPPROTO_DCCP: {
37482ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
37492ee92d46SJames Morris 
37502ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
37512ee92d46SJames Morris 		if (dh == NULL)
37522ee92d46SJames Morris 			break;
37532ee92d46SJames Morris 
375448c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
375548c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
37562ee92d46SJames Morris 		break;
37572ee92d46SJames Morris 	}
37582ee92d46SJames Morris 
37591da177e4SLinus Torvalds 	/* includes fragments */
37601da177e4SLinus Torvalds 	default:
37611da177e4SLinus Torvalds 		break;
37621da177e4SLinus Torvalds 	}
37631da177e4SLinus Torvalds out:
37641da177e4SLinus Torvalds 	return ret;
37651da177e4SLinus Torvalds }
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds #endif /* IPV6 */
37681da177e4SLinus Torvalds 
37692bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3770cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
37711da177e4SLinus Torvalds {
3772cf9481e2SDavid Howells 	char *addrp;
3773cf9481e2SDavid Howells 	int ret;
37741da177e4SLinus Torvalds 
377548c62af6SEric Paris 	switch (ad->u.net->family) {
37761da177e4SLinus Torvalds 	case PF_INET:
377767f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3778cf9481e2SDavid Howells 		if (ret)
3779cf9481e2SDavid Howells 			goto parse_error;
378048c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
378148c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
3782cf9481e2SDavid Howells 		goto okay;
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37851da177e4SLinus Torvalds 	case PF_INET6:
378667f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3787cf9481e2SDavid Howells 		if (ret)
3788cf9481e2SDavid Howells 			goto parse_error;
378948c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
379048c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
3791cf9481e2SDavid Howells 		goto okay;
37921da177e4SLinus Torvalds #endif	/* IPV6 */
37931da177e4SLinus Torvalds 	default:
3794cf9481e2SDavid Howells 		addrp = NULL;
3795cf9481e2SDavid Howells 		goto okay;
37961da177e4SLinus Torvalds 	}
37971da177e4SLinus Torvalds 
3798cf9481e2SDavid Howells parse_error:
379971f1cb05SPaul Moore 	printk(KERN_WARNING
380071f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
380171f1cb05SPaul Moore 	       " unable to parse packet\n");
38021da177e4SLinus Torvalds 	return ret;
3803cf9481e2SDavid Howells 
3804cf9481e2SDavid Howells okay:
3805cf9481e2SDavid Howells 	if (_addrp)
3806cf9481e2SDavid Howells 		*_addrp = addrp;
3807cf9481e2SDavid Howells 	return 0;
38081da177e4SLinus Torvalds }
38091da177e4SLinus Torvalds 
38104f6a993fSPaul Moore /**
3811220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
38124f6a993fSPaul Moore  * @skb: the packet
381375e22910SPaul Moore  * @family: protocol family
3814220deb96SPaul Moore  * @sid: the packet's peer label SID
38154f6a993fSPaul Moore  *
38164f6a993fSPaul Moore  * Description:
3817220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3818220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3819220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3820220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3821220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3822220deb96SPaul Moore  * peer labels.
38234f6a993fSPaul Moore  *
38244f6a993fSPaul Moore  */
3825220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
38264f6a993fSPaul Moore {
382771f1cb05SPaul Moore 	int err;
38284f6a993fSPaul Moore 	u32 xfrm_sid;
38294f6a993fSPaul Moore 	u32 nlbl_sid;
3830220deb96SPaul Moore 	u32 nlbl_type;
38314f6a993fSPaul Moore 
3832bed4d7efSPaul Moore 	err = selinux_skb_xfrm_sid(skb, &xfrm_sid);
3833bed4d7efSPaul Moore 	if (unlikely(err))
3834bed4d7efSPaul Moore 		return -EACCES;
3835bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3836bed4d7efSPaul Moore 	if (unlikely(err))
3837bed4d7efSPaul Moore 		return -EACCES;
3838220deb96SPaul Moore 
383971f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
384071f1cb05SPaul Moore 	if (unlikely(err)) {
384171f1cb05SPaul Moore 		printk(KERN_WARNING
384271f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
384371f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3844220deb96SPaul Moore 		return -EACCES;
384571f1cb05SPaul Moore 	}
3846220deb96SPaul Moore 
3847220deb96SPaul Moore 	return 0;
38484f6a993fSPaul Moore }
38494f6a993fSPaul Moore 
38507f721643SPaul Moore /**
38517f721643SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
38527f721643SPaul Moore  * @sk_sid: the parent socket's SID
38537f721643SPaul Moore  * @skb_sid: the packet's SID
38547f721643SPaul Moore  * @conn_sid: the resulting connection SID
38557f721643SPaul Moore  *
38567f721643SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
38577f721643SPaul Moore  * combined with the MLS information from @skb_sid in order to create
38587f721643SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
38597f721643SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
38607f721643SPaul Moore  *
38617f721643SPaul Moore  */
38627f721643SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
38637f721643SPaul Moore {
38647f721643SPaul Moore 	int err = 0;
38657f721643SPaul Moore 
38667f721643SPaul Moore 	if (skb_sid != SECSID_NULL)
38677f721643SPaul Moore 		err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
38687f721643SPaul Moore 	else
38697f721643SPaul Moore 		*conn_sid = sk_sid;
38707f721643SPaul Moore 
38717f721643SPaul Moore 	return err;
38727f721643SPaul Moore }
38737f721643SPaul Moore 
38741da177e4SLinus Torvalds /* socket security operations */
3875d4f2d978SPaul Moore 
38762ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
38772ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
3878d4f2d978SPaul Moore {
38792ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
38802ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
38812ad18bdfSHarry Ciao 		return 0;
38822ad18bdfSHarry Ciao 	}
38832ad18bdfSHarry Ciao 
38842ad18bdfSHarry Ciao 	return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
38852ad18bdfSHarry Ciao 				       socksid);
3886d4f2d978SPaul Moore }
3887d4f2d978SPaul Moore 
3888253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
38891da177e4SLinus Torvalds {
3890253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
38912bf49690SThomas Liu 	struct common_audit_data ad;
389248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
3893253bfae6SPaul Moore 	u32 tsid = task_sid(task);
38941da177e4SLinus Torvalds 
3895253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
3896253bfae6SPaul Moore 		return 0;
38971da177e4SLinus Torvalds 
389850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
389948c62af6SEric Paris 	ad.u.net = &net;
390048c62af6SEric Paris 	ad.u.net->sk = sk;
39011da177e4SLinus Torvalds 
3902253bfae6SPaul Moore 	return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
39031da177e4SLinus Torvalds }
39041da177e4SLinus Torvalds 
39051da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
39061da177e4SLinus Torvalds 				 int protocol, int kern)
39071da177e4SLinus Torvalds {
39085fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3909d4f2d978SPaul Moore 	u32 newsid;
3910275bb41eSDavid Howells 	u16 secclass;
39112ad18bdfSHarry Ciao 	int rc;
39121da177e4SLinus Torvalds 
39131da177e4SLinus Torvalds 	if (kern)
3914d4f2d978SPaul Moore 		return 0;
39151da177e4SLinus Torvalds 
3916275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
39172ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
39182ad18bdfSHarry Ciao 	if (rc)
39192ad18bdfSHarry Ciao 		return rc;
39202ad18bdfSHarry Ciao 
3921d4f2d978SPaul Moore 	return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
39221da177e4SLinus Torvalds }
39231da177e4SLinus Torvalds 
39247420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
39251da177e4SLinus Torvalds 				      int type, int protocol, int kern)
39261da177e4SLinus Torvalds {
39275fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3928d4f2d978SPaul Moore 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3929892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
3930275bb41eSDavid Howells 	int err = 0;
3931275bb41eSDavid Howells 
39322ad18bdfSHarry Ciao 	isec->sclass = socket_type_to_security_class(family, type, protocol);
39332ad18bdfSHarry Ciao 
3934275bb41eSDavid Howells 	if (kern)
3935275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
39362ad18bdfSHarry Ciao 	else {
39372ad18bdfSHarry Ciao 		err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
39382ad18bdfSHarry Ciao 		if (err)
39392ad18bdfSHarry Ciao 			return err;
39402ad18bdfSHarry Ciao 	}
3941275bb41eSDavid Howells 
39421da177e4SLinus Torvalds 	isec->initialized = 1;
39431da177e4SLinus Torvalds 
3944892c141eSVenkat Yekkirala 	if (sock->sk) {
3945892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3946892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
3947220deb96SPaul Moore 		sksec->sclass = isec->sclass;
3948389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
3949892c141eSVenkat Yekkirala 	}
3950892c141eSVenkat Yekkirala 
39517420ed23SVenkat Yekkirala 	return err;
39521da177e4SLinus Torvalds }
39531da177e4SLinus Torvalds 
39541da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
39551da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
39561da177e4SLinus Torvalds    permission check between the socket and the port number. */
39571da177e4SLinus Torvalds 
39581da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
39591da177e4SLinus Torvalds {
3960253bfae6SPaul Moore 	struct sock *sk = sock->sk;
39611da177e4SLinus Torvalds 	u16 family;
39621da177e4SLinus Torvalds 	int err;
39631da177e4SLinus Torvalds 
3964253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__BIND);
39651da177e4SLinus Torvalds 	if (err)
39661da177e4SLinus Torvalds 		goto out;
39671da177e4SLinus Torvalds 
39681da177e4SLinus Torvalds 	/*
39691da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
397013402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
397113402580SJames Morris 	 * check the first address now.
39721da177e4SLinus Torvalds 	 */
3973253bfae6SPaul Moore 	family = sk->sk_family;
39741da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
39751da177e4SLinus Torvalds 		char *addrp;
3976253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
39772bf49690SThomas Liu 		struct common_audit_data ad;
397848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
39791da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
39801da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
39811da177e4SLinus Torvalds 		unsigned short snum;
3982e399f982SJames Morris 		u32 sid, node_perm;
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 		if (family == PF_INET) {
39851da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
39861da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
39871da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
39881da177e4SLinus Torvalds 		} else {
39891da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
39901da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
39911da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
39921da177e4SLinus Torvalds 		}
39931da177e4SLinus Torvalds 
3994227b60f5SStephen Hemminger 		if (snum) {
3995227b60f5SStephen Hemminger 			int low, high;
3996227b60f5SStephen Hemminger 
3997227b60f5SStephen Hemminger 			inet_get_local_port_range(&low, &high);
3998227b60f5SStephen Hemminger 
3999227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
40003e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
40013e112172SPaul Moore 						      snum, &sid);
40021da177e4SLinus Torvalds 				if (err)
40031da177e4SLinus Torvalds 					goto out;
400450c205f5SEric Paris 				ad.type = LSM_AUDIT_DATA_NET;
400548c62af6SEric Paris 				ad.u.net = &net;
400648c62af6SEric Paris 				ad.u.net->sport = htons(snum);
400748c62af6SEric Paris 				ad.u.net->family = family;
4008253bfae6SPaul Moore 				err = avc_has_perm(sksec->sid, sid,
4009253bfae6SPaul Moore 						   sksec->sclass,
40101da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
40111da177e4SLinus Torvalds 				if (err)
40121da177e4SLinus Torvalds 					goto out;
40131da177e4SLinus Torvalds 			}
4014227b60f5SStephen Hemminger 		}
40151da177e4SLinus Torvalds 
4016253bfae6SPaul Moore 		switch (sksec->sclass) {
401713402580SJames Morris 		case SECCLASS_TCP_SOCKET:
40181da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
40191da177e4SLinus Torvalds 			break;
40201da177e4SLinus Torvalds 
402113402580SJames Morris 		case SECCLASS_UDP_SOCKET:
40221da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
40231da177e4SLinus Torvalds 			break;
40241da177e4SLinus Torvalds 
40252ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
40262ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
40272ee92d46SJames Morris 			break;
40282ee92d46SJames Morris 
40291da177e4SLinus Torvalds 		default:
40301da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
40311da177e4SLinus Torvalds 			break;
40321da177e4SLinus Torvalds 		}
40331da177e4SLinus Torvalds 
4034224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
40351da177e4SLinus Torvalds 		if (err)
40361da177e4SLinus Torvalds 			goto out;
40371da177e4SLinus Torvalds 
403850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
403948c62af6SEric Paris 		ad.u.net = &net;
404048c62af6SEric Paris 		ad.u.net->sport = htons(snum);
404148c62af6SEric Paris 		ad.u.net->family = family;
40421da177e4SLinus Torvalds 
40431da177e4SLinus Torvalds 		if (family == PF_INET)
404448c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
40451da177e4SLinus Torvalds 		else
404648c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
40471da177e4SLinus Torvalds 
4048253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid,
4049253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
40501da177e4SLinus Torvalds 		if (err)
40511da177e4SLinus Torvalds 			goto out;
40521da177e4SLinus Torvalds 	}
40531da177e4SLinus Torvalds out:
40541da177e4SLinus Torvalds 	return err;
40551da177e4SLinus Torvalds }
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
40581da177e4SLinus Torvalds {
4059014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4060253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
40611da177e4SLinus Torvalds 	int err;
40621da177e4SLinus Torvalds 
4063253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__CONNECT);
40641da177e4SLinus Torvalds 	if (err)
40651da177e4SLinus Torvalds 		return err;
40661da177e4SLinus Torvalds 
40671da177e4SLinus Torvalds 	/*
40682ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
40691da177e4SLinus Torvalds 	 */
4070253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4071253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_DCCP_SOCKET) {
40722bf49690SThomas Liu 		struct common_audit_data ad;
407348c62af6SEric Paris 		struct lsm_network_audit net = {0,};
40741da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
40751da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
40761da177e4SLinus Torvalds 		unsigned short snum;
40772ee92d46SJames Morris 		u32 sid, perm;
40781da177e4SLinus Torvalds 
40791da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
40801da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4081911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
40821da177e4SLinus Torvalds 				return -EINVAL;
40831da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
40841da177e4SLinus Torvalds 		} else {
40851da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4086911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
40871da177e4SLinus Torvalds 				return -EINVAL;
40881da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
40891da177e4SLinus Torvalds 		}
40901da177e4SLinus Torvalds 
40913e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
40921da177e4SLinus Torvalds 		if (err)
40931da177e4SLinus Torvalds 			goto out;
40941da177e4SLinus Torvalds 
4095253bfae6SPaul Moore 		perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
40962ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
40972ee92d46SJames Morris 
409850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
409948c62af6SEric Paris 		ad.u.net = &net;
410048c62af6SEric Paris 		ad.u.net->dport = htons(snum);
410148c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
4102253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
41031da177e4SLinus Torvalds 		if (err)
41041da177e4SLinus Torvalds 			goto out;
41051da177e4SLinus Torvalds 	}
41061da177e4SLinus Torvalds 
4107014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
4108014ab19aSPaul Moore 
41091da177e4SLinus Torvalds out:
41101da177e4SLinus Torvalds 	return err;
41111da177e4SLinus Torvalds }
41121da177e4SLinus Torvalds 
41131da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
41141da177e4SLinus Torvalds {
4115253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
41161da177e4SLinus Torvalds }
41171da177e4SLinus Torvalds 
41181da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
41191da177e4SLinus Torvalds {
41201da177e4SLinus Torvalds 	int err;
41211da177e4SLinus Torvalds 	struct inode_security_struct *isec;
41221da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
41231da177e4SLinus Torvalds 
4124253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
41251da177e4SLinus Torvalds 	if (err)
41261da177e4SLinus Torvalds 		return err;
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
41311da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
41321da177e4SLinus Torvalds 	newisec->sid = isec->sid;
41331da177e4SLinus Torvalds 	newisec->initialized = 1;
41341da177e4SLinus Torvalds 
41351da177e4SLinus Torvalds 	return 0;
41361da177e4SLinus Torvalds }
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
41391da177e4SLinus Torvalds 				  int size)
41401da177e4SLinus Torvalds {
4141253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__WRITE);
41421da177e4SLinus Torvalds }
41431da177e4SLinus Torvalds 
41441da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
41451da177e4SLinus Torvalds 				  int size, int flags)
41461da177e4SLinus Torvalds {
4147253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__READ);
41481da177e4SLinus Torvalds }
41491da177e4SLinus Torvalds 
41501da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
41511da177e4SLinus Torvalds {
4152253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
41531da177e4SLinus Torvalds }
41541da177e4SLinus Torvalds 
41551da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
41561da177e4SLinus Torvalds {
4157253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
41581da177e4SLinus Torvalds }
41591da177e4SLinus Torvalds 
41601da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
41611da177e4SLinus Torvalds {
4162f8687afeSPaul Moore 	int err;
4163f8687afeSPaul Moore 
4164253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4165f8687afeSPaul Moore 	if (err)
4166f8687afeSPaul Moore 		return err;
4167f8687afeSPaul Moore 
4168f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
41691da177e4SLinus Torvalds }
41701da177e4SLinus Torvalds 
41711da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
41721da177e4SLinus Torvalds 				     int optname)
41731da177e4SLinus Torvalds {
4174253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
41751da177e4SLinus Torvalds }
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
41781da177e4SLinus Torvalds {
4179253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
41801da177e4SLinus Torvalds }
41811da177e4SLinus Torvalds 
41823610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
41833610cda5SDavid S. Miller 					      struct sock *other,
41841da177e4SLinus Torvalds 					      struct sock *newsk)
41851da177e4SLinus Torvalds {
41863610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
41873610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
41884d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
41892bf49690SThomas Liu 	struct common_audit_data ad;
419048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
41911da177e4SLinus Torvalds 	int err;
41921da177e4SLinus Torvalds 
419350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
419448c62af6SEric Paris 	ad.u.net = &net;
419548c62af6SEric Paris 	ad.u.net->sk = other;
41961da177e4SLinus Torvalds 
41974d1e2451SPaul Moore 	err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
41984d1e2451SPaul Moore 			   sksec_other->sclass,
41991da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
42001da177e4SLinus Torvalds 	if (err)
42011da177e4SLinus Torvalds 		return err;
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 	/* server child socket */
42044d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
42054d1e2451SPaul Moore 	err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
42064d1e2451SPaul Moore 				    &sksec_new->sid);
42074d1e2451SPaul Moore 	if (err)
42084237c75cSVenkat Yekkirala 		return err;
42094d1e2451SPaul Moore 
42104d1e2451SPaul Moore 	/* connecting socket */
42114d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
42124d1e2451SPaul Moore 
42134d1e2451SPaul Moore 	return 0;
42141da177e4SLinus Torvalds }
42151da177e4SLinus Torvalds 
42161da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
42171da177e4SLinus Torvalds 					struct socket *other)
42181da177e4SLinus Torvalds {
4219253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4220253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
42212bf49690SThomas Liu 	struct common_audit_data ad;
422248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
42231da177e4SLinus Torvalds 
422450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
422548c62af6SEric Paris 	ad.u.net = &net;
422648c62af6SEric Paris 	ad.u.net->sk = other->sk;
42271da177e4SLinus Torvalds 
4228253bfae6SPaul Moore 	return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4229253bfae6SPaul Moore 			    &ad);
42301da177e4SLinus Torvalds }
42311da177e4SLinus Torvalds 
4232effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4233effad8dfSPaul Moore 				    u32 peer_sid,
42342bf49690SThomas Liu 				    struct common_audit_data *ad)
4235effad8dfSPaul Moore {
4236effad8dfSPaul Moore 	int err;
4237effad8dfSPaul Moore 	u32 if_sid;
4238effad8dfSPaul Moore 	u32 node_sid;
4239effad8dfSPaul Moore 
4240effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
4241effad8dfSPaul Moore 	if (err)
4242effad8dfSPaul Moore 		return err;
4243effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4244effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4245effad8dfSPaul Moore 	if (err)
4246effad8dfSPaul Moore 		return err;
4247effad8dfSPaul Moore 
4248effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4249effad8dfSPaul Moore 	if (err)
4250effad8dfSPaul Moore 		return err;
4251effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4252effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4253effad8dfSPaul Moore }
4254effad8dfSPaul Moore 
4255220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4256d8395c87SPaul Moore 				       u16 family)
4257220deb96SPaul Moore {
4258277d342fSPaul Moore 	int err = 0;
4259220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4260220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
42612bf49690SThomas Liu 	struct common_audit_data ad;
426248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4263d8395c87SPaul Moore 	char *addrp;
4264d8395c87SPaul Moore 
426550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
426648c62af6SEric Paris 	ad.u.net = &net;
426748c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
426848c62af6SEric Paris 	ad.u.net->family = family;
4269d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4270d8395c87SPaul Moore 	if (err)
4271d8395c87SPaul Moore 		return err;
4272220deb96SPaul Moore 
427358bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
4274220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4275d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4276220deb96SPaul Moore 		if (err)
4277220deb96SPaul Moore 			return err;
427858bfbb51SPaul Moore 	}
4279220deb96SPaul Moore 
4280d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4281220deb96SPaul Moore 	if (err)
4282220deb96SPaul Moore 		return err;
4283d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4284220deb96SPaul Moore 
42854e5ab4cbSJames Morris 	return err;
42864e5ab4cbSJames Morris }
4287d28d1e08STrent Jaeger 
42884e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
42894e5ab4cbSJames Morris {
4290220deb96SPaul Moore 	int err;
42914237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4292220deb96SPaul Moore 	u16 family = sk->sk_family;
4293220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
42942bf49690SThomas Liu 	struct common_audit_data ad;
429548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4296220deb96SPaul Moore 	char *addrp;
4297d8395c87SPaul Moore 	u8 secmark_active;
4298d8395c87SPaul Moore 	u8 peerlbl_active;
42994e5ab4cbSJames Morris 
43004e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4301220deb96SPaul Moore 		return 0;
43024e5ab4cbSJames Morris 
43034e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
430487fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
43054e5ab4cbSJames Morris 		family = PF_INET;
43064e5ab4cbSJames Morris 
4307d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4308d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4309d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4310d8395c87SPaul Moore 	 * as fast and as clean as possible. */
431158bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4312d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4313d8395c87SPaul Moore 
4314d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
43152be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4316d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4317d8395c87SPaul Moore 		return 0;
4318d8395c87SPaul Moore 
431950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
432048c62af6SEric Paris 	ad.u.net = &net;
432148c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
432248c62af6SEric Paris 	ad.u.net->family = family;
4323224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
43244e5ab4cbSJames Morris 	if (err)
4325220deb96SPaul Moore 		return err;
43264e5ab4cbSJames Morris 
4327d8395c87SPaul Moore 	if (peerlbl_active) {
4328d621d35eSPaul Moore 		u32 peer_sid;
4329220deb96SPaul Moore 
4330220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4331220deb96SPaul Moore 		if (err)
4332220deb96SPaul Moore 			return err;
43338964be4aSEric Dumazet 		err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
4334effad8dfSPaul Moore 					       peer_sid, &ad);
4335dfaebe98SPaul Moore 		if (err) {
4336dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4337effad8dfSPaul Moore 			return err;
4338dfaebe98SPaul Moore 		}
4339d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4340d621d35eSPaul Moore 				   PEER__RECV, &ad);
4341dfaebe98SPaul Moore 		if (err)
4342dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4343d621d35eSPaul Moore 	}
4344d621d35eSPaul Moore 
4345d8395c87SPaul Moore 	if (secmark_active) {
4346effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4347effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4348effad8dfSPaul Moore 		if (err)
4349effad8dfSPaul Moore 			return err;
4350effad8dfSPaul Moore 	}
4351effad8dfSPaul Moore 
4352d621d35eSPaul Moore 	return err;
43531da177e4SLinus Torvalds }
43541da177e4SLinus Torvalds 
43552c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
43561da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
43571da177e4SLinus Torvalds {
43581da177e4SLinus Torvalds 	int err = 0;
43591da177e4SLinus Torvalds 	char *scontext;
43601da177e4SLinus Torvalds 	u32 scontext_len;
4361253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
43623de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
43631da177e4SLinus Torvalds 
4364253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4365253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_TCP_SOCKET)
4366dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4367253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4368253bfae6SPaul Moore 		return -ENOPROTOOPT;
43691da177e4SLinus Torvalds 
43702c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
43711da177e4SLinus Torvalds 	if (err)
4372253bfae6SPaul Moore 		return err;
43731da177e4SLinus Torvalds 
43741da177e4SLinus Torvalds 	if (scontext_len > len) {
43751da177e4SLinus Torvalds 		err = -ERANGE;
43761da177e4SLinus Torvalds 		goto out_len;
43771da177e4SLinus Torvalds 	}
43781da177e4SLinus Torvalds 
43791da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
43801da177e4SLinus Torvalds 		err = -EFAULT;
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds out_len:
43831da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
43841da177e4SLinus Torvalds 		err = -EFAULT;
43851da177e4SLinus Torvalds 	kfree(scontext);
43861da177e4SLinus Torvalds 	return err;
43871da177e4SLinus Torvalds }
43881da177e4SLinus Torvalds 
4389dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
43902c7946a7SCatherine Zhang {
4391dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
439275e22910SPaul Moore 	u16 family;
4393877ce7c1SCatherine Zhang 
4394aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4395aa862900SPaul Moore 		family = PF_INET;
4396aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4397aa862900SPaul Moore 		family = PF_INET6;
4398aa862900SPaul Moore 	else if (sock)
439975e22910SPaul Moore 		family = sock->sk->sk_family;
440075e22910SPaul Moore 	else
440175e22910SPaul Moore 		goto out;
440275e22910SPaul Moore 
440375e22910SPaul Moore 	if (sock && family == PF_UNIX)
4404713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
44053de4bab5SPaul Moore 	else if (skb)
4406220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
44072c7946a7SCatherine Zhang 
440875e22910SPaul Moore out:
4409dc49c1f9SCatherine Zhang 	*secid = peer_secid;
441075e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
441175e22910SPaul Moore 		return -EINVAL;
441275e22910SPaul Moore 	return 0;
44132c7946a7SCatherine Zhang }
44142c7946a7SCatherine Zhang 
44157d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
44161da177e4SLinus Torvalds {
441784914b7eSPaul Moore 	struct sk_security_struct *sksec;
441884914b7eSPaul Moore 
441984914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
442084914b7eSPaul Moore 	if (!sksec)
442184914b7eSPaul Moore 		return -ENOMEM;
442284914b7eSPaul Moore 
442384914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
442484914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
442584914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
442684914b7eSPaul Moore 	sk->sk_security = sksec;
442784914b7eSPaul Moore 
442884914b7eSPaul Moore 	return 0;
44291da177e4SLinus Torvalds }
44301da177e4SLinus Torvalds 
44311da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
44321da177e4SLinus Torvalds {
443384914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
443484914b7eSPaul Moore 
443584914b7eSPaul Moore 	sk->sk_security = NULL;
443684914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
443784914b7eSPaul Moore 	kfree(sksec);
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds 
4440892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4441892c141eSVenkat Yekkirala {
4442dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4443dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4444892c141eSVenkat Yekkirala 
4445dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4446dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4447dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
444899f59ed0SPaul Moore 
4449dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4450892c141eSVenkat Yekkirala }
4451892c141eSVenkat Yekkirala 
4452beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4453d28d1e08STrent Jaeger {
4454d28d1e08STrent Jaeger 	if (!sk)
4455beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4456892c141eSVenkat Yekkirala 	else {
4457892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4458d28d1e08STrent Jaeger 
4459beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4460892c141eSVenkat Yekkirala 	}
4461d28d1e08STrent Jaeger }
4462d28d1e08STrent Jaeger 
44639a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
44644237c75cSVenkat Yekkirala {
44654237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
44664237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
44674237c75cSVenkat Yekkirala 
44682148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
44692148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
44704237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4471220deb96SPaul Moore 	sksec->sclass = isec->sclass;
44724237c75cSVenkat Yekkirala }
44734237c75cSVenkat Yekkirala 
44749a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
44754237c75cSVenkat Yekkirala 				     struct request_sock *req)
44764237c75cSVenkat Yekkirala {
44774237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
44784237c75cSVenkat Yekkirala 	int err;
4479*0b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
44807f721643SPaul Moore 	u32 connsid;
44814237c75cSVenkat Yekkirala 	u32 peersid;
44824237c75cSVenkat Yekkirala 
4483aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4484220deb96SPaul Moore 	if (err)
4485220deb96SPaul Moore 		return err;
44867f721643SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
44874237c75cSVenkat Yekkirala 	if (err)
44884237c75cSVenkat Yekkirala 		return err;
44897f721643SPaul Moore 	req->secid = connsid;
44906b877699SVenkat Yekkirala 	req->peer_secid = peersid;
4491389fb800SPaul Moore 
4492389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
44934237c75cSVenkat Yekkirala }
44944237c75cSVenkat Yekkirala 
44959a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
44969a673e56SAdrian Bunk 				   const struct request_sock *req)
44974237c75cSVenkat Yekkirala {
44984237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
44994237c75cSVenkat Yekkirala 
45004237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
45016b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
45024237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
45034237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
45044237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
45054237c75cSVenkat Yekkirala 	   time it will have been created and available. */
450699f59ed0SPaul Moore 
45079f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
45089f2ad665SPaul Moore 	 * thread with access to newsksec */
4509389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
45104237c75cSVenkat Yekkirala }
45114237c75cSVenkat Yekkirala 
4512014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
45136b877699SVenkat Yekkirala {
4514aa862900SPaul Moore 	u16 family = sk->sk_family;
45156b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
45166b877699SVenkat Yekkirala 
4517aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4518aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4519aa862900SPaul Moore 		family = PF_INET;
4520aa862900SPaul Moore 
4521aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
45226b877699SVenkat Yekkirala }
45236b877699SVenkat Yekkirala 
4524ca10b9e9SEric Dumazet static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4525ca10b9e9SEric Dumazet {
4526ca10b9e9SEric Dumazet 	skb_set_owner_w(skb, sk);
4527ca10b9e9SEric Dumazet }
4528ca10b9e9SEric Dumazet 
45292606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
45302606fd1fSEric Paris {
45312606fd1fSEric Paris 	const struct task_security_struct *__tsec;
45322606fd1fSEric Paris 	u32 tsid;
45332606fd1fSEric Paris 
45342606fd1fSEric Paris 	__tsec = current_security();
45352606fd1fSEric Paris 	tsid = __tsec->sid;
45362606fd1fSEric Paris 
45372606fd1fSEric Paris 	return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
45382606fd1fSEric Paris }
45392606fd1fSEric Paris 
45402606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
45412606fd1fSEric Paris {
45422606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
45432606fd1fSEric Paris }
45442606fd1fSEric Paris 
45452606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
45462606fd1fSEric Paris {
45472606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
45482606fd1fSEric Paris }
45492606fd1fSEric Paris 
45509a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
45519a673e56SAdrian Bunk 				      struct flowi *fl)
45524237c75cSVenkat Yekkirala {
45531d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
45544237c75cSVenkat Yekkirala }
45554237c75cSVenkat Yekkirala 
45565dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
45575dbbaf2dSPaul Moore {
45585dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
45595dbbaf2dSPaul Moore 
45605dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
45615dbbaf2dSPaul Moore 	if (!tunsec)
45625dbbaf2dSPaul Moore 		return -ENOMEM;
45635dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
45645dbbaf2dSPaul Moore 
45655dbbaf2dSPaul Moore 	*security = tunsec;
45665dbbaf2dSPaul Moore 	return 0;
45675dbbaf2dSPaul Moore }
45685dbbaf2dSPaul Moore 
45695dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
45705dbbaf2dSPaul Moore {
45715dbbaf2dSPaul Moore 	kfree(security);
45725dbbaf2dSPaul Moore }
45735dbbaf2dSPaul Moore 
4574ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
4575ed6d76e4SPaul Moore {
4576ed6d76e4SPaul Moore 	u32 sid = current_sid();
4577ed6d76e4SPaul Moore 
4578ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
4579ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
4580ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
4581ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
4582ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
4583ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
4584ed6d76e4SPaul Moore 
4585ed6d76e4SPaul Moore 	return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4586ed6d76e4SPaul Moore 			    NULL);
4587ed6d76e4SPaul Moore }
4588ed6d76e4SPaul Moore 
45895dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
4590ed6d76e4SPaul Moore {
45915dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
45925dbbaf2dSPaul Moore 
45935dbbaf2dSPaul Moore 	return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
45945dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
45955dbbaf2dSPaul Moore }
45965dbbaf2dSPaul Moore 
45975dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
45985dbbaf2dSPaul Moore {
45995dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4600ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4601ed6d76e4SPaul Moore 
4602ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
4603ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
4604ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
4605ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
4606ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
4607ed6d76e4SPaul Moore 	 * protocols were being used */
4608ed6d76e4SPaul Moore 
46095dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
4610ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
46115dbbaf2dSPaul Moore 
46125dbbaf2dSPaul Moore 	return 0;
4613ed6d76e4SPaul Moore }
4614ed6d76e4SPaul Moore 
46155dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
4616ed6d76e4SPaul Moore {
46175dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4618ed6d76e4SPaul Moore 	u32 sid = current_sid();
4619ed6d76e4SPaul Moore 	int err;
4620ed6d76e4SPaul Moore 
46215dbbaf2dSPaul Moore 	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4622ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
4623ed6d76e4SPaul Moore 	if (err)
4624ed6d76e4SPaul Moore 		return err;
4625ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4626ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
4627ed6d76e4SPaul Moore 	if (err)
4628ed6d76e4SPaul Moore 		return err;
46295dbbaf2dSPaul Moore 	tunsec->sid = sid;
4630ed6d76e4SPaul Moore 
4631ed6d76e4SPaul Moore 	return 0;
4632ed6d76e4SPaul Moore }
4633ed6d76e4SPaul Moore 
46341da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
46351da177e4SLinus Torvalds {
46361da177e4SLinus Torvalds 	int err = 0;
46371da177e4SLinus Torvalds 	u32 perm;
46381da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4639253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
46401da177e4SLinus Torvalds 
464177954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
46421da177e4SLinus Torvalds 		err = -EINVAL;
46431da177e4SLinus Torvalds 		goto out;
46441da177e4SLinus Torvalds 	}
4645b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
46461da177e4SLinus Torvalds 
4647253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
46481da177e4SLinus Torvalds 	if (err) {
46491da177e4SLinus Torvalds 		if (err == -EINVAL) {
46509ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
46511da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
46521da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
4653253bfae6SPaul Moore 				  nlh->nlmsg_type, sksec->sclass);
465439c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
46551da177e4SLinus Torvalds 				err = 0;
46561da177e4SLinus Torvalds 		}
46571da177e4SLinus Torvalds 
46581da177e4SLinus Torvalds 		/* Ignore */
46591da177e4SLinus Torvalds 		if (err == -ENOENT)
46601da177e4SLinus Torvalds 			err = 0;
46611da177e4SLinus Torvalds 		goto out;
46621da177e4SLinus Torvalds 	}
46631da177e4SLinus Torvalds 
4664253bfae6SPaul Moore 	err = sock_has_perm(current, sk, perm);
46651da177e4SLinus Torvalds out:
46661da177e4SLinus Torvalds 	return err;
46671da177e4SLinus Torvalds }
46681da177e4SLinus Torvalds 
46691da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
46701da177e4SLinus Torvalds 
4671effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4672effad8dfSPaul Moore 				       u16 family)
46731da177e4SLinus Torvalds {
4674dfaebe98SPaul Moore 	int err;
4675effad8dfSPaul Moore 	char *addrp;
4676effad8dfSPaul Moore 	u32 peer_sid;
46772bf49690SThomas Liu 	struct common_audit_data ad;
467848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4679effad8dfSPaul Moore 	u8 secmark_active;
4680948bf85cSPaul Moore 	u8 netlbl_active;
4681effad8dfSPaul Moore 	u8 peerlbl_active;
46824237c75cSVenkat Yekkirala 
4683effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4684effad8dfSPaul Moore 		return NF_ACCEPT;
46854237c75cSVenkat Yekkirala 
4686effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4687948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
46882be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4689effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4690effad8dfSPaul Moore 		return NF_ACCEPT;
46914237c75cSVenkat Yekkirala 
4692d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4693d8395c87SPaul Moore 		return NF_DROP;
4694d8395c87SPaul Moore 
469550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
469648c62af6SEric Paris 	ad.u.net = &net;
469748c62af6SEric Paris 	ad.u.net->netif = ifindex;
469848c62af6SEric Paris 	ad.u.net->family = family;
4699effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4700effad8dfSPaul Moore 		return NF_DROP;
47011da177e4SLinus Torvalds 
4702dfaebe98SPaul Moore 	if (peerlbl_active) {
4703dfaebe98SPaul Moore 		err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4704dfaebe98SPaul Moore 					       peer_sid, &ad);
4705dfaebe98SPaul Moore 		if (err) {
4706dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4707effad8dfSPaul Moore 			return NF_DROP;
4708dfaebe98SPaul Moore 		}
4709dfaebe98SPaul Moore 	}
4710effad8dfSPaul Moore 
4711effad8dfSPaul Moore 	if (secmark_active)
4712effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4713effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4714effad8dfSPaul Moore 			return NF_DROP;
4715effad8dfSPaul Moore 
4716948bf85cSPaul Moore 	if (netlbl_active)
4717948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4718948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4719948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4720948bf85cSPaul Moore 		 * protection */
4721948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4722948bf85cSPaul Moore 			return NF_DROP;
4723948bf85cSPaul Moore 
4724effad8dfSPaul Moore 	return NF_ACCEPT;
4725effad8dfSPaul Moore }
4726effad8dfSPaul Moore 
4727effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4728effad8dfSPaul Moore 					 struct sk_buff *skb,
4729effad8dfSPaul Moore 					 const struct net_device *in,
4730effad8dfSPaul Moore 					 const struct net_device *out,
4731effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4732effad8dfSPaul Moore {
4733effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET);
4734effad8dfSPaul Moore }
4735effad8dfSPaul Moore 
4736effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4737effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4738effad8dfSPaul Moore 					 struct sk_buff *skb,
4739effad8dfSPaul Moore 					 const struct net_device *in,
4740effad8dfSPaul Moore 					 const struct net_device *out,
4741effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4742effad8dfSPaul Moore {
4743effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4744effad8dfSPaul Moore }
4745effad8dfSPaul Moore #endif	/* IPV6 */
4746effad8dfSPaul Moore 
4747948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4748948bf85cSPaul Moore 				      u16 family)
4749948bf85cSPaul Moore {
4750da2ea0d0SPaul Moore 	struct sock *sk;
4751948bf85cSPaul Moore 	u32 sid;
4752948bf85cSPaul Moore 
4753948bf85cSPaul Moore 	if (!netlbl_enabled())
4754948bf85cSPaul Moore 		return NF_ACCEPT;
4755948bf85cSPaul Moore 
4756948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4757948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4758948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
4759da2ea0d0SPaul Moore 	sk = skb->sk;
4760da2ea0d0SPaul Moore 	if (sk) {
4761da2ea0d0SPaul Moore 		struct sk_security_struct *sksec;
4762da2ea0d0SPaul Moore 
4763da2ea0d0SPaul Moore 		if (sk->sk_state == TCP_LISTEN)
4764da2ea0d0SPaul Moore 			/* if the socket is the listening state then this
4765da2ea0d0SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
4766da2ea0d0SPaul Moore 			 * be labeled based on the connection/request_sock and
4767da2ea0d0SPaul Moore 			 * not the parent socket.  unfortunately, we can't
4768da2ea0d0SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
4769da2ea0d0SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
4770da2ea0d0SPaul Moore 			 * the "solution" is to simply pass the packet as-is
4771da2ea0d0SPaul Moore 			 * as any IP option based labeling should be copied
4772da2ea0d0SPaul Moore 			 * from the initial connection request (in the IP
4773da2ea0d0SPaul Moore 			 * layer).  it is far from ideal, but until we get a
4774da2ea0d0SPaul Moore 			 * security label in the packet itself this is the
4775da2ea0d0SPaul Moore 			 * best we can do. */
4776da2ea0d0SPaul Moore 			return NF_ACCEPT;
4777da2ea0d0SPaul Moore 
4778da2ea0d0SPaul Moore 		/* standard practice, label using the parent socket */
4779da2ea0d0SPaul Moore 		sksec = sk->sk_security;
4780948bf85cSPaul Moore 		sid = sksec->sid;
4781948bf85cSPaul Moore 	} else
4782948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4783948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4784948bf85cSPaul Moore 		return NF_DROP;
4785948bf85cSPaul Moore 
4786948bf85cSPaul Moore 	return NF_ACCEPT;
4787948bf85cSPaul Moore }
4788948bf85cSPaul Moore 
4789948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum,
4790948bf85cSPaul Moore 					struct sk_buff *skb,
4791948bf85cSPaul Moore 					const struct net_device *in,
4792948bf85cSPaul Moore 					const struct net_device *out,
4793948bf85cSPaul Moore 					int (*okfn)(struct sk_buff *))
4794948bf85cSPaul Moore {
4795948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4796948bf85cSPaul Moore }
4797948bf85cSPaul Moore 
4798effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4799effad8dfSPaul Moore 						int ifindex,
4800d8395c87SPaul Moore 						u16 family)
48014e5ab4cbSJames Morris {
4802effad8dfSPaul Moore 	struct sock *sk = skb->sk;
48034237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
48042bf49690SThomas Liu 	struct common_audit_data ad;
480548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4806d8395c87SPaul Moore 	char *addrp;
4807d8395c87SPaul Moore 	u8 proto;
48084e5ab4cbSJames Morris 
4809effad8dfSPaul Moore 	if (sk == NULL)
4810effad8dfSPaul Moore 		return NF_ACCEPT;
48114237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
48124e5ab4cbSJames Morris 
481350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
481448c62af6SEric Paris 	ad.u.net = &net;
481548c62af6SEric Paris 	ad.u.net->netif = ifindex;
481648c62af6SEric Paris 	ad.u.net->family = family;
4817d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4818d8395c87SPaul Moore 		return NF_DROP;
4819d8395c87SPaul Moore 
482058bfbb51SPaul Moore 	if (selinux_secmark_enabled())
4821effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4822d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
48232fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
48241da177e4SLinus Torvalds 
4825d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
48262fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
4827effad8dfSPaul Moore 
4828effad8dfSPaul Moore 	return NF_ACCEPT;
4829effad8dfSPaul Moore }
4830effad8dfSPaul Moore 
4831effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4832effad8dfSPaul Moore 					 u16 family)
4833effad8dfSPaul Moore {
4834effad8dfSPaul Moore 	u32 secmark_perm;
4835effad8dfSPaul Moore 	u32 peer_sid;
4836effad8dfSPaul Moore 	struct sock *sk;
48372bf49690SThomas Liu 	struct common_audit_data ad;
483848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4839effad8dfSPaul Moore 	char *addrp;
4840effad8dfSPaul Moore 	u8 secmark_active;
4841effad8dfSPaul Moore 	u8 peerlbl_active;
4842effad8dfSPaul Moore 
4843effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4844effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4845effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4846effad8dfSPaul Moore 	 * as fast and as clean as possible. */
484758bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4848d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4849def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
4850effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4851effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4852effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4853effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4854effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4855effad8dfSPaul Moore 	 *       is NULL, in this case go ahead and apply access control. */
4856adf30907SEric Dumazet 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
4857effad8dfSPaul Moore 		return NF_ACCEPT;
4858def8b4faSAlexey Dobriyan #endif
4859effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
48602be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4861effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4862effad8dfSPaul Moore 		return NF_ACCEPT;
4863effad8dfSPaul Moore 
4864effad8dfSPaul Moore 	sk = skb->sk;
4865d8395c87SPaul Moore 	if (sk == NULL) {
48667f721643SPaul Moore 		/* Without an associated socket the packet is either coming
48677f721643SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
48687f721643SPaul Moore 		 * to determine which and if the packet is being forwarded
48697f721643SPaul Moore 		 * query the packet directly to determine the security label. */
48704a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
4871d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
4872d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
487304f6d70fSEric Paris 				return NF_DROP;
48744a7ab3dcSSteffen Klassert 		} else {
48754a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
4876d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
48774a7ab3dcSSteffen Klassert 		}
48787f721643SPaul Moore 	} else if (sk->sk_state == TCP_LISTEN) {
48797f721643SPaul Moore 		/* Locally generated packet but the associated socket is in the
48807f721643SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
48817f721643SPaul Moore 		 * this particular case the correct security label is assigned
48827f721643SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
48837f721643SPaul Moore 		 * query the request_sock as it isn't queued on the parent
48847f721643SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
48857f721643SPaul Moore 		 * viable choice is to regenerate the label like we do in
48867f721643SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
48877f721643SPaul Moore 		 * for similar problems. */
48887f721643SPaul Moore 		u32 skb_sid;
48897f721643SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
48907f721643SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
48917f721643SPaul Moore 			return NF_DROP;
48927f721643SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
48937f721643SPaul Moore 			return NF_DROP;
48947f721643SPaul Moore 		secmark_perm = PACKET__SEND;
4895d8395c87SPaul Moore 	} else {
48967f721643SPaul Moore 		/* Locally generated packet, fetch the security label from the
48977f721643SPaul Moore 		 * associated socket. */
4898effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4899effad8dfSPaul Moore 		peer_sid = sksec->sid;
4900effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4901effad8dfSPaul Moore 	}
4902effad8dfSPaul Moore 
490350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
490448c62af6SEric Paris 	ad.u.net = &net;
490548c62af6SEric Paris 	ad.u.net->netif = ifindex;
490648c62af6SEric Paris 	ad.u.net->family = family;
4907d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
490804f6d70fSEric Paris 		return NF_DROP;
4909d8395c87SPaul Moore 
4910effad8dfSPaul Moore 	if (secmark_active)
4911effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4912effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
49131f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4914effad8dfSPaul Moore 
4915effad8dfSPaul Moore 	if (peerlbl_active) {
4916effad8dfSPaul Moore 		u32 if_sid;
4917effad8dfSPaul Moore 		u32 node_sid;
4918effad8dfSPaul Moore 
4919effad8dfSPaul Moore 		if (sel_netif_sid(ifindex, &if_sid))
492004f6d70fSEric Paris 			return NF_DROP;
4921effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
4922effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
49231f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4924effad8dfSPaul Moore 
4925effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
492604f6d70fSEric Paris 			return NF_DROP;
4927effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
4928effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
49291f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4930effad8dfSPaul Moore 	}
4931effad8dfSPaul Moore 
4932effad8dfSPaul Moore 	return NF_ACCEPT;
4933effad8dfSPaul Moore }
4934effad8dfSPaul Moore 
4935effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4936a224be76SDavid S. Miller 					   struct sk_buff *skb,
49371da177e4SLinus Torvalds 					   const struct net_device *in,
49381da177e4SLinus Torvalds 					   const struct net_device *out,
49391da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
49401da177e4SLinus Torvalds {
4941effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET);
49421da177e4SLinus Torvalds }
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4945effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4946a224be76SDavid S. Miller 					   struct sk_buff *skb,
49471da177e4SLinus Torvalds 					   const struct net_device *in,
49481da177e4SLinus Torvalds 					   const struct net_device *out,
49491da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
49501da177e4SLinus Torvalds {
4951effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
49521da177e4SLinus Torvalds }
49531da177e4SLinus Torvalds #endif	/* IPV6 */
49541da177e4SLinus Torvalds 
49551da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
49581da177e4SLinus Torvalds {
49591da177e4SLinus Torvalds 	int err;
49601da177e4SLinus Torvalds 
4961200ac532SEric Paris 	err = cap_netlink_send(sk, skb);
49621da177e4SLinus Torvalds 	if (err)
49631da177e4SLinus Torvalds 		return err;
49641da177e4SLinus Torvalds 
4965941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
49661da177e4SLinus Torvalds }
49671da177e4SLinus Torvalds 
49681da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
49691da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
49701da177e4SLinus Torvalds 			      u16 sclass)
49711da177e4SLinus Torvalds {
49721da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
4973275bb41eSDavid Howells 	u32 sid;
49741da177e4SLinus Torvalds 
497589d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
49761da177e4SLinus Torvalds 	if (!isec)
49771da177e4SLinus Torvalds 		return -ENOMEM;
49781da177e4SLinus Torvalds 
4979275bb41eSDavid Howells 	sid = task_sid(task);
49801da177e4SLinus Torvalds 	isec->sclass = sclass;
4981275bb41eSDavid Howells 	isec->sid = sid;
49821da177e4SLinus Torvalds 	perm->security = isec;
49831da177e4SLinus Torvalds 
49841da177e4SLinus Torvalds 	return 0;
49851da177e4SLinus Torvalds }
49861da177e4SLinus Torvalds 
49871da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
49881da177e4SLinus Torvalds {
49891da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
49901da177e4SLinus Torvalds 	perm->security = NULL;
49911da177e4SLinus Torvalds 	kfree(isec);
49921da177e4SLinus Torvalds }
49931da177e4SLinus Torvalds 
49941da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
49951da177e4SLinus Torvalds {
49961da177e4SLinus Torvalds 	struct msg_security_struct *msec;
49971da177e4SLinus Torvalds 
499889d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
49991da177e4SLinus Torvalds 	if (!msec)
50001da177e4SLinus Torvalds 		return -ENOMEM;
50011da177e4SLinus Torvalds 
50021da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
50031da177e4SLinus Torvalds 	msg->security = msec;
50041da177e4SLinus Torvalds 
50051da177e4SLinus Torvalds 	return 0;
50061da177e4SLinus Torvalds }
50071da177e4SLinus Torvalds 
50081da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
50091da177e4SLinus Torvalds {
50101da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
50111da177e4SLinus Torvalds 
50121da177e4SLinus Torvalds 	msg->security = NULL;
50131da177e4SLinus Torvalds 	kfree(msec);
50141da177e4SLinus Torvalds }
50151da177e4SLinus Torvalds 
50161da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
50176af963f1SStephen Smalley 			u32 perms)
50181da177e4SLinus Torvalds {
50191da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50202bf49690SThomas Liu 	struct common_audit_data ad;
5021275bb41eSDavid Howells 	u32 sid = current_sid();
50221da177e4SLinus Torvalds 
50231da177e4SLinus Torvalds 	isec = ipc_perms->security;
50241da177e4SLinus Torvalds 
502550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50261da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
50271da177e4SLinus Torvalds 
5028275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
50291da177e4SLinus Torvalds }
50301da177e4SLinus Torvalds 
50311da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
50321da177e4SLinus Torvalds {
50331da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
50341da177e4SLinus Torvalds }
50351da177e4SLinus Torvalds 
50361da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
50371da177e4SLinus Torvalds {
50381da177e4SLinus Torvalds 	msg_msg_free_security(msg);
50391da177e4SLinus Torvalds }
50401da177e4SLinus Torvalds 
50411da177e4SLinus Torvalds /* message queue security operations */
50421da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
50431da177e4SLinus Torvalds {
50441da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50452bf49690SThomas Liu 	struct common_audit_data ad;
5046275bb41eSDavid Howells 	u32 sid = current_sid();
50471da177e4SLinus Torvalds 	int rc;
50481da177e4SLinus Torvalds 
50491da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
50501da177e4SLinus Torvalds 	if (rc)
50511da177e4SLinus Torvalds 		return rc;
50521da177e4SLinus Torvalds 
50531da177e4SLinus Torvalds 	isec = msq->q_perm.security;
50541da177e4SLinus Torvalds 
505550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50561da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
50571da177e4SLinus Torvalds 
5058275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
50591da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
50601da177e4SLinus Torvalds 	if (rc) {
50611da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
50621da177e4SLinus Torvalds 		return rc;
50631da177e4SLinus Torvalds 	}
50641da177e4SLinus Torvalds 	return 0;
50651da177e4SLinus Torvalds }
50661da177e4SLinus Torvalds 
50671da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
50681da177e4SLinus Torvalds {
50691da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
50701da177e4SLinus Torvalds }
50711da177e4SLinus Torvalds 
50721da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
50731da177e4SLinus Torvalds {
50741da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50752bf49690SThomas Liu 	struct common_audit_data ad;
5076275bb41eSDavid Howells 	u32 sid = current_sid();
50771da177e4SLinus Torvalds 
50781da177e4SLinus Torvalds 	isec = msq->q_perm.security;
50791da177e4SLinus Torvalds 
508050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50811da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
50821da177e4SLinus Torvalds 
5083275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
50841da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
50851da177e4SLinus Torvalds }
50861da177e4SLinus Torvalds 
50871da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
50881da177e4SLinus Torvalds {
50891da177e4SLinus Torvalds 	int err;
50901da177e4SLinus Torvalds 	int perms;
50911da177e4SLinus Torvalds 
50921da177e4SLinus Torvalds 	switch (cmd) {
50931da177e4SLinus Torvalds 	case IPC_INFO:
50941da177e4SLinus Torvalds 	case MSG_INFO:
50951da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
50961da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
50971da177e4SLinus Torvalds 	case IPC_STAT:
50981da177e4SLinus Torvalds 	case MSG_STAT:
50991da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
51001da177e4SLinus Torvalds 		break;
51011da177e4SLinus Torvalds 	case IPC_SET:
51021da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
51031da177e4SLinus Torvalds 		break;
51041da177e4SLinus Torvalds 	case IPC_RMID:
51051da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
51061da177e4SLinus Torvalds 		break;
51071da177e4SLinus Torvalds 	default:
51081da177e4SLinus Torvalds 		return 0;
51091da177e4SLinus Torvalds 	}
51101da177e4SLinus Torvalds 
51116af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
51121da177e4SLinus Torvalds 	return err;
51131da177e4SLinus Torvalds }
51141da177e4SLinus Torvalds 
51151da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
51161da177e4SLinus Torvalds {
51171da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51181da177e4SLinus Torvalds 	struct msg_security_struct *msec;
51192bf49690SThomas Liu 	struct common_audit_data ad;
5120275bb41eSDavid Howells 	u32 sid = current_sid();
51211da177e4SLinus Torvalds 	int rc;
51221da177e4SLinus Torvalds 
51231da177e4SLinus Torvalds 	isec = msq->q_perm.security;
51241da177e4SLinus Torvalds 	msec = msg->security;
51251da177e4SLinus Torvalds 
51261da177e4SLinus Torvalds 	/*
51271da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
51281da177e4SLinus Torvalds 	 */
51291da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
51301da177e4SLinus Torvalds 		/*
51311da177e4SLinus Torvalds 		 * Compute new sid based on current process and
51321da177e4SLinus Torvalds 		 * message queue this message will be stored in
51331da177e4SLinus Torvalds 		 */
5134275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5135652bb9b0SEric Paris 					     NULL, &msec->sid);
51361da177e4SLinus Torvalds 		if (rc)
51371da177e4SLinus Torvalds 			return rc;
51381da177e4SLinus Torvalds 	}
51391da177e4SLinus Torvalds 
514050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51411da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
51421da177e4SLinus Torvalds 
51431da177e4SLinus Torvalds 	/* Can this process write to the queue? */
5144275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
51451da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
51461da177e4SLinus Torvalds 	if (!rc)
51471da177e4SLinus Torvalds 		/* Can this process send the message */
5148275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5149275bb41eSDavid Howells 				  MSG__SEND, &ad);
51501da177e4SLinus Torvalds 	if (!rc)
51511da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
5152275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5153275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
51541da177e4SLinus Torvalds 
51551da177e4SLinus Torvalds 	return rc;
51561da177e4SLinus Torvalds }
51571da177e4SLinus Torvalds 
51581da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
51591da177e4SLinus Torvalds 				    struct task_struct *target,
51601da177e4SLinus Torvalds 				    long type, int mode)
51611da177e4SLinus Torvalds {
51621da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51631da177e4SLinus Torvalds 	struct msg_security_struct *msec;
51642bf49690SThomas Liu 	struct common_audit_data ad;
5165275bb41eSDavid Howells 	u32 sid = task_sid(target);
51661da177e4SLinus Torvalds 	int rc;
51671da177e4SLinus Torvalds 
51681da177e4SLinus Torvalds 	isec = msq->q_perm.security;
51691da177e4SLinus Torvalds 	msec = msg->security;
51701da177e4SLinus Torvalds 
517150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51721da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
51731da177e4SLinus Torvalds 
5174275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
51751da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
51761da177e4SLinus Torvalds 	if (!rc)
5177275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
51781da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
51791da177e4SLinus Torvalds 	return rc;
51801da177e4SLinus Torvalds }
51811da177e4SLinus Torvalds 
51821da177e4SLinus Torvalds /* Shared Memory security operations */
51831da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
51841da177e4SLinus Torvalds {
51851da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51862bf49690SThomas Liu 	struct common_audit_data ad;
5187275bb41eSDavid Howells 	u32 sid = current_sid();
51881da177e4SLinus Torvalds 	int rc;
51891da177e4SLinus Torvalds 
51901da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
51911da177e4SLinus Torvalds 	if (rc)
51921da177e4SLinus Torvalds 		return rc;
51931da177e4SLinus Torvalds 
51941da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
51951da177e4SLinus Torvalds 
519650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51971da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
51981da177e4SLinus Torvalds 
5199275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
52001da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
52011da177e4SLinus Torvalds 	if (rc) {
52021da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
52031da177e4SLinus Torvalds 		return rc;
52041da177e4SLinus Torvalds 	}
52051da177e4SLinus Torvalds 	return 0;
52061da177e4SLinus Torvalds }
52071da177e4SLinus Torvalds 
52081da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
52091da177e4SLinus Torvalds {
52101da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
52111da177e4SLinus Torvalds }
52121da177e4SLinus Torvalds 
52131da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
52141da177e4SLinus Torvalds {
52151da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52162bf49690SThomas Liu 	struct common_audit_data ad;
5217275bb41eSDavid Howells 	u32 sid = current_sid();
52181da177e4SLinus Torvalds 
52191da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
52201da177e4SLinus Torvalds 
522150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52221da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
52231da177e4SLinus Torvalds 
5224275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
52251da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
52261da177e4SLinus Torvalds }
52271da177e4SLinus Torvalds 
52281da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
52291da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
52301da177e4SLinus Torvalds {
52311da177e4SLinus Torvalds 	int perms;
52321da177e4SLinus Torvalds 	int err;
52331da177e4SLinus Torvalds 
52341da177e4SLinus Torvalds 	switch (cmd) {
52351da177e4SLinus Torvalds 	case IPC_INFO:
52361da177e4SLinus Torvalds 	case SHM_INFO:
52371da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
52381da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
52391da177e4SLinus Torvalds 	case IPC_STAT:
52401da177e4SLinus Torvalds 	case SHM_STAT:
52411da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
52421da177e4SLinus Torvalds 		break;
52431da177e4SLinus Torvalds 	case IPC_SET:
52441da177e4SLinus Torvalds 		perms = SHM__SETATTR;
52451da177e4SLinus Torvalds 		break;
52461da177e4SLinus Torvalds 	case SHM_LOCK:
52471da177e4SLinus Torvalds 	case SHM_UNLOCK:
52481da177e4SLinus Torvalds 		perms = SHM__LOCK;
52491da177e4SLinus Torvalds 		break;
52501da177e4SLinus Torvalds 	case IPC_RMID:
52511da177e4SLinus Torvalds 		perms = SHM__DESTROY;
52521da177e4SLinus Torvalds 		break;
52531da177e4SLinus Torvalds 	default:
52541da177e4SLinus Torvalds 		return 0;
52551da177e4SLinus Torvalds 	}
52561da177e4SLinus Torvalds 
52576af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
52581da177e4SLinus Torvalds 	return err;
52591da177e4SLinus Torvalds }
52601da177e4SLinus Torvalds 
52611da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
52621da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
52631da177e4SLinus Torvalds {
52641da177e4SLinus Torvalds 	u32 perms;
52651da177e4SLinus Torvalds 
52661da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
52671da177e4SLinus Torvalds 		perms = SHM__READ;
52681da177e4SLinus Torvalds 	else
52691da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
52701da177e4SLinus Torvalds 
52716af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
52721da177e4SLinus Torvalds }
52731da177e4SLinus Torvalds 
52741da177e4SLinus Torvalds /* Semaphore security operations */
52751da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
52761da177e4SLinus Torvalds {
52771da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52782bf49690SThomas Liu 	struct common_audit_data ad;
5279275bb41eSDavid Howells 	u32 sid = current_sid();
52801da177e4SLinus Torvalds 	int rc;
52811da177e4SLinus Torvalds 
52821da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
52831da177e4SLinus Torvalds 	if (rc)
52841da177e4SLinus Torvalds 		return rc;
52851da177e4SLinus Torvalds 
52861da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
52871da177e4SLinus Torvalds 
528850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52891da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
52901da177e4SLinus Torvalds 
5291275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
52921da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
52931da177e4SLinus Torvalds 	if (rc) {
52941da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
52951da177e4SLinus Torvalds 		return rc;
52961da177e4SLinus Torvalds 	}
52971da177e4SLinus Torvalds 	return 0;
52981da177e4SLinus Torvalds }
52991da177e4SLinus Torvalds 
53001da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
53011da177e4SLinus Torvalds {
53021da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
53031da177e4SLinus Torvalds }
53041da177e4SLinus Torvalds 
53051da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
53061da177e4SLinus Torvalds {
53071da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53082bf49690SThomas Liu 	struct common_audit_data ad;
5309275bb41eSDavid Howells 	u32 sid = current_sid();
53101da177e4SLinus Torvalds 
53111da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
53121da177e4SLinus Torvalds 
531350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53141da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
53151da177e4SLinus Torvalds 
5316275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
53171da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
53181da177e4SLinus Torvalds }
53191da177e4SLinus Torvalds 
53201da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
53211da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
53221da177e4SLinus Torvalds {
53231da177e4SLinus Torvalds 	int err;
53241da177e4SLinus Torvalds 	u32 perms;
53251da177e4SLinus Torvalds 
53261da177e4SLinus Torvalds 	switch (cmd) {
53271da177e4SLinus Torvalds 	case IPC_INFO:
53281da177e4SLinus Torvalds 	case SEM_INFO:
53291da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
53301da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
53311da177e4SLinus Torvalds 	case GETPID:
53321da177e4SLinus Torvalds 	case GETNCNT:
53331da177e4SLinus Torvalds 	case GETZCNT:
53341da177e4SLinus Torvalds 		perms = SEM__GETATTR;
53351da177e4SLinus Torvalds 		break;
53361da177e4SLinus Torvalds 	case GETVAL:
53371da177e4SLinus Torvalds 	case GETALL:
53381da177e4SLinus Torvalds 		perms = SEM__READ;
53391da177e4SLinus Torvalds 		break;
53401da177e4SLinus Torvalds 	case SETVAL:
53411da177e4SLinus Torvalds 	case SETALL:
53421da177e4SLinus Torvalds 		perms = SEM__WRITE;
53431da177e4SLinus Torvalds 		break;
53441da177e4SLinus Torvalds 	case IPC_RMID:
53451da177e4SLinus Torvalds 		perms = SEM__DESTROY;
53461da177e4SLinus Torvalds 		break;
53471da177e4SLinus Torvalds 	case IPC_SET:
53481da177e4SLinus Torvalds 		perms = SEM__SETATTR;
53491da177e4SLinus Torvalds 		break;
53501da177e4SLinus Torvalds 	case IPC_STAT:
53511da177e4SLinus Torvalds 	case SEM_STAT:
53521da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
53531da177e4SLinus Torvalds 		break;
53541da177e4SLinus Torvalds 	default:
53551da177e4SLinus Torvalds 		return 0;
53561da177e4SLinus Torvalds 	}
53571da177e4SLinus Torvalds 
53586af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
53591da177e4SLinus Torvalds 	return err;
53601da177e4SLinus Torvalds }
53611da177e4SLinus Torvalds 
53621da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
53631da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
53641da177e4SLinus Torvalds {
53651da177e4SLinus Torvalds 	u32 perms;
53661da177e4SLinus Torvalds 
53671da177e4SLinus Torvalds 	if (alter)
53681da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
53691da177e4SLinus Torvalds 	else
53701da177e4SLinus Torvalds 		perms = SEM__READ;
53711da177e4SLinus Torvalds 
53726af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
53731da177e4SLinus Torvalds }
53741da177e4SLinus Torvalds 
53751da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
53761da177e4SLinus Torvalds {
53771da177e4SLinus Torvalds 	u32 av = 0;
53781da177e4SLinus Torvalds 
53791da177e4SLinus Torvalds 	av = 0;
53801da177e4SLinus Torvalds 	if (flag & S_IRUGO)
53811da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
53821da177e4SLinus Torvalds 	if (flag & S_IWUGO)
53831da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
53841da177e4SLinus Torvalds 
53851da177e4SLinus Torvalds 	if (av == 0)
53861da177e4SLinus Torvalds 		return 0;
53871da177e4SLinus Torvalds 
53886af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
53891da177e4SLinus Torvalds }
53901da177e4SLinus Torvalds 
5391713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5392713a04aeSAhmed S. Darwish {
5393713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5394713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5395713a04aeSAhmed S. Darwish }
5396713a04aeSAhmed S. Darwish 
53971da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
53981da177e4SLinus Torvalds {
53991da177e4SLinus Torvalds 	if (inode)
54001da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
54011da177e4SLinus Torvalds }
54021da177e4SLinus Torvalds 
54031da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
540404ff9708SAl Viro 			       char *name, char **value)
54051da177e4SLinus Torvalds {
5406275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
54078c8570fbSDustin Kirkland 	u32 sid;
54081da177e4SLinus Torvalds 	int error;
540904ff9708SAl Viro 	unsigned len;
54101da177e4SLinus Torvalds 
54111da177e4SLinus Torvalds 	if (current != p) {
54123b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
54131da177e4SLinus Torvalds 		if (error)
54141da177e4SLinus Torvalds 			return error;
54151da177e4SLinus Torvalds 	}
54161da177e4SLinus Torvalds 
5417275bb41eSDavid Howells 	rcu_read_lock();
5418275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
54191da177e4SLinus Torvalds 
54201da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5421275bb41eSDavid Howells 		sid = __tsec->sid;
54221da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5423275bb41eSDavid Howells 		sid = __tsec->osid;
54241da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5425275bb41eSDavid Howells 		sid = __tsec->exec_sid;
54261da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5427275bb41eSDavid Howells 		sid = __tsec->create_sid;
54284eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5429275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
543042c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5431275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
54321da177e4SLinus Torvalds 	else
5433275bb41eSDavid Howells 		goto invalid;
5434275bb41eSDavid Howells 	rcu_read_unlock();
54351da177e4SLinus Torvalds 
54361da177e4SLinus Torvalds 	if (!sid)
54371da177e4SLinus Torvalds 		return 0;
54381da177e4SLinus Torvalds 
543904ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
544004ff9708SAl Viro 	if (error)
544104ff9708SAl Viro 		return error;
544204ff9708SAl Viro 	return len;
5443275bb41eSDavid Howells 
5444275bb41eSDavid Howells invalid:
5445275bb41eSDavid Howells 	rcu_read_unlock();
5446275bb41eSDavid Howells 	return -EINVAL;
54471da177e4SLinus Torvalds }
54481da177e4SLinus Torvalds 
54491da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
54501da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
54511da177e4SLinus Torvalds {
54521da177e4SLinus Torvalds 	struct task_security_struct *tsec;
54530356357cSRoland McGrath 	struct task_struct *tracer;
5454d84f4f99SDavid Howells 	struct cred *new;
5455d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
54561da177e4SLinus Torvalds 	int error;
54571da177e4SLinus Torvalds 	char *str = value;
54581da177e4SLinus Torvalds 
54591da177e4SLinus Torvalds 	if (current != p) {
54601da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
54611da177e4SLinus Torvalds 		   security attributes. */
54621da177e4SLinus Torvalds 		return -EACCES;
54631da177e4SLinus Torvalds 	}
54641da177e4SLinus Torvalds 
54651da177e4SLinus Torvalds 	/*
54661da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
54671da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
54681da177e4SLinus Torvalds 	 * above restriction is ever removed.
54691da177e4SLinus Torvalds 	 */
54701da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
54713b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
54721da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
54733b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
54744eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
54753b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
547642c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
54773b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
54781da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
54793b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
54801da177e4SLinus Torvalds 	else
54811da177e4SLinus Torvalds 		error = -EINVAL;
54821da177e4SLinus Torvalds 	if (error)
54831da177e4SLinus Torvalds 		return error;
54841da177e4SLinus Torvalds 
54851da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
54861da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
54871da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
54881da177e4SLinus Torvalds 			str[size-1] = 0;
54891da177e4SLinus Torvalds 			size--;
54901da177e4SLinus Torvalds 		}
54911da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
549212b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
5493d6ea83ecSEric Paris 			if (!capable(CAP_MAC_ADMIN)) {
5494d6ea83ecSEric Paris 				struct audit_buffer *ab;
5495d6ea83ecSEric Paris 				size_t audit_size;
5496d6ea83ecSEric Paris 
5497d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
5498d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
5499d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
5500d6ea83ecSEric Paris 					audit_size = size - 1;
5501d6ea83ecSEric Paris 				else
5502d6ea83ecSEric Paris 					audit_size = size;
5503d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5504d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
5505d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
5506d6ea83ecSEric Paris 				audit_log_end(ab);
5507d6ea83ecSEric Paris 
550812b29f34SStephen Smalley 				return error;
5509d6ea83ecSEric Paris 			}
551012b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
551112b29f34SStephen Smalley 							      &sid);
551212b29f34SStephen Smalley 		}
55131da177e4SLinus Torvalds 		if (error)
55141da177e4SLinus Torvalds 			return error;
55151da177e4SLinus Torvalds 	}
55161da177e4SLinus Torvalds 
5517d84f4f99SDavid Howells 	new = prepare_creds();
5518d84f4f99SDavid Howells 	if (!new)
5519d84f4f99SDavid Howells 		return -ENOMEM;
5520d84f4f99SDavid Howells 
55211da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
55221da177e4SLinus Torvalds 	   performed during the actual operation (execve,
55231da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5524d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
55251da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
55261da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5527d84f4f99SDavid Howells 	tsec = new->security;
5528d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
55291da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5530d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
55311da177e4SLinus Torvalds 		tsec->create_sid = sid;
5532d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
55334eb582cfSMichael LeMay 		error = may_create_key(sid, p);
55344eb582cfSMichael LeMay 		if (error)
5535d84f4f99SDavid Howells 			goto abort_change;
55364eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5537d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
553842c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5539d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5540d84f4f99SDavid Howells 		error = -EINVAL;
55411da177e4SLinus Torvalds 		if (sid == 0)
5542d84f4f99SDavid Howells 			goto abort_change;
5543d9250deaSKaiGai Kohei 
5544d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5545d84f4f99SDavid Howells 		error = -EPERM;
55465bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
5547d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5548d84f4f99SDavid Howells 			if (error)
5549d84f4f99SDavid Howells 				goto abort_change;
55501da177e4SLinus Torvalds 		}
55511da177e4SLinus Torvalds 
55521da177e4SLinus Torvalds 		/* Check permissions for the transition. */
55531da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
55541da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
55551da177e4SLinus Torvalds 		if (error)
5556d84f4f99SDavid Howells 			goto abort_change;
55571da177e4SLinus Torvalds 
55581da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
55591da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5560d84f4f99SDavid Howells 		ptsid = 0;
55611da177e4SLinus Torvalds 		task_lock(p);
556206d98473STejun Heo 		tracer = ptrace_parent(p);
5563d84f4f99SDavid Howells 		if (tracer)
5564d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
55651da177e4SLinus Torvalds 		task_unlock(p);
55661da177e4SLinus Torvalds 
5567d84f4f99SDavid Howells 		if (tracer) {
5568d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5569d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5570d84f4f99SDavid Howells 			if (error)
5571d84f4f99SDavid Howells 				goto abort_change;
5572d84f4f99SDavid Howells 		}
5573d84f4f99SDavid Howells 
5574d84f4f99SDavid Howells 		tsec->sid = sid;
5575d84f4f99SDavid Howells 	} else {
5576d84f4f99SDavid Howells 		error = -EINVAL;
5577d84f4f99SDavid Howells 		goto abort_change;
5578d84f4f99SDavid Howells 	}
5579d84f4f99SDavid Howells 
5580d84f4f99SDavid Howells 	commit_creds(new);
55811da177e4SLinus Torvalds 	return size;
5582d84f4f99SDavid Howells 
5583d84f4f99SDavid Howells abort_change:
5584d84f4f99SDavid Howells 	abort_creds(new);
5585d84f4f99SDavid Howells 	return error;
55861da177e4SLinus Torvalds }
55871da177e4SLinus Torvalds 
5588746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
5589746df9b5SDavid Quigley {
5590746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5591746df9b5SDavid Quigley }
5592746df9b5SDavid Quigley 
5593dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5594dc49c1f9SCatherine Zhang {
5595dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5596dc49c1f9SCatherine Zhang }
5597dc49c1f9SCatherine Zhang 
55987bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
559963cb3449SDavid Howells {
560063cb3449SDavid Howells 	return security_context_to_sid(secdata, seclen, secid);
560163cb3449SDavid Howells }
560263cb3449SDavid Howells 
5603dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5604dc49c1f9SCatherine Zhang {
5605dc49c1f9SCatherine Zhang 	kfree(secdata);
5606dc49c1f9SCatherine Zhang }
5607dc49c1f9SCatherine Zhang 
56081ee65e37SDavid P. Quigley /*
56091ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
56101ee65e37SDavid P. Quigley  */
56111ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
56121ee65e37SDavid P. Quigley {
56131ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
56141ee65e37SDavid P. Quigley }
56151ee65e37SDavid P. Quigley 
56161ee65e37SDavid P. Quigley /*
56171ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
56181ee65e37SDavid P. Quigley  */
56191ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
56201ee65e37SDavid P. Quigley {
56211ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
56221ee65e37SDavid P. Quigley }
56231ee65e37SDavid P. Quigley 
56241ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
56251ee65e37SDavid P. Quigley {
56261ee65e37SDavid P. Quigley 	int len = 0;
56271ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
56281ee65e37SDavid P. Quigley 						ctx, true);
56291ee65e37SDavid P. Quigley 	if (len < 0)
56301ee65e37SDavid P. Quigley 		return len;
56311ee65e37SDavid P. Quigley 	*ctxlen = len;
56321ee65e37SDavid P. Quigley 	return 0;
56331ee65e37SDavid P. Quigley }
5634d720024eSMichael LeMay #ifdef CONFIG_KEYS
5635d720024eSMichael LeMay 
5636d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
56377e047ef5SDavid Howells 			     unsigned long flags)
5638d720024eSMichael LeMay {
5639d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5640d720024eSMichael LeMay 	struct key_security_struct *ksec;
5641d720024eSMichael LeMay 
5642d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5643d720024eSMichael LeMay 	if (!ksec)
5644d720024eSMichael LeMay 		return -ENOMEM;
5645d720024eSMichael LeMay 
5646d84f4f99SDavid Howells 	tsec = cred->security;
5647d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5648d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
56494eb582cfSMichael LeMay 	else
5650d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5651d720024eSMichael LeMay 
5652275bb41eSDavid Howells 	k->security = ksec;
5653d720024eSMichael LeMay 	return 0;
5654d720024eSMichael LeMay }
5655d720024eSMichael LeMay 
5656d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5657d720024eSMichael LeMay {
5658d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5659d720024eSMichael LeMay 
5660d720024eSMichael LeMay 	k->security = NULL;
5661d720024eSMichael LeMay 	kfree(ksec);
5662d720024eSMichael LeMay }
5663d720024eSMichael LeMay 
5664d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5665d84f4f99SDavid Howells 				  const struct cred *cred,
5666d720024eSMichael LeMay 				  key_perm_t perm)
5667d720024eSMichael LeMay {
5668d720024eSMichael LeMay 	struct key *key;
5669d720024eSMichael LeMay 	struct key_security_struct *ksec;
5670275bb41eSDavid Howells 	u32 sid;
5671d720024eSMichael LeMay 
5672d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5673d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5674d720024eSMichael LeMay 	   appear to be created. */
5675d720024eSMichael LeMay 	if (perm == 0)
5676d720024eSMichael LeMay 		return 0;
5677d720024eSMichael LeMay 
5678d84f4f99SDavid Howells 	sid = cred_sid(cred);
5679275bb41eSDavid Howells 
5680275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5681275bb41eSDavid Howells 	ksec = key->security;
5682275bb41eSDavid Howells 
5683275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5684d720024eSMichael LeMay }
5685d720024eSMichael LeMay 
568670a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
568770a5bb72SDavid Howells {
568870a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
568970a5bb72SDavid Howells 	char *context = NULL;
569070a5bb72SDavid Howells 	unsigned len;
569170a5bb72SDavid Howells 	int rc;
569270a5bb72SDavid Howells 
569370a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
569470a5bb72SDavid Howells 	if (!rc)
569570a5bb72SDavid Howells 		rc = len;
569670a5bb72SDavid Howells 	*_buffer = context;
569770a5bb72SDavid Howells 	return rc;
569870a5bb72SDavid Howells }
569970a5bb72SDavid Howells 
5700d720024eSMichael LeMay #endif
5701d720024eSMichael LeMay 
57021da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5703076c54c5SAhmed S. Darwish 	.name =				"selinux",
5704076c54c5SAhmed S. Darwish 
57059e48858fSIngo Molnar 	.ptrace_access_check =		selinux_ptrace_access_check,
57065cd9c58fSDavid Howells 	.ptrace_traceme =		selinux_ptrace_traceme,
57071da177e4SLinus Torvalds 	.capget =			selinux_capget,
5708d84f4f99SDavid Howells 	.capset =			selinux_capset,
57091da177e4SLinus Torvalds 	.capable =			selinux_capable,
57101da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
57111da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
57121da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
57131da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
57141da177e4SLinus Torvalds 
57151da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
57161da177e4SLinus Torvalds 
5717a6f76f23SDavid Howells 	.bprm_set_creds =		selinux_bprm_set_creds,
5718a6f76f23SDavid Howells 	.bprm_committing_creds =	selinux_bprm_committing_creds,
5719a6f76f23SDavid Howells 	.bprm_committed_creds =		selinux_bprm_committed_creds,
57201da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
57211da177e4SLinus Torvalds 
57221da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
57231da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
57241da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
5725026eb167SEric Paris 	.sb_remount =			selinux_sb_remount,
57261da177e4SLinus Torvalds 	.sb_kern_mount =		selinux_sb_kern_mount,
57272069f457SEric Paris 	.sb_show_options =		selinux_sb_show_options,
57281da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
57291da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
57301da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5731c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5732c9180a57SEric Paris 	.sb_clone_mnt_opts =		selinux_sb_clone_mnt_opts,
5733e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5734e0007529SEric Paris 
5735d47be3dfSDavid Quigley 	.dentry_init_security =		selinux_dentry_init_security,
57361da177e4SLinus Torvalds 
57371da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
57381da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
57395e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
57401da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
57411da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
57421da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
57431da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
57441da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
57451da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
57461da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
57471da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
57481da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
57491da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
57501da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
57511da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
57521da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
57531da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
57541da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
57551da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
57561da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
57571da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
57581da177e4SLinus Torvalds 	.inode_getsecurity =		selinux_inode_getsecurity,
57591da177e4SLinus Torvalds 	.inode_setsecurity =		selinux_inode_setsecurity,
57601da177e4SLinus Torvalds 	.inode_listsecurity =		selinux_inode_listsecurity,
5761713a04aeSAhmed S. Darwish 	.inode_getsecid =		selinux_inode_getsecid,
57621da177e4SLinus Torvalds 
57631da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
57641da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
57651da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
57661da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
5767e5467859SAl Viro 	.mmap_file =			selinux_mmap_file,
5768e5467859SAl Viro 	.mmap_addr =			selinux_mmap_addr,
57691da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
57701da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
57711da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
57721da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
57731da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
57741da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
57751da177e4SLinus Torvalds 
577683d49856SEric Paris 	.file_open =			selinux_file_open,
5777788e7dd4SYuichi Nakamura 
57781da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
5779ee18d64cSDavid Howells 	.cred_alloc_blank =		selinux_cred_alloc_blank,
5780f1752eecSDavid Howells 	.cred_free =			selinux_cred_free,
5781d84f4f99SDavid Howells 	.cred_prepare =			selinux_cred_prepare,
5782ee18d64cSDavid Howells 	.cred_transfer =		selinux_cred_transfer,
57833a3b7ce9SDavid Howells 	.kernel_act_as =		selinux_kernel_act_as,
57843a3b7ce9SDavid Howells 	.kernel_create_files_as =	selinux_kernel_create_files_as,
578525354c4fSEric Paris 	.kernel_module_request =	selinux_kernel_module_request,
57861da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
57871da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
57881da177e4SLinus Torvalds 	.task_getsid =			selinux_task_getsid,
5789f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
57901da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
579103e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5792a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
57931da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
57941da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
57951da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
579635601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
57971da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
57981da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
57991da177e4SLinus Torvalds 	.task_to_inode =		selinux_task_to_inode,
58001da177e4SLinus Torvalds 
58011da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5802713a04aeSAhmed S. Darwish 	.ipc_getsecid =			selinux_ipc_getsecid,
58031da177e4SLinus Torvalds 
58041da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
58051da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
58061da177e4SLinus Torvalds 
58071da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
58081da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
58091da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
58101da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
58111da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
58121da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
58131da177e4SLinus Torvalds 
58141da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
58151da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
58161da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
58171da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
58181da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
58191da177e4SLinus Torvalds 
58201da177e4SLinus Torvalds 	.sem_alloc_security =		selinux_sem_alloc_security,
58211da177e4SLinus Torvalds 	.sem_free_security =		selinux_sem_free_security,
58221da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
58231da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
58241da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
58251da177e4SLinus Torvalds 
58261da177e4SLinus Torvalds 	.d_instantiate =		selinux_d_instantiate,
58271da177e4SLinus Torvalds 
58281da177e4SLinus Torvalds 	.getprocattr =			selinux_getprocattr,
58291da177e4SLinus Torvalds 	.setprocattr =			selinux_setprocattr,
58301da177e4SLinus Torvalds 
5831746df9b5SDavid Quigley 	.ismaclabel =			selinux_ismaclabel,
5832dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
583363cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5834dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
58351ee65e37SDavid P. Quigley 	.inode_notifysecctx =		selinux_inode_notifysecctx,
58361ee65e37SDavid P. Quigley 	.inode_setsecctx =		selinux_inode_setsecctx,
58371ee65e37SDavid P. Quigley 	.inode_getsecctx =		selinux_inode_getsecctx,
5838dc49c1f9SCatherine Zhang 
58391da177e4SLinus Torvalds 	.unix_stream_connect =		selinux_socket_unix_stream_connect,
58401da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
58411da177e4SLinus Torvalds 
58421da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
58431da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
58441da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
58451da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
58461da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
58471da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
58481da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
58491da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
58501da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
58511da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
58521da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
58531da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
58541da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
58551da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
58562c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
58572c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
58581da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
58591da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
5860892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
5861beb8d13bSVenkat Yekkirala 	.sk_getsecid =			selinux_sk_getsecid,
58624237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
58634237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
58644237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
58656b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
58662606fd1fSEric Paris 	.secmark_relabel_packet =	selinux_secmark_relabel_packet,
58672606fd1fSEric Paris 	.secmark_refcount_inc =		selinux_secmark_refcount_inc,
58682606fd1fSEric Paris 	.secmark_refcount_dec =		selinux_secmark_refcount_dec,
58694237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
58705dbbaf2dSPaul Moore 	.tun_dev_alloc_security =	selinux_tun_dev_alloc_security,
58715dbbaf2dSPaul Moore 	.tun_dev_free_security =	selinux_tun_dev_free_security,
5872ed6d76e4SPaul Moore 	.tun_dev_create =		selinux_tun_dev_create,
58735dbbaf2dSPaul Moore 	.tun_dev_attach_queue =		selinux_tun_dev_attach_queue,
5874ed6d76e4SPaul Moore 	.tun_dev_attach =		selinux_tun_dev_attach,
58755dbbaf2dSPaul Moore 	.tun_dev_open =			selinux_tun_dev_open,
5876ca10b9e9SEric Dumazet 	.skb_owned_by =			selinux_skb_owned_by,
5877d28d1e08STrent Jaeger 
5878d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5879d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
5880d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
5881d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
5882c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
58832e5aa866SPaul Moore 	.xfrm_state_alloc =		selinux_xfrm_state_alloc,
58842e5aa866SPaul Moore 	.xfrm_state_alloc_acquire =	selinux_xfrm_state_alloc_acquire,
5885d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
5886c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
5887d28d1e08STrent Jaeger 	.xfrm_policy_lookup =		selinux_xfrm_policy_lookup,
5888e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
5889e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
58901da177e4SLinus Torvalds #endif
5891d720024eSMichael LeMay 
5892d720024eSMichael LeMay #ifdef CONFIG_KEYS
5893d720024eSMichael LeMay 	.key_alloc =			selinux_key_alloc,
5894d720024eSMichael LeMay 	.key_free =			selinux_key_free,
5895d720024eSMichael LeMay 	.key_permission =		selinux_key_permission,
589670a5bb72SDavid Howells 	.key_getsecurity =		selinux_key_getsecurity,
5897d720024eSMichael LeMay #endif
58989d57a7f9SAhmed S. Darwish 
58999d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
59009d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
59019d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
59029d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
59039d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
59049d57a7f9SAhmed S. Darwish #endif
59051da177e4SLinus Torvalds };
59061da177e4SLinus Torvalds 
59071da177e4SLinus Torvalds static __init int selinux_init(void)
59081da177e4SLinus Torvalds {
5909076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
5910076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
5911076c54c5SAhmed S. Darwish 		return 0;
5912076c54c5SAhmed S. Darwish 	}
5913076c54c5SAhmed S. Darwish 
59141da177e4SLinus Torvalds 	if (!selinux_enabled) {
59151da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
59161da177e4SLinus Torvalds 		return 0;
59171da177e4SLinus Torvalds 	}
59181da177e4SLinus Torvalds 
59191da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
59201da177e4SLinus Torvalds 
59211da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
5922d84f4f99SDavid Howells 	cred_init_security();
59231da177e4SLinus Torvalds 
5924fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5925fcaaade1SStephen Smalley 
59267cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
59277cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
592820c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
59291da177e4SLinus Torvalds 	avc_init();
59301da177e4SLinus Torvalds 
59311da177e4SLinus Torvalds 	if (register_security(&selinux_ops))
59321da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
59331da177e4SLinus Torvalds 
5934828dfe1dSEric Paris 	if (selinux_enforcing)
5935fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
5936828dfe1dSEric Paris 	else
5937fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
5938d720024eSMichael LeMay 
59391da177e4SLinus Torvalds 	return 0;
59401da177e4SLinus Torvalds }
59411da177e4SLinus Torvalds 
5942e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
5943e8c26255SAl Viro {
5944e8c26255SAl Viro 	superblock_doinit(sb, NULL);
5945e8c26255SAl Viro }
5946e8c26255SAl Viro 
59471da177e4SLinus Torvalds void selinux_complete_init(void)
59481da177e4SLinus Torvalds {
5949fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
59501da177e4SLinus Torvalds 
59511da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
5952fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
5953e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
59541da177e4SLinus Torvalds }
59551da177e4SLinus Torvalds 
59561da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
59571da177e4SLinus Torvalds    all processes and objects when they are created. */
59581da177e4SLinus Torvalds security_initcall(selinux_init);
59591da177e4SLinus Torvalds 
5960c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
59611da177e4SLinus Torvalds 
5962effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = {
5963effad8dfSPaul Moore 	{
5964effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
59651da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
59662597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
59676e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
59681da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
5969effad8dfSPaul Moore 	},
5970effad8dfSPaul Moore 	{
5971effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
5972effad8dfSPaul Moore 		.owner =	THIS_MODULE,
59732597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
5974effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5975effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5976948bf85cSPaul Moore 	},
5977948bf85cSPaul Moore 	{
5978948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
5979948bf85cSPaul Moore 		.owner =	THIS_MODULE,
59802597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
5981948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
5982948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5983effad8dfSPaul Moore 	}
59841da177e4SLinus Torvalds };
59851da177e4SLinus Torvalds 
59861da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
59871da177e4SLinus Torvalds 
5988effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = {
5989effad8dfSPaul Moore 	{
5990effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
59911da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
59922597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
59936e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
59941da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
5995effad8dfSPaul Moore 	},
5996effad8dfSPaul Moore 	{
5997effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
5998effad8dfSPaul Moore 		.owner =	THIS_MODULE,
59992597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
6000effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
6001effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
6002effad8dfSPaul Moore 	}
60031da177e4SLinus Torvalds };
60041da177e4SLinus Torvalds 
60051da177e4SLinus Torvalds #endif	/* IPV6 */
60061da177e4SLinus Torvalds 
60071da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
60081da177e4SLinus Torvalds {
60091da177e4SLinus Torvalds 	int err = 0;
60101da177e4SLinus Torvalds 
60111da177e4SLinus Torvalds 	if (!selinux_enabled)
60121da177e4SLinus Torvalds 		goto out;
60131da177e4SLinus Torvalds 
6014fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
60151da177e4SLinus Torvalds 
60166c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
60171da177e4SLinus Torvalds 	if (err)
60186c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
60191da177e4SLinus Torvalds 
60201da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
60216c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
60221da177e4SLinus Torvalds 	if (err)
60236c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
60241da177e4SLinus Torvalds #endif	/* IPV6 */
6025d28d1e08STrent Jaeger 
60261da177e4SLinus Torvalds out:
60271da177e4SLinus Torvalds 	return err;
60281da177e4SLinus Torvalds }
60291da177e4SLinus Torvalds 
60301da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
60311da177e4SLinus Torvalds 
60321da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
60331da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
60341da177e4SLinus Torvalds {
6035fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
60361da177e4SLinus Torvalds 
60376c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
60381da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
60396c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
60401da177e4SLinus Torvalds #endif	/* IPV6 */
60411da177e4SLinus Torvalds }
60421da177e4SLinus Torvalds #endif
60431da177e4SLinus Torvalds 
6044c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
60451da177e4SLinus Torvalds 
60461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
60471da177e4SLinus Torvalds #define selinux_nf_ip_exit()
60481da177e4SLinus Torvalds #endif
60491da177e4SLinus Torvalds 
6050c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
60511da177e4SLinus Torvalds 
60521da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6053828dfe1dSEric Paris static int selinux_disabled;
6054828dfe1dSEric Paris 
60551da177e4SLinus Torvalds int selinux_disable(void)
60561da177e4SLinus Torvalds {
60571da177e4SLinus Torvalds 	if (ss_initialized) {
60581da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
60591da177e4SLinus Torvalds 		return -EINVAL;
60601da177e4SLinus Torvalds 	}
60611da177e4SLinus Torvalds 
60621da177e4SLinus Torvalds 	if (selinux_disabled) {
60631da177e4SLinus Torvalds 		/* Only do this once. */
60641da177e4SLinus Torvalds 		return -EINVAL;
60651da177e4SLinus Torvalds 	}
60661da177e4SLinus Torvalds 
60671da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
60681da177e4SLinus Torvalds 
60691da177e4SLinus Torvalds 	selinux_disabled = 1;
607030d55280SStephen Smalley 	selinux_enabled = 0;
60711da177e4SLinus Torvalds 
6072189b3b1cSwzt.wzt@gmail.com 	reset_security_ops();
60731da177e4SLinus Torvalds 
6074af8ff049SEric Paris 	/* Try to destroy the avc node cache */
6075af8ff049SEric Paris 	avc_disable();
6076af8ff049SEric Paris 
60771da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
60781da177e4SLinus Torvalds 	selinux_nf_ip_exit();
60791da177e4SLinus Torvalds 
60801da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
60811da177e4SLinus Torvalds 	exit_sel_fs();
60821da177e4SLinus Torvalds 
60831da177e4SLinus Torvalds 	return 0;
60841da177e4SLinus Torvalds }
60851da177e4SLinus Torvalds #endif
6086