xref: /openbmc/linux/security/selinux/hooks.c (revision 2e08df3c7c4e4e74e3dd5104c100f0bf6288aaa8)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  NSA Security-Enhanced Linux (SELinux) security module
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This file contains the SELinux hook function implementations.
61da177e4SLinus Torvalds  *
77efbb60bSStephen Smalley  *  Authors:  Stephen Smalley, <sds@tycho.nsa.gov>
81da177e4SLinus Torvalds  *	      Chris Vance, <cvance@nai.com>
91da177e4SLinus Torvalds  *	      Wayne Salamon, <wsalamon@nai.com>
101da177e4SLinus Torvalds  *	      James Morris <jmorris@redhat.com>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
132069f457SEric Paris  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
142069f457SEric Paris  *					   Eric Paris <eparis@redhat.com>
151da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
161da177e4SLinus Torvalds  *			    <dgoeddel@trustedcs.com>
17ed6d76e4SPaul Moore  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
1882c21bfaSPaul Moore  *	Paul Moore <paul@paul-moore.com>
19788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
20788e7dd4SYuichi Nakamura  *		       Yuichi Nakamura <ynakam@hitachisoft.jp>
213a976fa6SDaniel Jurgens  *  Copyright (C) 2016 Mellanox Technologies
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/init.h>
250b24dcb7SEric Paris #include <linux/kd.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
27b89999d0SScott Branden #include <linux/kernel_read_file.h>
280d094efeSRoland McGrath #include <linux/tracehook.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
303f07c014SIngo Molnar #include <linux/sched/signal.h>
3129930025SIngo Molnar #include <linux/sched/task.h>
323c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.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>
49442155c1SDavid Howells #include <linux/fs_context.h>
50442155c1SDavid Howells #include <linux/fs_parser.h>
511da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
521da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
531da177e4SLinus Torvalds #include <linux/tty.h>
541da177e4SLinus Torvalds #include <net/icmp.h>
55227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
561da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5747180068SPaul Moore #include <net/inet_connection_sock.h>
58220deb96SPaul Moore #include <net/net_namespace.h>
59d621d35eSPaul Moore #include <net/netlabel.h>
60f5269710SEric Paris #include <linux/uaccess.h>
611da177e4SLinus Torvalds #include <asm/ioctls.h>
6260063497SArun Sharma #include <linux/atomic.h>
631da177e4SLinus Torvalds #include <linux/bitops.h>
641da177e4SLinus Torvalds #include <linux/interrupt.h>
651da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6677954983SHong zhi guo #include <net/netlink.h>
671da177e4SLinus Torvalds #include <linux/tcp.h>
681da177e4SLinus Torvalds #include <linux/udp.h>
692ee92d46SJames Morris #include <linux/dccp.h>
70d452930fSRichard Haines #include <linux/sctp.h>
71d452930fSRichard Haines #include <net/sctp/structs.h>
721da177e4SLinus Torvalds #include <linux/quota.h>
731da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
741da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
751da177e4SLinus Torvalds #include <linux/parser.h>
761da177e4SLinus Torvalds #include <linux/nfs_mount.h>
771da177e4SLinus Torvalds #include <net/ipv6.h>
781da177e4SLinus Torvalds #include <linux/hugetlb.h>
791da177e4SLinus Torvalds #include <linux/personality.h>
801da177e4SLinus Torvalds #include <linux/audit.h>
816931dfc9SEric Paris #include <linux/string.h>
8223970741SEric Paris #include <linux/mutex.h>
83f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8400234592SKees Cook #include <linux/syslog.h>
853486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8644fc7ea0SPaul Gortmaker #include <linux/export.h>
8740401530SAl Viro #include <linux/msg.h>
8840401530SAl Viro #include <linux/shm.h>
89ec27c356SChenbo Feng #include <linux/bpf.h>
90ec882da5SOndrej Mosnacek #include <linux/kernfs.h>
91ec882da5SOndrej Mosnacek #include <linux/stringhash.h>	/* for hashlen_string() */
92e262e32dSDavid Howells #include <uapi/linux/mount.h>
93ac5656d8SAaron Goidel #include <linux/fsnotify.h>
94ac5656d8SAaron Goidel #include <linux/fanotify.h>
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #include "avc.h"
971da177e4SLinus Torvalds #include "objsec.h"
981da177e4SLinus Torvalds #include "netif.h"
99224dfbd8SPaul Moore #include "netnode.h"
1003e112172SPaul Moore #include "netport.h"
101409dcf31SDaniel Jurgens #include "ibpkey.h"
102d28d1e08STrent Jaeger #include "xfrm.h"
103c60475bfSPaul Moore #include "netlabel.h"
1049d57a7f9SAhmed S. Darwish #include "audit.h"
1057b98a585SJames Morris #include "avc_ss.h"
1061da177e4SLinus Torvalds 
107aa8e712cSStephen Smalley struct selinux_state selinux_state;
108aa8e712cSStephen Smalley 
109d621d35eSPaul Moore /* SECMARK reference count */
11056a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
111d621d35eSPaul Moore 
1121da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1136c5a682eSStephen Smalley static int selinux_enforcing_boot __initdata;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1161da177e4SLinus Torvalds {
117f5269710SEric Paris 	unsigned long enforcing;
11829707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
119aa8e712cSStephen Smalley 		selinux_enforcing_boot = enforcing ? 1 : 0;
1201da177e4SLinus Torvalds 	return 1;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
123aa8e712cSStephen Smalley #else
124aa8e712cSStephen Smalley #define selinux_enforcing_boot 1
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1276c5a682eSStephen Smalley int selinux_enabled_boot __initdata = 1;
1281da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1291da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1301da177e4SLinus Torvalds {
131f5269710SEric Paris 	unsigned long enabled;
13229707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
1336c5a682eSStephen Smalley 		selinux_enabled_boot = enabled ? 1 : 0;
1341da177e4SLinus Torvalds 	return 1;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
139aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot =
140aa8e712cSStephen Smalley 	CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141aa8e712cSStephen Smalley 
142aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str)
143aa8e712cSStephen Smalley {
144aa8e712cSStephen Smalley 	unsigned long checkreqprot;
145aa8e712cSStephen Smalley 
146e9c38f9fSStephen Smalley 	if (!kstrtoul(str, 0, &checkreqprot)) {
147aa8e712cSStephen Smalley 		selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148e9c38f9fSStephen Smalley 		if (checkreqprot)
149e9c38f9fSStephen Smalley 			pr_warn("SELinux: checkreqprot set to 1 via kernel parameter.  This is deprecated and will be rejected in a future kernel release.\n");
150e9c38f9fSStephen Smalley 	}
151aa8e712cSStephen Smalley 	return 1;
152aa8e712cSStephen Smalley }
153aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup);
154aa8e712cSStephen Smalley 
155d621d35eSPaul Moore /**
156d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157d621d35eSPaul Moore  *
158d621d35eSPaul Moore  * Description:
159d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
160d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
161d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1622be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1632be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
164d621d35eSPaul Moore  *
165d621d35eSPaul Moore  */
166d621d35eSPaul Moore static int selinux_secmark_enabled(void)
167d621d35eSPaul Moore {
168aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
169aa8e712cSStephen Smalley 		atomic_read(&selinux_secmark_refcount));
1702be4d74fSChris PeBenito }
1712be4d74fSChris PeBenito 
1722be4d74fSChris PeBenito /**
1732be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1742be4d74fSChris PeBenito  *
1752be4d74fSChris PeBenito  * Description:
1762be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1772be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1782be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1792be4d74fSChris PeBenito  * is always considered enabled.
1802be4d74fSChris PeBenito  *
1812be4d74fSChris PeBenito  */
1822be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1832be4d74fSChris PeBenito {
184aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
185aa8e712cSStephen Smalley 		netlbl_enabled() || selinux_xfrm_enabled());
186d621d35eSPaul Moore }
187d621d35eSPaul Moore 
188615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
189615e51fdSPaul Moore {
190615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
191615e51fdSPaul Moore 		sel_netif_flush();
192615e51fdSPaul Moore 		sel_netnode_flush();
193615e51fdSPaul Moore 		sel_netport_flush();
194615e51fdSPaul Moore 		synchronize_net();
195615e51fdSPaul Moore 	}
196615e51fdSPaul Moore 	return 0;
197615e51fdSPaul Moore }
198615e51fdSPaul Moore 
1998f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event)
2008f408ab6SDaniel Jurgens {
201409dcf31SDaniel Jurgens 	if (event == AVC_CALLBACK_RESET) {
202409dcf31SDaniel Jurgens 		sel_ib_pkey_flush();
20342df744cSJanne Karhunen 		call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
204409dcf31SDaniel Jurgens 	}
2058f408ab6SDaniel Jurgens 
2068f408ab6SDaniel Jurgens 	return 0;
2078f408ab6SDaniel Jurgens }
2088f408ab6SDaniel Jurgens 
209d84f4f99SDavid Howells /*
210d84f4f99SDavid Howells  * initialise the security for the init task
211d84f4f99SDavid Howells  */
212d84f4f99SDavid Howells static void cred_init_security(void)
2131da177e4SLinus Torvalds {
2143b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
2151da177e4SLinus Torvalds 	struct task_security_struct *tsec;
2161da177e4SLinus Torvalds 
217bbd3662aSCasey Schaufler 	tsec = selinux_cred(cred);
218d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
221275bb41eSDavid Howells /*
22288e67f3bSDavid Howells  * get the security ID of a set of credentials
22388e67f3bSDavid Howells  */
22488e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
22588e67f3bSDavid Howells {
22688e67f3bSDavid Howells 	const struct task_security_struct *tsec;
22788e67f3bSDavid Howells 
2280c6cfa62SCasey Schaufler 	tsec = selinux_cred(cred);
22988e67f3bSDavid Howells 	return tsec->sid;
23088e67f3bSDavid Howells }
23188e67f3bSDavid Howells 
23288e67f3bSDavid Howells /*
2333b11a1deSDavid Howells  * get the objective security ID of a task
234275bb41eSDavid Howells  */
235eb1231f7SPaul Moore static inline u32 task_sid_obj(const struct task_struct *task)
236275bb41eSDavid Howells {
237275bb41eSDavid Howells 	u32 sid;
238275bb41eSDavid Howells 
239275bb41eSDavid Howells 	rcu_read_lock();
24088e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
241275bb41eSDavid Howells 	rcu_read_unlock();
242275bb41eSDavid Howells 	return sid;
243275bb41eSDavid Howells }
244275bb41eSDavid Howells 
2455d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2465d226df4SAndreas Gruenbacher 
2475d226df4SAndreas Gruenbacher /*
2485d226df4SAndreas Gruenbacher  * Try reloading inode security labels that have been marked as invalid.  The
2495d226df4SAndreas Gruenbacher  * @may_sleep parameter indicates when sleeping and thus reloading labels is
25042059112SAndreas Gruenbacher  * allowed; when set to false, returns -ECHILD when the label is
251e9193288SAl Viro  * invalid.  The @dentry parameter should be set to a dentry of the inode.
2525d226df4SAndreas Gruenbacher  */
2535d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode,
254e9193288SAl Viro 				       struct dentry *dentry,
2555d226df4SAndreas Gruenbacher 				       bool may_sleep)
2565d226df4SAndreas Gruenbacher {
25780788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
2585d226df4SAndreas Gruenbacher 
2595d226df4SAndreas Gruenbacher 	might_sleep_if(may_sleep);
2605d226df4SAndreas Gruenbacher 
26165cddd50SOndrej Mosnacek 	if (selinux_initialized(&selinux_state) &&
262aa8e712cSStephen Smalley 	    isec->initialized != LABEL_INITIALIZED) {
2635d226df4SAndreas Gruenbacher 		if (!may_sleep)
2645d226df4SAndreas Gruenbacher 			return -ECHILD;
2655d226df4SAndreas Gruenbacher 
2665d226df4SAndreas Gruenbacher 		/*
2675d226df4SAndreas Gruenbacher 		 * Try reloading the inode security label.  This will fail if
2685d226df4SAndreas Gruenbacher 		 * @opt_dentry is NULL and no dentry for this inode can be
2695d226df4SAndreas Gruenbacher 		 * found; in that case, continue using the old label.
2705d226df4SAndreas Gruenbacher 		 */
271e9193288SAl Viro 		inode_doinit_with_dentry(inode, dentry);
2725d226df4SAndreas Gruenbacher 	}
2735d226df4SAndreas Gruenbacher 	return 0;
2745d226df4SAndreas Gruenbacher }
2755d226df4SAndreas Gruenbacher 
2765d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
2775d226df4SAndreas Gruenbacher {
27880788c22SCasey Schaufler 	return selinux_inode(inode);
2795d226df4SAndreas Gruenbacher }
2805d226df4SAndreas Gruenbacher 
2815d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
2825d226df4SAndreas Gruenbacher {
2835d226df4SAndreas Gruenbacher 	int error;
2845d226df4SAndreas Gruenbacher 
2855d226df4SAndreas Gruenbacher 	error = __inode_security_revalidate(inode, NULL, !rcu);
2865d226df4SAndreas Gruenbacher 	if (error)
2875d226df4SAndreas Gruenbacher 		return ERR_PTR(error);
28880788c22SCasey Schaufler 	return selinux_inode(inode);
2895d226df4SAndreas Gruenbacher }
2905d226df4SAndreas Gruenbacher 
29183da53c5SAndreas Gruenbacher /*
29283da53c5SAndreas Gruenbacher  * Get the security label of an inode.
29383da53c5SAndreas Gruenbacher  */
29483da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode)
29583da53c5SAndreas Gruenbacher {
2965d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, NULL, true);
29780788c22SCasey Schaufler 	return selinux_inode(inode);
29883da53c5SAndreas Gruenbacher }
29983da53c5SAndreas Gruenbacher 
3002c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
3012c97165bSPaul Moore {
3022c97165bSPaul Moore 	struct inode *inode = d_backing_inode(dentry);
3032c97165bSPaul Moore 
30480788c22SCasey Schaufler 	return selinux_inode(inode);
3052c97165bSPaul Moore }
3062c97165bSPaul Moore 
30783da53c5SAndreas Gruenbacher /*
30883da53c5SAndreas Gruenbacher  * Get the security label of a dentry's backing inode.
30983da53c5SAndreas Gruenbacher  */
31083da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
31183da53c5SAndreas Gruenbacher {
31283da53c5SAndreas Gruenbacher 	struct inode *inode = d_backing_inode(dentry);
31383da53c5SAndreas Gruenbacher 
3145d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
31580788c22SCasey Schaufler 	return selinux_inode(inode);
31683da53c5SAndreas Gruenbacher }
31783da53c5SAndreas Gruenbacher 
3181da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
3191da177e4SLinus Torvalds {
32080788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
321afb1cbe3SCasey Schaufler 	struct superblock_security_struct *sbsec;
3221da177e4SLinus Torvalds 
323afb1cbe3SCasey Schaufler 	if (!isec)
324afb1cbe3SCasey Schaufler 		return;
3251aea7808SCasey Schaufler 	sbsec = selinux_superblock(inode->i_sb);
3269629d04aSWaiman Long 	/*
3279629d04aSWaiman Long 	 * As not all inode security structures are in a list, we check for
3289629d04aSWaiman Long 	 * empty list outside of the lock to make sure that we won't waste
3299629d04aSWaiman Long 	 * time taking a lock doing nothing.
3309629d04aSWaiman Long 	 *
3319629d04aSWaiman Long 	 * The list_del_init() function can be safely called more than once.
3329629d04aSWaiman Long 	 * It should not be possible for this function to be called with
3339629d04aSWaiman Long 	 * concurrent list_add(), but for better safety against future changes
3349629d04aSWaiman Long 	 * in the code, we use list_empty_careful() here.
3359629d04aSWaiman Long 	 */
3369629d04aSWaiman Long 	if (!list_empty_careful(&isec->list)) {
3371da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
3381da177e4SLinus Torvalds 		list_del_init(&isec->list);
3391da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
3409629d04aSWaiman Long 	}
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
343bd323655SAl Viro struct selinux_mnt_opts {
344bd323655SAl Viro 	const char *fscontext, *context, *rootcontext, *defcontext;
345bd323655SAl Viro };
346bd323655SAl Viro 
347204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts)
348204cc0ccSAl Viro {
349bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
350bd323655SAl Viro 	kfree(opts->fscontext);
351bd323655SAl Viro 	kfree(opts->context);
352bd323655SAl Viro 	kfree(opts->rootcontext);
353bd323655SAl Viro 	kfree(opts->defcontext);
354204cc0ccSAl Viro 	kfree(opts);
355204cc0ccSAl Viro }
356204cc0ccSAl Viro 
3571da177e4SLinus Torvalds enum {
35831e87930SEric Paris 	Opt_error = -1,
359442155c1SDavid Howells 	Opt_context = 0,
360442155c1SDavid Howells 	Opt_defcontext = 1,
3611da177e4SLinus Torvalds 	Opt_fscontext = 2,
362442155c1SDavid Howells 	Opt_rootcontext = 3,
363442155c1SDavid Howells 	Opt_seclabel = 4,
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
366da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
367169d68efSAl Viro static struct {
368169d68efSAl Viro 	const char *name;
369169d68efSAl Viro 	int len;
370169d68efSAl Viro 	int opt;
371169d68efSAl Viro 	bool has_arg;
372169d68efSAl Viro } tokens[] = {
373da3d76abSAl Viro 	A(context, true),
374da3d76abSAl Viro 	A(fscontext, true),
375da3d76abSAl Viro 	A(defcontext, true),
376da3d76abSAl Viro 	A(rootcontext, true),
377da3d76abSAl Viro 	A(seclabel, false),
3781da177e4SLinus Torvalds };
379169d68efSAl Viro #undef A
380169d68efSAl Viro 
381169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg)
382169d68efSAl Viro {
383169d68efSAl Viro 	int i;
384169d68efSAl Viro 
385169d68efSAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
386169d68efSAl Viro 		size_t len = tokens[i].len;
387169d68efSAl Viro 		if (len > l || memcmp(s, tokens[i].name, len))
388169d68efSAl Viro 			continue;
389169d68efSAl Viro 		if (tokens[i].has_arg) {
390169d68efSAl Viro 			if (len == l || s[len] != '=')
391169d68efSAl Viro 				continue;
392169d68efSAl Viro 			*arg = s + len + 1;
393169d68efSAl Viro 		} else if (len != l)
394169d68efSAl Viro 			continue;
395169d68efSAl Viro 		return tokens[i].opt;
396169d68efSAl Viro 	}
397169d68efSAl Viro 	return Opt_error;
398169d68efSAl Viro }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4011da177e4SLinus Torvalds 
402c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
403c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
404275bb41eSDavid Howells 			const struct cred *cred)
405c312feb2SEric Paris {
4060c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
407c312feb2SEric Paris 	int rc;
408c312feb2SEric Paris 
4096b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4106b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
411c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
412c312feb2SEric Paris 	if (rc)
413c312feb2SEric Paris 		return rc;
414c312feb2SEric Paris 
4156b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4166b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
417c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
418c312feb2SEric Paris 	return rc;
419c312feb2SEric Paris }
420c312feb2SEric Paris 
4210808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4220808925eSEric Paris 			struct superblock_security_struct *sbsec,
423275bb41eSDavid Howells 			const struct cred *cred)
4240808925eSEric Paris {
4250c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
4260808925eSEric Paris 	int rc;
4276b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4286b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4290808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
4300808925eSEric Paris 	if (rc)
4310808925eSEric Paris 		return rc;
4320808925eSEric Paris 
4336b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4346b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
4350808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4360808925eSEric Paris 	return rc;
4370808925eSEric Paris }
4380808925eSEric Paris 
439a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb)
440b43e725dSEric Paris {
441d5f3a5f6SMark Salyzyn 	/* Special handling. Genfs but also in-core setxattr handler */
442a83d6ddaSOndrej Mosnacek 	return	!strcmp(sb->s_type->name, "sysfs") ||
443d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
444d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
445a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
4462651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
447aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
4482651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
4492651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
450b43e725dSEric Paris }
451b43e725dSEric Paris 
452a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb)
453a83d6ddaSOndrej Mosnacek {
4541aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
455a83d6ddaSOndrej Mosnacek 
456a83d6ddaSOndrej Mosnacek 	/*
457a83d6ddaSOndrej Mosnacek 	 * IMPORTANT: Double-check logic in this function when adding a new
458a83d6ddaSOndrej Mosnacek 	 * SECURITY_FS_USE_* definition!
459a83d6ddaSOndrej Mosnacek 	 */
460a83d6ddaSOndrej Mosnacek 	BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
461a83d6ddaSOndrej Mosnacek 
462a83d6ddaSOndrej Mosnacek 	switch (sbsec->behavior) {
463a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_XATTR:
464a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TRANS:
465a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TASK:
466a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NATIVE:
467a83d6ddaSOndrej Mosnacek 		return 1;
468a83d6ddaSOndrej Mosnacek 
469a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_GENFS:
470a83d6ddaSOndrej Mosnacek 		return selinux_is_genfs_special_handling(sb);
471a83d6ddaSOndrej Mosnacek 
472a83d6ddaSOndrej Mosnacek 	/* Never allow relabeling on context mounts */
473a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_MNTPOINT:
474a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NONE:
475a83d6ddaSOndrej Mosnacek 	default:
476a83d6ddaSOndrej Mosnacek 		return 0;
477a83d6ddaSOndrej Mosnacek 	}
478a83d6ddaSOndrej Mosnacek }
479a83d6ddaSOndrej Mosnacek 
48008abe46bSOndrej Mosnacek static int sb_check_xattr_support(struct super_block *sb)
48108abe46bSOndrej Mosnacek {
48208abe46bSOndrej Mosnacek 	struct superblock_security_struct *sbsec = sb->s_security;
48308abe46bSOndrej Mosnacek 	struct dentry *root = sb->s_root;
48408abe46bSOndrej Mosnacek 	struct inode *root_inode = d_backing_inode(root);
48508abe46bSOndrej Mosnacek 	u32 sid;
48608abe46bSOndrej Mosnacek 	int rc;
48708abe46bSOndrej Mosnacek 
48808abe46bSOndrej Mosnacek 	/*
48908abe46bSOndrej Mosnacek 	 * Make sure that the xattr handler exists and that no
49008abe46bSOndrej Mosnacek 	 * error other than -ENODATA is returned by getxattr on
49108abe46bSOndrej Mosnacek 	 * the root directory.  -ENODATA is ok, as this may be
49208abe46bSOndrej Mosnacek 	 * the first boot of the SELinux kernel before we have
49308abe46bSOndrej Mosnacek 	 * assigned xattr values to the filesystem.
49408abe46bSOndrej Mosnacek 	 */
49508abe46bSOndrej Mosnacek 	if (!(root_inode->i_opflags & IOP_XATTR)) {
49608abe46bSOndrej Mosnacek 		pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
49708abe46bSOndrej Mosnacek 			sb->s_id, sb->s_type->name);
49808abe46bSOndrej Mosnacek 		goto fallback;
49908abe46bSOndrej Mosnacek 	}
50008abe46bSOndrej Mosnacek 
50108abe46bSOndrej Mosnacek 	rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
50208abe46bSOndrej Mosnacek 	if (rc < 0 && rc != -ENODATA) {
50308abe46bSOndrej Mosnacek 		if (rc == -EOPNOTSUPP) {
50408abe46bSOndrej Mosnacek 			pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
50508abe46bSOndrej Mosnacek 				sb->s_id, sb->s_type->name);
50608abe46bSOndrej Mosnacek 			goto fallback;
50708abe46bSOndrej Mosnacek 		} else {
50808abe46bSOndrej Mosnacek 			pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
50908abe46bSOndrej Mosnacek 				sb->s_id, sb->s_type->name, -rc);
51008abe46bSOndrej Mosnacek 			return rc;
51108abe46bSOndrej Mosnacek 		}
51208abe46bSOndrej Mosnacek 	}
51308abe46bSOndrej Mosnacek 	return 0;
51408abe46bSOndrej Mosnacek 
51508abe46bSOndrej Mosnacek fallback:
51608abe46bSOndrej Mosnacek 	/* No xattr support - try to fallback to genfs if possible. */
51708abe46bSOndrej Mosnacek 	rc = security_genfs_sid(&selinux_state, sb->s_type->name, "/",
51808abe46bSOndrej Mosnacek 				SECCLASS_DIR, &sid);
51908abe46bSOndrej Mosnacek 	if (rc)
52008abe46bSOndrej Mosnacek 		return -EOPNOTSUPP;
52108abe46bSOndrej Mosnacek 
52208abe46bSOndrej Mosnacek 	pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
52308abe46bSOndrej Mosnacek 		sb->s_id, sb->s_type->name);
52408abe46bSOndrej Mosnacek 	sbsec->behavior = SECURITY_FS_USE_GENFS;
52508abe46bSOndrej Mosnacek 	sbsec->sid = sid;
52608abe46bSOndrej Mosnacek 	return 0;
52708abe46bSOndrej Mosnacek }
52808abe46bSOndrej Mosnacek 
529c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5301da177e4SLinus Torvalds {
5311aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
5321da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
533c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5341da177e4SLinus Torvalds 	int rc = 0;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
53708abe46bSOndrej Mosnacek 		rc = sb_check_xattr_support(sb);
53808abe46bSOndrej Mosnacek 		if (rc)
53908abe46bSOndrej Mosnacek 			return rc;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
542eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5430b4d3452SScott Mayhew 
5440b4d3452SScott Mayhew 	/*
5450b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5460b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5470b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5480b4d3452SScott Mayhew 	 */
549b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
55012f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5510b4d3452SScott Mayhew 	else
5520b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
553ddd29ec6SDavid P. Quigley 
5541da177e4SLinus Torvalds 	/* Initialize the root inode. */
555c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5581da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5591da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5601da177e4SLinus Torvalds 	   populates itself. */
5611da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5628d64124aSAl Viro 	while (!list_empty(&sbsec->isec_head)) {
5631da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5648d64124aSAl Viro 				list_first_entry(&sbsec->isec_head,
5651da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5661da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
567923190d3SStephen Smalley 		list_del_init(&isec->list);
5681da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5691da177e4SLinus Torvalds 		inode = igrab(inode);
5701da177e4SLinus Torvalds 		if (inode) {
5711da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
572cb89e246SPaul Moore 				inode_doinit_with_dentry(inode, NULL);
5731da177e4SLinus Torvalds 			iput(inode);
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
578c9180a57SEric Paris 	return rc;
579c9180a57SEric Paris }
580c9180a57SEric Paris 
581c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
582c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
583c9180a57SEric Paris {
5840d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5850d90a7ecSDavid P. Quigley 
586c9180a57SEric Paris 	/* check if the old mount command had the same options */
5870d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
588c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
589c9180a57SEric Paris 		    (old_sid != new_sid))
590c9180a57SEric Paris 			return 1;
591c9180a57SEric Paris 
592c9180a57SEric Paris 	/* check if we were passed the same options twice,
593c9180a57SEric Paris 	 * aka someone passed context=a,context=b
594c9180a57SEric Paris 	 */
5950d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5960d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
597c9180a57SEric Paris 			return 1;
598c9180a57SEric Paris 	return 0;
599c9180a57SEric Paris }
600e0007529SEric Paris 
601bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
602bd323655SAl Viro {
603bd323655SAl Viro 	int rc = security_context_str_to_sid(&selinux_state, s,
604bd323655SAl Viro 					     sid, GFP_KERNEL);
605bd323655SAl Viro 	if (rc)
606bd323655SAl Viro 		pr_warn("SELinux: security_context_str_to_sid"
607bd323655SAl Viro 		       "(%s) failed for (dev %s, type %s) errno=%d\n",
608bd323655SAl Viro 		       s, sb->s_id, sb->s_type->name, rc);
609bd323655SAl Viro 	return rc;
610bd323655SAl Viro }
611bd323655SAl Viro 
612c9180a57SEric Paris /*
613c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
614c9180a57SEric Paris  * labeling information.
615c9180a57SEric Paris  */
616e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
617204cc0ccSAl Viro 				void *mnt_opts,
618649f6e77SDavid Quigley 				unsigned long kern_flags,
619649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
620c9180a57SEric Paris {
621275bb41eSDavid Howells 	const struct cred *cred = current_cred();
6221aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
623b159e86bSOndrej Mosnacek 	struct dentry *root = sb->s_root;
624bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
6252c97165bSPaul Moore 	struct inode_security_struct *root_isec;
626c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
627c9180a57SEric Paris 	u32 defcontext_sid = 0;
628bd323655SAl Viro 	int rc = 0;
629c9180a57SEric Paris 
630c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
631c9180a57SEric Paris 
63265cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
633bd323655SAl Viro 		if (!opts) {
634c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
635c9180a57SEric Paris 			   after the initial policy is loaded and the security
636c9180a57SEric Paris 			   server is ready to handle calls. */
637c9180a57SEric Paris 			goto out;
638c9180a57SEric Paris 		}
639c9180a57SEric Paris 		rc = -EINVAL;
640c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
641744ba35eSEric Paris 			"before the security server is initialized\n");
642c9180a57SEric Paris 		goto out;
643c9180a57SEric Paris 	}
644649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
645649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
646649f6e77SDavid Quigley 		 * place the results is not allowed */
647649f6e77SDavid Quigley 		rc = -EINVAL;
648649f6e77SDavid Quigley 		goto out;
649649f6e77SDavid Quigley 	}
650c9180a57SEric Paris 
651c9180a57SEric Paris 	/*
652e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
653e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
654e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
655e0007529SEric Paris 	 * we need to skip the double mount verification.
656e0007529SEric Paris 	 *
657e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
658e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
659e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
660e0007529SEric Paris 	 * will be used for both mounts)
661e0007529SEric Paris 	 */
6620d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
663bd323655SAl Viro 	    && !opts)
664e0007529SEric Paris 		goto out;
665e0007529SEric Paris 
6662c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
6672c97165bSPaul Moore 
668e0007529SEric Paris 	/*
669c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
670c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
671c9180a57SEric Paris 	 * than once with different security options.
672c9180a57SEric Paris 	 */
673bd323655SAl Viro 	if (opts) {
674bd323655SAl Viro 		if (opts->fscontext) {
675bd323655SAl Viro 			rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
676bd323655SAl Viro 			if (rc)
677c9180a57SEric Paris 				goto out;
678c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
679c9180a57SEric Paris 					fscontext_sid))
680c9180a57SEric Paris 				goto out_double_mount;
681c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
682bd323655SAl Viro 		}
683bd323655SAl Viro 		if (opts->context) {
684bd323655SAl Viro 			rc = parse_sid(sb, opts->context, &context_sid);
685bd323655SAl Viro 			if (rc)
686bd323655SAl Viro 				goto out;
687c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
688c9180a57SEric Paris 					context_sid))
689c9180a57SEric Paris 				goto out_double_mount;
690c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
691bd323655SAl Viro 		}
692bd323655SAl Viro 		if (opts->rootcontext) {
693bd323655SAl Viro 			rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
694bd323655SAl Viro 			if (rc)
695bd323655SAl Viro 				goto out;
696c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
697c9180a57SEric Paris 					rootcontext_sid))
698c9180a57SEric Paris 				goto out_double_mount;
699c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
700bd323655SAl Viro 		}
701bd323655SAl Viro 		if (opts->defcontext) {
702bd323655SAl Viro 			rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
703bd323655SAl Viro 			if (rc)
704bd323655SAl Viro 				goto out;
705c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
706c9180a57SEric Paris 					defcontext_sid))
707c9180a57SEric Paris 				goto out_double_mount;
708c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
709c9180a57SEric Paris 		}
710c9180a57SEric Paris 	}
711c9180a57SEric Paris 
7120d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
713c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
714bd323655SAl Viro 		if ((sbsec->flags & SE_MNTMASK) && !opts)
715c9180a57SEric Paris 			goto out_double_mount;
716c9180a57SEric Paris 		rc = 0;
717c9180a57SEric Paris 		goto out;
718c9180a57SEric Paris 	}
719c9180a57SEric Paris 
720089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
721134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
722134509d5SStephen Smalley 
7238e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
7246a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
725a20456aeSHridya Valsaraju 	    !strcmp(sb->s_type->name, "binder") ||
7264ca54d3dSConnor O'Brien 	    !strcmp(sb->s_type->name, "bpf") ||
7278a764ef1SChristian Göttsche 	    !strcmp(sb->s_type->name, "pstore") ||
7288a764ef1SChristian Göttsche 	    !strcmp(sb->s_type->name, "securityfs"))
729b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS;
730b754026bSOndrej Mosnacek 
731b754026bSOndrej Mosnacek 	if (!strcmp(sb->s_type->name, "sysfs") ||
732901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
733901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
734b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
735c9180a57SEric Paris 
736eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
737eb9ae686SDavid Quigley 		/*
738eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
739eb9ae686SDavid Quigley 		 * filesystem type.
740eb9ae686SDavid Quigley 		 */
741aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
742c9180a57SEric Paris 		if (rc) {
743c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
744089be43eSJames Morris 					__func__, sb->s_type->name, rc);
745c9180a57SEric Paris 			goto out;
746c9180a57SEric Paris 		}
747eb9ae686SDavid Quigley 	}
748aad82892SSeth Forshee 
749aad82892SSeth Forshee 	/*
75001593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
75101593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
75201593d32SStephen Smalley 	 * line and security labels must be ignored.
753aad82892SSeth Forshee 	 */
75401593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
75501593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
75601593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
7577fa2e79aSVivek Goyal 	    strcmp(sb->s_type->name, "devpts") &&
7587fa2e79aSVivek Goyal 	    strcmp(sb->s_type->name, "overlay")) {
759aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
760aad82892SSeth Forshee 		    defcontext_sid) {
761aad82892SSeth Forshee 			rc = -EACCES;
762aad82892SSeth Forshee 			goto out;
763aad82892SSeth Forshee 		}
764aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
765aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
766aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
767aa8e712cSStephen Smalley 						     current_sid(),
768aa8e712cSStephen Smalley 						     current_sid(),
769aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
770aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
771aad82892SSeth Forshee 			if (rc)
772aad82892SSeth Forshee 				goto out;
773aad82892SSeth Forshee 		}
774aad82892SSeth Forshee 		goto out_set_opts;
775aad82892SSeth Forshee 	}
776aad82892SSeth Forshee 
777c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
778c9180a57SEric Paris 	if (fscontext_sid) {
779275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
780c9180a57SEric Paris 		if (rc)
781c9180a57SEric Paris 			goto out;
782c9180a57SEric Paris 
783c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
784c9180a57SEric Paris 	}
785c9180a57SEric Paris 
786c9180a57SEric Paris 	/*
787c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
788c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
789c9180a57SEric Paris 	 * the superblock context if not already set.
790c9180a57SEric Paris 	 */
791eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
792eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
793eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
794eb9ae686SDavid Quigley 	}
795eb9ae686SDavid Quigley 
796c9180a57SEric Paris 	if (context_sid) {
797c9180a57SEric Paris 		if (!fscontext_sid) {
798275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
799275bb41eSDavid Howells 							  cred);
800c9180a57SEric Paris 			if (rc)
801c9180a57SEric Paris 				goto out;
802c9180a57SEric Paris 			sbsec->sid = context_sid;
803c9180a57SEric Paris 		} else {
804275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
805275bb41eSDavid Howells 							     cred);
806c9180a57SEric Paris 			if (rc)
807c9180a57SEric Paris 				goto out;
808c9180a57SEric Paris 		}
809c9180a57SEric Paris 		if (!rootcontext_sid)
810c9180a57SEric Paris 			rootcontext_sid = context_sid;
811c9180a57SEric Paris 
812c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
813c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
814c9180a57SEric Paris 	}
815c9180a57SEric Paris 
816c9180a57SEric Paris 	if (rootcontext_sid) {
817275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
818275bb41eSDavid Howells 						     cred);
819c9180a57SEric Paris 		if (rc)
820c9180a57SEric Paris 			goto out;
821c9180a57SEric Paris 
822c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
8236f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
824c9180a57SEric Paris 	}
825c9180a57SEric Paris 
826c9180a57SEric Paris 	if (defcontext_sid) {
827eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
828eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
829c9180a57SEric Paris 			rc = -EINVAL;
830c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
831c9180a57SEric Paris 			       "invalid for this filesystem type\n");
832c9180a57SEric Paris 			goto out;
833c9180a57SEric Paris 		}
834c9180a57SEric Paris 
835c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
836c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
837275bb41eSDavid Howells 							     sbsec, cred);
838c9180a57SEric Paris 			if (rc)
839c9180a57SEric Paris 				goto out;
840c9180a57SEric Paris 		}
841c9180a57SEric Paris 
842c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
843c9180a57SEric Paris 	}
844c9180a57SEric Paris 
845aad82892SSeth Forshee out_set_opts:
846c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
847c9180a57SEric Paris out:
848bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8491da177e4SLinus Torvalds 	return rc;
850c9180a57SEric Paris out_double_mount:
851c9180a57SEric Paris 	rc = -EINVAL;
852c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
853bd323655SAl Viro 	       "security settings for (dev %s, type %s)\n", sb->s_id,
854bd323655SAl Viro 	       sb->s_type->name);
855c9180a57SEric Paris 	goto out;
856c9180a57SEric Paris }
857c9180a57SEric Paris 
858094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
859094f7b69SJeff Layton 				    const struct super_block *newsb)
860094f7b69SJeff Layton {
8611aea7808SCasey Schaufler 	struct superblock_security_struct *old = selinux_superblock(oldsb);
8621aea7808SCasey Schaufler 	struct superblock_security_struct *new = selinux_superblock(newsb);
863094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
864094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
865094f7b69SJeff Layton 
866094f7b69SJeff Layton 	if (oldflags != newflags)
867094f7b69SJeff Layton 		goto mismatch;
868094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
869094f7b69SJeff Layton 		goto mismatch;
870094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
871094f7b69SJeff Layton 		goto mismatch;
872094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
873094f7b69SJeff Layton 		goto mismatch;
874094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
87583da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
87683da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
877094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
878094f7b69SJeff Layton 			goto mismatch;
879094f7b69SJeff Layton 	}
880094f7b69SJeff Layton 	return 0;
881094f7b69SJeff Layton mismatch:
882c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
883094f7b69SJeff Layton 			    "different security settings for (dev %s, "
884094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
885094f7b69SJeff Layton 	return -EBUSY;
886094f7b69SJeff Layton }
887094f7b69SJeff Layton 
888094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
8890b4d3452SScott Mayhew 					struct super_block *newsb,
8900b4d3452SScott Mayhew 					unsigned long kern_flags,
8910b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
892c9180a57SEric Paris {
8930b4d3452SScott Mayhew 	int rc = 0;
8941aea7808SCasey Schaufler 	const struct superblock_security_struct *oldsbsec =
8951aea7808SCasey Schaufler 						selinux_superblock(oldsb);
8961aea7808SCasey Schaufler 	struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
897c9180a57SEric Paris 
898c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
899c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
900c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
901c9180a57SEric Paris 
9020f5e6420SEric Paris 	/*
9030f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
904e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
9050f5e6420SEric Paris 	 */
90665cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
907094f7b69SJeff Layton 		return 0;
908c9180a57SEric Paris 
9090b4d3452SScott Mayhew 	/*
9100b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
9110b4d3452SScott Mayhew 	 * place the results is not allowed.
9120b4d3452SScott Mayhew 	 */
9130b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
9140b4d3452SScott Mayhew 		return -EINVAL;
9150b4d3452SScott Mayhew 
916c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
9170d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
918c9180a57SEric Paris 
919094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
9203815a245SJ. Bruce Fields 	if (newsbsec->flags & SE_SBINITIALIZED) {
9213815a245SJ. Bruce Fields 		if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
9223815a245SJ. Bruce Fields 			*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
923094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
9243815a245SJ. Bruce Fields 	}
9255a552617SEric Paris 
926c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
927c9180a57SEric Paris 
928c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
929c9180a57SEric Paris 
930c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
931c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
932c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
933c9180a57SEric Paris 
9340b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
9350b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
936aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
9370b4d3452SScott Mayhew 		if (rc)
9380b4d3452SScott Mayhew 			goto out;
9390b4d3452SScott Mayhew 	}
9400b4d3452SScott Mayhew 
9410b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
9420b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
9430b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
9440b4d3452SScott Mayhew 	}
9450b4d3452SScott Mayhew 
946c9180a57SEric Paris 	if (set_context) {
947c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
948c9180a57SEric Paris 
949c9180a57SEric Paris 		if (!set_fscontext)
950c9180a57SEric Paris 			newsbsec->sid = sid;
951c9180a57SEric Paris 		if (!set_rootcontext) {
95283da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
953c9180a57SEric Paris 			newisec->sid = sid;
954c9180a57SEric Paris 		}
955c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
956c9180a57SEric Paris 	}
957c9180a57SEric Paris 	if (set_rootcontext) {
95883da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
95983da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
960c9180a57SEric Paris 
961c9180a57SEric Paris 		newisec->sid = oldisec->sid;
962c9180a57SEric Paris 	}
963c9180a57SEric Paris 
964c9180a57SEric Paris 	sb_finish_set_opts(newsb);
9650b4d3452SScott Mayhew out:
966c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
9670b4d3452SScott Mayhew 	return rc;
968c9180a57SEric Paris }
969c9180a57SEric Paris 
970ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts)
971c9180a57SEric Paris {
972ba641862SAl Viro 	struct selinux_mnt_opts *opts = *mnt_opts;
973*2e08df3cSBernard Zhao 	bool is_alloc_opts = false;
974c9180a57SEric Paris 
975da3d76abSAl Viro 	if (token == Opt_seclabel)	/* eaten and completely ignored */
976e0007529SEric Paris 		return 0;
977e0007529SEric Paris 
978*2e08df3cSBernard Zhao 	if (!s)
979*2e08df3cSBernard Zhao 		return -ENOMEM;
980*2e08df3cSBernard Zhao 
981ba641862SAl Viro 	if (!opts) {
982ba641862SAl Viro 		opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
983ba641862SAl Viro 		if (!opts)
984ba641862SAl Viro 			return -ENOMEM;
985ba641862SAl Viro 		*mnt_opts = opts;
986*2e08df3cSBernard Zhao 		is_alloc_opts = true;
987ba641862SAl Viro 	}
988*2e08df3cSBernard Zhao 
989ba641862SAl Viro 	switch (token) {
990ba641862SAl Viro 	case Opt_context:
991ba641862SAl Viro 		if (opts->context || opts->defcontext)
992ba641862SAl Viro 			goto Einval;
993ba641862SAl Viro 		opts->context = s;
994ba641862SAl Viro 		break;
995ba641862SAl Viro 	case Opt_fscontext:
996ba641862SAl Viro 		if (opts->fscontext)
997ba641862SAl Viro 			goto Einval;
998ba641862SAl Viro 		opts->fscontext = s;
999ba641862SAl Viro 		break;
1000ba641862SAl Viro 	case Opt_rootcontext:
1001ba641862SAl Viro 		if (opts->rootcontext)
1002ba641862SAl Viro 			goto Einval;
1003ba641862SAl Viro 		opts->rootcontext = s;
1004ba641862SAl Viro 		break;
1005ba641862SAl Viro 	case Opt_defcontext:
1006ba641862SAl Viro 		if (opts->context || opts->defcontext)
1007ba641862SAl Viro 			goto Einval;
1008ba641862SAl Viro 		opts->defcontext = s;
1009ba641862SAl Viro 		break;
1010ba641862SAl Viro 	}
1011ba641862SAl Viro 	return 0;
1012ba641862SAl Viro Einval:
1013*2e08df3cSBernard Zhao 	if (is_alloc_opts) {
1014*2e08df3cSBernard Zhao 		kfree(opts);
1015*2e08df3cSBernard Zhao 		*mnt_opts = NULL;
1016*2e08df3cSBernard Zhao 	}
1017ba641862SAl Viro 	pr_warn(SEL_MOUNT_FAIL_MSG);
1018ba641862SAl Viro 	return -EINVAL;
1019ba641862SAl Viro }
1020ba641862SAl Viro 
1021e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid)
10222069f457SEric Paris {
1023e3489f89SAl Viro 	char *context = NULL;
1024e3489f89SAl Viro 	u32 len;
1025e3489f89SAl Viro 	int rc;
10262069f457SEric Paris 
1027e3489f89SAl Viro 	rc = security_sid_to_context(&selinux_state, sid,
1028e3489f89SAl Viro 					     &context, &len);
1029e3489f89SAl Viro 	if (!rc) {
1030e3489f89SAl Viro 		bool has_comma = context && strchr(context, ',');
103111689d47SDavid P. Quigley 
1032442155c1SDavid Howells 		seq_putc(m, '=');
10332069f457SEric Paris 		if (has_comma)
10342069f457SEric Paris 			seq_putc(m, '\"');
1035e3489f89SAl Viro 		seq_escape(m, context, "\"\n\\");
10362069f457SEric Paris 		if (has_comma)
10372069f457SEric Paris 			seq_putc(m, '\"');
10382069f457SEric Paris 	}
1039c9180a57SEric Paris 	kfree(context);
1040c9180a57SEric Paris 	return rc;
1041c9180a57SEric Paris }
10422069f457SEric Paris 
10432069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10442069f457SEric Paris {
10451aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
10462069f457SEric Paris 	int rc;
10472069f457SEric Paris 
1048e3489f89SAl Viro 	if (!(sbsec->flags & SE_SBINITIALIZED))
1049e3489f89SAl Viro 		return 0;
1050e3489f89SAl Viro 
105165cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
1052e3489f89SAl Viro 		return 0;
1053e3489f89SAl Viro 
1054e3489f89SAl Viro 	if (sbsec->flags & FSCONTEXT_MNT) {
1055e3489f89SAl Viro 		seq_putc(m, ',');
1056e3489f89SAl Viro 		seq_puts(m, FSCONTEXT_STR);
1057e3489f89SAl Viro 		rc = show_sid(m, sbsec->sid);
1058e3489f89SAl Viro 		if (rc)
10592069f457SEric Paris 			return rc;
1060383795c2SEric Paris 	}
1061e3489f89SAl Viro 	if (sbsec->flags & CONTEXT_MNT) {
1062e3489f89SAl Viro 		seq_putc(m, ',');
1063e3489f89SAl Viro 		seq_puts(m, CONTEXT_STR);
1064e3489f89SAl Viro 		rc = show_sid(m, sbsec->mntpoint_sid);
1065e3489f89SAl Viro 		if (rc)
10662069f457SEric Paris 			return rc;
10672069f457SEric Paris 	}
1068e3489f89SAl Viro 	if (sbsec->flags & DEFCONTEXT_MNT) {
1069e3489f89SAl Viro 		seq_putc(m, ',');
1070e3489f89SAl Viro 		seq_puts(m, DEFCONTEXT_STR);
1071e3489f89SAl Viro 		rc = show_sid(m, sbsec->def_sid);
1072e3489f89SAl Viro 		if (rc)
1073e3489f89SAl Viro 			return rc;
1074e3489f89SAl Viro 	}
1075e3489f89SAl Viro 	if (sbsec->flags & ROOTCONTEXT_MNT) {
1076b159e86bSOndrej Mosnacek 		struct dentry *root = sb->s_root;
1077e3489f89SAl Viro 		struct inode_security_struct *isec = backing_inode_security(root);
1078e3489f89SAl Viro 		seq_putc(m, ',');
1079e3489f89SAl Viro 		seq_puts(m, ROOTCONTEXT_STR);
1080e3489f89SAl Viro 		rc = show_sid(m, isec->sid);
1081e3489f89SAl Viro 		if (rc)
1082e3489f89SAl Viro 			return rc;
1083e3489f89SAl Viro 	}
1084e3489f89SAl Viro 	if (sbsec->flags & SBLABEL_MNT) {
1085e3489f89SAl Viro 		seq_putc(m, ',');
1086442155c1SDavid Howells 		seq_puts(m, SECLABEL_STR);
1087e3489f89SAl Viro 	}
1088e3489f89SAl Viro 	return 0;
1089e3489f89SAl Viro }
10902069f457SEric Paris 
10911da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
10941da177e4SLinus Torvalds 	case S_IFSOCK:
10951da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
10961da177e4SLinus Torvalds 	case S_IFLNK:
10971da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
10981da177e4SLinus Torvalds 	case S_IFREG:
10991da177e4SLinus Torvalds 		return SECCLASS_FILE;
11001da177e4SLinus Torvalds 	case S_IFBLK:
11011da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11021da177e4SLinus Torvalds 	case S_IFDIR:
11031da177e4SLinus Torvalds 		return SECCLASS_DIR;
11041da177e4SLinus Torvalds 	case S_IFCHR:
11051da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11061da177e4SLinus Torvalds 	case S_IFIFO:
11071da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	return SECCLASS_FILE;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
111413402580SJames Morris static inline int default_protocol_stream(int protocol)
111513402580SJames Morris {
111695ca9072SPaolo Abeni 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
111795ca9072SPaolo Abeni 		protocol == IPPROTO_MPTCP);
111813402580SJames Morris }
111913402580SJames Morris 
112013402580SJames Morris static inline int default_protocol_dgram(int protocol)
112113402580SJames Morris {
112213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
112313402580SJames Morris }
112413402580SJames Morris 
11251da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11261da177e4SLinus Torvalds {
1127aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1128da69a530SStephen Smalley 
11291da177e4SLinus Torvalds 	switch (family) {
11301da177e4SLinus Torvalds 	case PF_UNIX:
11311da177e4SLinus Torvalds 		switch (type) {
11321da177e4SLinus Torvalds 		case SOCK_STREAM:
11331da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11341da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11351da177e4SLinus Torvalds 		case SOCK_DGRAM:
11362a764b52SLuis Ressel 		case SOCK_RAW:
11371da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11381da177e4SLinus Torvalds 		}
11391da177e4SLinus Torvalds 		break;
11401da177e4SLinus Torvalds 	case PF_INET:
11411da177e4SLinus Torvalds 	case PF_INET6:
11421da177e4SLinus Torvalds 		switch (type) {
11431da177e4SLinus Torvalds 		case SOCK_STREAM:
1144da69a530SStephen Smalley 		case SOCK_SEQPACKET:
114513402580SJames Morris 			if (default_protocol_stream(protocol))
11461da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1147da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1148da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
114913402580SJames Morris 			else
115013402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11511da177e4SLinus Torvalds 		case SOCK_DGRAM:
115213402580SJames Morris 			if (default_protocol_dgram(protocol))
11531da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1154ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1155ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1156da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
115713402580SJames Morris 			else
115813402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11592ee92d46SJames Morris 		case SOCK_DCCP:
11602ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
116113402580SJames Morris 		default:
11621da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 		break;
11651da177e4SLinus Torvalds 	case PF_NETLINK:
11661da177e4SLinus Torvalds 		switch (protocol) {
11671da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11681da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11697f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11701da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11711da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11721da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11731da177e4SLinus Torvalds 		case NETLINK_XFRM:
11741da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11751da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11761da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11776c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
11786c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
11791da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11801da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11816c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
11826c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
11836c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
11846c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
11856c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
11866c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
11871da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
11881da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
11890c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
11900c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
11916c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
11926c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
11936c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
11946c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
11956c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
11966c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
11976c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
11986c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
11991da177e4SLinus Torvalds 		default:
12001da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12011da177e4SLinus Torvalds 		}
12021da177e4SLinus Torvalds 	case PF_PACKET:
12031da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12041da177e4SLinus Torvalds 	case PF_KEY:
12051da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12063e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12073e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12081da177e4SLinus Torvalds 	}
12091da177e4SLinus Torvalds 
1210da69a530SStephen Smalley 	if (extsockclass) {
1211da69a530SStephen Smalley 		switch (family) {
1212da69a530SStephen Smalley 		case PF_AX25:
1213da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1214da69a530SStephen Smalley 		case PF_IPX:
1215da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1216da69a530SStephen Smalley 		case PF_NETROM:
1217da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1218da69a530SStephen Smalley 		case PF_ATMPVC:
1219da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1220da69a530SStephen Smalley 		case PF_X25:
1221da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1222da69a530SStephen Smalley 		case PF_ROSE:
1223da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1224da69a530SStephen Smalley 		case PF_DECnet:
1225da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1226da69a530SStephen Smalley 		case PF_ATMSVC:
1227da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1228da69a530SStephen Smalley 		case PF_RDS:
1229da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1230da69a530SStephen Smalley 		case PF_IRDA:
1231da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1232da69a530SStephen Smalley 		case PF_PPPOX:
1233da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1234da69a530SStephen Smalley 		case PF_LLC:
1235da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1236da69a530SStephen Smalley 		case PF_CAN:
1237da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1238da69a530SStephen Smalley 		case PF_TIPC:
1239da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1240da69a530SStephen Smalley 		case PF_BLUETOOTH:
1241da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1242da69a530SStephen Smalley 		case PF_IUCV:
1243da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1244da69a530SStephen Smalley 		case PF_RXRPC:
1245da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1246da69a530SStephen Smalley 		case PF_ISDN:
1247da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1248da69a530SStephen Smalley 		case PF_PHONET:
1249da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1250da69a530SStephen Smalley 		case PF_IEEE802154:
1251da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1252da69a530SStephen Smalley 		case PF_CAIF:
1253da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1254da69a530SStephen Smalley 		case PF_ALG:
1255da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1256da69a530SStephen Smalley 		case PF_NFC:
1257da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1258da69a530SStephen Smalley 		case PF_VSOCK:
1259da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1260da69a530SStephen Smalley 		case PF_KCM:
1261da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1262da69a530SStephen Smalley 		case PF_QIPCRTR:
1263da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
12643051bf36SLinus Torvalds 		case PF_SMC:
12653051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
126668e8b849SBjörn Töpel 		case PF_XDP:
126768e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
1268bc49d816SJeremy Kerr 		case PF_MCTP:
1269bc49d816SJeremy Kerr 			return SECCLASS_MCTP_SOCKET;
1270bc49d816SJeremy Kerr #if PF_MAX > 46
1271da69a530SStephen Smalley #error New address family defined, please update this function.
1272da69a530SStephen Smalley #endif
1273da69a530SStephen Smalley 		}
1274da69a530SStephen Smalley 	}
1275da69a530SStephen Smalley 
12761da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
1279134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
12801da177e4SLinus Torvalds 				 u16 tclass,
1281134509d5SStephen Smalley 				 u16 flags,
12821da177e4SLinus Torvalds 				 u32 *sid)
12831da177e4SLinus Torvalds {
12848e6c9693SLucian Adrian Grijincu 	int rc;
1285fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
12868e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
12891da177e4SLinus Torvalds 	if (!buffer)
12901da177e4SLinus Torvalds 		return -ENOMEM;
12911da177e4SLinus Torvalds 
12928e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
12938e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
12948e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
12958e6c9693SLucian Adrian Grijincu 	else {
1296134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
12978e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
12988e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
12998e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
13008e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
13018e6c9693SLucian Adrian Grijincu 				path[1] = '/';
13028e6c9693SLucian Adrian Grijincu 				path++;
13031da177e4SLinus Torvalds 			}
1304134509d5SStephen Smalley 		}
1305aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1306aa8e712cSStephen Smalley 					path, tclass, sid);
13077bb185edSStephen Smalley 		if (rc == -ENOENT) {
13087bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
13097bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
13107bb185edSStephen Smalley 			rc = 0;
13117bb185edSStephen Smalley 		}
13128e6c9693SLucian Adrian Grijincu 	}
13131da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
13141da177e4SLinus Torvalds 	return rc;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
1317b754026bSOndrej Mosnacek static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1318b754026bSOndrej Mosnacek 				  u32 def_sid, u32 *sid)
1319b754026bSOndrej Mosnacek {
1320b754026bSOndrej Mosnacek #define INITCONTEXTLEN 255
1321b754026bSOndrej Mosnacek 	char *context;
1322b754026bSOndrej Mosnacek 	unsigned int len;
1323b754026bSOndrej Mosnacek 	int rc;
1324b754026bSOndrej Mosnacek 
1325b754026bSOndrej Mosnacek 	len = INITCONTEXTLEN;
1326b754026bSOndrej Mosnacek 	context = kmalloc(len + 1, GFP_NOFS);
1327b754026bSOndrej Mosnacek 	if (!context)
1328b754026bSOndrej Mosnacek 		return -ENOMEM;
1329b754026bSOndrej Mosnacek 
1330b754026bSOndrej Mosnacek 	context[len] = '\0';
1331b754026bSOndrej Mosnacek 	rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1332b754026bSOndrej Mosnacek 	if (rc == -ERANGE) {
1333b754026bSOndrej Mosnacek 		kfree(context);
1334b754026bSOndrej Mosnacek 
1335b754026bSOndrej Mosnacek 		/* Need a larger buffer.  Query for the right size. */
1336b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1337b754026bSOndrej Mosnacek 		if (rc < 0)
1338b754026bSOndrej Mosnacek 			return rc;
1339b754026bSOndrej Mosnacek 
1340b754026bSOndrej Mosnacek 		len = rc;
1341b754026bSOndrej Mosnacek 		context = kmalloc(len + 1, GFP_NOFS);
1342b754026bSOndrej Mosnacek 		if (!context)
1343b754026bSOndrej Mosnacek 			return -ENOMEM;
1344b754026bSOndrej Mosnacek 
1345b754026bSOndrej Mosnacek 		context[len] = '\0';
1346b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1347b754026bSOndrej Mosnacek 				    context, len);
1348b754026bSOndrej Mosnacek 	}
1349b754026bSOndrej Mosnacek 	if (rc < 0) {
1350b754026bSOndrej Mosnacek 		kfree(context);
1351b754026bSOndrej Mosnacek 		if (rc != -ENODATA) {
1352b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  getxattr returned %d for dev=%s ino=%ld\n",
1353b754026bSOndrej Mosnacek 				__func__, -rc, inode->i_sb->s_id, inode->i_ino);
1354b754026bSOndrej Mosnacek 			return rc;
1355b754026bSOndrej Mosnacek 		}
1356b754026bSOndrej Mosnacek 		*sid = def_sid;
1357b754026bSOndrej Mosnacek 		return 0;
1358b754026bSOndrej Mosnacek 	}
1359b754026bSOndrej Mosnacek 
1360b754026bSOndrej Mosnacek 	rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1361b754026bSOndrej Mosnacek 					     def_sid, GFP_NOFS);
1362b754026bSOndrej Mosnacek 	if (rc) {
1363b754026bSOndrej Mosnacek 		char *dev = inode->i_sb->s_id;
1364b754026bSOndrej Mosnacek 		unsigned long ino = inode->i_ino;
1365b754026bSOndrej Mosnacek 
1366b754026bSOndrej Mosnacek 		if (rc == -EINVAL) {
1367b754026bSOndrej Mosnacek 			pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s.  This indicates you may need to relabel the inode or the filesystem in question.\n",
1368b754026bSOndrej Mosnacek 					      ino, dev, context);
1369b754026bSOndrej Mosnacek 		} else {
1370b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1371b754026bSOndrej Mosnacek 				__func__, context, -rc, dev, ino);
1372b754026bSOndrej Mosnacek 		}
1373b754026bSOndrej Mosnacek 	}
1374b754026bSOndrej Mosnacek 	kfree(context);
1375b754026bSOndrej Mosnacek 	return 0;
1376b754026bSOndrej Mosnacek }
1377b754026bSOndrej Mosnacek 
13781da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
13791da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
138280788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
13839287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
13849287aed2SAndreas Gruenbacher 	u16 sclass;
13851da177e4SLinus Torvalds 	struct dentry *dentry;
13861da177e4SLinus Torvalds 	int rc = 0;
13871da177e4SLinus Torvalds 
13886f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
138913457d07SAndreas Gruenbacher 		return 0;
13901da177e4SLinus Torvalds 
13919287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
13926f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
139323970741SEric Paris 		goto out_unlock;
13941da177e4SLinus Torvalds 
139513457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
139613457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
139713457d07SAndreas Gruenbacher 
13981aea7808SCasey Schaufler 	sbsec = selinux_superblock(inode->i_sb);
13990d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
14001da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
14011da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
14021da177e4SLinus Torvalds 		   server is ready to handle calls. */
14031da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
14041da177e4SLinus Torvalds 		if (list_empty(&isec->list))
14051da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
14061da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
140723970741SEric Paris 		goto out_unlock;
14081da177e4SLinus Torvalds 	}
14091da177e4SLinus Torvalds 
14109287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
14119287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
14129287aed2SAndreas Gruenbacher 	sid = isec->sid;
14139287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
14149287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
14159287aed2SAndreas Gruenbacher 
14161da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1417eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1418eb9ae686SDavid Quigley 		break;
14191da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
14205d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
14219287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
14221da177e4SLinus Torvalds 			break;
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
14251da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
14261da177e4SLinus Torvalds 		if (opt_dentry) {
14271da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
14281da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
14291da177e4SLinus Torvalds 		} else {
1430b127125dSAl Viro 			/*
1431b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1432b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1433b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1434b127125dSAl Viro 			 * two, depending upon that...
1435b127125dSAl Viro 			 */
14361da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1437b127125dSAl Viro 			if (!dentry)
1438b127125dSAl Viro 				dentry = d_find_any_alias(inode);
14391da177e4SLinus Torvalds 		}
14401da177e4SLinus Torvalds 		if (!dentry) {
1441df7f54c0SEric Paris 			/*
1442df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1443df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1444df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1445df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1446df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1447df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1448df7f54c0SEric Paris 			 * be used again by userspace.
1449df7f54c0SEric Paris 			 */
1450200ea5a2SPaul Moore 			goto out_invalid;
14511da177e4SLinus Torvalds 		}
14521da177e4SLinus Torvalds 
1453b754026bSOndrej Mosnacek 		rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1454b754026bSOndrej Mosnacek 					    &sid);
14551da177e4SLinus Torvalds 		dput(dentry);
1456b754026bSOndrej Mosnacek 		if (rc)
14579287aed2SAndreas Gruenbacher 			goto out;
14581da177e4SLinus Torvalds 		break;
14591da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
14609287aed2SAndreas Gruenbacher 		sid = task_sid;
14611da177e4SLinus Torvalds 		break;
14621da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
14631da177e4SLinus Torvalds 		/* Default to the fs SID. */
14649287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1467aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1468aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
14691da177e4SLinus Torvalds 		if (rc)
14709287aed2SAndreas Gruenbacher 			goto out;
14711da177e4SLinus Torvalds 		break;
1472c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
14739287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1474c312feb2SEric Paris 		break;
14751da177e4SLinus Torvalds 	default:
1476c312feb2SEric Paris 		/* Default to the fs superblock SID. */
14779287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
14781da177e4SLinus Torvalds 
14797470d0d1SChristian Göttsche 		if ((sbsec->flags & SE_SBGENFS) &&
14807470d0d1SChristian Göttsche 		     (!S_ISLNK(inode->i_mode) ||
14817470d0d1SChristian Göttsche 		      selinux_policycap_genfs_seclabel_symlinks())) {
1482f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1483f64410ecSPaul Moore 			 * procfs inodes */
1484b127125dSAl Viro 			if (opt_dentry) {
1485f64410ecSPaul Moore 				/* Called from d_instantiate or
1486f64410ecSPaul Moore 				 * d_splice_alias. */
1487f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1488b127125dSAl Viro 			} else {
1489f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1490b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1491b127125dSAl Viro 				 * a connected one, so try that first.
1492b127125dSAl Viro 				 */
1493f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1494b127125dSAl Viro 				if (!dentry)
1495b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1496b127125dSAl Viro 			}
1497f64410ecSPaul Moore 			/*
1498f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1499f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1500f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1501f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1502f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1503f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1504f64410ecSPaul Moore 			 * could be used again by userspace.
1505f64410ecSPaul Moore 			 */
1506f64410ecSPaul Moore 			if (!dentry)
1507200ea5a2SPaul Moore 				goto out_invalid;
15089287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1509134509d5SStephen Smalley 						   sbsec->flags, &sid);
1510b754026bSOndrej Mosnacek 			if (rc) {
1511f64410ecSPaul Moore 				dput(dentry);
15129287aed2SAndreas Gruenbacher 				goto out;
15131da177e4SLinus Torvalds 			}
1514b754026bSOndrej Mosnacek 
1515b754026bSOndrej Mosnacek 			if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1516b754026bSOndrej Mosnacek 			    (inode->i_opflags & IOP_XATTR)) {
1517b754026bSOndrej Mosnacek 				rc = inode_doinit_use_xattr(inode, dentry,
1518b754026bSOndrej Mosnacek 							    sid, &sid);
1519b754026bSOndrej Mosnacek 				if (rc) {
1520b754026bSOndrej Mosnacek 					dput(dentry);
1521b754026bSOndrej Mosnacek 					goto out;
1522b754026bSOndrej Mosnacek 				}
1523b754026bSOndrej Mosnacek 			}
1524b754026bSOndrej Mosnacek 			dput(dentry);
1525b754026bSOndrej Mosnacek 		}
15261da177e4SLinus Torvalds 		break;
15271da177e4SLinus Torvalds 	}
15281da177e4SLinus Torvalds 
15299287aed2SAndreas Gruenbacher out:
15309287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15319287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
1532200ea5a2SPaul Moore 		if (rc) {
15339287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
15349287aed2SAndreas Gruenbacher 			goto out_unlock;
15359287aed2SAndreas Gruenbacher 		}
15366f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
15379287aed2SAndreas Gruenbacher 		isec->sid = sid;
15389287aed2SAndreas Gruenbacher 	}
15391da177e4SLinus Torvalds 
154023970741SEric Paris out_unlock:
15419287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15421da177e4SLinus Torvalds 	return rc;
1543200ea5a2SPaul Moore 
1544200ea5a2SPaul Moore out_invalid:
1545200ea5a2SPaul Moore 	spin_lock(&isec->lock);
1546200ea5a2SPaul Moore 	if (isec->initialized == LABEL_PENDING) {
1547200ea5a2SPaul Moore 		isec->initialized = LABEL_INVALID;
1548200ea5a2SPaul Moore 		isec->sid = sid;
1549200ea5a2SPaul Moore 	}
1550200ea5a2SPaul Moore 	spin_unlock(&isec->lock);
1551200ea5a2SPaul Moore 	return 0;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
15551da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	u32 perm = 0;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	switch (sig) {
15601da177e4SLinus Torvalds 	case SIGCHLD:
15611da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
15621da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
15631da177e4SLinus Torvalds 		break;
15641da177e4SLinus Torvalds 	case SIGKILL:
15651da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
15661da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
15671da177e4SLinus Torvalds 		break;
15681da177e4SLinus Torvalds 	case SIGSTOP:
15691da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
15701da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
15711da177e4SLinus Torvalds 		break;
15721da177e4SLinus Torvalds 	default:
15731da177e4SLinus Torvalds 		/* All other signals. */
15741da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
15751da177e4SLinus Torvalds 		break;
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	return perm;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
1581b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1582b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1583b68e418cSStephen Smalley #endif
1584b68e418cSStephen Smalley 
15851da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
15866a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
1587c1a85a00SMicah Morton 			       int cap, unsigned int opts, bool initns)
15881da177e4SLinus Torvalds {
15892bf49690SThomas Liu 	struct common_audit_data ad;
159006112163SEric Paris 	struct av_decision avd;
1591b68e418cSStephen Smalley 	u16 sclass;
15923699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1593b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
159406112163SEric Paris 	int rc;
15951da177e4SLinus Torvalds 
159650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
15971da177e4SLinus Torvalds 	ad.u.cap = cap;
15981da177e4SLinus Torvalds 
1599b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1600b68e418cSStephen Smalley 	case 0:
16018e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1602b68e418cSStephen Smalley 		break;
1603b68e418cSStephen Smalley 	case 1:
16048e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1605b68e418cSStephen Smalley 		break;
1606b68e418cSStephen Smalley 	default:
1607c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1608b68e418cSStephen Smalley 		BUG();
1609a35c6c83SEric Paris 		return -EINVAL;
1610b68e418cSStephen Smalley 	}
161106112163SEric Paris 
16126b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
16136b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
1614c1a85a00SMicah Morton 	if (!(opts & CAP_OPT_NOAUDIT)) {
16156b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
1616d99cf13fSAl Viro 				    sid, sid, sclass, av, &avd, rc, &ad);
16179ade0cf4SEric Paris 		if (rc2)
16189ade0cf4SEric Paris 			return rc2;
16199ade0cf4SEric Paris 	}
162006112163SEric Paris 	return rc;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
16241da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
16251da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
162688e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
16271da177e4SLinus Torvalds 			  struct inode *inode,
16281da177e4SLinus Torvalds 			  u32 perms,
162919e49834SLinus Torvalds 			  struct common_audit_data *adp)
16301da177e4SLinus Torvalds {
16311da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1632275bb41eSDavid Howells 	u32 sid;
16331da177e4SLinus Torvalds 
1634e0e81739SDavid Howells 	validate_creds(cred);
1635e0e81739SDavid Howells 
1636bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1637bbaca6c2SStephen Smalley 		return 0;
1638bbaca6c2SStephen Smalley 
163988e67f3bSDavid Howells 	sid = cred_sid(cred);
164080788c22SCasey Schaufler 	isec = selinux_inode(inode);
16411da177e4SLinus Torvalds 
16426b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
16436b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16471da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16481da177e4SLinus Torvalds    pathname if needed. */
164988e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16501da177e4SLinus Torvalds 				  struct dentry *dentry,
16511da177e4SLinus Torvalds 				  u32 av)
16521da177e4SLinus Torvalds {
1653c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16542bf49690SThomas Liu 	struct common_audit_data ad;
165588e67f3bSDavid Howells 
165650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16572875fa00SEric Paris 	ad.u.dentry = dentry;
16585d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
165919e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16602875fa00SEric Paris }
16612875fa00SEric Paris 
16622875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16632875fa00SEric Paris    the path to help the auditing code to more easily generate the
16642875fa00SEric Paris    pathname if needed. */
16652875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
16663f7036a0SAl Viro 				const struct path *path,
16672875fa00SEric Paris 				u32 av)
16682875fa00SEric Paris {
1669c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
16702875fa00SEric Paris 	struct common_audit_data ad;
16712875fa00SEric Paris 
167250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
16732875fa00SEric Paris 	ad.u.path = *path;
16745d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
167519e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16761da177e4SLinus Torvalds }
16771da177e4SLinus Torvalds 
167813f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
167913f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
168013f8e981SDavid Howells 				     struct file *file,
168113f8e981SDavid Howells 				     u32 av)
168213f8e981SDavid Howells {
168313f8e981SDavid Howells 	struct common_audit_data ad;
168413f8e981SDavid Howells 
168543af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
168643af5de7SVivek Goyal 	ad.u.file = file;
168719e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
168813f8e981SDavid Howells }
168913f8e981SDavid Howells 
1690f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1691f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1692f66e448cSChenbo Feng #endif
1693f66e448cSChenbo Feng 
16941da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
16951da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
16961da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
16971da177e4SLinus Torvalds    check a particular permission to the file.
16981da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
16991da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
17001da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
17011da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
170288e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
17031da177e4SLinus Torvalds 			 struct file *file,
17041da177e4SLinus Torvalds 			 u32 av)
17051da177e4SLinus Torvalds {
1706bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
1707496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17082bf49690SThomas Liu 	struct common_audit_data ad;
170988e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17101da177e4SLinus Torvalds 	int rc;
17111da177e4SLinus Torvalds 
171243af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
171343af5de7SVivek Goyal 	ad.u.file = file;
17141da177e4SLinus Torvalds 
1715275bb41eSDavid Howells 	if (sid != fsec->sid) {
17166b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
17176b6bc620SStephen Smalley 				  sid, fsec->sid,
17181da177e4SLinus Torvalds 				  SECCLASS_FD,
17191da177e4SLinus Torvalds 				  FD__USE,
17201da177e4SLinus Torvalds 				  &ad);
17211da177e4SLinus Torvalds 		if (rc)
172288e67f3bSDavid Howells 			goto out;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 
1725f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1726f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1727f66e448cSChenbo Feng 	if (rc)
1728f66e448cSChenbo Feng 		return rc;
1729f66e448cSChenbo Feng #endif
1730f66e448cSChenbo Feng 
17311da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
173288e67f3bSDavid Howells 	rc = 0;
17331da177e4SLinus Torvalds 	if (av)
173419e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
17351da177e4SLinus Torvalds 
173688e67f3bSDavid Howells out:
173788e67f3bSDavid Howells 	return rc;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
1740c3c188b2SDavid Howells /*
1741c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1742c3c188b2SDavid Howells  */
1743c957f6dfSVivek Goyal static int
1744c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1745c957f6dfSVivek Goyal 				 struct inode *dir,
1746c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1747c3c188b2SDavid Howells 				 u32 *_new_isid)
1748c3c188b2SDavid Howells {
17491aea7808SCasey Schaufler 	const struct superblock_security_struct *sbsec =
17501aea7808SCasey Schaufler 						selinux_superblock(dir->i_sb);
1751c3c188b2SDavid Howells 
1752c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1753c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1754c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1755c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1756c3c188b2SDavid Howells 		   tsec->create_sid) {
1757c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1758c3c188b2SDavid Howells 	} else {
175920cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1760aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1761aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1762c3c188b2SDavid Howells 					       name, _new_isid);
1763c3c188b2SDavid Howells 	}
1764c3c188b2SDavid Howells 
1765c3c188b2SDavid Howells 	return 0;
1766c3c188b2SDavid Howells }
1767c3c188b2SDavid Howells 
17681da177e4SLinus Torvalds /* Check whether a task can create a file. */
17691da177e4SLinus Torvalds static int may_create(struct inode *dir,
17701da177e4SLinus Torvalds 		      struct dentry *dentry,
17711da177e4SLinus Torvalds 		      u16 tclass)
17721da177e4SLinus Torvalds {
17730c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
17741da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
17751da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1776275bb41eSDavid Howells 	u32 sid, newsid;
17772bf49690SThomas Liu 	struct common_audit_data ad;
17781da177e4SLinus Torvalds 	int rc;
17791da177e4SLinus Torvalds 
178083da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
17811aea7808SCasey Schaufler 	sbsec = selinux_superblock(dir->i_sb);
17821da177e4SLinus Torvalds 
1783275bb41eSDavid Howells 	sid = tsec->sid;
1784275bb41eSDavid Howells 
178550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1786a269434dSEric Paris 	ad.u.dentry = dentry;
17871da177e4SLinus Torvalds 
17886b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
17896b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
17901da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
17911da177e4SLinus Torvalds 			  &ad);
17921da177e4SLinus Torvalds 	if (rc)
17931da177e4SLinus Torvalds 		return rc;
17941da177e4SLinus Torvalds 
1795210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1796210a2928SYang Guo 					   &newsid);
17971da177e4SLinus Torvalds 	if (rc)
17981da177e4SLinus Torvalds 		return rc;
17991da177e4SLinus Torvalds 
18006b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18016b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
18021da177e4SLinus Torvalds 	if (rc)
18031da177e4SLinus Torvalds 		return rc;
18041da177e4SLinus Torvalds 
18056b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18066b6bc620SStephen Smalley 			    newsid, sbsec->sid,
18071da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
18081da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds #define MAY_LINK	0
18121da177e4SLinus Torvalds #define MAY_UNLINK	1
18131da177e4SLinus Torvalds #define MAY_RMDIR	2
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
18161da177e4SLinus Torvalds static int may_link(struct inode *dir,
18171da177e4SLinus Torvalds 		    struct dentry *dentry,
18181da177e4SLinus Torvalds 		    int kind)
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds {
18211da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
18222bf49690SThomas Liu 	struct common_audit_data ad;
1823275bb41eSDavid Howells 	u32 sid = current_sid();
18241da177e4SLinus Torvalds 	u32 av;
18251da177e4SLinus Torvalds 	int rc;
18261da177e4SLinus Torvalds 
182783da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
182883da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
18291da177e4SLinus Torvalds 
183050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1831a269434dSEric Paris 	ad.u.dentry = dentry;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	av = DIR__SEARCH;
18341da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
18356b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18366b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
18371da177e4SLinus Torvalds 	if (rc)
18381da177e4SLinus Torvalds 		return rc;
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	switch (kind) {
18411da177e4SLinus Torvalds 	case MAY_LINK:
18421da177e4SLinus Torvalds 		av = FILE__LINK;
18431da177e4SLinus Torvalds 		break;
18441da177e4SLinus Torvalds 	case MAY_UNLINK:
18451da177e4SLinus Torvalds 		av = FILE__UNLINK;
18461da177e4SLinus Torvalds 		break;
18471da177e4SLinus Torvalds 	case MAY_RMDIR:
18481da177e4SLinus Torvalds 		av = DIR__RMDIR;
18491da177e4SLinus Torvalds 		break;
18501da177e4SLinus Torvalds 	default:
1851c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
1852744ba35eSEric Paris 			__func__, kind);
18531da177e4SLinus Torvalds 		return 0;
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 
18566b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18576b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
18581da177e4SLinus Torvalds 	return rc;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
18621da177e4SLinus Torvalds 			     struct dentry *old_dentry,
18631da177e4SLinus Torvalds 			     struct inode *new_dir,
18641da177e4SLinus Torvalds 			     struct dentry *new_dentry)
18651da177e4SLinus Torvalds {
18661da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
18672bf49690SThomas Liu 	struct common_audit_data ad;
1868275bb41eSDavid Howells 	u32 sid = current_sid();
18691da177e4SLinus Torvalds 	u32 av;
18701da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
18711da177e4SLinus Torvalds 	int rc;
18721da177e4SLinus Torvalds 
187383da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
187483da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
1875e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
187683da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
18771da177e4SLinus Torvalds 
187850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18791da177e4SLinus Torvalds 
1880a269434dSEric Paris 	ad.u.dentry = old_dentry;
18816b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18826b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
18831da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
18841da177e4SLinus Torvalds 	if (rc)
18851da177e4SLinus Torvalds 		return rc;
18866b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18876b6bc620SStephen Smalley 			  sid, old_isec->sid,
18881da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
18891da177e4SLinus Torvalds 	if (rc)
18901da177e4SLinus Torvalds 		return rc;
18911da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
18926b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
18936b6bc620SStephen Smalley 				  sid, old_isec->sid,
18941da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
18951da177e4SLinus Torvalds 		if (rc)
18961da177e4SLinus Torvalds 			return rc;
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 
1899a269434dSEric Paris 	ad.u.dentry = new_dentry;
19001da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
19012c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
19021da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
19036b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19046b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
19051da177e4SLinus Torvalds 	if (rc)
19061da177e4SLinus Torvalds 		return rc;
19072c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
190883da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
1909e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
19106b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19116b6bc620SStephen Smalley 				  sid, new_isec->sid,
19121da177e4SLinus Torvalds 				  new_isec->sclass,
19131da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
19141da177e4SLinus Torvalds 		if (rc)
19151da177e4SLinus Torvalds 			return rc;
19161da177e4SLinus Torvalds 	}
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
192288e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
19231da177e4SLinus Torvalds 			       struct super_block *sb,
19241da177e4SLinus Torvalds 			       u32 perms,
19252bf49690SThomas Liu 			       struct common_audit_data *ad)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
192888e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
19291da177e4SLinus Torvalds 
19301aea7808SCasey Schaufler 	sbsec = selinux_superblock(sb);
19316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19326b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
19361da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	u32 av = 0;
19391da177e4SLinus Torvalds 
1940dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
19411da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19421da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
19431da177e4SLinus Torvalds 		if (mask & MAY_READ)
19441da177e4SLinus Torvalds 			av |= FILE__READ;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
19471da177e4SLinus Torvalds 			av |= FILE__APPEND;
19481da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
19491da177e4SLinus Torvalds 			av |= FILE__WRITE;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	} else {
19521da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19531da177e4SLinus Torvalds 			av |= DIR__SEARCH;
19541da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
19551da177e4SLinus Torvalds 			av |= DIR__WRITE;
19561da177e4SLinus Torvalds 		if (mask & MAY_READ)
19571da177e4SLinus Torvalds 			av |= DIR__READ;
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	return av;
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
19641da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	u32 av = 0;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
19691da177e4SLinus Torvalds 		av |= FILE__READ;
19701da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
19711da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
19721da177e4SLinus Torvalds 			av |= FILE__APPEND;
19731da177e4SLinus Torvalds 		else
19741da177e4SLinus Torvalds 			av |= FILE__WRITE;
19751da177e4SLinus Torvalds 	}
19760794c66dSStephen Smalley 	if (!av) {
19770794c66dSStephen Smalley 		/*
19780794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
19790794c66dSStephen Smalley 		 */
19800794c66dSStephen Smalley 		av = FILE__IOCTL;
19810794c66dSStephen Smalley 	}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	return av;
19841da177e4SLinus Torvalds }
19851da177e4SLinus Torvalds 
19868b6a5a37SEric Paris /*
1987c76a2f9eSRandy Dunlap  * Convert a file to an access vector and include the correct
19888b6a5a37SEric Paris  * open permission.
19898b6a5a37SEric Paris  */
19908b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
19918b6a5a37SEric Paris {
19928b6a5a37SEric Paris 	u32 av = file_to_av(file);
1993ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
19948b6a5a37SEric Paris 
1995aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
1996aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
19978b6a5a37SEric Paris 		av |= FILE__OPEN;
199849b7b8deSEric Paris 
19998b6a5a37SEric Paris 	return av;
20008b6a5a37SEric Paris }
20018b6a5a37SEric Paris 
20021da177e4SLinus Torvalds /* Hook functions begin here. */
20031da177e4SLinus Torvalds 
200452f88693STodd Kjos static int selinux_binder_set_context_mgr(const struct cred *mgr)
200579af7307SStephen Smalley {
20066b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
200752f88693STodd Kjos 			    current_sid(), cred_sid(mgr), SECCLASS_BINDER,
200879af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
200979af7307SStephen Smalley }
201079af7307SStephen Smalley 
201152f88693STodd Kjos static int selinux_binder_transaction(const struct cred *from,
201252f88693STodd Kjos 				      const struct cred *to)
201379af7307SStephen Smalley {
201479af7307SStephen Smalley 	u32 mysid = current_sid();
201552f88693STodd Kjos 	u32 fromsid = cred_sid(from);
201652f88693STodd Kjos 	u32 tosid = cred_sid(to);
201779af7307SStephen Smalley 	int rc;
201879af7307SStephen Smalley 
201979af7307SStephen Smalley 	if (mysid != fromsid) {
20206b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20216b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
202279af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
202379af7307SStephen Smalley 		if (rc)
202479af7307SStephen Smalley 			return rc;
202579af7307SStephen Smalley 	}
202679af7307SStephen Smalley 
202752f88693STodd Kjos 	return avc_has_perm(&selinux_state, fromsid, tosid,
2028eb1231f7SPaul Moore 			    SECCLASS_BINDER, BINDER__CALL, NULL);
202979af7307SStephen Smalley }
203079af7307SStephen Smalley 
203152f88693STodd Kjos static int selinux_binder_transfer_binder(const struct cred *from,
203252f88693STodd Kjos 					  const struct cred *to)
203379af7307SStephen Smalley {
20346b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
203552f88693STodd Kjos 			    cred_sid(from), cred_sid(to),
2036eb1231f7SPaul Moore 			    SECCLASS_BINDER, BINDER__TRANSFER,
203779af7307SStephen Smalley 			    NULL);
203879af7307SStephen Smalley }
203979af7307SStephen Smalley 
204052f88693STodd Kjos static int selinux_binder_transfer_file(const struct cred *from,
204152f88693STodd Kjos 					const struct cred *to,
204279af7307SStephen Smalley 					struct file *file)
204379af7307SStephen Smalley {
204452f88693STodd Kjos 	u32 sid = cred_sid(to);
2045bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
204683da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
204720cdef8dSPaul Moore 	struct inode_security_struct *isec;
204879af7307SStephen Smalley 	struct common_audit_data ad;
204979af7307SStephen Smalley 	int rc;
205079af7307SStephen Smalley 
205179af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
205279af7307SStephen Smalley 	ad.u.path = file->f_path;
205379af7307SStephen Smalley 
205479af7307SStephen Smalley 	if (sid != fsec->sid) {
20556b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20566b6bc620SStephen Smalley 				  sid, fsec->sid,
205779af7307SStephen Smalley 				  SECCLASS_FD,
205879af7307SStephen Smalley 				  FD__USE,
205979af7307SStephen Smalley 				  &ad);
206079af7307SStephen Smalley 		if (rc)
206179af7307SStephen Smalley 			return rc;
206279af7307SStephen Smalley 	}
206379af7307SStephen Smalley 
2064f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2065f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2066f66e448cSChenbo Feng 	if (rc)
2067f66e448cSChenbo Feng 		return rc;
2068f66e448cSChenbo Feng #endif
2069f66e448cSChenbo Feng 
207083da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
207179af7307SStephen Smalley 		return 0;
207279af7307SStephen Smalley 
207320cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
20746b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20756b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
207679af7307SStephen Smalley 			    &ad);
207779af7307SStephen Smalley }
207879af7307SStephen Smalley 
20799e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2080006ebb40SStephen Smalley 				       unsigned int mode)
20811da177e4SLinus Torvalds {
2082275bb41eSDavid Howells 	u32 sid = current_sid();
2083eb1231f7SPaul Moore 	u32 csid = task_sid_obj(child);
2084006ebb40SStephen Smalley 
2085be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
20866b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
20876b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2088be0554c9SStephen Smalley 
20896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20906b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
20915cd9c58fSDavid Howells }
20925cd9c58fSDavid Howells 
20935cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
20945cd9c58fSDavid Howells {
20956b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
2096a3727a8bSPaul Moore 			    task_sid_obj(parent), task_sid_obj(current),
2097eb1231f7SPaul Moore 			    SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
21011da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
21021da177e4SLinus Torvalds {
21036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
2104eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(target), SECCLASS_PROCESS,
2105be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
2108d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2109d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
211015a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
211115a2460eSDavid Howells 			  const kernel_cap_t *permitted)
21121da177e4SLinus Torvalds {
21136b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21146b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2115be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds 
21185626d3e8SJames Morris /*
21195626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
21205626d3e8SJames Morris  * which was removed).
21215626d3e8SJames Morris  *
21225626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
21235626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
21245626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
21255626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
21265626d3e8SJames Morris  */
21275626d3e8SJames Morris 
21286a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2129c1a85a00SMicah Morton 			   int cap, unsigned int opts)
21301da177e4SLinus Torvalds {
2131c1a85a00SMicah Morton 	return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
21351da177e4SLinus Torvalds {
213688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
21371da177e4SLinus Torvalds 	int rc = 0;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	if (!sb)
21401da177e4SLinus Torvalds 		return 0;
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	switch (cmds) {
21431da177e4SLinus Torvalds 	case Q_SYNC:
21441da177e4SLinus Torvalds 	case Q_QUOTAON:
21451da177e4SLinus Torvalds 	case Q_QUOTAOFF:
21461da177e4SLinus Torvalds 	case Q_SETINFO:
21471da177e4SLinus Torvalds 	case Q_SETQUOTA:
2148e4cfa05eSRichard Haines 	case Q_XQUOTAOFF:
2149e4cfa05eSRichard Haines 	case Q_XQUOTAON:
2150e4cfa05eSRichard Haines 	case Q_XSETQLIM:
215188e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
21521da177e4SLinus Torvalds 		break;
21531da177e4SLinus Torvalds 	case Q_GETFMT:
21541da177e4SLinus Torvalds 	case Q_GETINFO:
21551da177e4SLinus Torvalds 	case Q_GETQUOTA:
2156e4cfa05eSRichard Haines 	case Q_XGETQUOTA:
2157e4cfa05eSRichard Haines 	case Q_XGETQSTAT:
2158e4cfa05eSRichard Haines 	case Q_XGETQSTATV:
2159e4cfa05eSRichard Haines 	case Q_XGETNEXTQUOTA:
216088e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
21611da177e4SLinus Torvalds 		break;
21621da177e4SLinus Torvalds 	default:
21631da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
21641da177e4SLinus Torvalds 		break;
21651da177e4SLinus Torvalds 	}
21661da177e4SLinus Torvalds 	return rc;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
21701da177e4SLinus Torvalds {
217188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
217288e67f3bSDavid Howells 
21732875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
217612b3052cSEric Paris static int selinux_syslog(int type)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	switch (type) {
2179d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2180d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
21816b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21826b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2183be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2184d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2185d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2186d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2187d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
21886b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21896b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2190be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2191be0554c9SStephen Smalley 				    NULL);
21921da177e4SLinus Torvalds 	}
2193be0554c9SStephen Smalley 	/* All other syslog types */
21946b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21956b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2196be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
21971da177e4SLinus Torvalds }
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds /*
22001da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
22011da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
22021da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
22031da177e4SLinus Torvalds  *
22041da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
22051da177e4SLinus Torvalds  * processes that allocate mappings.
22061da177e4SLinus Torvalds  */
220734b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22081da177e4SLinus Torvalds {
22091da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
22101da177e4SLinus Torvalds 
2211b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2212c1a85a00SMicah Morton 				 CAP_OPT_NOAUDIT, true);
22131da177e4SLinus Torvalds 	if (rc == 0)
22141da177e4SLinus Torvalds 		cap_sys_admin = 1;
22151da177e4SLinus Torvalds 
2216b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds /* binprm security operations */
22201da177e4SLinus Torvalds 
2221be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
22220c6181cbSPaul Moore {
22230c6181cbSPaul Moore 	u32 sid = 0;
22240c6181cbSPaul Moore 	struct task_struct *tracer;
22250c6181cbSPaul Moore 
22260c6181cbSPaul Moore 	rcu_read_lock();
2227be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
22280c6181cbSPaul Moore 	if (tracer)
2229eb1231f7SPaul Moore 		sid = task_sid_obj(tracer);
22300c6181cbSPaul Moore 	rcu_read_unlock();
22310c6181cbSPaul Moore 
22320c6181cbSPaul Moore 	return sid;
22330c6181cbSPaul Moore }
22340c6181cbSPaul Moore 
22357b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
22367b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
22377b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
22387b0d0b40SStephen Smalley {
22397b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2240380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
22417b0d0b40SStephen Smalley 	int rc;
2242af63f419SStephen Smalley 	u32 av;
22437b0d0b40SStephen Smalley 
22447b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
22457b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
22467b0d0b40SStephen Smalley 
22477b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
22487b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
22497b0d0b40SStephen Smalley 
22507b0d0b40SStephen Smalley 	/*
2251af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2252af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2253af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2254af63f419SStephen Smalley 	 * the old and new contexts.
2255af63f419SStephen Smalley 	 */
2256aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2257af63f419SStephen Smalley 		av = 0;
2258af63f419SStephen Smalley 		if (nnp)
2259af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2260af63f419SStephen Smalley 		if (nosuid)
2261af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
22626b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22636b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2264af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2265af63f419SStephen Smalley 		if (!rc)
2266af63f419SStephen Smalley 			return 0;
2267af63f419SStephen Smalley 	}
2268af63f419SStephen Smalley 
2269af63f419SStephen Smalley 	/*
2270af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2271af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2272af63f419SStephen Smalley 	 * of the permissions of the current SID.
22737b0d0b40SStephen Smalley 	 */
2274aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2275aa8e712cSStephen Smalley 					 new_tsec->sid);
2276af63f419SStephen Smalley 	if (!rc)
2277af63f419SStephen Smalley 		return 0;
2278af63f419SStephen Smalley 
22797b0d0b40SStephen Smalley 	/*
22807b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
22817b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
22827b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
22837b0d0b40SStephen Smalley 	 */
22847b0d0b40SStephen Smalley 	if (nnp)
22857b0d0b40SStephen Smalley 		return -EPERM;
22867b0d0b40SStephen Smalley 	return -EACCES;
22877b0d0b40SStephen Smalley }
22887b0d0b40SStephen Smalley 
2289b8bff599SEric W. Biederman static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
22901da177e4SLinus Torvalds {
2291a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2292a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
22931da177e4SLinus Torvalds 	struct inode_security_struct *isec;
22942bf49690SThomas Liu 	struct common_audit_data ad;
2295496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
22961da177e4SLinus Torvalds 	int rc;
22971da177e4SLinus Torvalds 
2298a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2299a6f76f23SDavid Howells 	 * the script interpreter */
23001da177e4SLinus Torvalds 
23010c6cfa62SCasey Schaufler 	old_tsec = selinux_cred(current_cred());
23020c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
230383da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	/* Default to the current task SID. */
2306a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2307a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
23081da177e4SLinus Torvalds 
230928eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2310a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2311a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2312a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
23131da177e4SLinus Torvalds 
2314a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2315a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
23161da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2317a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2318259e5e6cSAndy Lutomirski 
23197b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
23207b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23217b0d0b40SStephen Smalley 		if (rc)
23227b0d0b40SStephen Smalley 			return rc;
23231da177e4SLinus Torvalds 	} else {
23241da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2325aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2326aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2327652bb9b0SEric Paris 					     &new_tsec->sid);
23281da177e4SLinus Torvalds 		if (rc)
23291da177e4SLinus Torvalds 			return rc;
23307b0d0b40SStephen Smalley 
23317b0d0b40SStephen Smalley 		/*
23327b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
23337b0d0b40SStephen Smalley 		 * transition.
23347b0d0b40SStephen Smalley 		 */
23357b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23367b0d0b40SStephen Smalley 		if (rc)
23377b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
23381da177e4SLinus Torvalds 	}
23391da177e4SLinus Torvalds 
234043af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
234143af5de7SVivek Goyal 	ad.u.file = bprm->file;
23421da177e4SLinus Torvalds 
2343a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
23446b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23456b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
23461da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
23471da177e4SLinus Torvalds 		if (rc)
23481da177e4SLinus Torvalds 			return rc;
23491da177e4SLinus Torvalds 	} else {
23501da177e4SLinus Torvalds 		/* Check permissions for the transition. */
23516b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23526b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
23531da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
23541da177e4SLinus Torvalds 		if (rc)
23551da177e4SLinus Torvalds 			return rc;
23561da177e4SLinus Torvalds 
23576b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23586b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
23591da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
23601da177e4SLinus Torvalds 		if (rc)
23611da177e4SLinus Torvalds 			return rc;
23621da177e4SLinus Torvalds 
2363a6f76f23SDavid Howells 		/* Check for shared state */
2364a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
23656b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
23666b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2367a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2368a6f76f23SDavid Howells 					  NULL);
2369a6f76f23SDavid Howells 			if (rc)
2370a6f76f23SDavid Howells 				return -EPERM;
23711da177e4SLinus Torvalds 		}
23721da177e4SLinus Torvalds 
2373a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2374a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
23759227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2376be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2377a6f76f23SDavid Howells 			if (ptsid != 0) {
23786b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
23796b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2380a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2381a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2382a6f76f23SDavid Howells 				if (rc)
2383a6f76f23SDavid Howells 					return -EPERM;
2384a6f76f23SDavid Howells 			}
2385a6f76f23SDavid Howells 		}
2386a6f76f23SDavid Howells 
2387a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2388a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2389a6f76f23SDavid Howells 
23901da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
23911da177e4SLinus Torvalds 		   the noatsecure permission is granted between
23921da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
23936b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23946b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
239562874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
239662874c3aSKees Cook 				  NULL);
239762874c3aSKees Cook 		bprm->secureexec |= !!rc;
23981da177e4SLinus Torvalds 	}
23991da177e4SLinus Torvalds 
240062874c3aSKees Cook 	return 0;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
2403c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2404c3c073f8SAl Viro {
2405c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2406c3c073f8SAl Viro }
2407c3c073f8SAl Viro 
24081da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2409745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2410745ca247SDavid Howells 					    struct files_struct *files)
24111da177e4SLinus Torvalds {
24121da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2413b20c8122SStephen Smalley 	struct tty_struct *tty;
241424ec839cSPeter Zijlstra 	int drop_tty = 0;
2415c3c073f8SAl Viro 	unsigned n;
24161da177e4SLinus Torvalds 
241724ec839cSPeter Zijlstra 	tty = get_current_tty();
24181da177e4SLinus Torvalds 	if (tty) {
24194a510969SPeter Hurley 		spin_lock(&tty->files_lock);
242037dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2421d996b62aSNick Piggin 			struct tty_file_private *file_priv;
242237dd0bd0SEric Paris 
24231da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
242413f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
242513f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
242613f8e981SDavid Howells 			   open file may belong to another process and we are
242713f8e981SDavid Howells 			   only interested in the inode-based check here. */
2428d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2429d996b62aSNick Piggin 						struct tty_file_private, list);
2430d996b62aSNick Piggin 			file = file_priv->file;
243113f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
243224ec839cSPeter Zijlstra 				drop_tty = 1;
24331da177e4SLinus Torvalds 		}
24344a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2435452a00d2SAlan Cox 		tty_kref_put(tty);
24361da177e4SLinus Torvalds 	}
243798a27ba4SEric W. Biederman 	/* Reset controlling tty. */
243898a27ba4SEric W. Biederman 	if (drop_tty)
243998a27ba4SEric W. Biederman 		no_tty();
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2442c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2443c3c073f8SAl Viro 	if (!n) /* none found? */
2444c3c073f8SAl Viro 		return;
24451da177e4SLinus Torvalds 
2446c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
244745525b26SAl Viro 	if (IS_ERR(devnull))
244845525b26SAl Viro 		devnull = NULL;
2449c3c073f8SAl Viro 	/* replace all the matching ones with this */
2450c3c073f8SAl Viro 	do {
245145525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2452c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
245345525b26SAl Viro 	if (devnull)
2454c3c073f8SAl Viro 		fput(devnull);
24551da177e4SLinus Torvalds }
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds /*
2458a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
24591da177e4SLinus Torvalds  */
2460a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
24611da177e4SLinus Torvalds {
2462a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24631da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
24641da177e4SLinus Torvalds 	int rc, i;
24651da177e4SLinus Torvalds 
24660c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
2467a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
24681da177e4SLinus Torvalds 		return;
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2471a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
24721da177e4SLinus Torvalds 
2473a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2474a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2475a6f76f23SDavid Howells 
2476a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2477a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2478a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2479a6f76f23SDavid Howells 	 *
2480a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2481a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2482a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2483a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2484a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2485a6f76f23SDavid Howells 	 */
24866b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
24876b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2488a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2489a6f76f23SDavid Howells 	if (rc) {
2490eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2491eb2d55a3SOleg Nesterov 		task_lock(current);
2492a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2493a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2494a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2495a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2496a6f76f23SDavid Howells 		}
2497eb2d55a3SOleg Nesterov 		task_unlock(current);
2498baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2499eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2500a6f76f23SDavid Howells 	}
2501a6f76f23SDavid Howells }
2502a6f76f23SDavid Howells 
2503a6f76f23SDavid Howells /*
2504a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2505a6f76f23SDavid Howells  * due to exec
2506a6f76f23SDavid Howells  */
2507a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2508a6f76f23SDavid Howells {
25090c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
2510a6f76f23SDavid Howells 	u32 osid, sid;
2511ddbc7d06SArnd Bergmann 	int rc;
2512a6f76f23SDavid Howells 
2513a6f76f23SDavid Howells 	osid = tsec->osid;
2514a6f76f23SDavid Howells 	sid = tsec->sid;
2515a6f76f23SDavid Howells 
2516a6f76f23SDavid Howells 	if (sid == osid)
2517a6f76f23SDavid Howells 		return;
2518a6f76f23SDavid Howells 
2519a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2520a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2521a6f76f23SDavid Howells 	 * flush and unblock signals.
2522a6f76f23SDavid Howells 	 *
2523a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2524a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2525a6f76f23SDavid Howells 	 */
25266b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25276b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
25281da177e4SLinus Torvalds 	if (rc) {
2529ddbc7d06SArnd Bergmann 		clear_itimer();
2530ddbc7d06SArnd Bergmann 
25311da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
25329e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
25339e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
25349e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
25351da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
25361da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
25379e7c8f8cSOleg Nesterov 			recalc_sigpending();
25383bcac026SDavid Howells 		}
25391da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
25401da177e4SLinus Torvalds 	}
25411da177e4SLinus Torvalds 
2542a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2543a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2544ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
25450b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2546ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds /* superblock security operations */
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
25521da177e4SLinus Torvalds {
25531aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
2554cb89e246SPaul Moore 
2555cb89e246SPaul Moore 	mutex_init(&sbsec->lock);
2556cb89e246SPaul Moore 	INIT_LIST_HEAD(&sbsec->isec_head);
2557cb89e246SPaul Moore 	spin_lock_init(&sbsec->isec_lock);
2558cb89e246SPaul Moore 	sbsec->sid = SECINITSID_UNLABELED;
2559cb89e246SPaul Moore 	sbsec->def_sid = SECINITSID_FILE;
2560cb89e246SPaul Moore 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2561cb89e246SPaul Moore 
2562cb89e246SPaul Moore 	return 0;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
256599dbbb59SAl Viro static inline int opt_len(const char *s)
25661da177e4SLinus Torvalds {
256799dbbb59SAl Viro 	bool open_quote = false;
256899dbbb59SAl Viro 	int len;
256999dbbb59SAl Viro 	char c;
25701da177e4SLinus Torvalds 
257199dbbb59SAl Viro 	for (len = 0; (c = s[len]) != '\0'; len++) {
257299dbbb59SAl Viro 		if (c == '"')
25733528a953SCory Olmo 			open_quote = !open_quote;
257499dbbb59SAl Viro 		if (c == ',' && !open_quote)
257599dbbb59SAl Viro 			break;
25761da177e4SLinus Torvalds 	}
257799dbbb59SAl Viro 	return len;
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
2580204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
25815b400239SAl Viro {
258299dbbb59SAl Viro 	char *from = options;
258399dbbb59SAl Viro 	char *to = options;
258499dbbb59SAl Viro 	bool first = true;
2585fec63753SGen Zhang 	int rc;
25865b400239SAl Viro 
258799dbbb59SAl Viro 	while (1) {
258899dbbb59SAl Viro 		int len = opt_len(from);
2589fec63753SGen Zhang 		int token;
259099dbbb59SAl Viro 		char *arg = NULL;
259199dbbb59SAl Viro 
259299dbbb59SAl Viro 		token = match_opt_prefix(from, len, &arg);
259399dbbb59SAl Viro 
259499dbbb59SAl Viro 		if (token != Opt_error) {
259599dbbb59SAl Viro 			char *p, *q;
259699dbbb59SAl Viro 
259799dbbb59SAl Viro 			/* strip quotes */
259899dbbb59SAl Viro 			if (arg) {
259999dbbb59SAl Viro 				for (p = q = arg; p < from + len; p++) {
260099dbbb59SAl Viro 					char c = *p;
260199dbbb59SAl Viro 					if (c != '"')
260299dbbb59SAl Viro 						*q++ = c;
260399dbbb59SAl Viro 				}
260499dbbb59SAl Viro 				arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2605fec63753SGen Zhang 				if (!arg) {
2606fec63753SGen Zhang 					rc = -ENOMEM;
2607fec63753SGen Zhang 					goto free_opt;
2608fec63753SGen Zhang 				}
260999dbbb59SAl Viro 			}
261099dbbb59SAl Viro 			rc = selinux_add_opt(token, arg, mnt_opts);
261199dbbb59SAl Viro 			if (unlikely(rc)) {
261299dbbb59SAl Viro 				kfree(arg);
2613fec63753SGen Zhang 				goto free_opt;
26141da177e4SLinus Torvalds 			}
261599dbbb59SAl Viro 		} else {
261699dbbb59SAl Viro 			if (!first) {	// copy with preceding comma
261799dbbb59SAl Viro 				from--;
261899dbbb59SAl Viro 				len++;
261999dbbb59SAl Viro 			}
262099dbbb59SAl Viro 			if (to != from)
262199dbbb59SAl Viro 				memmove(to, from, len);
262299dbbb59SAl Viro 			to += len;
262399dbbb59SAl Viro 			first = false;
262499dbbb59SAl Viro 		}
262599dbbb59SAl Viro 		if (!from[len])
262699dbbb59SAl Viro 			break;
262799dbbb59SAl Viro 		from += len + 1;
262899dbbb59SAl Viro 	}
262999dbbb59SAl Viro 	*to = '\0';
263099dbbb59SAl Viro 	return 0;
2631fec63753SGen Zhang 
2632fec63753SGen Zhang free_opt:
2633fec63753SGen Zhang 	if (*mnt_opts) {
2634fec63753SGen Zhang 		selinux_free_mnt_opts(*mnt_opts);
2635fec63753SGen Zhang 		*mnt_opts = NULL;
2636fec63753SGen Zhang 	}
2637fec63753SGen Zhang 	return rc;
26385b400239SAl Viro }
26391da177e4SLinus Torvalds 
264069c4a42dSOlga Kornievskaia static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
264169c4a42dSOlga Kornievskaia {
264269c4a42dSOlga Kornievskaia 	struct selinux_mnt_opts *opts = mnt_opts;
264369c4a42dSOlga Kornievskaia 	struct superblock_security_struct *sbsec = sb->s_security;
264469c4a42dSOlga Kornievskaia 	u32 sid;
264569c4a42dSOlga Kornievskaia 	int rc;
264669c4a42dSOlga Kornievskaia 
264769c4a42dSOlga Kornievskaia 	/*
264869c4a42dSOlga Kornievskaia 	 * Superblock not initialized (i.e. no options) - reject if any
264969c4a42dSOlga Kornievskaia 	 * options specified, otherwise accept.
265069c4a42dSOlga Kornievskaia 	 */
265169c4a42dSOlga Kornievskaia 	if (!(sbsec->flags & SE_SBINITIALIZED))
265269c4a42dSOlga Kornievskaia 		return opts ? 1 : 0;
265369c4a42dSOlga Kornievskaia 
265469c4a42dSOlga Kornievskaia 	/*
265569c4a42dSOlga Kornievskaia 	 * Superblock initialized and no options specified - reject if
265669c4a42dSOlga Kornievskaia 	 * superblock has any options set, otherwise accept.
265769c4a42dSOlga Kornievskaia 	 */
265869c4a42dSOlga Kornievskaia 	if (!opts)
265969c4a42dSOlga Kornievskaia 		return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
266069c4a42dSOlga Kornievskaia 
266169c4a42dSOlga Kornievskaia 	if (opts->fscontext) {
266269c4a42dSOlga Kornievskaia 		rc = parse_sid(sb, opts->fscontext, &sid);
266369c4a42dSOlga Kornievskaia 		if (rc)
266469c4a42dSOlga Kornievskaia 			return 1;
266569c4a42dSOlga Kornievskaia 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
266669c4a42dSOlga Kornievskaia 			return 1;
266769c4a42dSOlga Kornievskaia 	}
266869c4a42dSOlga Kornievskaia 	if (opts->context) {
266969c4a42dSOlga Kornievskaia 		rc = parse_sid(sb, opts->context, &sid);
267069c4a42dSOlga Kornievskaia 		if (rc)
267169c4a42dSOlga Kornievskaia 			return 1;
267269c4a42dSOlga Kornievskaia 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
267369c4a42dSOlga Kornievskaia 			return 1;
267469c4a42dSOlga Kornievskaia 	}
267569c4a42dSOlga Kornievskaia 	if (opts->rootcontext) {
267669c4a42dSOlga Kornievskaia 		struct inode_security_struct *root_isec;
267769c4a42dSOlga Kornievskaia 
267869c4a42dSOlga Kornievskaia 		root_isec = backing_inode_security(sb->s_root);
267969c4a42dSOlga Kornievskaia 		rc = parse_sid(sb, opts->rootcontext, &sid);
268069c4a42dSOlga Kornievskaia 		if (rc)
268169c4a42dSOlga Kornievskaia 			return 1;
268269c4a42dSOlga Kornievskaia 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
268369c4a42dSOlga Kornievskaia 			return 1;
268469c4a42dSOlga Kornievskaia 	}
268569c4a42dSOlga Kornievskaia 	if (opts->defcontext) {
268669c4a42dSOlga Kornievskaia 		rc = parse_sid(sb, opts->defcontext, &sid);
268769c4a42dSOlga Kornievskaia 		if (rc)
268869c4a42dSOlga Kornievskaia 			return 1;
268969c4a42dSOlga Kornievskaia 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
269069c4a42dSOlga Kornievskaia 			return 1;
269169c4a42dSOlga Kornievskaia 	}
269269c4a42dSOlga Kornievskaia 	return 0;
269369c4a42dSOlga Kornievskaia }
269469c4a42dSOlga Kornievskaia 
2695204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2696026eb167SEric Paris {
2697bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
26981aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec = selinux_superblock(sb);
2699bd323655SAl Viro 	u32 sid;
2700bd323655SAl Viro 	int rc;
2701026eb167SEric Paris 
2702026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2703026eb167SEric Paris 		return 0;
2704026eb167SEric Paris 
2705204cc0ccSAl Viro 	if (!opts)
2706026eb167SEric Paris 		return 0;
2707026eb167SEric Paris 
2708bd323655SAl Viro 	if (opts->fscontext) {
2709bd323655SAl Viro 		rc = parse_sid(sb, opts->fscontext, &sid);
2710026eb167SEric Paris 		if (rc)
2711c039bc3cSAl Viro 			return rc;
2712026eb167SEric Paris 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2713026eb167SEric Paris 			goto out_bad_option;
2714bd323655SAl Viro 	}
2715bd323655SAl Viro 	if (opts->context) {
2716bd323655SAl Viro 		rc = parse_sid(sb, opts->context, &sid);
2717bd323655SAl Viro 		if (rc)
2718bd323655SAl Viro 			return rc;
2719026eb167SEric Paris 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2720026eb167SEric Paris 			goto out_bad_option;
2721bd323655SAl Viro 	}
2722bd323655SAl Viro 	if (opts->rootcontext) {
2723026eb167SEric Paris 		struct inode_security_struct *root_isec;
272483da53c5SAndreas Gruenbacher 		root_isec = backing_inode_security(sb->s_root);
2725bd323655SAl Viro 		rc = parse_sid(sb, opts->rootcontext, &sid);
2726bd323655SAl Viro 		if (rc)
2727bd323655SAl Viro 			return rc;
2728026eb167SEric Paris 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2729026eb167SEric Paris 			goto out_bad_option;
2730026eb167SEric Paris 	}
2731bd323655SAl Viro 	if (opts->defcontext) {
2732bd323655SAl Viro 		rc = parse_sid(sb, opts->defcontext, &sid);
2733bd323655SAl Viro 		if (rc)
2734bd323655SAl Viro 			return rc;
2735026eb167SEric Paris 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2736026eb167SEric Paris 			goto out_bad_option;
2737026eb167SEric Paris 	}
2738c039bc3cSAl Viro 	return 0;
2739026eb167SEric Paris 
2740026eb167SEric Paris out_bad_option:
2741c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
274229b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
274329b1deb2SLinus Torvalds 	       sb->s_type->name);
2744c039bc3cSAl Viro 	return -EINVAL;
2745026eb167SEric Paris }
2746026eb167SEric Paris 
2747a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb)
27481da177e4SLinus Torvalds {
274988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27502bf49690SThomas Liu 	struct common_audit_data ad;
275174192246SJames Morris 
275250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2753a269434dSEric Paris 	ad.u.dentry = sb->s_root;
275488e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
2757726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
27581da177e4SLinus Torvalds {
275988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27602bf49690SThomas Liu 	struct common_audit_data ad;
27611da177e4SLinus Torvalds 
276250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2763a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
276488e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
27651da177e4SLinus Torvalds }
27661da177e4SLinus Torvalds 
2767808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
27688a04c43bSAl Viro 			 const struct path *path,
2769808d4e3cSAl Viro 			 const char *type,
27701da177e4SLinus Torvalds 			 unsigned long flags,
27711da177e4SLinus Torvalds 			 void *data)
27721da177e4SLinus Torvalds {
277388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2776d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
27771da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
27781da177e4SLinus Torvalds 	else
27792875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27801da177e4SLinus Torvalds }
27811da177e4SLinus Torvalds 
278298aa0034SStephen Smalley static int selinux_move_mount(const struct path *from_path,
278398aa0034SStephen Smalley 			      const struct path *to_path)
278498aa0034SStephen Smalley {
278598aa0034SStephen Smalley 	const struct cred *cred = current_cred();
278698aa0034SStephen Smalley 
278798aa0034SStephen Smalley 	return path_has_perm(cred, to_path, FILE__MOUNTON);
278898aa0034SStephen Smalley }
278998aa0034SStephen Smalley 
27901da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27911da177e4SLinus Torvalds {
279288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27931da177e4SLinus Torvalds 
279488e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27951da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
27980b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc,
27990b52075eSAl Viro 				  struct fs_context *src_fc)
28000b52075eSAl Viro {
28010b52075eSAl Viro 	const struct selinux_mnt_opts *src = src_fc->security;
28020b52075eSAl Viro 	struct selinux_mnt_opts *opts;
28030b52075eSAl Viro 
28040b52075eSAl Viro 	if (!src)
28050b52075eSAl Viro 		return 0;
28060b52075eSAl Viro 
28070b52075eSAl Viro 	fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
28080b52075eSAl Viro 	if (!fc->security)
28090b52075eSAl Viro 		return -ENOMEM;
28100b52075eSAl Viro 
28110b52075eSAl Viro 	opts = fc->security;
28120b52075eSAl Viro 
28130b52075eSAl Viro 	if (src->fscontext) {
28140b52075eSAl Viro 		opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
28150b52075eSAl Viro 		if (!opts->fscontext)
28160b52075eSAl Viro 			return -ENOMEM;
28170b52075eSAl Viro 	}
28180b52075eSAl Viro 	if (src->context) {
28190b52075eSAl Viro 		opts->context = kstrdup(src->context, GFP_KERNEL);
28200b52075eSAl Viro 		if (!opts->context)
28210b52075eSAl Viro 			return -ENOMEM;
28220b52075eSAl Viro 	}
28230b52075eSAl Viro 	if (src->rootcontext) {
28240b52075eSAl Viro 		opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
28250b52075eSAl Viro 		if (!opts->rootcontext)
28260b52075eSAl Viro 			return -ENOMEM;
28270b52075eSAl Viro 	}
28280b52075eSAl Viro 	if (src->defcontext) {
28290b52075eSAl Viro 		opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
28300b52075eSAl Viro 		if (!opts->defcontext)
28310b52075eSAl Viro 			return -ENOMEM;
28320b52075eSAl Viro 	}
28330b52075eSAl Viro 	return 0;
28340b52075eSAl Viro }
28350b52075eSAl Viro 
2836d7167b14SAl Viro static const struct fs_parameter_spec selinux_fs_parameters[] = {
2837442155c1SDavid Howells 	fsparam_string(CONTEXT_STR,	Opt_context),
2838442155c1SDavid Howells 	fsparam_string(DEFCONTEXT_STR,	Opt_defcontext),
2839442155c1SDavid Howells 	fsparam_string(FSCONTEXT_STR,	Opt_fscontext),
2840442155c1SDavid Howells 	fsparam_string(ROOTCONTEXT_STR,	Opt_rootcontext),
2841442155c1SDavid Howells 	fsparam_flag  (SECLABEL_STR,	Opt_seclabel),
2842442155c1SDavid Howells 	{}
2843442155c1SDavid Howells };
2844442155c1SDavid Howells 
2845442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc,
2846442155c1SDavid Howells 					  struct fs_parameter *param)
2847442155c1SDavid Howells {
2848442155c1SDavid Howells 	struct fs_parse_result result;
2849442155c1SDavid Howells 	int opt, rc;
2850442155c1SDavid Howells 
2851d7167b14SAl Viro 	opt = fs_parse(fc, selinux_fs_parameters, param, &result);
2852442155c1SDavid Howells 	if (opt < 0)
2853442155c1SDavid Howells 		return opt;
2854442155c1SDavid Howells 
2855442155c1SDavid Howells 	rc = selinux_add_opt(opt, param->string, &fc->security);
2856442155c1SDavid Howells 	if (!rc) {
2857442155c1SDavid Howells 		param->string = NULL;
2858442155c1SDavid Howells 		rc = 1;
2859442155c1SDavid Howells 	}
2860442155c1SDavid Howells 	return rc;
2861442155c1SDavid Howells }
2862442155c1SDavid Howells 
28631da177e4SLinus Torvalds /* inode security operations */
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
28661da177e4SLinus Torvalds {
2867cb89e246SPaul Moore 	struct inode_security_struct *isec = selinux_inode(inode);
2868cb89e246SPaul Moore 	u32 sid = current_sid();
2869cb89e246SPaul Moore 
2870cb89e246SPaul Moore 	spin_lock_init(&isec->lock);
2871cb89e246SPaul Moore 	INIT_LIST_HEAD(&isec->list);
2872cb89e246SPaul Moore 	isec->inode = inode;
2873cb89e246SPaul Moore 	isec->sid = SECINITSID_UNLABELED;
2874cb89e246SPaul Moore 	isec->sclass = SECCLASS_FILE;
2875cb89e246SPaul Moore 	isec->task_sid = sid;
2876cb89e246SPaul Moore 	isec->initialized = LABEL_INVALID;
2877cb89e246SPaul Moore 
2878cb89e246SPaul Moore 	return 0;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
28821da177e4SLinus Torvalds {
28831da177e4SLinus Torvalds 	inode_free_security(inode);
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
2886d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
288715bf3239SVivek Goyal 					const struct qstr *name,
288815bf3239SVivek Goyal 					const char **xattr_name, void **ctx,
2889d47be3dfSDavid Quigley 					u32 *ctxlen)
2890d47be3dfSDavid Quigley {
2891d47be3dfSDavid Quigley 	u32 newsid;
2892d47be3dfSDavid Quigley 	int rc;
2893d47be3dfSDavid Quigley 
28940c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2895c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2896d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2897d47be3dfSDavid Quigley 					   &newsid);
2898c3c188b2SDavid Howells 	if (rc)
2899d47be3dfSDavid Quigley 		return rc;
2900d47be3dfSDavid Quigley 
290115bf3239SVivek Goyal 	if (xattr_name)
290215bf3239SVivek Goyal 		*xattr_name = XATTR_NAME_SELINUX;
290315bf3239SVivek Goyal 
2904aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2905aa8e712cSStephen Smalley 				       ctxlen);
2906d47be3dfSDavid Quigley }
2907d47be3dfSDavid Quigley 
2908a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2909a518b0a5SVivek Goyal 					  struct qstr *name,
2910a518b0a5SVivek Goyal 					  const struct cred *old,
2911a518b0a5SVivek Goyal 					  struct cred *new)
2912a518b0a5SVivek Goyal {
2913a518b0a5SVivek Goyal 	u32 newsid;
2914a518b0a5SVivek Goyal 	int rc;
2915a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2916a518b0a5SVivek Goyal 
29170c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(old),
2918a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
2919a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
2920a518b0a5SVivek Goyal 					   &newsid);
2921a518b0a5SVivek Goyal 	if (rc)
2922a518b0a5SVivek Goyal 		return rc;
2923a518b0a5SVivek Goyal 
29240c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
2925a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
2926a518b0a5SVivek Goyal 	return 0;
2927a518b0a5SVivek Goyal }
2928a518b0a5SVivek Goyal 
29295e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
29309548906bSTetsuo Handa 				       const struct qstr *qstr,
29319548906bSTetsuo Handa 				       const char **name,
29322a7dba39SEric Paris 				       void **value, size_t *len)
29335e41ff9eSStephen Smalley {
29340c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
29355e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2936c0d4f464SCorentin LABBE 	u32 newsid, clen;
29375e41ff9eSStephen Smalley 	int rc;
29389548906bSTetsuo Handa 	char *context;
29395e41ff9eSStephen Smalley 
29401aea7808SCasey Schaufler 	sbsec = selinux_superblock(dir->i_sb);
29415e41ff9eSStephen Smalley 
29425e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2943275bb41eSDavid Howells 
2944210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, qstr,
29455e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
2946c3c188b2SDavid Howells 		&newsid);
2947c3c188b2SDavid Howells 	if (rc)
29485e41ff9eSStephen Smalley 		return rc;
29495e41ff9eSStephen Smalley 
2950296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
29510d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
295280788c22SCasey Schaufler 		struct inode_security_struct *isec = selinux_inode(inode);
2953296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2954296fddf7SEric Paris 		isec->sid = newsid;
29556f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
2956296fddf7SEric Paris 	}
29575e41ff9eSStephen Smalley 
295865cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state) ||
295965cddd50SOndrej Mosnacek 	    !(sbsec->flags & SBLABEL_MNT))
296025a74f3bSStephen Smalley 		return -EOPNOTSUPP;
296125a74f3bSStephen Smalley 
29629548906bSTetsuo Handa 	if (name)
29639548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
29645e41ff9eSStephen Smalley 
2965570bc1c2SStephen Smalley 	if (value && len) {
2966aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
2967aa8e712cSStephen Smalley 						   &context, &clen);
29689548906bSTetsuo Handa 		if (rc)
29695e41ff9eSStephen Smalley 			return rc;
29705e41ff9eSStephen Smalley 		*value = context;
2971570bc1c2SStephen Smalley 		*len = clen;
2972570bc1c2SStephen Smalley 	}
29735e41ff9eSStephen Smalley 
29745e41ff9eSStephen Smalley 	return 0;
29755e41ff9eSStephen Smalley }
29765e41ff9eSStephen Smalley 
297729cd6591SDaniel Colascione static int selinux_inode_init_security_anon(struct inode *inode,
297829cd6591SDaniel Colascione 					    const struct qstr *name,
297929cd6591SDaniel Colascione 					    const struct inode *context_inode)
298029cd6591SDaniel Colascione {
298129cd6591SDaniel Colascione 	const struct task_security_struct *tsec = selinux_cred(current_cred());
298229cd6591SDaniel Colascione 	struct common_audit_data ad;
298329cd6591SDaniel Colascione 	struct inode_security_struct *isec;
298429cd6591SDaniel Colascione 	int rc;
298529cd6591SDaniel Colascione 
298629cd6591SDaniel Colascione 	if (unlikely(!selinux_initialized(&selinux_state)))
298729cd6591SDaniel Colascione 		return 0;
298829cd6591SDaniel Colascione 
298929cd6591SDaniel Colascione 	isec = selinux_inode(inode);
299029cd6591SDaniel Colascione 
299129cd6591SDaniel Colascione 	/*
299229cd6591SDaniel Colascione 	 * We only get here once per ephemeral inode.  The inode has
299329cd6591SDaniel Colascione 	 * been initialized via inode_alloc_security but is otherwise
299429cd6591SDaniel Colascione 	 * untouched.
299529cd6591SDaniel Colascione 	 */
299629cd6591SDaniel Colascione 
299729cd6591SDaniel Colascione 	if (context_inode) {
299829cd6591SDaniel Colascione 		struct inode_security_struct *context_isec =
299929cd6591SDaniel Colascione 			selinux_inode(context_inode);
300029cd6591SDaniel Colascione 		if (context_isec->initialized != LABEL_INITIALIZED) {
300129cd6591SDaniel Colascione 			pr_err("SELinux:  context_inode is not initialized");
300229cd6591SDaniel Colascione 			return -EACCES;
300329cd6591SDaniel Colascione 		}
300429cd6591SDaniel Colascione 
300529cd6591SDaniel Colascione 		isec->sclass = context_isec->sclass;
300629cd6591SDaniel Colascione 		isec->sid = context_isec->sid;
300729cd6591SDaniel Colascione 	} else {
300829cd6591SDaniel Colascione 		isec->sclass = SECCLASS_ANON_INODE;
300929cd6591SDaniel Colascione 		rc = security_transition_sid(
301029cd6591SDaniel Colascione 			&selinux_state, tsec->sid, tsec->sid,
301129cd6591SDaniel Colascione 			isec->sclass, name, &isec->sid);
301229cd6591SDaniel Colascione 		if (rc)
301329cd6591SDaniel Colascione 			return rc;
301429cd6591SDaniel Colascione 	}
301529cd6591SDaniel Colascione 
301629cd6591SDaniel Colascione 	isec->initialized = LABEL_INITIALIZED;
301729cd6591SDaniel Colascione 	/*
301829cd6591SDaniel Colascione 	 * Now that we've initialized security, check whether we're
301929cd6591SDaniel Colascione 	 * allowed to actually create this type of anonymous inode.
302029cd6591SDaniel Colascione 	 */
302129cd6591SDaniel Colascione 
302229cd6591SDaniel Colascione 	ad.type = LSM_AUDIT_DATA_INODE;
302329cd6591SDaniel Colascione 	ad.u.inode = inode;
302429cd6591SDaniel Colascione 
302529cd6591SDaniel Colascione 	return avc_has_perm(&selinux_state,
302629cd6591SDaniel Colascione 			    tsec->sid,
302729cd6591SDaniel Colascione 			    isec->sid,
302829cd6591SDaniel Colascione 			    isec->sclass,
302929cd6591SDaniel Colascione 			    FILE__CREATE,
303029cd6591SDaniel Colascione 			    &ad);
303129cd6591SDaniel Colascione }
303229cd6591SDaniel Colascione 
30334acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
30341da177e4SLinus Torvalds {
30351da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
30361da177e4SLinus Torvalds }
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30391da177e4SLinus Torvalds {
30401da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
30411da177e4SLinus Torvalds }
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
30441da177e4SLinus Torvalds {
30451da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
30461da177e4SLinus Torvalds }
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
30491da177e4SLinus Torvalds {
30501da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
30511da177e4SLinus Torvalds }
30521da177e4SLinus Torvalds 
305318bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
30541da177e4SLinus Torvalds {
30551da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
30561da177e4SLinus Torvalds }
30571da177e4SLinus Torvalds 
30581da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
30591da177e4SLinus Torvalds {
30601da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
30631a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
30641da177e4SLinus Torvalds {
30651da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
30691da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
30701da177e4SLinus Torvalds {
30711da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
30751da177e4SLinus Torvalds {
307688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
307788e67f3bSDavid Howells 
30782875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
30791da177e4SLinus Torvalds }
30801da177e4SLinus Torvalds 
3081bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3082bda0be7aSNeilBrown 				     bool rcu)
30831da177e4SLinus Torvalds {
308488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3085bda0be7aSNeilBrown 	struct common_audit_data ad;
3086bda0be7aSNeilBrown 	struct inode_security_struct *isec;
3087bda0be7aSNeilBrown 	u32 sid;
30881da177e4SLinus Torvalds 
3089bda0be7aSNeilBrown 	validate_creds(cred);
3090bda0be7aSNeilBrown 
3091bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
3092bda0be7aSNeilBrown 	ad.u.dentry = dentry;
3093bda0be7aSNeilBrown 	sid = cred_sid(cred);
30945d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
30955d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
30965d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
3097bda0be7aSNeilBrown 
3098d99cf13fSAl Viro 	return avc_has_perm(&selinux_state,
3099d99cf13fSAl Viro 				  sid, isec->sid, isec->sclass, FILE__READ, &ad);
31001da177e4SLinus Torvalds }
31011da177e4SLinus Torvalds 
3102d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
3103d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
31040188d5c0SStephen Smalley 					   int result)
3105d4cf970dSEric Paris {
3106d4cf970dSEric Paris 	struct common_audit_data ad;
310780788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
3108d4cf970dSEric Paris 
310950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
3110d4cf970dSEric Paris 	ad.u.inode = inode;
3111d4cf970dSEric Paris 
3112b17ec22fSAl Viro 	return slow_avc_audit(&selinux_state,
31136b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
31140188d5c0SStephen Smalley 			    audited, denied, result, &ad);
3115d4cf970dSEric Paris }
3116d4cf970dSEric Paris 
3117e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
31181da177e4SLinus Torvalds {
311988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3120b782e0a6SEric Paris 	u32 perms;
3121b782e0a6SEric Paris 	bool from_access;
31225298d0b9SStephen Smalley 	bool no_block = mask & MAY_NOT_BLOCK;
31232e334057SEric Paris 	struct inode_security_struct *isec;
31242e334057SEric Paris 	u32 sid;
31252e334057SEric Paris 	struct av_decision avd;
31262e334057SEric Paris 	int rc, rc2;
31272e334057SEric Paris 	u32 audited, denied;
31281da177e4SLinus Torvalds 
3129b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3130d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3131d09ca739SEric Paris 
31321da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3133b782e0a6SEric Paris 	if (!mask)
31341da177e4SLinus Torvalds 		return 0;
31351da177e4SLinus Torvalds 
31362e334057SEric Paris 	validate_creds(cred);
3137b782e0a6SEric Paris 
31382e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
31392e334057SEric Paris 		return 0;
3140b782e0a6SEric Paris 
3141b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3142b782e0a6SEric Paris 
31432e334057SEric Paris 	sid = cred_sid(cred);
31445298d0b9SStephen Smalley 	isec = inode_security_rcu(inode, no_block);
31455d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31465d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
31472e334057SEric Paris 
31486b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
3149b17ec22fSAl Viro 				  sid, isec->sid, isec->sclass, perms, 0,
31503a28cff3SStephen Smalley 				  &avd);
31512e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
31522e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
31532e334057SEric Paris 				     &denied);
31542e334057SEric Paris 	if (likely(!audited))
31552e334057SEric Paris 		return rc;
31562e334057SEric Paris 
31570188d5c0SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
31582e334057SEric Paris 	if (rc2)
31592e334057SEric Paris 		return rc2;
31602e334057SEric Paris 	return rc;
31611da177e4SLinus Torvalds }
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
31641da177e4SLinus Torvalds {
316588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3166ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3167bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
316895dbf739SEric Paris 	__u32 av = FILE__WRITE;
31691da177e4SLinus Torvalds 
3170bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3171bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3172bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3173bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3174bc6a6008SAmerigo Wang 		if (!ia_valid)
31751da177e4SLinus Torvalds 			return 0;
3176bc6a6008SAmerigo Wang 	}
31771da177e4SLinus Torvalds 
3178bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3179bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
31802875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
31811da177e4SLinus Torvalds 
3182aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3183ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3184ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3185ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
318695dbf739SEric Paris 		av |= FILE__OPEN;
318795dbf739SEric Paris 
318895dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
31891da177e4SLinus Torvalds }
31901da177e4SLinus Torvalds 
31913f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
31921da177e4SLinus Torvalds {
31933f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
31941da177e4SLinus Torvalds }
31951da177e4SLinus Torvalds 
3196db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3197db59000aSStephen Smalley {
3198db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3199c1a85a00SMicah Morton 	unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3200db59000aSStephen Smalley 
3201c1a85a00SMicah Morton 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3202db59000aSStephen Smalley 		return false;
3203c1a85a00SMicah Morton 	if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3204db59000aSStephen Smalley 		return false;
3205db59000aSStephen Smalley 	return true;
3206db59000aSStephen Smalley }
3207db59000aSStephen Smalley 
320871bc356fSChristian Brauner static int selinux_inode_setxattr(struct user_namespace *mnt_userns,
320971bc356fSChristian Brauner 				  struct dentry *dentry, const char *name,
32108f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
32111da177e4SLinus Torvalds {
3212c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
321320cdef8dSPaul Moore 	struct inode_security_struct *isec;
32141da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
32152bf49690SThomas Liu 	struct common_audit_data ad;
3216275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
32171da177e4SLinus Torvalds 	int rc = 0;
32181da177e4SLinus Torvalds 
32196b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32206b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
32216b240306SEric W. Biederman 		if (rc)
32226b240306SEric W. Biederman 			return rc;
32236b240306SEric W. Biederman 
32246b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
32256b240306SEric W. Biederman 		   ordinary setattr permission. */
32266b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32276b240306SEric W. Biederman 	}
32281da177e4SLinus Torvalds 
322965cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
323071bc356fSChristian Brauner 		return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM);
32313e3e24b4SJonathan Lebon 
32321aea7808SCasey Schaufler 	sbsec = selinux_superblock(inode->i_sb);
323312f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
32341da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32351da177e4SLinus Torvalds 
323671bc356fSChristian Brauner 	if (!inode_owner_or_capable(mnt_userns, inode))
32371da177e4SLinus Torvalds 		return -EPERM;
32381da177e4SLinus Torvalds 
323950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3240a269434dSEric Paris 	ad.u.dentry = dentry;
32411da177e4SLinus Torvalds 
324220cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
32436b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32446b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
32451da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
32461da177e4SLinus Torvalds 	if (rc)
32471da177e4SLinus Torvalds 		return rc;
32481da177e4SLinus Torvalds 
3249aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3250aa8e712cSStephen Smalley 				     GFP_KERNEL);
325112b29f34SStephen Smalley 	if (rc == -EINVAL) {
3252db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3253d6ea83ecSEric Paris 			struct audit_buffer *ab;
3254d6ea83ecSEric Paris 			size_t audit_size;
3255d6ea83ecSEric Paris 
3256d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3257d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3258e3fea3f7SAl Viro 			if (value) {
3259add24372SColin Ian King 				const char *str = value;
3260add24372SColin Ian King 
3261d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3262d6ea83ecSEric Paris 					audit_size = size - 1;
3263d6ea83ecSEric Paris 				else
3264d6ea83ecSEric Paris 					audit_size = size;
3265e3fea3f7SAl Viro 			} else {
3266e3fea3f7SAl Viro 				audit_size = 0;
3267e3fea3f7SAl Viro 			}
3268cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3269cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3270893c47d1SAustin Kim 			if (!ab)
3271893c47d1SAustin Kim 				return rc;
3272d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3273d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3274d6ea83ecSEric Paris 			audit_log_end(ab);
3275d6ea83ecSEric Paris 
327612b29f34SStephen Smalley 			return rc;
3277d6ea83ecSEric Paris 		}
3278aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3279aa8e712cSStephen Smalley 						   size, &newsid);
328012b29f34SStephen Smalley 	}
32811da177e4SLinus Torvalds 	if (rc)
32821da177e4SLinus Torvalds 		return rc;
32831da177e4SLinus Torvalds 
32846b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32856b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
32861da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
32871da177e4SLinus Torvalds 	if (rc)
32881da177e4SLinus Torvalds 		return rc;
32891da177e4SLinus Torvalds 
3290aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3291aa8e712cSStephen Smalley 					  sid, isec->sclass);
32921da177e4SLinus Torvalds 	if (rc)
32931da177e4SLinus Torvalds 		return rc;
32941da177e4SLinus Torvalds 
32956b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
32966b6bc620SStephen Smalley 			    newsid,
32971da177e4SLinus Torvalds 			    sbsec->sid,
32981da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
32991da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
33001da177e4SLinus Torvalds 			    &ad);
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
33038f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
33048f0cfa52SDavid Howells 					const void *value, size_t size,
33058f0cfa52SDavid Howells 					int flags)
33061da177e4SLinus Torvalds {
3307c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
330820cdef8dSPaul Moore 	struct inode_security_struct *isec;
33091da177e4SLinus Torvalds 	u32 newsid;
33101da177e4SLinus Torvalds 	int rc;
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33131da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
33141da177e4SLinus Torvalds 		return;
33151da177e4SLinus Torvalds 	}
33161da177e4SLinus Torvalds 
331765cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
33183e3e24b4SJonathan Lebon 		/* If we haven't even been initialized, then we can't validate
33193e3e24b4SJonathan Lebon 		 * against a policy, so leave the label as invalid. It may
33203e3e24b4SJonathan Lebon 		 * resolve to a valid label on the next revalidation try if
33213e3e24b4SJonathan Lebon 		 * we've since initialized.
33223e3e24b4SJonathan Lebon 		 */
33233e3e24b4SJonathan Lebon 		return;
33243e3e24b4SJonathan Lebon 	}
33253e3e24b4SJonathan Lebon 
3326aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3327aa8e712cSStephen Smalley 					   &newsid);
33281da177e4SLinus Torvalds 	if (rc) {
3329c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
333012b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
333112b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
33321da177e4SLinus Torvalds 		return;
33331da177e4SLinus Torvalds 	}
33341da177e4SLinus Torvalds 
333520cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
33369287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3337aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
33381da177e4SLinus Torvalds 	isec->sid = newsid;
33396f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
33409287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3341aa9c2669SDavid Quigley 
33421da177e4SLinus Torvalds 	return;
33431da177e4SLinus Torvalds }
33441da177e4SLinus Torvalds 
33458f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
33461da177e4SLinus Torvalds {
334788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
334888e67f3bSDavid Howells 
33492875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
33531da177e4SLinus Torvalds {
335488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
335588e67f3bSDavid Howells 
33562875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33571da177e4SLinus Torvalds }
33581da177e4SLinus Torvalds 
335971bc356fSChristian Brauner static int selinux_inode_removexattr(struct user_namespace *mnt_userns,
336071bc356fSChristian Brauner 				     struct dentry *dentry, const char *name)
33611da177e4SLinus Torvalds {
33626b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
336371bc356fSChristian Brauner 		int rc = cap_inode_removexattr(mnt_userns, dentry, name);
33646b240306SEric W. Biederman 		if (rc)
33656b240306SEric W. Biederman 			return rc;
33666b240306SEric W. Biederman 
33676b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
33686b240306SEric W. Biederman 		   ordinary setattr permission. */
33696b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
33706b240306SEric W. Biederman 	}
33711da177e4SLinus Torvalds 
33729530a3e0SStephen Smalley 	if (!selinux_initialized(&selinux_state))
33739530a3e0SStephen Smalley 		return 0;
33749530a3e0SStephen Smalley 
33751da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
33761da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
33771da177e4SLinus Torvalds 	return -EACCES;
33781da177e4SLinus Torvalds }
33791da177e4SLinus Torvalds 
3380ac5656d8SAaron Goidel static int selinux_path_notify(const struct path *path, u64 mask,
3381ac5656d8SAaron Goidel 						unsigned int obj_type)
3382ac5656d8SAaron Goidel {
3383ac5656d8SAaron Goidel 	int ret;
3384ac5656d8SAaron Goidel 	u32 perm;
3385ac5656d8SAaron Goidel 
3386ac5656d8SAaron Goidel 	struct common_audit_data ad;
3387ac5656d8SAaron Goidel 
3388ac5656d8SAaron Goidel 	ad.type = LSM_AUDIT_DATA_PATH;
3389ac5656d8SAaron Goidel 	ad.u.path = *path;
3390ac5656d8SAaron Goidel 
3391ac5656d8SAaron Goidel 	/*
3392ac5656d8SAaron Goidel 	 * Set permission needed based on the type of mark being set.
3393ac5656d8SAaron Goidel 	 * Performs an additional check for sb watches.
3394ac5656d8SAaron Goidel 	 */
3395ac5656d8SAaron Goidel 	switch (obj_type) {
3396ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3397ac5656d8SAaron Goidel 		perm = FILE__WATCH_MOUNT;
3398ac5656d8SAaron Goidel 		break;
3399ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_SB:
3400ac5656d8SAaron Goidel 		perm = FILE__WATCH_SB;
3401ac5656d8SAaron Goidel 		ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3402ac5656d8SAaron Goidel 						FILESYSTEM__WATCH, &ad);
3403ac5656d8SAaron Goidel 		if (ret)
3404ac5656d8SAaron Goidel 			return ret;
3405ac5656d8SAaron Goidel 		break;
3406ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_INODE:
3407ac5656d8SAaron Goidel 		perm = FILE__WATCH;
3408ac5656d8SAaron Goidel 		break;
3409ac5656d8SAaron Goidel 	default:
3410ac5656d8SAaron Goidel 		return -EINVAL;
3411ac5656d8SAaron Goidel 	}
3412ac5656d8SAaron Goidel 
3413ac5656d8SAaron Goidel 	/* blocking watches require the file:watch_with_perm permission */
3414ac5656d8SAaron Goidel 	if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3415ac5656d8SAaron Goidel 		perm |= FILE__WATCH_WITH_PERM;
3416ac5656d8SAaron Goidel 
3417ac5656d8SAaron Goidel 	/* watches on read-like events need the file:watch_reads permission */
3418ac5656d8SAaron Goidel 	if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3419ac5656d8SAaron Goidel 		perm |= FILE__WATCH_READS;
3420ac5656d8SAaron Goidel 
3421ac5656d8SAaron Goidel 	return path_has_perm(current_cred(), path, perm);
3422ac5656d8SAaron Goidel }
3423ac5656d8SAaron Goidel 
3424d381d8a9SJames Morris /*
3425abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3426d381d8a9SJames Morris  *
3427d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3428d381d8a9SJames Morris  */
342971bc356fSChristian Brauner static int selinux_inode_getsecurity(struct user_namespace *mnt_userns,
343071bc356fSChristian Brauner 				     struct inode *inode, const char *name,
343171bc356fSChristian Brauner 				     void **buffer, bool alloc)
34321da177e4SLinus Torvalds {
343342492594SDavid P. Quigley 	u32 size;
343442492594SDavid P. Quigley 	int error;
343542492594SDavid P. Quigley 	char *context = NULL;
343620cdef8dSPaul Moore 	struct inode_security_struct *isec;
34371da177e4SLinus Torvalds 
3438c8e22261SJonathan Lebon 	/*
3439c8e22261SJonathan Lebon 	 * If we're not initialized yet, then we can't validate contexts, so
3440c8e22261SJonathan Lebon 	 * just let vfs_getxattr fall back to using the on-disk xattr.
3441c8e22261SJonathan Lebon 	 */
3442c8e22261SJonathan Lebon 	if (!selinux_initialized(&selinux_state) ||
3443c8e22261SJonathan Lebon 	    strcmp(name, XATTR_SELINUX_SUFFIX))
34448c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
34451da177e4SLinus Torvalds 
3446abc69bb6SStephen Smalley 	/*
3447abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3448abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3449abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3450abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3451abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3452abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3453abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3454abc69bb6SStephen Smalley 	 */
345520cdef8dSPaul Moore 	isec = inode_security(inode);
3456db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3457aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3458aa8e712cSStephen Smalley 						      isec->sid, &context,
3459abc69bb6SStephen Smalley 						      &size);
3460abc69bb6SStephen Smalley 	else
3461aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3462aa8e712cSStephen Smalley 						&context, &size);
346342492594SDavid P. Quigley 	if (error)
346442492594SDavid P. Quigley 		return error;
346542492594SDavid P. Quigley 	error = size;
346642492594SDavid P. Quigley 	if (alloc) {
346742492594SDavid P. Quigley 		*buffer = context;
346842492594SDavid P. Quigley 		goto out_nofree;
346942492594SDavid P. Quigley 	}
347042492594SDavid P. Quigley 	kfree(context);
347142492594SDavid P. Quigley out_nofree:
347242492594SDavid P. Quigley 	return error;
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
34761da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
34771da177e4SLinus Torvalds {
34782c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
34791aea7808SCasey Schaufler 	struct superblock_security_struct *sbsec;
34801da177e4SLinus Torvalds 	u32 newsid;
34811da177e4SLinus Torvalds 	int rc;
34821da177e4SLinus Torvalds 
34831da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34841da177e4SLinus Torvalds 		return -EOPNOTSUPP;
34851da177e4SLinus Torvalds 
34861aea7808SCasey Schaufler 	sbsec = selinux_superblock(inode->i_sb);
348753e0c2aaSOndrej Mosnacek 	if (!(sbsec->flags & SBLABEL_MNT))
348853e0c2aaSOndrej Mosnacek 		return -EOPNOTSUPP;
348953e0c2aaSOndrej Mosnacek 
34901da177e4SLinus Torvalds 	if (!value || !size)
34911da177e4SLinus Torvalds 		return -EACCES;
34921da177e4SLinus Torvalds 
3493aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3494aa8e712cSStephen Smalley 				     GFP_KERNEL);
34951da177e4SLinus Torvalds 	if (rc)
34961da177e4SLinus Torvalds 		return rc;
34971da177e4SLinus Torvalds 
34989287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3499aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
35001da177e4SLinus Torvalds 	isec->sid = newsid;
35016f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
35029287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
35031da177e4SLinus Torvalds 	return 0;
35041da177e4SLinus Torvalds }
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
35071da177e4SLinus Torvalds {
35081da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
3509a9ffe682SAmir Goldstein 
3510a9ffe682SAmir Goldstein 	if (!selinux_initialized(&selinux_state))
3511a9ffe682SAmir Goldstein 		return 0;
3512a9ffe682SAmir Goldstein 
35131da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
35141da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
35151da177e4SLinus Torvalds 	return len;
35161da177e4SLinus Torvalds }
35171da177e4SLinus Torvalds 
3518d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3519713a04aeSAhmed S. Darwish {
3520e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3521713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3522713a04aeSAhmed S. Darwish }
3523713a04aeSAhmed S. Darwish 
352456909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
352556909eb3SVivek Goyal {
352656909eb3SVivek Goyal 	u32 sid;
352756909eb3SVivek Goyal 	struct task_security_struct *tsec;
352856909eb3SVivek Goyal 	struct cred *new_creds = *new;
352956909eb3SVivek Goyal 
353056909eb3SVivek Goyal 	if (new_creds == NULL) {
353156909eb3SVivek Goyal 		new_creds = prepare_creds();
353256909eb3SVivek Goyal 		if (!new_creds)
353356909eb3SVivek Goyal 			return -ENOMEM;
353456909eb3SVivek Goyal 	}
353556909eb3SVivek Goyal 
35360c6cfa62SCasey Schaufler 	tsec = selinux_cred(new_creds);
353756909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
353856909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
353956909eb3SVivek Goyal 	tsec->create_sid = sid;
354056909eb3SVivek Goyal 	*new = new_creds;
354156909eb3SVivek Goyal 	return 0;
354256909eb3SVivek Goyal }
354356909eb3SVivek Goyal 
354419472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
354519472b69SVivek Goyal {
354619472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
354719472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
354819472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
354919472b69SVivek Goyal 	 */
355019472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
355119472b69SVivek Goyal 		return 1; /* Discard */
355219472b69SVivek Goyal 	/*
355319472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
355419472b69SVivek Goyal 	 * by selinux.
355519472b69SVivek Goyal 	 */
355619472b69SVivek Goyal 	return -EOPNOTSUPP;
355719472b69SVivek Goyal }
355819472b69SVivek Goyal 
3559ec882da5SOndrej Mosnacek /* kernfs node operations */
3560ec882da5SOndrej Mosnacek 
3561c72c4cdeSYueHaibing static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3562ec882da5SOndrej Mosnacek 					struct kernfs_node *kn)
3563ec882da5SOndrej Mosnacek {
3564169ce0c0SStephen Smalley 	const struct task_security_struct *tsec = selinux_cred(current_cred());
3565ec882da5SOndrej Mosnacek 	u32 parent_sid, newsid, clen;
3566ec882da5SOndrej Mosnacek 	int rc;
3567ec882da5SOndrej Mosnacek 	char *context;
3568ec882da5SOndrej Mosnacek 
35691537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3570ec882da5SOndrej Mosnacek 	if (rc == -ENODATA)
3571ec882da5SOndrej Mosnacek 		return 0;
3572ec882da5SOndrej Mosnacek 	else if (rc < 0)
3573ec882da5SOndrej Mosnacek 		return rc;
3574ec882da5SOndrej Mosnacek 
3575ec882da5SOndrej Mosnacek 	clen = (u32)rc;
3576ec882da5SOndrej Mosnacek 	context = kmalloc(clen, GFP_KERNEL);
3577ec882da5SOndrej Mosnacek 	if (!context)
3578ec882da5SOndrej Mosnacek 		return -ENOMEM;
3579ec882da5SOndrej Mosnacek 
35801537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3581ec882da5SOndrej Mosnacek 	if (rc < 0) {
3582ec882da5SOndrej Mosnacek 		kfree(context);
3583ec882da5SOndrej Mosnacek 		return rc;
3584ec882da5SOndrej Mosnacek 	}
3585ec882da5SOndrej Mosnacek 
3586ec882da5SOndrej Mosnacek 	rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3587ec882da5SOndrej Mosnacek 				     GFP_KERNEL);
3588ec882da5SOndrej Mosnacek 	kfree(context);
3589ec882da5SOndrej Mosnacek 	if (rc)
3590ec882da5SOndrej Mosnacek 		return rc;
3591ec882da5SOndrej Mosnacek 
3592ec882da5SOndrej Mosnacek 	if (tsec->create_sid) {
3593ec882da5SOndrej Mosnacek 		newsid = tsec->create_sid;
3594ec882da5SOndrej Mosnacek 	} else {
3595ec882da5SOndrej Mosnacek 		u16 secclass = inode_mode_to_security_class(kn->mode);
3596ec882da5SOndrej Mosnacek 		struct qstr q;
3597ec882da5SOndrej Mosnacek 
3598ec882da5SOndrej Mosnacek 		q.name = kn->name;
3599ec882da5SOndrej Mosnacek 		q.hash_len = hashlen_string(kn_dir, kn->name);
3600ec882da5SOndrej Mosnacek 
3601ec882da5SOndrej Mosnacek 		rc = security_transition_sid(&selinux_state, tsec->sid,
3602ec882da5SOndrej Mosnacek 					     parent_sid, secclass, &q,
3603ec882da5SOndrej Mosnacek 					     &newsid);
3604ec882da5SOndrej Mosnacek 		if (rc)
3605ec882da5SOndrej Mosnacek 			return rc;
3606ec882da5SOndrej Mosnacek 	}
3607ec882da5SOndrej Mosnacek 
3608ec882da5SOndrej Mosnacek 	rc = security_sid_to_context_force(&selinux_state, newsid,
3609ec882da5SOndrej Mosnacek 					   &context, &clen);
3610ec882da5SOndrej Mosnacek 	if (rc)
3611ec882da5SOndrej Mosnacek 		return rc;
3612ec882da5SOndrej Mosnacek 
36131537ad15SOndrej Mosnacek 	rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3614ec882da5SOndrej Mosnacek 			      XATTR_CREATE);
3615ec882da5SOndrej Mosnacek 	kfree(context);
3616ec882da5SOndrej Mosnacek 	return rc;
3617ec882da5SOndrej Mosnacek }
3618ec882da5SOndrej Mosnacek 
3619ec882da5SOndrej Mosnacek 
36201da177e4SLinus Torvalds /* file security operations */
36211da177e4SLinus Torvalds 
3622788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
36231da177e4SLinus Torvalds {
362488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3625496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
36261da177e4SLinus Torvalds 
36271da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
36281da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
36291da177e4SLinus Torvalds 		mask |= MAY_APPEND;
36301da177e4SLinus Torvalds 
3631389fb800SPaul Moore 	return file_has_perm(cred, file,
36321da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
36331da177e4SLinus Torvalds }
36341da177e4SLinus Torvalds 
3635788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3636788e7dd4SYuichi Nakamura {
3637496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3638bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3639b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
364020dda18bSStephen Smalley 	u32 sid = current_sid();
364120dda18bSStephen Smalley 
3642389fb800SPaul Moore 	if (!mask)
3643788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3644788e7dd4SYuichi Nakamura 		return 0;
3645788e7dd4SYuichi Nakamura 
3646b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
364720dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
36486b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
364983d49856SEric Paris 		/* No change since file_open check. */
365020dda18bSStephen Smalley 		return 0;
365120dda18bSStephen Smalley 
3652788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3653788e7dd4SYuichi Nakamura }
3654788e7dd4SYuichi Nakamura 
36551da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
36561da177e4SLinus Torvalds {
3657cb89e246SPaul Moore 	struct file_security_struct *fsec = selinux_file(file);
3658cb89e246SPaul Moore 	u32 sid = current_sid();
3659cb89e246SPaul Moore 
3660cb89e246SPaul Moore 	fsec->sid = sid;
3661cb89e246SPaul Moore 	fsec->fown_sid = sid;
3662cb89e246SPaul Moore 
3663cb89e246SPaul Moore 	return 0;
36641da177e4SLinus Torvalds }
36651da177e4SLinus Torvalds 
3666fa1aa143SJeff Vander Stoep /*
3667fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3668fa1aa143SJeff Vander Stoep  * operation to an inode.
3669fa1aa143SJeff Vander Stoep  */
36701d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3671fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3672fa1aa143SJeff Vander Stoep {
3673fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3674bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3675fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
367620cdef8dSPaul Moore 	struct inode_security_struct *isec;
3677fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3678fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3679fa1aa143SJeff Vander Stoep 	int rc;
3680fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3681fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3682fa1aa143SJeff Vander Stoep 
3683fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3684fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3685fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3686fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3687fa1aa143SJeff Vander Stoep 
3688fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
36896b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36906b6bc620SStephen Smalley 				  ssid, fsec->sid,
3691fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3692fa1aa143SJeff Vander Stoep 				FD__USE,
3693fa1aa143SJeff Vander Stoep 				&ad);
3694fa1aa143SJeff Vander Stoep 		if (rc)
3695fa1aa143SJeff Vander Stoep 			goto out;
3696fa1aa143SJeff Vander Stoep 	}
3697fa1aa143SJeff Vander Stoep 
3698fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3699fa1aa143SJeff Vander Stoep 		return 0;
3700fa1aa143SJeff Vander Stoep 
370120cdef8dSPaul Moore 	isec = inode_security(inode);
37026b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
37036b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3704fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3705fa1aa143SJeff Vander Stoep out:
3706fa1aa143SJeff Vander Stoep 	return rc;
3707fa1aa143SJeff Vander Stoep }
3708fa1aa143SJeff Vander Stoep 
37091da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
37101da177e4SLinus Torvalds 			      unsigned long arg)
37111da177e4SLinus Torvalds {
371288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
37130b24dcb7SEric Paris 	int error = 0;
37141da177e4SLinus Torvalds 
37150b24dcb7SEric Paris 	switch (cmd) {
37160b24dcb7SEric Paris 	case FIONREAD:
37170b24dcb7SEric Paris 	case FIBMAP:
37180b24dcb7SEric Paris 	case FIGETBSZ:
37192f99c369SAl Viro 	case FS_IOC_GETFLAGS:
37202f99c369SAl Viro 	case FS_IOC_GETVERSION:
37210b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
37220b24dcb7SEric Paris 		break;
37231da177e4SLinus Torvalds 
37242f99c369SAl Viro 	case FS_IOC_SETFLAGS:
37252f99c369SAl Viro 	case FS_IOC_SETVERSION:
37260b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
37270b24dcb7SEric Paris 		break;
37280b24dcb7SEric Paris 
37290b24dcb7SEric Paris 	/* sys_ioctl() checks */
37300b24dcb7SEric Paris 	case FIONBIO:
37310b24dcb7SEric Paris 	case FIOASYNC:
37320b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
37330b24dcb7SEric Paris 		break;
37340b24dcb7SEric Paris 
37350b24dcb7SEric Paris 	case KDSKBENT:
37360b24dcb7SEric Paris 	case KDSKBSENT:
37376a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3738c1a85a00SMicah Morton 					    CAP_OPT_NONE, true);
37390b24dcb7SEric Paris 		break;
37400b24dcb7SEric Paris 
37410b24dcb7SEric Paris 	/* default case assumes that the command will go
37420b24dcb7SEric Paris 	 * to the file's ioctl() function.
37430b24dcb7SEric Paris 	 */
37440b24dcb7SEric Paris 	default:
3745fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
37460b24dcb7SEric Paris 	}
37470b24dcb7SEric Paris 	return error;
37481da177e4SLinus Torvalds }
37491da177e4SLinus Torvalds 
3750b78b7d59SStephen Smalley static int default_noexec __ro_after_init;
3751fcaaade1SStephen Smalley 
37521da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
37531da177e4SLinus Torvalds {
375488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3755be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3756d84f4f99SDavid Howells 	int rc = 0;
375788e67f3bSDavid Howells 
3758fcaaade1SStephen Smalley 	if (default_noexec &&
3759892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3760892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
37611da177e4SLinus Torvalds 		/*
37621da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
37631da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
37641da177e4SLinus Torvalds 		 * This has an additional check.
37651da177e4SLinus Torvalds 		 */
37666b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
37676b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3768be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
37691da177e4SLinus Torvalds 		if (rc)
3770d84f4f99SDavid Howells 			goto error;
37711da177e4SLinus Torvalds 	}
37721da177e4SLinus Torvalds 
37731da177e4SLinus Torvalds 	if (file) {
37741da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
37751da177e4SLinus Torvalds 		u32 av = FILE__READ;
37761da177e4SLinus Torvalds 
37771da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
37781da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
37791da177e4SLinus Torvalds 			av |= FILE__WRITE;
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
37821da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
37831da177e4SLinus Torvalds 
378488e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
37851da177e4SLinus Torvalds 	}
3786d84f4f99SDavid Howells 
3787d84f4f99SDavid Howells error:
3788d84f4f99SDavid Howells 	return rc;
37891da177e4SLinus Torvalds }
37901da177e4SLinus Torvalds 
3791e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
37921da177e4SLinus Torvalds {
3793b1d9e6b0SCasey Schaufler 	int rc = 0;
379498883bfdSPaul Moore 
379598883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
379698883bfdSPaul Moore 		u32 sid = current_sid();
37976b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
37986b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
379998883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
380098883bfdSPaul Moore 	}
380198883bfdSPaul Moore 
380298883bfdSPaul Moore 	return rc;
3803e5467859SAl Viro }
38041da177e4SLinus Torvalds 
3805e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3806e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3807e5467859SAl Viro {
38083ba4bf5fSStephen Smalley 	struct common_audit_data ad;
38093ba4bf5fSStephen Smalley 	int rc;
38103ba4bf5fSStephen Smalley 
38113ba4bf5fSStephen Smalley 	if (file) {
38123ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
38133ba4bf5fSStephen Smalley 		ad.u.file = file;
38143ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
38153ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
38163ba4bf5fSStephen Smalley 		if (rc)
38173ba4bf5fSStephen Smalley 			return rc;
38183ba4bf5fSStephen Smalley 	}
38193ba4bf5fSStephen Smalley 
38208861d0afSLakshmi Ramasubramanian 	if (checkreqprot_get(&selinux_state))
38211da177e4SLinus Torvalds 		prot = reqprot;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
38241da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
38251da177e4SLinus Torvalds }
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
38281da177e4SLinus Torvalds 				 unsigned long reqprot,
38291da177e4SLinus Torvalds 				 unsigned long prot)
38301da177e4SLinus Torvalds {
383188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3832be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
38331da177e4SLinus Torvalds 
38348861d0afSLakshmi Ramasubramanian 	if (checkreqprot_get(&selinux_state))
38351da177e4SLinus Torvalds 		prot = reqprot;
38361da177e4SLinus Torvalds 
3837fcaaade1SStephen Smalley 	if (default_noexec &&
3838fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3839d541bbeeSJames Morris 		int rc = 0;
3840db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3841db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
38426b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
38436b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3844be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3845db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3846c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3847c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3848d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
38496b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
38506b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3851be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3852db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3853db4c9641SStephen Smalley 			/*
3854db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3855db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3856db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3857db4c9641SStephen Smalley 			 * modified content.  This typically should only
3858db4c9641SStephen Smalley 			 * occur for text relocations.
3859db4c9641SStephen Smalley 			 */
3860d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3861db4c9641SStephen Smalley 		}
38626b992197SLorenzo Hernandez García-Hierro 		if (rc)
38636b992197SLorenzo Hernandez García-Hierro 			return rc;
38646b992197SLorenzo Hernandez García-Hierro 	}
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
38671da177e4SLinus Torvalds }
38681da177e4SLinus Torvalds 
38691da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
38701da177e4SLinus Torvalds {
387188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
387288e67f3bSDavid Howells 
387388e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
38741da177e4SLinus Torvalds }
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
38771da177e4SLinus Torvalds 			      unsigned long arg)
38781da177e4SLinus Torvalds {
387988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
38801da177e4SLinus Torvalds 	int err = 0;
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	switch (cmd) {
38831da177e4SLinus Torvalds 	case F_SETFL:
38841da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
388588e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
38861da177e4SLinus Torvalds 			break;
38871da177e4SLinus Torvalds 		}
3888df561f66SGustavo A. R. Silva 		fallthrough;
38891da177e4SLinus Torvalds 	case F_SETOWN:
38901da177e4SLinus Torvalds 	case F_SETSIG:
38911da177e4SLinus Torvalds 	case F_GETFL:
38921da177e4SLinus Torvalds 	case F_GETOWN:
38931da177e4SLinus Torvalds 	case F_GETSIG:
38941d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
38951da177e4SLinus Torvalds 		/* Just check FD__USE permission */
389688e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
38971da177e4SLinus Torvalds 		break;
38981da177e4SLinus Torvalds 	case F_GETLK:
38991da177e4SLinus Torvalds 	case F_SETLK:
39001da177e4SLinus Torvalds 	case F_SETLKW:
39010d3f7a2dSJeff Layton 	case F_OFD_GETLK:
39020d3f7a2dSJeff Layton 	case F_OFD_SETLK:
39030d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
39041da177e4SLinus Torvalds #if BITS_PER_LONG == 32
39051da177e4SLinus Torvalds 	case F_GETLK64:
39061da177e4SLinus Torvalds 	case F_SETLK64:
39071da177e4SLinus Torvalds 	case F_SETLKW64:
39081da177e4SLinus Torvalds #endif
390988e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
39101da177e4SLinus Torvalds 		break;
39111da177e4SLinus Torvalds 	}
39121da177e4SLinus Torvalds 
39131da177e4SLinus Torvalds 	return err;
39141da177e4SLinus Torvalds }
39151da177e4SLinus Torvalds 
3916e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
39171da177e4SLinus Torvalds {
39181da177e4SLinus Torvalds 	struct file_security_struct *fsec;
39191da177e4SLinus Torvalds 
3920bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
3921275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
39221da177e4SLinus Torvalds }
39231da177e4SLinus Torvalds 
39241da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
39251da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
39261da177e4SLinus Torvalds {
39271da177e4SLinus Torvalds 	struct file *file;
3928eb1231f7SPaul Moore 	u32 sid = task_sid_obj(tsk);
39291da177e4SLinus Torvalds 	u32 perm;
39301da177e4SLinus Torvalds 	struct file_security_struct *fsec;
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3933b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
39341da177e4SLinus Torvalds 
3935bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
39361da177e4SLinus Torvalds 
39371da177e4SLinus Torvalds 	if (!signum)
39381da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
39391da177e4SLinus Torvalds 	else
39401da177e4SLinus Torvalds 		perm = signal_to_av(signum);
39411da177e4SLinus Torvalds 
39426b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39436b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
39441da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
39451da177e4SLinus Torvalds }
39461da177e4SLinus Torvalds 
39471da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
39481da177e4SLinus Torvalds {
394988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
395088e67f3bSDavid Howells 
395188e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
39521da177e4SLinus Torvalds }
39531da177e4SLinus Torvalds 
395494817692SAl Viro static int selinux_file_open(struct file *file)
3955788e7dd4SYuichi Nakamura {
3956788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3957788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3958d84f4f99SDavid Howells 
3959bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
396083da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3961788e7dd4SYuichi Nakamura 	/*
3962788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3963788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3964788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3965788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3966788e7dd4SYuichi Nakamura 	 * struct as its SID.
3967788e7dd4SYuichi Nakamura 	 */
3968788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
39696b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3970788e7dd4SYuichi Nakamura 	/*
3971788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3972788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3973788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3974788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3975788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3976788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3977788e7dd4SYuichi Nakamura 	 */
397894817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3979788e7dd4SYuichi Nakamura }
3980788e7dd4SYuichi Nakamura 
39811da177e4SLinus Torvalds /* task security operations */
39821da177e4SLinus Torvalds 
3983a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3984a79be238STetsuo Handa 			      unsigned long clone_flags)
39851da177e4SLinus Torvalds {
3986be0554c9SStephen Smalley 	u32 sid = current_sid();
3987be0554c9SStephen Smalley 
39886b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39896b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
39901da177e4SLinus Torvalds }
39911da177e4SLinus Torvalds 
3992f1752eecSDavid Howells /*
3993d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3994d84f4f99SDavid Howells  */
3995d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3996d84f4f99SDavid Howells 				gfp_t gfp)
3997d84f4f99SDavid Howells {
3998bbd3662aSCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
3999bbd3662aSCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
4000d84f4f99SDavid Howells 
4001bbd3662aSCasey Schaufler 	*tsec = *old_tsec;
4002d84f4f99SDavid Howells 	return 0;
4003d84f4f99SDavid Howells }
4004d84f4f99SDavid Howells 
4005d84f4f99SDavid Howells /*
4006ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
4007ee18d64cSDavid Howells  */
4008ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
4009ee18d64cSDavid Howells {
40100c6cfa62SCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
40110c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
4012ee18d64cSDavid Howells 
4013ee18d64cSDavid Howells 	*tsec = *old_tsec;
4014ee18d64cSDavid Howells }
4015ee18d64cSDavid Howells 
40163ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
40173ec30113SMatthew Garrett {
40183ec30113SMatthew Garrett 	*secid = cred_sid(c);
40193ec30113SMatthew Garrett }
40203ec30113SMatthew Garrett 
4021ee18d64cSDavid Howells /*
40223a3b7ce9SDavid Howells  * set the security data for a kernel service
40233a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
40243a3b7ce9SDavid Howells  */
40253a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
40263a3b7ce9SDavid Howells {
40270c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
40283a3b7ce9SDavid Howells 	u32 sid = current_sid();
40293a3b7ce9SDavid Howells 	int ret;
40303a3b7ce9SDavid Howells 
40316b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
40326b6bc620SStephen Smalley 			   sid, secid,
40333a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
40343a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
40353a3b7ce9SDavid Howells 			   NULL);
40363a3b7ce9SDavid Howells 	if (ret == 0) {
40373a3b7ce9SDavid Howells 		tsec->sid = secid;
40383a3b7ce9SDavid Howells 		tsec->create_sid = 0;
40393a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
40403a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
40413a3b7ce9SDavid Howells 	}
40423a3b7ce9SDavid Howells 	return ret;
40433a3b7ce9SDavid Howells }
40443a3b7ce9SDavid Howells 
40453a3b7ce9SDavid Howells /*
40463a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
40473a3b7ce9SDavid Howells  * objective context of the specified inode
40483a3b7ce9SDavid Howells  */
40493a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
40503a3b7ce9SDavid Howells {
405183da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
40520c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
40533a3b7ce9SDavid Howells 	u32 sid = current_sid();
40543a3b7ce9SDavid Howells 	int ret;
40553a3b7ce9SDavid Howells 
40566b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
40576b6bc620SStephen Smalley 			   sid, isec->sid,
40583a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
40593a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
40603a3b7ce9SDavid Howells 			   NULL);
40613a3b7ce9SDavid Howells 
40623a3b7ce9SDavid Howells 	if (ret == 0)
40633a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
4064ef57471aSDavid Howells 	return ret;
40653a3b7ce9SDavid Howells }
40663a3b7ce9SDavid Howells 
4067dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
406825354c4fSEric Paris {
4069dd8dbf2eSEric Paris 	struct common_audit_data ad;
4070dd8dbf2eSEric Paris 
407150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
4072dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
4073dd8dbf2eSEric Paris 
40746b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40756b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4076dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
407725354c4fSEric Paris }
407825354c4fSEric Paris 
407961d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
408061d612eaSJeff Vander Stoep {
408161d612eaSJeff Vander Stoep 	struct common_audit_data ad;
408261d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
408361d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
408461d612eaSJeff Vander Stoep 	u32 sid = current_sid();
408561d612eaSJeff Vander Stoep 	int rc;
408661d612eaSJeff Vander Stoep 
408761d612eaSJeff Vander Stoep 	/* init_module */
408861d612eaSJeff Vander Stoep 	if (file == NULL)
40896b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
40906b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
409161d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
409261d612eaSJeff Vander Stoep 
409361d612eaSJeff Vander Stoep 	/* finit_module */
409420cdef8dSPaul Moore 
409543af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
409643af5de7SVivek Goyal 	ad.u.file = file;
409761d612eaSJeff Vander Stoep 
4098bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
409961d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
41006b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
41016b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
410261d612eaSJeff Vander Stoep 		if (rc)
410361d612eaSJeff Vander Stoep 			return rc;
410461d612eaSJeff Vander Stoep 	}
410561d612eaSJeff Vander Stoep 
410620cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
41076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41086b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
410961d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
411061d612eaSJeff Vander Stoep }
411161d612eaSJeff Vander Stoep 
411261d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
41132039bda1SKees Cook 				    enum kernel_read_file_id id,
41142039bda1SKees Cook 				    bool contents)
411561d612eaSJeff Vander Stoep {
411661d612eaSJeff Vander Stoep 	int rc = 0;
411761d612eaSJeff Vander Stoep 
411861d612eaSJeff Vander Stoep 	switch (id) {
411961d612eaSJeff Vander Stoep 	case READING_MODULE:
41202039bda1SKees Cook 		rc = selinux_kernel_module_from_file(contents ? file : NULL);
412161d612eaSJeff Vander Stoep 		break;
412261d612eaSJeff Vander Stoep 	default:
412361d612eaSJeff Vander Stoep 		break;
412461d612eaSJeff Vander Stoep 	}
412561d612eaSJeff Vander Stoep 
412661d612eaSJeff Vander Stoep 	return rc;
412761d612eaSJeff Vander Stoep }
412861d612eaSJeff Vander Stoep 
4129b64fcae7SKees Cook static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
4130c77b8cdfSMimi Zohar {
4131c77b8cdfSMimi Zohar 	int rc = 0;
4132c77b8cdfSMimi Zohar 
4133c77b8cdfSMimi Zohar 	switch (id) {
4134c77b8cdfSMimi Zohar 	case LOADING_MODULE:
4135c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
4136b2d99bcbSGustavo A. R. Silva 		break;
4137c77b8cdfSMimi Zohar 	default:
4138c77b8cdfSMimi Zohar 		break;
4139c77b8cdfSMimi Zohar 	}
4140c77b8cdfSMimi Zohar 
4141c77b8cdfSMimi Zohar 	return rc;
4142c77b8cdfSMimi Zohar }
4143c77b8cdfSMimi Zohar 
41441da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
41451da177e4SLinus Torvalds {
41466b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4147eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4148be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
41491da177e4SLinus Torvalds }
41501da177e4SLinus Torvalds 
41511da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
41521da177e4SLinus Torvalds {
41536b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4154eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4155be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
41561da177e4SLinus Torvalds }
41571da177e4SLinus Torvalds 
41581da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
41591da177e4SLinus Torvalds {
41606b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4161eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4162be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
41631da177e4SLinus Torvalds }
41641da177e4SLinus Torvalds 
41656326948fSPaul Moore static void selinux_current_getsecid_subj(u32 *secid)
4166f9008e4cSDavid Quigley {
41676326948fSPaul Moore 	*secid = current_sid();
4168eb1231f7SPaul Moore }
4169eb1231f7SPaul Moore 
4170eb1231f7SPaul Moore static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
4171eb1231f7SPaul Moore {
4172eb1231f7SPaul Moore 	*secid = task_sid_obj(p);
4173f9008e4cSDavid Quigley }
4174f9008e4cSDavid Quigley 
41751da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
41761da177e4SLinus Torvalds {
41776b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4178eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4179be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41801da177e4SLinus Torvalds }
41811da177e4SLinus Torvalds 
418203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
418303e68060SJames Morris {
41846b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4185eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4186be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
418703e68060SJames Morris }
418803e68060SJames Morris 
4189a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
4190a1836a42SDavid Quigley {
41916b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4192eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4193be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
4194a1836a42SDavid Quigley }
4195a1836a42SDavid Quigley 
41964298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4197791ec491SStephen Smalley 				unsigned int flags)
4198791ec491SStephen Smalley {
4199791ec491SStephen Smalley 	u32 av = 0;
4200791ec491SStephen Smalley 
420184e6885eSStephen Smalley 	if (!flags)
420284e6885eSStephen Smalley 		return 0;
4203791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
4204791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
4205791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
4206791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
42076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42086b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
4209791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
4210791ec491SStephen Smalley }
4211791ec491SStephen Smalley 
42128fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
42138fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
42141da177e4SLinus Torvalds {
42158fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
42161da177e4SLinus Torvalds 
42171da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
42181da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
42191da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
4220d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
42211da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
42226b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
4223eb1231f7SPaul Moore 				    current_sid(), task_sid_obj(p),
4224be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
42251da177e4SLinus Torvalds 
42261da177e4SLinus Torvalds 	return 0;
42271da177e4SLinus Torvalds }
42281da177e4SLinus Torvalds 
4229b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
42301da177e4SLinus Torvalds {
42316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4232eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4233be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
42341da177e4SLinus Torvalds }
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
42371da177e4SLinus Torvalds {
42386b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4239eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4240be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
42411da177e4SLinus Torvalds }
42421da177e4SLinus Torvalds 
424335601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
424435601547SDavid Quigley {
42456b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4246eb1231f7SPaul Moore 			    current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4247be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
424835601547SDavid Quigley }
424935601547SDavid Quigley 
4250ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
42516b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
42521da177e4SLinus Torvalds {
42536b4f3d01SStephen Smalley 	u32 secid;
42541da177e4SLinus Torvalds 	u32 perm;
42551da177e4SLinus Torvalds 
42561da177e4SLinus Torvalds 	if (!sig)
42571da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
42581da177e4SLinus Torvalds 	else
42591da177e4SLinus Torvalds 		perm = signal_to_av(sig);
42606b4f3d01SStephen Smalley 	if (!cred)
4261be0554c9SStephen Smalley 		secid = current_sid();
42626b4f3d01SStephen Smalley 	else
42636b4f3d01SStephen Smalley 		secid = cred_sid(cred);
42646b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
4265eb1231f7SPaul Moore 			    secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
42661da177e4SLinus Torvalds }
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
42691da177e4SLinus Torvalds 				  struct inode *inode)
42701da177e4SLinus Torvalds {
427180788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
4272eb1231f7SPaul Moore 	u32 sid = task_sid_obj(p);
42731da177e4SLinus Torvalds 
42749287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4275db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4276275bb41eSDavid Howells 	isec->sid = sid;
42776f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
42789287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
42791da177e4SLinus Torvalds }
42801da177e4SLinus Torvalds 
42811da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
428267f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
42832bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
42841da177e4SLinus Torvalds {
42851da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
42861da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
42871da177e4SLinus Torvalds 
4288bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
42891da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
42901da177e4SLinus Torvalds 	if (ih == NULL)
42911da177e4SLinus Torvalds 		goto out;
42921da177e4SLinus Torvalds 
42931da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
42941da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
42951da177e4SLinus Torvalds 		goto out;
42961da177e4SLinus Torvalds 
429748c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
429848c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
42991da177e4SLinus Torvalds 	ret = 0;
43001da177e4SLinus Torvalds 
430167f83cbfSVenkat Yekkirala 	if (proto)
430267f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
430367f83cbfSVenkat Yekkirala 
43041da177e4SLinus Torvalds 	switch (ih->protocol) {
43051da177e4SLinus Torvalds 	case IPPROTO_TCP: {
43061da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
43071da177e4SLinus Torvalds 
43081da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
43091da177e4SLinus Torvalds 			break;
43101da177e4SLinus Torvalds 
43111da177e4SLinus Torvalds 		offset += ihlen;
43121da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
43131da177e4SLinus Torvalds 		if (th == NULL)
43141da177e4SLinus Torvalds 			break;
43151da177e4SLinus Torvalds 
431648c62af6SEric Paris 		ad->u.net->sport = th->source;
431748c62af6SEric Paris 		ad->u.net->dport = th->dest;
43181da177e4SLinus Torvalds 		break;
43191da177e4SLinus Torvalds 	}
43201da177e4SLinus Torvalds 
43211da177e4SLinus Torvalds 	case IPPROTO_UDP: {
43221da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
43231da177e4SLinus Torvalds 
43241da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
43251da177e4SLinus Torvalds 			break;
43261da177e4SLinus Torvalds 
43271da177e4SLinus Torvalds 		offset += ihlen;
43281da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
43291da177e4SLinus Torvalds 		if (uh == NULL)
43301da177e4SLinus Torvalds 			break;
43311da177e4SLinus Torvalds 
433248c62af6SEric Paris 		ad->u.net->sport = uh->source;
433348c62af6SEric Paris 		ad->u.net->dport = uh->dest;
43341da177e4SLinus Torvalds 		break;
43351da177e4SLinus Torvalds 	}
43361da177e4SLinus Torvalds 
43372ee92d46SJames Morris 	case IPPROTO_DCCP: {
43382ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
43392ee92d46SJames Morris 
43402ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
43412ee92d46SJames Morris 			break;
43422ee92d46SJames Morris 
43432ee92d46SJames Morris 		offset += ihlen;
43442ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
43452ee92d46SJames Morris 		if (dh == NULL)
43462ee92d46SJames Morris 			break;
43472ee92d46SJames Morris 
434848c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
434948c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
43502ee92d46SJames Morris 		break;
43512ee92d46SJames Morris 	}
43522ee92d46SJames Morris 
4353d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4354d452930fSRichard Haines 	case IPPROTO_SCTP: {
4355d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4356d452930fSRichard Haines 
4357d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4358d452930fSRichard Haines 			break;
4359d452930fSRichard Haines 
4360d452930fSRichard Haines 		offset += ihlen;
4361d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4362d452930fSRichard Haines 		if (sh == NULL)
4363d452930fSRichard Haines 			break;
4364d452930fSRichard Haines 
4365d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4366d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4367d452930fSRichard Haines 		break;
4368d452930fSRichard Haines 	}
4369d452930fSRichard Haines #endif
43701da177e4SLinus Torvalds 	default:
43711da177e4SLinus Torvalds 		break;
43721da177e4SLinus Torvalds 	}
43731da177e4SLinus Torvalds out:
43741da177e4SLinus Torvalds 	return ret;
43751da177e4SLinus Torvalds }
43761da177e4SLinus Torvalds 
43771a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
43781da177e4SLinus Torvalds 
43791da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
438067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
43812bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
43821da177e4SLinus Torvalds {
43831da177e4SLinus Torvalds 	u8 nexthdr;
43841da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
43851da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
438675f2811cSJesse Gross 	__be16 frag_off;
43871da177e4SLinus Torvalds 
4388bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
43891da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
43901da177e4SLinus Torvalds 	if (ip6 == NULL)
43911da177e4SLinus Torvalds 		goto out;
43921da177e4SLinus Torvalds 
439348c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
439448c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
43951da177e4SLinus Torvalds 	ret = 0;
43961da177e4SLinus Torvalds 
43971da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
43981da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
439975f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
44001da177e4SLinus Torvalds 	if (offset < 0)
44011da177e4SLinus Torvalds 		goto out;
44021da177e4SLinus Torvalds 
440367f83cbfSVenkat Yekkirala 	if (proto)
440467f83cbfSVenkat Yekkirala 		*proto = nexthdr;
440567f83cbfSVenkat Yekkirala 
44061da177e4SLinus Torvalds 	switch (nexthdr) {
44071da177e4SLinus Torvalds 	case IPPROTO_TCP: {
44081da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
44091da177e4SLinus Torvalds 
44101da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
44111da177e4SLinus Torvalds 		if (th == NULL)
44121da177e4SLinus Torvalds 			break;
44131da177e4SLinus Torvalds 
441448c62af6SEric Paris 		ad->u.net->sport = th->source;
441548c62af6SEric Paris 		ad->u.net->dport = th->dest;
44161da177e4SLinus Torvalds 		break;
44171da177e4SLinus Torvalds 	}
44181da177e4SLinus Torvalds 
44191da177e4SLinus Torvalds 	case IPPROTO_UDP: {
44201da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
44211da177e4SLinus Torvalds 
44221da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
44231da177e4SLinus Torvalds 		if (uh == NULL)
44241da177e4SLinus Torvalds 			break;
44251da177e4SLinus Torvalds 
442648c62af6SEric Paris 		ad->u.net->sport = uh->source;
442748c62af6SEric Paris 		ad->u.net->dport = uh->dest;
44281da177e4SLinus Torvalds 		break;
44291da177e4SLinus Torvalds 	}
44301da177e4SLinus Torvalds 
44312ee92d46SJames Morris 	case IPPROTO_DCCP: {
44322ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
44332ee92d46SJames Morris 
44342ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
44352ee92d46SJames Morris 		if (dh == NULL)
44362ee92d46SJames Morris 			break;
44372ee92d46SJames Morris 
443848c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
443948c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
44402ee92d46SJames Morris 		break;
44412ee92d46SJames Morris 	}
44422ee92d46SJames Morris 
4443d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4444d452930fSRichard Haines 	case IPPROTO_SCTP: {
4445d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4446d452930fSRichard Haines 
4447d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4448d452930fSRichard Haines 		if (sh == NULL)
4449d452930fSRichard Haines 			break;
4450d452930fSRichard Haines 
4451d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4452d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4453d452930fSRichard Haines 		break;
4454d452930fSRichard Haines 	}
4455d452930fSRichard Haines #endif
44561da177e4SLinus Torvalds 	/* includes fragments */
44571da177e4SLinus Torvalds 	default:
44581da177e4SLinus Torvalds 		break;
44591da177e4SLinus Torvalds 	}
44601da177e4SLinus Torvalds out:
44611da177e4SLinus Torvalds 	return ret;
44621da177e4SLinus Torvalds }
44631da177e4SLinus Torvalds 
44641da177e4SLinus Torvalds #endif /* IPV6 */
44651da177e4SLinus Torvalds 
44662bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4467cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
44681da177e4SLinus Torvalds {
4469cf9481e2SDavid Howells 	char *addrp;
4470cf9481e2SDavid Howells 	int ret;
44711da177e4SLinus Torvalds 
447248c62af6SEric Paris 	switch (ad->u.net->family) {
44731da177e4SLinus Torvalds 	case PF_INET:
447467f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4475cf9481e2SDavid Howells 		if (ret)
4476cf9481e2SDavid Howells 			goto parse_error;
447748c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
447848c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4479cf9481e2SDavid Howells 		goto okay;
44801da177e4SLinus Torvalds 
44811a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
44821da177e4SLinus Torvalds 	case PF_INET6:
448367f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4484cf9481e2SDavid Howells 		if (ret)
4485cf9481e2SDavid Howells 			goto parse_error;
448648c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
448748c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4488cf9481e2SDavid Howells 		goto okay;
44891da177e4SLinus Torvalds #endif	/* IPV6 */
44901da177e4SLinus Torvalds 	default:
4491cf9481e2SDavid Howells 		addrp = NULL;
4492cf9481e2SDavid Howells 		goto okay;
44931da177e4SLinus Torvalds 	}
44941da177e4SLinus Torvalds 
4495cf9481e2SDavid Howells parse_error:
4496c103a91eSpeter enderborg 	pr_warn(
449771f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
449871f1cb05SPaul Moore 	       " unable to parse packet\n");
44991da177e4SLinus Torvalds 	return ret;
4500cf9481e2SDavid Howells 
4501cf9481e2SDavid Howells okay:
4502cf9481e2SDavid Howells 	if (_addrp)
4503cf9481e2SDavid Howells 		*_addrp = addrp;
4504cf9481e2SDavid Howells 	return 0;
45051da177e4SLinus Torvalds }
45061da177e4SLinus Torvalds 
45074f6a993fSPaul Moore /**
4508220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
45094f6a993fSPaul Moore  * @skb: the packet
451075e22910SPaul Moore  * @family: protocol family
4511220deb96SPaul Moore  * @sid: the packet's peer label SID
45124f6a993fSPaul Moore  *
45134f6a993fSPaul Moore  * Description:
4514220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4515220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4516220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4517220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4518220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4519220deb96SPaul Moore  * peer labels.
45204f6a993fSPaul Moore  *
45214f6a993fSPaul Moore  */
4522220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
45234f6a993fSPaul Moore {
452471f1cb05SPaul Moore 	int err;
45254f6a993fSPaul Moore 	u32 xfrm_sid;
45264f6a993fSPaul Moore 	u32 nlbl_sid;
4527220deb96SPaul Moore 	u32 nlbl_type;
45284f6a993fSPaul Moore 
4529817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4530bed4d7efSPaul Moore 	if (unlikely(err))
4531bed4d7efSPaul Moore 		return -EACCES;
4532bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4533bed4d7efSPaul Moore 	if (unlikely(err))
4534bed4d7efSPaul Moore 		return -EACCES;
4535220deb96SPaul Moore 
4536aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4537aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
453871f1cb05SPaul Moore 	if (unlikely(err)) {
4539c103a91eSpeter enderborg 		pr_warn(
454071f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
454171f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4542220deb96SPaul Moore 		return -EACCES;
454371f1cb05SPaul Moore 	}
4544220deb96SPaul Moore 
4545220deb96SPaul Moore 	return 0;
45464f6a993fSPaul Moore }
45474f6a993fSPaul Moore 
4548446b8024SPaul Moore /**
4549446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4550446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4551446b8024SPaul Moore  * @skb_sid: the packet's SID
4552446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4553446b8024SPaul Moore  *
4554446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4555446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4556c76a2f9eSRandy Dunlap  * @conn_sid.  If @skb_sid is not valid then @conn_sid is simply a copy
4557446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4558446b8024SPaul Moore  *
4559446b8024SPaul Moore  */
4560446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4561446b8024SPaul Moore {
4562446b8024SPaul Moore 	int err = 0;
4563446b8024SPaul Moore 
4564446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4565aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4566aa8e712cSStephen Smalley 					    conn_sid);
4567446b8024SPaul Moore 	else
4568446b8024SPaul Moore 		*conn_sid = sk_sid;
4569446b8024SPaul Moore 
4570446b8024SPaul Moore 	return err;
4571446b8024SPaul Moore }
4572446b8024SPaul Moore 
45731da177e4SLinus Torvalds /* socket security operations */
4574d4f2d978SPaul Moore 
45752ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
45762ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4577d4f2d978SPaul Moore {
45782ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
45792ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
45802ad18bdfSHarry Ciao 		return 0;
45812ad18bdfSHarry Ciao 	}
45822ad18bdfSHarry Ciao 
4583aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4584aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4585d4f2d978SPaul Moore }
4586d4f2d978SPaul Moore 
4587be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
45881da177e4SLinus Torvalds {
4589253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45902bf49690SThomas Liu 	struct common_audit_data ad;
459148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
45921da177e4SLinus Torvalds 
4593253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4594253bfae6SPaul Moore 		return 0;
45951da177e4SLinus Torvalds 
459650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
459748c62af6SEric Paris 	ad.u.net = &net;
459848c62af6SEric Paris 	ad.u.net->sk = sk;
45991da177e4SLinus Torvalds 
46006b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
46016b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4602be0554c9SStephen Smalley 			    &ad);
46031da177e4SLinus Torvalds }
46041da177e4SLinus Torvalds 
46051da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
46061da177e4SLinus Torvalds 				 int protocol, int kern)
46071da177e4SLinus Torvalds {
46080c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
4609d4f2d978SPaul Moore 	u32 newsid;
4610275bb41eSDavid Howells 	u16 secclass;
46112ad18bdfSHarry Ciao 	int rc;
46121da177e4SLinus Torvalds 
46131da177e4SLinus Torvalds 	if (kern)
4614d4f2d978SPaul Moore 		return 0;
46151da177e4SLinus Torvalds 
4616275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
46172ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
46182ad18bdfSHarry Ciao 	if (rc)
46192ad18bdfSHarry Ciao 		return rc;
46202ad18bdfSHarry Ciao 
46216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
46226b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
46231da177e4SLinus Torvalds }
46241da177e4SLinus Torvalds 
46257420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
46261da177e4SLinus Torvalds 				      int type, int protocol, int kern)
46271da177e4SLinus Torvalds {
46280c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
46295d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4630892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
46319287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
46329287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4633275bb41eSDavid Howells 	int err = 0;
4634275bb41eSDavid Howells 
46359287aed2SAndreas Gruenbacher 	if (!kern) {
46369287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
46372ad18bdfSHarry Ciao 		if (err)
46382ad18bdfSHarry Ciao 			return err;
46392ad18bdfSHarry Ciao 	}
4640275bb41eSDavid Howells 
46419287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
46429287aed2SAndreas Gruenbacher 	isec->sid = sid;
46436f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
46441da177e4SLinus Torvalds 
4645892c141eSVenkat Yekkirala 	if (sock->sk) {
4646892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
46479287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
46489287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4649d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4650d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4651d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4652d452930fSRichard Haines 
4653389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4654892c141eSVenkat Yekkirala 	}
4655892c141eSVenkat Yekkirala 
46567420ed23SVenkat Yekkirala 	return err;
46571da177e4SLinus Torvalds }
46581da177e4SLinus Torvalds 
46590b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
46600b811db2SDavid Herrmann 				     struct socket *sockb)
46610b811db2SDavid Herrmann {
46620b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
46630b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
46640b811db2SDavid Herrmann 
46650b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
46660b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
46670b811db2SDavid Herrmann 
46680b811db2SDavid Herrmann 	return 0;
46690b811db2SDavid Herrmann }
46700b811db2SDavid Herrmann 
46711da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
46721da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
46731da177e4SLinus Torvalds    permission check between the socket and the port number. */
46741da177e4SLinus Torvalds 
46751da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
46761da177e4SLinus Torvalds {
4677253bfae6SPaul Moore 	struct sock *sk = sock->sk;
46780f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
46791da177e4SLinus Torvalds 	u16 family;
46801da177e4SLinus Torvalds 	int err;
46811da177e4SLinus Torvalds 
4682be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
46831da177e4SLinus Torvalds 	if (err)
46841da177e4SLinus Torvalds 		goto out;
46851da177e4SLinus Torvalds 
4686d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4687253bfae6SPaul Moore 	family = sk->sk_family;
46881da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
46891da177e4SLinus Torvalds 		char *addrp;
46902bf49690SThomas Liu 		struct common_audit_data ad;
469148c62af6SEric Paris 		struct lsm_network_audit net = {0,};
46921da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
46931da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
4694c750e692STetsuo Handa 		u16 family_sa;
46951da177e4SLinus Torvalds 		unsigned short snum;
4696e399f982SJames Morris 		u32 sid, node_perm;
46971da177e4SLinus Torvalds 
4698d452930fSRichard Haines 		/*
4699d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4700d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4701d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4702d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4703d452930fSRichard Haines 		 */
4704c750e692STetsuo Handa 		if (addrlen < offsetofend(struct sockaddr, sa_family))
4705c750e692STetsuo Handa 			return -EINVAL;
4706c750e692STetsuo Handa 		family_sa = address->sa_family;
47070f8db8ccSAlexey Kodanev 		switch (family_sa) {
47080f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
470968741a8aSRichard Haines 		case AF_INET:
471068741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
471168741a8aSRichard Haines 				return -EINVAL;
47121da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
47130f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
47140f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
47150f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
47160f8db8ccSAlexey Kodanev 				 */
47170f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
47180f8db8ccSAlexey Kodanev 					goto err_af;
47190f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
47200f8db8ccSAlexey Kodanev 			}
47211da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
47221da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
472368741a8aSRichard Haines 			break;
472468741a8aSRichard Haines 		case AF_INET6:
472568741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
472668741a8aSRichard Haines 				return -EINVAL;
47271da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
47281da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
47291da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
473068741a8aSRichard Haines 			break;
473168741a8aSRichard Haines 		default:
47320f8db8ccSAlexey Kodanev 			goto err_af;
47331da177e4SLinus Torvalds 		}
47341da177e4SLinus Torvalds 
473588b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
473688b7d370SAlexey Kodanev 		ad.u.net = &net;
473788b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
473888b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
473988b7d370SAlexey Kodanev 
4740227b60f5SStephen Hemminger 		if (snum) {
4741227b60f5SStephen Hemminger 			int low, high;
4742227b60f5SStephen Hemminger 
47430bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4744227b60f5SStephen Hemminger 
474582f31ebfSMaciej Żenczykowski 			if (inet_port_requires_bind_service(sock_net(sk), snum) ||
474682f31ebfSMaciej Żenczykowski 			    snum < low || snum > high) {
47473e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
47483e112172SPaul Moore 						      snum, &sid);
47491da177e4SLinus Torvalds 				if (err)
47501da177e4SLinus Torvalds 					goto out;
47516b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
47526b6bc620SStephen Smalley 						   sksec->sid, sid,
4753253bfae6SPaul Moore 						   sksec->sclass,
47541da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
47551da177e4SLinus Torvalds 				if (err)
47561da177e4SLinus Torvalds 					goto out;
47571da177e4SLinus Torvalds 			}
4758227b60f5SStephen Hemminger 		}
47591da177e4SLinus Torvalds 
4760253bfae6SPaul Moore 		switch (sksec->sclass) {
476113402580SJames Morris 		case SECCLASS_TCP_SOCKET:
47621da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
47631da177e4SLinus Torvalds 			break;
47641da177e4SLinus Torvalds 
476513402580SJames Morris 		case SECCLASS_UDP_SOCKET:
47661da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
47671da177e4SLinus Torvalds 			break;
47681da177e4SLinus Torvalds 
47692ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
47702ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
47712ee92d46SJames Morris 			break;
47722ee92d46SJames Morris 
4773d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4774d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4775d452930fSRichard Haines 			break;
4776d452930fSRichard Haines 
47771da177e4SLinus Torvalds 		default:
47781da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
47791da177e4SLinus Torvalds 			break;
47801da177e4SLinus Torvalds 		}
47811da177e4SLinus Torvalds 
478288b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
47831da177e4SLinus Torvalds 		if (err)
47841da177e4SLinus Torvalds 			goto out;
47851da177e4SLinus Torvalds 
47860f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
478748c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
47881da177e4SLinus Torvalds 		else
478948c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
47901da177e4SLinus Torvalds 
47916b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
47926b6bc620SStephen Smalley 				   sksec->sid, sid,
4793253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
47941da177e4SLinus Torvalds 		if (err)
47951da177e4SLinus Torvalds 			goto out;
47961da177e4SLinus Torvalds 	}
47971da177e4SLinus Torvalds out:
47981da177e4SLinus Torvalds 	return err;
47990f8db8ccSAlexey Kodanev err_af:
48000f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
48010f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
48020f8db8ccSAlexey Kodanev 		return -EINVAL;
48030f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
48041da177e4SLinus Torvalds }
48051da177e4SLinus Torvalds 
4806d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4807d61330c6SKees Cook  * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4808d452930fSRichard Haines  */
4809d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4810d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
48111da177e4SLinus Torvalds {
4812014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4813253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
48141da177e4SLinus Torvalds 	int err;
48151da177e4SLinus Torvalds 
4816be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
48171da177e4SLinus Torvalds 	if (err)
48181da177e4SLinus Torvalds 		return err;
481905174c95SPaolo Abeni 	if (addrlen < offsetofend(struct sockaddr, sa_family))
482005174c95SPaolo Abeni 		return -EINVAL;
482105174c95SPaolo Abeni 
482205174c95SPaolo Abeni 	/* connect(AF_UNSPEC) has special handling, as it is a documented
482305174c95SPaolo Abeni 	 * way to disconnect the socket
482405174c95SPaolo Abeni 	 */
482505174c95SPaolo Abeni 	if (address->sa_family == AF_UNSPEC)
482605174c95SPaolo Abeni 		return 0;
48271da177e4SLinus Torvalds 
48281da177e4SLinus Torvalds 	/*
4829d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4830d452930fSRichard Haines 	 * for the port.
48311da177e4SLinus Torvalds 	 */
4832253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4833d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4834d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
48352bf49690SThomas Liu 		struct common_audit_data ad;
483648c62af6SEric Paris 		struct lsm_network_audit net = {0,};
48371da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
48381da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
48391da177e4SLinus Torvalds 		unsigned short snum;
48402ee92d46SJames Morris 		u32 sid, perm;
48411da177e4SLinus Torvalds 
4842d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4843d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4844d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4845d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4846d452930fSRichard Haines 		 */
484768741a8aSRichard Haines 		switch (address->sa_family) {
484868741a8aSRichard Haines 		case AF_INET:
48491da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4850911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
48511da177e4SLinus Torvalds 				return -EINVAL;
48521da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
485368741a8aSRichard Haines 			break;
485468741a8aSRichard Haines 		case AF_INET6:
48551da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4856911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
48571da177e4SLinus Torvalds 				return -EINVAL;
48581da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
485968741a8aSRichard Haines 			break;
486068741a8aSRichard Haines 		default:
486168741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
486268741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
486368741a8aSRichard Haines 			 */
486468741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
486568741a8aSRichard Haines 				return -EINVAL;
486668741a8aSRichard Haines 			else
486768741a8aSRichard Haines 				return -EAFNOSUPPORT;
48681da177e4SLinus Torvalds 		}
48691da177e4SLinus Torvalds 
48703e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
48711da177e4SLinus Torvalds 		if (err)
4872d452930fSRichard Haines 			return err;
48731da177e4SLinus Torvalds 
4874d452930fSRichard Haines 		switch (sksec->sclass) {
4875d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4876d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4877d452930fSRichard Haines 			break;
4878d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4879d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4880d452930fSRichard Haines 			break;
4881d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4882d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4883d452930fSRichard Haines 			break;
4884d452930fSRichard Haines 		}
48852ee92d46SJames Morris 
488650c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
488748c62af6SEric Paris 		ad.u.net = &net;
488848c62af6SEric Paris 		ad.u.net->dport = htons(snum);
488988b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
48906b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48916b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
48921da177e4SLinus Torvalds 		if (err)
4893d452930fSRichard Haines 			return err;
48941da177e4SLinus Torvalds 	}
48951da177e4SLinus Torvalds 
4896d452930fSRichard Haines 	return 0;
4897d452930fSRichard Haines }
4898014ab19aSPaul Moore 
4899d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4900d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4901d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4902d452930fSRichard Haines {
4903d452930fSRichard Haines 	int err;
4904d452930fSRichard Haines 	struct sock *sk = sock->sk;
4905d452930fSRichard Haines 
4906d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4907d452930fSRichard Haines 	if (err)
49081da177e4SLinus Torvalds 		return err;
4909d452930fSRichard Haines 
4910d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
49111da177e4SLinus Torvalds }
49121da177e4SLinus Torvalds 
49131da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
49141da177e4SLinus Torvalds {
4915be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
49161da177e4SLinus Torvalds }
49171da177e4SLinus Torvalds 
49181da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
49191da177e4SLinus Torvalds {
49201da177e4SLinus Torvalds 	int err;
49211da177e4SLinus Torvalds 	struct inode_security_struct *isec;
49221da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
49239287aed2SAndreas Gruenbacher 	u16 sclass;
49249287aed2SAndreas Gruenbacher 	u32 sid;
49251da177e4SLinus Torvalds 
4926be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
49271da177e4SLinus Torvalds 	if (err)
49281da177e4SLinus Torvalds 		return err;
49291da177e4SLinus Torvalds 
49305d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
49319287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
49329287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
49339287aed2SAndreas Gruenbacher 	sid = isec->sid;
49349287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
49359287aed2SAndreas Gruenbacher 
49369287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
49379287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
49389287aed2SAndreas Gruenbacher 	newisec->sid = sid;
49396f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
49401da177e4SLinus Torvalds 
49411da177e4SLinus Torvalds 	return 0;
49421da177e4SLinus Torvalds }
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
49451da177e4SLinus Torvalds 				  int size)
49461da177e4SLinus Torvalds {
4947be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
49481da177e4SLinus Torvalds }
49491da177e4SLinus Torvalds 
49501da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
49511da177e4SLinus Torvalds 				  int size, int flags)
49521da177e4SLinus Torvalds {
4953be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
49541da177e4SLinus Torvalds }
49551da177e4SLinus Torvalds 
49561da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
49571da177e4SLinus Torvalds {
4958be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
49591da177e4SLinus Torvalds }
49601da177e4SLinus Torvalds 
49611da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
49621da177e4SLinus Torvalds {
4963be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
49641da177e4SLinus Torvalds }
49651da177e4SLinus Torvalds 
49661da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
49671da177e4SLinus Torvalds {
4968f8687afeSPaul Moore 	int err;
4969f8687afeSPaul Moore 
4970be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4971f8687afeSPaul Moore 	if (err)
4972f8687afeSPaul Moore 		return err;
4973f8687afeSPaul Moore 
4974f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
49751da177e4SLinus Torvalds }
49761da177e4SLinus Torvalds 
49771da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
49781da177e4SLinus Torvalds 				     int optname)
49791da177e4SLinus Torvalds {
4980be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
49811da177e4SLinus Torvalds }
49821da177e4SLinus Torvalds 
49831da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
49841da177e4SLinus Torvalds {
4985be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
49861da177e4SLinus Torvalds }
49871da177e4SLinus Torvalds 
49883610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
49893610cda5SDavid S. Miller 					      struct sock *other,
49901da177e4SLinus Torvalds 					      struct sock *newsk)
49911da177e4SLinus Torvalds {
49923610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
49933610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
49944d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
49952bf49690SThomas Liu 	struct common_audit_data ad;
499648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49971da177e4SLinus Torvalds 	int err;
49981da177e4SLinus Torvalds 
499950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
500048c62af6SEric Paris 	ad.u.net = &net;
500148c62af6SEric Paris 	ad.u.net->sk = other;
50021da177e4SLinus Torvalds 
50036b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
50046b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
50054d1e2451SPaul Moore 			   sksec_other->sclass,
50061da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
50071da177e4SLinus Torvalds 	if (err)
50081da177e4SLinus Torvalds 		return err;
50091da177e4SLinus Torvalds 
50101da177e4SLinus Torvalds 	/* server child socket */
50114d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
5012aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
5013aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
50144d1e2451SPaul Moore 	if (err)
50154237c75cSVenkat Yekkirala 		return err;
50164d1e2451SPaul Moore 
50174d1e2451SPaul Moore 	/* connecting socket */
50184d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
50194d1e2451SPaul Moore 
50204d1e2451SPaul Moore 	return 0;
50211da177e4SLinus Torvalds }
50221da177e4SLinus Torvalds 
50231da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
50241da177e4SLinus Torvalds 					struct socket *other)
50251da177e4SLinus Torvalds {
5026253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
5027253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
50282bf49690SThomas Liu 	struct common_audit_data ad;
502948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
50301da177e4SLinus Torvalds 
503150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
503248c62af6SEric Paris 	ad.u.net = &net;
503348c62af6SEric Paris 	ad.u.net->sk = other->sk;
50341da177e4SLinus Torvalds 
50356b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
50366b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
5037253bfae6SPaul Moore 			    &ad);
50381da177e4SLinus Torvalds }
50391da177e4SLinus Torvalds 
5040cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
5041cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
50422bf49690SThomas Liu 				    struct common_audit_data *ad)
5043effad8dfSPaul Moore {
5044effad8dfSPaul Moore 	int err;
5045effad8dfSPaul Moore 	u32 if_sid;
5046effad8dfSPaul Moore 	u32 node_sid;
5047effad8dfSPaul Moore 
5048cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
5049effad8dfSPaul Moore 	if (err)
5050effad8dfSPaul Moore 		return err;
50516b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
50526b6bc620SStephen Smalley 			   peer_sid, if_sid,
5053effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
5054effad8dfSPaul Moore 	if (err)
5055effad8dfSPaul Moore 		return err;
5056effad8dfSPaul Moore 
5057effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
5058effad8dfSPaul Moore 	if (err)
5059effad8dfSPaul Moore 		return err;
50606b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
50616b6bc620SStephen Smalley 			    peer_sid, node_sid,
5062effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
5063effad8dfSPaul Moore }
5064effad8dfSPaul Moore 
5065220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5066d8395c87SPaul Moore 				       u16 family)
5067220deb96SPaul Moore {
5068277d342fSPaul Moore 	int err = 0;
5069220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5070220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
50712bf49690SThomas Liu 	struct common_audit_data ad;
507248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5073d8395c87SPaul Moore 	char *addrp;
5074d8395c87SPaul Moore 
507550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
507648c62af6SEric Paris 	ad.u.net = &net;
507748c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
507848c62af6SEric Paris 	ad.u.net->family = family;
5079d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5080d8395c87SPaul Moore 	if (err)
5081d8395c87SPaul Moore 		return err;
5082220deb96SPaul Moore 
508358bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
50846b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50856b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5086d8395c87SPaul Moore 				   PACKET__RECV, &ad);
5087220deb96SPaul Moore 		if (err)
5088220deb96SPaul Moore 			return err;
508958bfbb51SPaul Moore 	}
5090220deb96SPaul Moore 
5091d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5092220deb96SPaul Moore 	if (err)
5093220deb96SPaul Moore 		return err;
5094d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5095220deb96SPaul Moore 
50964e5ab4cbSJames Morris 	return err;
50974e5ab4cbSJames Morris }
5098d28d1e08STrent Jaeger 
50994e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
51004e5ab4cbSJames Morris {
5101220deb96SPaul Moore 	int err;
51024237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
5103220deb96SPaul Moore 	u16 family = sk->sk_family;
5104220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
51052bf49690SThomas Liu 	struct common_audit_data ad;
510648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5107220deb96SPaul Moore 	char *addrp;
5108d8395c87SPaul Moore 	u8 secmark_active;
5109d8395c87SPaul Moore 	u8 peerlbl_active;
51104e5ab4cbSJames Morris 
51114e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
5112220deb96SPaul Moore 		return 0;
51134e5ab4cbSJames Morris 
51144e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
511587fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
51164e5ab4cbSJames Morris 		family = PF_INET;
51174e5ab4cbSJames Morris 
5118d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5119d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
5120d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5121d8395c87SPaul Moore 	 * as fast and as clean as possible. */
5122aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5123d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
5124d8395c87SPaul Moore 
5125d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
51262be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5127d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
5128d8395c87SPaul Moore 		return 0;
5129d8395c87SPaul Moore 
513050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
513148c62af6SEric Paris 	ad.u.net = &net;
513248c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
513348c62af6SEric Paris 	ad.u.net->family = family;
5134224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
51354e5ab4cbSJames Morris 	if (err)
5136220deb96SPaul Moore 		return err;
51374e5ab4cbSJames Morris 
5138d8395c87SPaul Moore 	if (peerlbl_active) {
5139d621d35eSPaul Moore 		u32 peer_sid;
5140220deb96SPaul Moore 
5141220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5142220deb96SPaul Moore 		if (err)
5143220deb96SPaul Moore 			return err;
5144cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5145cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5146dfaebe98SPaul Moore 		if (err) {
5147a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
5148effad8dfSPaul Moore 			return err;
5149dfaebe98SPaul Moore 		}
51506b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
51516b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
5152d621d35eSPaul Moore 				   PEER__RECV, &ad);
515346d01d63SChad Hanson 		if (err) {
5154a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
515546d01d63SChad Hanson 			return err;
515646d01d63SChad Hanson 		}
5157d621d35eSPaul Moore 	}
5158d621d35eSPaul Moore 
5159d8395c87SPaul Moore 	if (secmark_active) {
51606b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
51616b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5162effad8dfSPaul Moore 				   PACKET__RECV, &ad);
5163effad8dfSPaul Moore 		if (err)
5164effad8dfSPaul Moore 			return err;
5165effad8dfSPaul Moore 	}
5166effad8dfSPaul Moore 
5167d621d35eSPaul Moore 	return err;
51681da177e4SLinus Torvalds }
51691da177e4SLinus Torvalds 
51702c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
51711da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
51721da177e4SLinus Torvalds {
51731da177e4SLinus Torvalds 	int err = 0;
51741da177e4SLinus Torvalds 	char *scontext;
51751da177e4SLinus Torvalds 	u32 scontext_len;
5176253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
51773de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
51781da177e4SLinus Torvalds 
5179253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5180d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
5181d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
5182dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
5183253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
5184253bfae6SPaul Moore 		return -ENOPROTOOPT;
51851da177e4SLinus Torvalds 
5186aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5187aa8e712cSStephen Smalley 				      &scontext_len);
51881da177e4SLinus Torvalds 	if (err)
5189253bfae6SPaul Moore 		return err;
51901da177e4SLinus Torvalds 
51911da177e4SLinus Torvalds 	if (scontext_len > len) {
51921da177e4SLinus Torvalds 		err = -ERANGE;
51931da177e4SLinus Torvalds 		goto out_len;
51941da177e4SLinus Torvalds 	}
51951da177e4SLinus Torvalds 
51961da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
51971da177e4SLinus Torvalds 		err = -EFAULT;
51981da177e4SLinus Torvalds 
51991da177e4SLinus Torvalds out_len:
52001da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
52011da177e4SLinus Torvalds 		err = -EFAULT;
52021da177e4SLinus Torvalds 	kfree(scontext);
52031da177e4SLinus Torvalds 	return err;
52041da177e4SLinus Torvalds }
52051da177e4SLinus Torvalds 
5206dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
52072c7946a7SCatherine Zhang {
5208dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
520975e22910SPaul Moore 	u16 family;
5210899134f2SPaul Moore 	struct inode_security_struct *isec;
5211877ce7c1SCatherine Zhang 
5212aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
5213aa862900SPaul Moore 		family = PF_INET;
5214aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
5215aa862900SPaul Moore 		family = PF_INET6;
5216aa862900SPaul Moore 	else if (sock)
521775e22910SPaul Moore 		family = sock->sk->sk_family;
521875e22910SPaul Moore 	else
521975e22910SPaul Moore 		goto out;
522075e22910SPaul Moore 
5221899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
5222899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
5223899134f2SPaul Moore 		peer_secid = isec->sid;
5224899134f2SPaul Moore 	} else if (skb)
5225220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
52262c7946a7SCatherine Zhang 
522775e22910SPaul Moore out:
5228dc49c1f9SCatherine Zhang 	*secid = peer_secid;
522975e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
523075e22910SPaul Moore 		return -EINVAL;
523175e22910SPaul Moore 	return 0;
52322c7946a7SCatherine Zhang }
52332c7946a7SCatherine Zhang 
52347d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
52351da177e4SLinus Torvalds {
523684914b7eSPaul Moore 	struct sk_security_struct *sksec;
523784914b7eSPaul Moore 
523884914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
523984914b7eSPaul Moore 	if (!sksec)
524084914b7eSPaul Moore 		return -ENOMEM;
524184914b7eSPaul Moore 
524284914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
524384914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
52445dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
524584914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
524684914b7eSPaul Moore 	sk->sk_security = sksec;
524784914b7eSPaul Moore 
524884914b7eSPaul Moore 	return 0;
52491da177e4SLinus Torvalds }
52501da177e4SLinus Torvalds 
52511da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
52521da177e4SLinus Torvalds {
525384914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
525484914b7eSPaul Moore 
525584914b7eSPaul Moore 	sk->sk_security = NULL;
525684914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
525784914b7eSPaul Moore 	kfree(sksec);
52581da177e4SLinus Torvalds }
52591da177e4SLinus Torvalds 
5260892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5261892c141eSVenkat Yekkirala {
5262dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5263dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5264892c141eSVenkat Yekkirala 
5265dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5266dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5267dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
526899f59ed0SPaul Moore 
5269dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5270892c141eSVenkat Yekkirala }
5271892c141eSVenkat Yekkirala 
5272beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5273d28d1e08STrent Jaeger {
5274d28d1e08STrent Jaeger 	if (!sk)
5275beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5276892c141eSVenkat Yekkirala 	else {
5277892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5278d28d1e08STrent Jaeger 
5279beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5280892c141eSVenkat Yekkirala 	}
5281d28d1e08STrent Jaeger }
5282d28d1e08STrent Jaeger 
52839a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
52844237c75cSVenkat Yekkirala {
52855d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
52865d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
52874237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52884237c75cSVenkat Yekkirala 
52892873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
52902873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
52914237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5292220deb96SPaul Moore 	sksec->sclass = isec->sclass;
52934237c75cSVenkat Yekkirala }
52944237c75cSVenkat Yekkirala 
5295d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5296d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5297d452930fSRichard Haines  * already present).
5298d452930fSRichard Haines  */
5299c081d53fSXin Long static int selinux_sctp_assoc_request(struct sctp_association *asoc,
5300d452930fSRichard Haines 				      struct sk_buff *skb)
5301d452930fSRichard Haines {
5302c081d53fSXin Long 	struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5303d452930fSRichard Haines 	struct common_audit_data ad;
5304d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5305d452930fSRichard Haines 	u8 peerlbl_active;
5306d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5307d452930fSRichard Haines 	u32 conn_sid;
5308d452930fSRichard Haines 	int err = 0;
5309d452930fSRichard Haines 
5310aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5311d452930fSRichard Haines 		return 0;
5312d452930fSRichard Haines 
5313d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5314d452930fSRichard Haines 
5315d452930fSRichard Haines 	if (peerlbl_active) {
5316d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5317d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5318d452930fSRichard Haines 		 */
5319c081d53fSXin Long 		err = selinux_skb_peerlbl_sid(skb, asoc->base.sk->sk_family,
5320d452930fSRichard Haines 					      &peer_sid);
5321d452930fSRichard Haines 		if (err)
5322d452930fSRichard Haines 			return err;
5323d452930fSRichard Haines 
5324d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5325d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5326d452930fSRichard Haines 	}
5327d452930fSRichard Haines 
5328d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5329d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5330d452930fSRichard Haines 
5331d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5332d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5333d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5334d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5335d452930fSRichard Haines 		 */
5336d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5337d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5338d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5339d452930fSRichard Haines 		 * consistency among the peer SIDs.
5340d452930fSRichard Haines 		 */
5341d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5342d452930fSRichard Haines 		ad.u.net = &net;
5343c081d53fSXin Long 		ad.u.net->sk = asoc->base.sk;
53446b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
53456b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5346d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5347d452930fSRichard Haines 		if (err)
5348d452930fSRichard Haines 			return err;
5349d452930fSRichard Haines 	}
5350d452930fSRichard Haines 
5351d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5352c081d53fSXin Long 	 * the information in asoc. This will be used by SCTP TCP type
5353d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5354d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5355d452930fSRichard Haines 	 * plug this into the new socket.
5356d452930fSRichard Haines 	 */
5357d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5358d452930fSRichard Haines 	if (err)
5359d452930fSRichard Haines 		return err;
5360d452930fSRichard Haines 
5361c081d53fSXin Long 	asoc->secid = conn_sid;
5362c081d53fSXin Long 	asoc->peer_secid = peer_sid;
5363d452930fSRichard Haines 
5364d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5365c081d53fSXin Long 	return selinux_netlbl_sctp_assoc_request(asoc, skb);
5366d452930fSRichard Haines }
5367d452930fSRichard Haines 
5368d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5369d452930fSRichard Haines  * based on their @optname.
5370d452930fSRichard Haines  */
5371d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5372d452930fSRichard Haines 				     struct sockaddr *address,
5373d452930fSRichard Haines 				     int addrlen)
5374d452930fSRichard Haines {
5375d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5376d452930fSRichard Haines 	void *addr_buf;
5377d452930fSRichard Haines 	struct sockaddr *addr;
5378d452930fSRichard Haines 	struct socket *sock;
5379d452930fSRichard Haines 
5380aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5381d452930fSRichard Haines 		return 0;
5382d452930fSRichard Haines 
5383d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5384d452930fSRichard Haines 	sock = sk->sk_socket;
5385d452930fSRichard Haines 	addr_buf = address;
5386d452930fSRichard Haines 
5387d452930fSRichard Haines 	while (walk_size < addrlen) {
5388c138325fSOndrej Mosnacek 		if (walk_size + sizeof(sa_family_t) > addrlen)
5389c138325fSOndrej Mosnacek 			return -EINVAL;
5390c138325fSOndrej Mosnacek 
5391d452930fSRichard Haines 		addr = addr_buf;
5392d452930fSRichard Haines 		switch (addr->sa_family) {
53934152dc91SAlexey Kodanev 		case AF_UNSPEC:
5394d452930fSRichard Haines 		case AF_INET:
5395d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5396d452930fSRichard Haines 			break;
5397d452930fSRichard Haines 		case AF_INET6:
5398d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5399d452930fSRichard Haines 			break;
5400d452930fSRichard Haines 		default:
54014152dc91SAlexey Kodanev 			return -EINVAL;
5402d452930fSRichard Haines 		}
5403d452930fSRichard Haines 
5404292c997aSXin Long 		if (walk_size + len > addrlen)
5405292c997aSXin Long 			return -EINVAL;
5406292c997aSXin Long 
5407d452930fSRichard Haines 		err = -EINVAL;
5408d452930fSRichard Haines 		switch (optname) {
5409d452930fSRichard Haines 		/* Bind checks */
5410d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5411d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5412d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5413d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5414d452930fSRichard Haines 			break;
5415d452930fSRichard Haines 		/* Connect checks */
5416d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5417d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5418d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5419d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5420d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5421d452930fSRichard Haines 			if (err)
5422d452930fSRichard Haines 				return err;
5423d452930fSRichard Haines 
5424d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5425d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5426c76a2f9eSRandy Dunlap 			 * therefore selinux_netlbl_socket_connect_locked()
5427d452930fSRichard Haines 			 * is called here. The situations handled are:
5428d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5429d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5430d452930fSRichard Haines 			 * primary address is selected.
5431d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5432d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5433d452930fSRichard Haines 			 * selinux_socket_connect().
5434d452930fSRichard Haines 			 */
5435d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5436d452930fSRichard Haines 			break;
5437d452930fSRichard Haines 		}
5438d452930fSRichard Haines 
5439d452930fSRichard Haines 		if (err)
5440d452930fSRichard Haines 			return err;
5441d452930fSRichard Haines 
5442d452930fSRichard Haines 		addr_buf += len;
5443d452930fSRichard Haines 		walk_size += len;
5444d452930fSRichard Haines 	}
5445d452930fSRichard Haines 
5446d452930fSRichard Haines 	return 0;
5447d452930fSRichard Haines }
5448d452930fSRichard Haines 
5449d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5450c081d53fSXin Long static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
5451d452930fSRichard Haines 				  struct sock *newsk)
5452d452930fSRichard Haines {
5453d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5454d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5455d452930fSRichard Haines 
5456d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5457d452930fSRichard Haines 	 * the non-sctp clone version.
5458d452930fSRichard Haines 	 */
5459aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5460d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5461d452930fSRichard Haines 
5462c081d53fSXin Long 	newsksec->sid = asoc->secid;
5463c081d53fSXin Long 	newsksec->peer_sid = asoc->peer_secid;
5464d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5465d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5466d452930fSRichard Haines }
5467d452930fSRichard Haines 
546841dd9596SFlorian Westphal static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
54694237c75cSVenkat Yekkirala 				     struct request_sock *req)
54704237c75cSVenkat Yekkirala {
54714237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
54724237c75cSVenkat Yekkirala 	int err;
54730b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5474446b8024SPaul Moore 	u32 connsid;
54754237c75cSVenkat Yekkirala 	u32 peersid;
54764237c75cSVenkat Yekkirala 
5477aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5478220deb96SPaul Moore 	if (err)
5479220deb96SPaul Moore 		return err;
5480446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
54814237c75cSVenkat Yekkirala 	if (err)
54824237c75cSVenkat Yekkirala 		return err;
5483446b8024SPaul Moore 	req->secid = connsid;
54846b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5485389fb800SPaul Moore 
5486389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
54874237c75cSVenkat Yekkirala }
54884237c75cSVenkat Yekkirala 
54899a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
54909a673e56SAdrian Bunk 				   const struct request_sock *req)
54914237c75cSVenkat Yekkirala {
54924237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
54934237c75cSVenkat Yekkirala 
54944237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
54956b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
54964237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
54974237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
54984237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
54994237c75cSVenkat Yekkirala 	   time it will have been created and available. */
550099f59ed0SPaul Moore 
55019f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
55029f2ad665SPaul Moore 	 * thread with access to newsksec */
5503389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
55044237c75cSVenkat Yekkirala }
55054237c75cSVenkat Yekkirala 
5506014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
55076b877699SVenkat Yekkirala {
5508aa862900SPaul Moore 	u16 family = sk->sk_family;
55096b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
55106b877699SVenkat Yekkirala 
5511aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5512aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5513aa862900SPaul Moore 		family = PF_INET;
5514aa862900SPaul Moore 
5515aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
55166b877699SVenkat Yekkirala }
55176b877699SVenkat Yekkirala 
55182606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
55192606fd1fSEric Paris {
55202606fd1fSEric Paris 	const struct task_security_struct *__tsec;
55212606fd1fSEric Paris 	u32 tsid;
55222606fd1fSEric Paris 
55230c6cfa62SCasey Schaufler 	__tsec = selinux_cred(current_cred());
55242606fd1fSEric Paris 	tsid = __tsec->sid;
55252606fd1fSEric Paris 
55266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55276b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
55286b6bc620SStephen Smalley 			    NULL);
55292606fd1fSEric Paris }
55302606fd1fSEric Paris 
55312606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
55322606fd1fSEric Paris {
55332606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
55342606fd1fSEric Paris }
55352606fd1fSEric Paris 
55362606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
55372606fd1fSEric Paris {
55382606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
55392606fd1fSEric Paris }
55402606fd1fSEric Paris 
55419a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
55423df98d79SPaul Moore 				      struct flowi_common *flic)
55434237c75cSVenkat Yekkirala {
55443df98d79SPaul Moore 	flic->flowic_secid = req->secid;
55454237c75cSVenkat Yekkirala }
55464237c75cSVenkat Yekkirala 
55475dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
55485dbbaf2dSPaul Moore {
55495dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
55505dbbaf2dSPaul Moore 
55515dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
55525dbbaf2dSPaul Moore 	if (!tunsec)
55535dbbaf2dSPaul Moore 		return -ENOMEM;
55545dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
55555dbbaf2dSPaul Moore 
55565dbbaf2dSPaul Moore 	*security = tunsec;
55575dbbaf2dSPaul Moore 	return 0;
55585dbbaf2dSPaul Moore }
55595dbbaf2dSPaul Moore 
55605dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
55615dbbaf2dSPaul Moore {
55625dbbaf2dSPaul Moore 	kfree(security);
55635dbbaf2dSPaul Moore }
55645dbbaf2dSPaul Moore 
5565ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5566ed6d76e4SPaul Moore {
5567ed6d76e4SPaul Moore 	u32 sid = current_sid();
5568ed6d76e4SPaul Moore 
5569ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5570ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5571ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5572ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5573ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5574ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5575ed6d76e4SPaul Moore 
55766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55776b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5578ed6d76e4SPaul Moore 			    NULL);
5579ed6d76e4SPaul Moore }
5580ed6d76e4SPaul Moore 
55815dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5582ed6d76e4SPaul Moore {
55835dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
55845dbbaf2dSPaul Moore 
55856b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55866b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
55875dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
55885dbbaf2dSPaul Moore }
55895dbbaf2dSPaul Moore 
55905dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
55915dbbaf2dSPaul Moore {
55925dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5593ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5594ed6d76e4SPaul Moore 
5595ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5596ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5597ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5598ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5599ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5600ed6d76e4SPaul Moore 	 * protocols were being used */
5601ed6d76e4SPaul Moore 
56025dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5603ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
56045dbbaf2dSPaul Moore 
56055dbbaf2dSPaul Moore 	return 0;
5606ed6d76e4SPaul Moore }
5607ed6d76e4SPaul Moore 
56085dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5609ed6d76e4SPaul Moore {
56105dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5611ed6d76e4SPaul Moore 	u32 sid = current_sid();
5612ed6d76e4SPaul Moore 	int err;
5613ed6d76e4SPaul Moore 
56146b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
56156b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5616ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5617ed6d76e4SPaul Moore 	if (err)
5618ed6d76e4SPaul Moore 		return err;
56196b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
56206b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5621ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5622ed6d76e4SPaul Moore 	if (err)
5623ed6d76e4SPaul Moore 		return err;
56245dbbaf2dSPaul Moore 	tunsec->sid = sid;
5625ed6d76e4SPaul Moore 
5626ed6d76e4SPaul Moore 	return 0;
5627ed6d76e4SPaul Moore }
5628ed6d76e4SPaul Moore 
56291da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
56301da177e4SLinus Torvalds 
56314342f705SFlorian Westphal static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
56324342f705SFlorian Westphal 				       const struct nf_hook_state *state)
56331da177e4SLinus Torvalds {
56341d1e1dedSPaul Moore 	int ifindex;
56351d1e1dedSPaul Moore 	u16 family;
5636effad8dfSPaul Moore 	char *addrp;
5637effad8dfSPaul Moore 	u32 peer_sid;
56382bf49690SThomas Liu 	struct common_audit_data ad;
563948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
56401d1e1dedSPaul Moore 	int secmark_active, peerlbl_active;
56414237c75cSVenkat Yekkirala 
5642aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5643effad8dfSPaul Moore 		return NF_ACCEPT;
56444237c75cSVenkat Yekkirala 
5645effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
56462be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5647effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5648effad8dfSPaul Moore 		return NF_ACCEPT;
56494237c75cSVenkat Yekkirala 
56501d1e1dedSPaul Moore 	family = state->pf;
5651d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5652d8395c87SPaul Moore 		return NF_DROP;
5653d8395c87SPaul Moore 
56541d1e1dedSPaul Moore 	ifindex = state->in->ifindex;
565550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
565648c62af6SEric Paris 	ad.u.net = &net;
56571d1e1dedSPaul Moore 	ad.u.net->netif = ifindex;
565848c62af6SEric Paris 	ad.u.net->family = family;
5659effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5660effad8dfSPaul Moore 		return NF_DROP;
56611da177e4SLinus Torvalds 
5662dfaebe98SPaul Moore 	if (peerlbl_active) {
56631d1e1dedSPaul Moore 		int err;
56641d1e1dedSPaul Moore 
56651d1e1dedSPaul Moore 		err = selinux_inet_sys_rcv_skb(state->net, ifindex,
5666cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5667dfaebe98SPaul Moore 		if (err) {
5668a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5669effad8dfSPaul Moore 			return NF_DROP;
5670dfaebe98SPaul Moore 		}
5671dfaebe98SPaul Moore 	}
5672effad8dfSPaul Moore 
5673effad8dfSPaul Moore 	if (secmark_active)
56746b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56756b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5676effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5677effad8dfSPaul Moore 			return NF_DROP;
5678effad8dfSPaul Moore 
56791d1e1dedSPaul Moore 	if (netlbl_enabled())
5680948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5681948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5682948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5683948bf85cSPaul Moore 		 * protection */
5684948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5685948bf85cSPaul Moore 			return NF_DROP;
5686948bf85cSPaul Moore 
5687effad8dfSPaul Moore 	return NF_ACCEPT;
5688effad8dfSPaul Moore }
5689effad8dfSPaul Moore 
56904342f705SFlorian Westphal static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
5691238e54c9SDavid S. Miller 				      const struct nf_hook_state *state)
5692effad8dfSPaul Moore {
569347180068SPaul Moore 	struct sock *sk;
5694948bf85cSPaul Moore 	u32 sid;
5695948bf85cSPaul Moore 
5696948bf85cSPaul Moore 	if (!netlbl_enabled())
5697948bf85cSPaul Moore 		return NF_ACCEPT;
5698948bf85cSPaul Moore 
5699948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5700948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5701948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
570247180068SPaul Moore 	sk = skb->sk;
570347180068SPaul Moore 	if (sk) {
570447180068SPaul Moore 		struct sk_security_struct *sksec;
570547180068SPaul Moore 
5706e446f9dfSEric Dumazet 		if (sk_listener(sk))
570747180068SPaul Moore 			/* if the socket is the listening state then this
570847180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
570947180068SPaul Moore 			 * be labeled based on the connection/request_sock and
571047180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
571147180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
571247180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
571347180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
571447180068SPaul Moore 			 * as any IP option based labeling should be copied
571547180068SPaul Moore 			 * from the initial connection request (in the IP
571647180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
571747180068SPaul Moore 			 * security label in the packet itself this is the
571847180068SPaul Moore 			 * best we can do. */
571947180068SPaul Moore 			return NF_ACCEPT;
572047180068SPaul Moore 
572147180068SPaul Moore 		/* standard practice, label using the parent socket */
572247180068SPaul Moore 		sksec = sk->sk_security;
5723948bf85cSPaul Moore 		sid = sksec->sid;
5724948bf85cSPaul Moore 	} else
5725948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
57261d1e1dedSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
5727948bf85cSPaul Moore 		return NF_DROP;
5728948bf85cSPaul Moore 
5729948bf85cSPaul Moore 	return NF_ACCEPT;
5730948bf85cSPaul Moore }
5731948bf85cSPaul Moore 
57322917f57bSHuw Davies 
5733effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
57341d1e1dedSPaul Moore 					const struct nf_hook_state *state)
57354e5ab4cbSJames Morris {
57361d1e1dedSPaul Moore 	struct sock *sk;
57374237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
57382bf49690SThomas Liu 	struct common_audit_data ad;
573948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5740d8395c87SPaul Moore 	u8 proto;
57414e5ab4cbSJames Morris 
57421d1e1dedSPaul Moore 	sk = skb_to_full_sk(skb);
5743effad8dfSPaul Moore 	if (sk == NULL)
5744effad8dfSPaul Moore 		return NF_ACCEPT;
57454237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
57464e5ab4cbSJames Morris 
574750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
574848c62af6SEric Paris 	ad.u.net = &net;
57491d1e1dedSPaul Moore 	ad.u.net->netif = state->out->ifindex;
57501d1e1dedSPaul Moore 	ad.u.net->family = state->pf;
57511d1e1dedSPaul Moore 	if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
5752d8395c87SPaul Moore 		return NF_DROP;
5753d8395c87SPaul Moore 
575458bfbb51SPaul Moore 	if (selinux_secmark_enabled())
57556b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57566b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5757d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
57582fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
57591da177e4SLinus Torvalds 
5760d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
57612fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5762effad8dfSPaul Moore 
5763effad8dfSPaul Moore 	return NF_ACCEPT;
5764effad8dfSPaul Moore }
5765effad8dfSPaul Moore 
57664342f705SFlorian Westphal static unsigned int selinux_ip_postroute(void *priv,
57674342f705SFlorian Westphal 					 struct sk_buff *skb,
57684342f705SFlorian Westphal 					 const struct nf_hook_state *state)
5769effad8dfSPaul Moore {
57701d1e1dedSPaul Moore 	u16 family;
5771effad8dfSPaul Moore 	u32 secmark_perm;
5772effad8dfSPaul Moore 	u32 peer_sid;
57731d1e1dedSPaul Moore 	int ifindex;
5774effad8dfSPaul Moore 	struct sock *sk;
57752bf49690SThomas Liu 	struct common_audit_data ad;
577648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5777effad8dfSPaul Moore 	char *addrp;
57781d1e1dedSPaul Moore 	int secmark_active, peerlbl_active;
5779effad8dfSPaul Moore 
5780effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5781effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5782effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5783effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5784aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
57851d1e1dedSPaul Moore 		return selinux_ip_postroute_compat(skb, state);
5786c0828e50SPaul Moore 
5787effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
57882be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5789effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5790effad8dfSPaul Moore 		return NF_ACCEPT;
5791effad8dfSPaul Moore 
579254abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5793c0828e50SPaul Moore 
5794effad8dfSPaul Moore #ifdef CONFIG_XFRM
5795effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5796effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5797effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5798effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5799effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5800c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5801c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5802c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5803c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5804c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5805c0828e50SPaul Moore 	 *       connection. */
5806c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5807e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5808effad8dfSPaul Moore 		return NF_ACCEPT;
5809effad8dfSPaul Moore #endif
5810effad8dfSPaul Moore 
58111d1e1dedSPaul Moore 	family = state->pf;
5812d8395c87SPaul Moore 	if (sk == NULL) {
5813446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5814446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5815446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5816446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
58174a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5818d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5819d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
582004f6d70fSEric Paris 				return NF_DROP;
58214a7ab3dcSSteffen Klassert 		} else {
58224a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5823d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
58244a7ab3dcSSteffen Klassert 		}
5825e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5826446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5827446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5828446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5829446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5830446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5831446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5832446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5833446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5834446b8024SPaul Moore 		 * for similar problems. */
5835446b8024SPaul Moore 		u32 skb_sid;
5836e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5837e446f9dfSEric Dumazet 
5838e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5839446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5840446b8024SPaul Moore 			return NF_DROP;
5841c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5842c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5843c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5844c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5845c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5846c0828e50SPaul Moore 		 * pass the packet. */
5847c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5848c0828e50SPaul Moore 			switch (family) {
5849c0828e50SPaul Moore 			case PF_INET:
5850c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5851c0828e50SPaul Moore 					return NF_ACCEPT;
5852c0828e50SPaul Moore 				break;
5853c0828e50SPaul Moore 			case PF_INET6:
5854c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5855c0828e50SPaul Moore 					return NF_ACCEPT;
5856a7a91a19SPaul Moore 				break;
5857c0828e50SPaul Moore 			default:
5858c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5859c0828e50SPaul Moore 			}
5860c0828e50SPaul Moore 		}
5861446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5862446b8024SPaul Moore 			return NF_DROP;
5863446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5864d8395c87SPaul Moore 	} else {
5865446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5866446b8024SPaul Moore 		 * associated socket. */
5867effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5868effad8dfSPaul Moore 		peer_sid = sksec->sid;
5869effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5870effad8dfSPaul Moore 	}
5871effad8dfSPaul Moore 
58721d1e1dedSPaul Moore 	ifindex = state->out->ifindex;
587350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
587448c62af6SEric Paris 	ad.u.net = &net;
587548c62af6SEric Paris 	ad.u.net->netif = ifindex;
587648c62af6SEric Paris 	ad.u.net->family = family;
5877d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
587804f6d70fSEric Paris 		return NF_DROP;
5879d8395c87SPaul Moore 
5880effad8dfSPaul Moore 	if (secmark_active)
58816b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58826b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5883effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
58841f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5885effad8dfSPaul Moore 
5886effad8dfSPaul Moore 	if (peerlbl_active) {
5887effad8dfSPaul Moore 		u32 if_sid;
5888effad8dfSPaul Moore 		u32 node_sid;
5889effad8dfSPaul Moore 
58901d1e1dedSPaul Moore 		if (sel_netif_sid(state->net, ifindex, &if_sid))
589104f6d70fSEric Paris 			return NF_DROP;
58926b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58936b6bc620SStephen Smalley 				 peer_sid, if_sid,
5894effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
58951f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5896effad8dfSPaul Moore 
5897effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
589804f6d70fSEric Paris 			return NF_DROP;
58996b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
59006b6bc620SStephen Smalley 				 peer_sid, node_sid,
5901effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
59021f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5903effad8dfSPaul Moore 	}
5904effad8dfSPaul Moore 
5905effad8dfSPaul Moore 	return NF_ACCEPT;
5906effad8dfSPaul Moore }
59071da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
59081da177e4SLinus Torvalds 
59091da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
59101da177e4SLinus Torvalds {
5911fb739741SPaul Moore 	int rc = 0;
5912fb739741SPaul Moore 	unsigned int msg_len;
5913fb739741SPaul Moore 	unsigned int data_len = skb->len;
5914fb739741SPaul Moore 	unsigned char *data = skb->data;
5915df4779b5SHuaisheng Ye 	struct nlmsghdr *nlh;
5916df4779b5SHuaisheng Ye 	struct sk_security_struct *sksec = sk->sk_security;
5917fb739741SPaul Moore 	u16 sclass = sksec->sclass;
5918fb739741SPaul Moore 	u32 perm;
5919df4779b5SHuaisheng Ye 
5920fb739741SPaul Moore 	while (data_len >= nlmsg_total_size(0)) {
5921fb739741SPaul Moore 		nlh = (struct nlmsghdr *)data;
5922df4779b5SHuaisheng Ye 
5923fb739741SPaul Moore 		/* NOTE: the nlmsg_len field isn't reliably set by some netlink
5924fb739741SPaul Moore 		 *       users which means we can't reject skb's with bogus
5925fb739741SPaul Moore 		 *       length fields; our solution is to follow what
5926fb739741SPaul Moore 		 *       netlink_rcv_skb() does and simply skip processing at
5927fb739741SPaul Moore 		 *       messages with length fields that are clearly junk
5928fb739741SPaul Moore 		 */
5929fb739741SPaul Moore 		if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5930fb739741SPaul Moore 			return 0;
5931fb739741SPaul Moore 
5932fb739741SPaul Moore 		rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5933fb739741SPaul Moore 		if (rc == 0) {
5934fb739741SPaul Moore 			rc = sock_has_perm(sk, perm);
5935fb739741SPaul Moore 			if (rc)
5936fb739741SPaul Moore 				return rc;
5937fb739741SPaul Moore 		} else if (rc == -EINVAL) {
5938fb739741SPaul Moore 			/* -EINVAL is a missing msg/perm mapping */
5939df4779b5SHuaisheng Ye 			pr_warn_ratelimited("SELinux: unrecognized netlink"
5940df4779b5SHuaisheng Ye 				" message: protocol=%hu nlmsg_type=%hu sclass=%s"
5941df4779b5SHuaisheng Ye 				" pid=%d comm=%s\n",
5942df4779b5SHuaisheng Ye 				sk->sk_protocol, nlh->nlmsg_type,
5943fb739741SPaul Moore 				secclass_map[sclass - 1].name,
5944df4779b5SHuaisheng Ye 				task_pid_nr(current), current->comm);
5945fb739741SPaul Moore 			if (enforcing_enabled(&selinux_state) &&
5946fb739741SPaul Moore 			    !security_get_allow_unknown(&selinux_state))
5947fb739741SPaul Moore 				return rc;
5948fb739741SPaul Moore 			rc = 0;
5949fb739741SPaul Moore 		} else if (rc == -ENOENT) {
5950fb739741SPaul Moore 			/* -ENOENT is a missing socket/class mapping, ignore */
5951fb739741SPaul Moore 			rc = 0;
5952fb739741SPaul Moore 		} else {
5953fb739741SPaul Moore 			return rc;
5954df4779b5SHuaisheng Ye 		}
5955df4779b5SHuaisheng Ye 
5956fb739741SPaul Moore 		/* move to the next message after applying netlink padding */
5957fb739741SPaul Moore 		msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5958fb739741SPaul Moore 		if (msg_len >= data_len)
5959fb739741SPaul Moore 			return 0;
5960fb739741SPaul Moore 		data_len -= msg_len;
5961fb739741SPaul Moore 		data += msg_len;
5962df4779b5SHuaisheng Ye 	}
5963df4779b5SHuaisheng Ye 
5964fb739741SPaul Moore 	return rc;
59651da177e4SLinus Torvalds }
59661da177e4SLinus Torvalds 
5967ecd5f82eSCasey Schaufler static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
59681da177e4SLinus Torvalds {
59691da177e4SLinus Torvalds 	isec->sclass = sclass;
5970be0554c9SStephen Smalley 	isec->sid = current_sid();
59711da177e4SLinus Torvalds }
59721da177e4SLinus Torvalds 
59731da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
59746af963f1SStephen Smalley 			u32 perms)
59751da177e4SLinus Torvalds {
59761da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59772bf49690SThomas Liu 	struct common_audit_data ad;
5978275bb41eSDavid Howells 	u32 sid = current_sid();
59791da177e4SLinus Torvalds 
59807c653828SCasey Schaufler 	isec = selinux_ipc(ipc_perms);
59811da177e4SLinus Torvalds 
598250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59831da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
59841da177e4SLinus Torvalds 
59856b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59866b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
59871da177e4SLinus Torvalds }
59881da177e4SLinus Torvalds 
59891da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
59901da177e4SLinus Torvalds {
5991b82f3f68SHuaisheng Ye 	struct msg_security_struct *msec;
5992b82f3f68SHuaisheng Ye 
5993b82f3f68SHuaisheng Ye 	msec = selinux_msg_msg(msg);
5994b82f3f68SHuaisheng Ye 	msec->sid = SECINITSID_UNLABELED;
5995b82f3f68SHuaisheng Ye 
5996b82f3f68SHuaisheng Ye 	return 0;
59971da177e4SLinus Torvalds }
59981da177e4SLinus Torvalds 
59991da177e4SLinus Torvalds /* message queue security operations */
6000d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
60011da177e4SLinus Torvalds {
60021da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60032bf49690SThomas Liu 	struct common_audit_data ad;
6004275bb41eSDavid Howells 	u32 sid = current_sid();
60051da177e4SLinus Torvalds 	int rc;
60061da177e4SLinus Torvalds 
6007ecd5f82eSCasey Schaufler 	isec = selinux_ipc(msq);
6008ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_MSGQ);
60091da177e4SLinus Torvalds 
601050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6011d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60121da177e4SLinus Torvalds 
60136b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60146b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
60151da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
60161da177e4SLinus Torvalds 	return rc;
60171da177e4SLinus Torvalds }
60181da177e4SLinus Torvalds 
6019d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
60201da177e4SLinus Torvalds {
60211da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60222bf49690SThomas Liu 	struct common_audit_data ad;
6023275bb41eSDavid Howells 	u32 sid = current_sid();
60241da177e4SLinus Torvalds 
60257c653828SCasey Schaufler 	isec = selinux_ipc(msq);
60261da177e4SLinus Torvalds 
602750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6028d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60291da177e4SLinus Torvalds 
60306b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60316b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
60321da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
60331da177e4SLinus Torvalds }
60341da177e4SLinus Torvalds 
6035d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
60361da177e4SLinus Torvalds {
60371da177e4SLinus Torvalds 	int err;
60381da177e4SLinus Torvalds 	int perms;
60391da177e4SLinus Torvalds 
60401da177e4SLinus Torvalds 	switch (cmd) {
60411da177e4SLinus Torvalds 	case IPC_INFO:
60421da177e4SLinus Torvalds 	case MSG_INFO:
60431da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60446b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60456b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6046be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60471da177e4SLinus Torvalds 	case IPC_STAT:
60481da177e4SLinus Torvalds 	case MSG_STAT:
604923c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
60501da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
60511da177e4SLinus Torvalds 		break;
60521da177e4SLinus Torvalds 	case IPC_SET:
60531da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
60541da177e4SLinus Torvalds 		break;
60551da177e4SLinus Torvalds 	case IPC_RMID:
60561da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
60571da177e4SLinus Torvalds 		break;
60581da177e4SLinus Torvalds 	default:
60591da177e4SLinus Torvalds 		return 0;
60601da177e4SLinus Torvalds 	}
60611da177e4SLinus Torvalds 
6062d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
60631da177e4SLinus Torvalds 	return err;
60641da177e4SLinus Torvalds }
60651da177e4SLinus Torvalds 
6066d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
60671da177e4SLinus Torvalds {
60681da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60691da177e4SLinus Torvalds 	struct msg_security_struct *msec;
60702bf49690SThomas Liu 	struct common_audit_data ad;
6071275bb41eSDavid Howells 	u32 sid = current_sid();
60721da177e4SLinus Torvalds 	int rc;
60731da177e4SLinus Torvalds 
60747c653828SCasey Schaufler 	isec = selinux_ipc(msq);
60757c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
60761da177e4SLinus Torvalds 
60771da177e4SLinus Torvalds 	/*
60781da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
60791da177e4SLinus Torvalds 	 */
60801da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
60811da177e4SLinus Torvalds 		/*
60821da177e4SLinus Torvalds 		 * Compute new sid based on current process and
60831da177e4SLinus Torvalds 		 * message queue this message will be stored in
60841da177e4SLinus Torvalds 		 */
6085aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
6086aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
60871da177e4SLinus Torvalds 		if (rc)
60881da177e4SLinus Torvalds 			return rc;
60891da177e4SLinus Torvalds 	}
60901da177e4SLinus Torvalds 
609150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6092d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60931da177e4SLinus Torvalds 
60941da177e4SLinus Torvalds 	/* Can this process write to the queue? */
60956b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60966b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
60971da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
60981da177e4SLinus Torvalds 	if (!rc)
60991da177e4SLinus Torvalds 		/* Can this process send the message */
61006b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61016b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
6102275bb41eSDavid Howells 				  MSG__SEND, &ad);
61031da177e4SLinus Torvalds 	if (!rc)
61041da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
61056b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61066b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
6107275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
61081da177e4SLinus Torvalds 
61091da177e4SLinus Torvalds 	return rc;
61101da177e4SLinus Torvalds }
61111da177e4SLinus Torvalds 
6112d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
61131da177e4SLinus Torvalds 				    struct task_struct *target,
61141da177e4SLinus Torvalds 				    long type, int mode)
61151da177e4SLinus Torvalds {
61161da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61171da177e4SLinus Torvalds 	struct msg_security_struct *msec;
61182bf49690SThomas Liu 	struct common_audit_data ad;
6119a3727a8bSPaul Moore 	u32 sid = task_sid_obj(target);
61201da177e4SLinus Torvalds 	int rc;
61211da177e4SLinus Torvalds 
61227c653828SCasey Schaufler 	isec = selinux_ipc(msq);
61237c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
61241da177e4SLinus Torvalds 
612550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6126d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
61271da177e4SLinus Torvalds 
61286b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61296b6bc620SStephen Smalley 			  sid, isec->sid,
61301da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
61311da177e4SLinus Torvalds 	if (!rc)
61326b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61336b6bc620SStephen Smalley 				  sid, msec->sid,
61341da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
61351da177e4SLinus Torvalds 	return rc;
61361da177e4SLinus Torvalds }
61371da177e4SLinus Torvalds 
61381da177e4SLinus Torvalds /* Shared Memory security operations */
61397191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
61401da177e4SLinus Torvalds {
61411da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61422bf49690SThomas Liu 	struct common_audit_data ad;
6143275bb41eSDavid Howells 	u32 sid = current_sid();
61441da177e4SLinus Torvalds 	int rc;
61451da177e4SLinus Torvalds 
6146ecd5f82eSCasey Schaufler 	isec = selinux_ipc(shp);
6147ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SHM);
61481da177e4SLinus Torvalds 
614950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61507191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61511da177e4SLinus Torvalds 
61526b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61536b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
61541da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
61551da177e4SLinus Torvalds 	return rc;
61561da177e4SLinus Torvalds }
61571da177e4SLinus Torvalds 
61587191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
61591da177e4SLinus Torvalds {
61601da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61612bf49690SThomas Liu 	struct common_audit_data ad;
6162275bb41eSDavid Howells 	u32 sid = current_sid();
61631da177e4SLinus Torvalds 
61647c653828SCasey Schaufler 	isec = selinux_ipc(shp);
61651da177e4SLinus Torvalds 
616650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61677191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61681da177e4SLinus Torvalds 
61696b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
61706b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
61711da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
61721da177e4SLinus Torvalds }
61731da177e4SLinus Torvalds 
61741da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
61757191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
61761da177e4SLinus Torvalds {
61771da177e4SLinus Torvalds 	int perms;
61781da177e4SLinus Torvalds 	int err;
61791da177e4SLinus Torvalds 
61801da177e4SLinus Torvalds 	switch (cmd) {
61811da177e4SLinus Torvalds 	case IPC_INFO:
61821da177e4SLinus Torvalds 	case SHM_INFO:
61831da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
61846b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
61856b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6186be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
61871da177e4SLinus Torvalds 	case IPC_STAT:
61881da177e4SLinus Torvalds 	case SHM_STAT:
6189c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
61901da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
61911da177e4SLinus Torvalds 		break;
61921da177e4SLinus Torvalds 	case IPC_SET:
61931da177e4SLinus Torvalds 		perms = SHM__SETATTR;
61941da177e4SLinus Torvalds 		break;
61951da177e4SLinus Torvalds 	case SHM_LOCK:
61961da177e4SLinus Torvalds 	case SHM_UNLOCK:
61971da177e4SLinus Torvalds 		perms = SHM__LOCK;
61981da177e4SLinus Torvalds 		break;
61991da177e4SLinus Torvalds 	case IPC_RMID:
62001da177e4SLinus Torvalds 		perms = SHM__DESTROY;
62011da177e4SLinus Torvalds 		break;
62021da177e4SLinus Torvalds 	default:
62031da177e4SLinus Torvalds 		return 0;
62041da177e4SLinus Torvalds 	}
62051da177e4SLinus Torvalds 
62067191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
62071da177e4SLinus Torvalds 	return err;
62081da177e4SLinus Torvalds }
62091da177e4SLinus Torvalds 
62107191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
62111da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
62121da177e4SLinus Torvalds {
62131da177e4SLinus Torvalds 	u32 perms;
62141da177e4SLinus Torvalds 
62151da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
62161da177e4SLinus Torvalds 		perms = SHM__READ;
62171da177e4SLinus Torvalds 	else
62181da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
62191da177e4SLinus Torvalds 
62207191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
62211da177e4SLinus Torvalds }
62221da177e4SLinus Torvalds 
62231da177e4SLinus Torvalds /* Semaphore security operations */
6224aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
62251da177e4SLinus Torvalds {
62261da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62272bf49690SThomas Liu 	struct common_audit_data ad;
6228275bb41eSDavid Howells 	u32 sid = current_sid();
62291da177e4SLinus Torvalds 	int rc;
62301da177e4SLinus Torvalds 
6231ecd5f82eSCasey Schaufler 	isec = selinux_ipc(sma);
6232ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SEM);
62331da177e4SLinus Torvalds 
623450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6235aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62361da177e4SLinus Torvalds 
62376b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
62386b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
62391da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
62401da177e4SLinus Torvalds 	return rc;
62411da177e4SLinus Torvalds }
62421da177e4SLinus Torvalds 
6243aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
62441da177e4SLinus Torvalds {
62451da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62462bf49690SThomas Liu 	struct common_audit_data ad;
6247275bb41eSDavid Howells 	u32 sid = current_sid();
62481da177e4SLinus Torvalds 
62497c653828SCasey Schaufler 	isec = selinux_ipc(sma);
62501da177e4SLinus Torvalds 
625150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6252aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62531da177e4SLinus Torvalds 
62546b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
62556b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
62561da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
62571da177e4SLinus Torvalds }
62581da177e4SLinus Torvalds 
62591da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6260aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
62611da177e4SLinus Torvalds {
62621da177e4SLinus Torvalds 	int err;
62631da177e4SLinus Torvalds 	u32 perms;
62641da177e4SLinus Torvalds 
62651da177e4SLinus Torvalds 	switch (cmd) {
62661da177e4SLinus Torvalds 	case IPC_INFO:
62671da177e4SLinus Torvalds 	case SEM_INFO:
62681da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
62696b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
62706b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6271be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
62721da177e4SLinus Torvalds 	case GETPID:
62731da177e4SLinus Torvalds 	case GETNCNT:
62741da177e4SLinus Torvalds 	case GETZCNT:
62751da177e4SLinus Torvalds 		perms = SEM__GETATTR;
62761da177e4SLinus Torvalds 		break;
62771da177e4SLinus Torvalds 	case GETVAL:
62781da177e4SLinus Torvalds 	case GETALL:
62791da177e4SLinus Torvalds 		perms = SEM__READ;
62801da177e4SLinus Torvalds 		break;
62811da177e4SLinus Torvalds 	case SETVAL:
62821da177e4SLinus Torvalds 	case SETALL:
62831da177e4SLinus Torvalds 		perms = SEM__WRITE;
62841da177e4SLinus Torvalds 		break;
62851da177e4SLinus Torvalds 	case IPC_RMID:
62861da177e4SLinus Torvalds 		perms = SEM__DESTROY;
62871da177e4SLinus Torvalds 		break;
62881da177e4SLinus Torvalds 	case IPC_SET:
62891da177e4SLinus Torvalds 		perms = SEM__SETATTR;
62901da177e4SLinus Torvalds 		break;
62911da177e4SLinus Torvalds 	case IPC_STAT:
62921da177e4SLinus Torvalds 	case SEM_STAT:
6293a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
62941da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
62951da177e4SLinus Torvalds 		break;
62961da177e4SLinus Torvalds 	default:
62971da177e4SLinus Torvalds 		return 0;
62981da177e4SLinus Torvalds 	}
62991da177e4SLinus Torvalds 
6300aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
63011da177e4SLinus Torvalds 	return err;
63021da177e4SLinus Torvalds }
63031da177e4SLinus Torvalds 
6304aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
63051da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
63061da177e4SLinus Torvalds {
63071da177e4SLinus Torvalds 	u32 perms;
63081da177e4SLinus Torvalds 
63091da177e4SLinus Torvalds 	if (alter)
63101da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
63111da177e4SLinus Torvalds 	else
63121da177e4SLinus Torvalds 		perms = SEM__READ;
63131da177e4SLinus Torvalds 
6314aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
63151da177e4SLinus Torvalds }
63161da177e4SLinus Torvalds 
63171da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
63181da177e4SLinus Torvalds {
63191da177e4SLinus Torvalds 	u32 av = 0;
63201da177e4SLinus Torvalds 
63211da177e4SLinus Torvalds 	av = 0;
63221da177e4SLinus Torvalds 	if (flag & S_IRUGO)
63231da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
63241da177e4SLinus Torvalds 	if (flag & S_IWUGO)
63251da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
63261da177e4SLinus Torvalds 
63271da177e4SLinus Torvalds 	if (av == 0)
63281da177e4SLinus Torvalds 		return 0;
63291da177e4SLinus Torvalds 
63306af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
63311da177e4SLinus Torvalds }
63321da177e4SLinus Torvalds 
6333713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6334713a04aeSAhmed S. Darwish {
63357c653828SCasey Schaufler 	struct ipc_security_struct *isec = selinux_ipc(ipcp);
6336713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6337713a04aeSAhmed S. Darwish }
6338713a04aeSAhmed S. Darwish 
63391da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
63401da177e4SLinus Torvalds {
63411da177e4SLinus Torvalds 	if (inode)
63421da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
63431da177e4SLinus Torvalds }
63441da177e4SLinus Torvalds 
63451da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
634604ff9708SAl Viro 			       char *name, char **value)
63471da177e4SLinus Torvalds {
6348275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
63498c8570fbSDustin Kirkland 	u32 sid;
63501da177e4SLinus Torvalds 	int error;
635104ff9708SAl Viro 	unsigned len;
63521da177e4SLinus Torvalds 
6353275bb41eSDavid Howells 	rcu_read_lock();
63540c6cfa62SCasey Schaufler 	__tsec = selinux_cred(__task_cred(p));
63551da177e4SLinus Torvalds 
6356be0554c9SStephen Smalley 	if (current != p) {
63576b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63586b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6359be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6360be0554c9SStephen Smalley 		if (error)
6361be0554c9SStephen Smalley 			goto bad;
6362be0554c9SStephen Smalley 	}
6363be0554c9SStephen Smalley 
63641da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6365275bb41eSDavid Howells 		sid = __tsec->sid;
63661da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6367275bb41eSDavid Howells 		sid = __tsec->osid;
63681da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6369275bb41eSDavid Howells 		sid = __tsec->exec_sid;
63701da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6371275bb41eSDavid Howells 		sid = __tsec->create_sid;
63724eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6373275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
637442c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6375275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6376be0554c9SStephen Smalley 	else {
6377be0554c9SStephen Smalley 		error = -EINVAL;
6378be0554c9SStephen Smalley 		goto bad;
6379be0554c9SStephen Smalley 	}
6380275bb41eSDavid Howells 	rcu_read_unlock();
63811da177e4SLinus Torvalds 
63821da177e4SLinus Torvalds 	if (!sid)
63831da177e4SLinus Torvalds 		return 0;
63841da177e4SLinus Torvalds 
6385aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
638604ff9708SAl Viro 	if (error)
638704ff9708SAl Viro 		return error;
638804ff9708SAl Viro 	return len;
6389275bb41eSDavid Howells 
6390be0554c9SStephen Smalley bad:
6391275bb41eSDavid Howells 	rcu_read_unlock();
6392be0554c9SStephen Smalley 	return error;
63931da177e4SLinus Torvalds }
63941da177e4SLinus Torvalds 
6395b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
63961da177e4SLinus Torvalds {
63971da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6398d84f4f99SDavid Howells 	struct cred *new;
6399be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
64001da177e4SLinus Torvalds 	int error;
64011da177e4SLinus Torvalds 	char *str = value;
64021da177e4SLinus Torvalds 
64031da177e4SLinus Torvalds 	/*
64041da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
64051da177e4SLinus Torvalds 	 */
64061da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
64076b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64086b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6409be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
64101da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
64116b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64126b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6413be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
64144eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
64156b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64166b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6417be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
641842c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
64196b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64206b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6421be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
64221da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
64236b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64246b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6425be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
64261da177e4SLinus Torvalds 	else
64271da177e4SLinus Torvalds 		error = -EINVAL;
64281da177e4SLinus Torvalds 	if (error)
64291da177e4SLinus Torvalds 		return error;
64301da177e4SLinus Torvalds 
64311da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6432a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
64331da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
64341da177e4SLinus Torvalds 			str[size-1] = 0;
64351da177e4SLinus Torvalds 			size--;
64361da177e4SLinus Torvalds 		}
6437aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6438aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
643912b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6440db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6441d6ea83ecSEric Paris 				struct audit_buffer *ab;
6442d6ea83ecSEric Paris 				size_t audit_size;
6443d6ea83ecSEric Paris 
6444d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6445d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6446d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6447d6ea83ecSEric Paris 					audit_size = size - 1;
6448d6ea83ecSEric Paris 				else
6449d6ea83ecSEric Paris 					audit_size = size;
6450cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6451cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6452cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6453893c47d1SAustin Kim 				if (!ab)
6454893c47d1SAustin Kim 					return error;
6455d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6456d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6457d6ea83ecSEric Paris 				audit_log_end(ab);
6458d6ea83ecSEric Paris 
645912b29f34SStephen Smalley 				return error;
6460d6ea83ecSEric Paris 			}
6461aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6462aa8e712cSStephen Smalley 						      &selinux_state,
6463aa8e712cSStephen Smalley 						      value, size, &sid);
646412b29f34SStephen Smalley 		}
64651da177e4SLinus Torvalds 		if (error)
64661da177e4SLinus Torvalds 			return error;
64671da177e4SLinus Torvalds 	}
64681da177e4SLinus Torvalds 
6469d84f4f99SDavid Howells 	new = prepare_creds();
6470d84f4f99SDavid Howells 	if (!new)
6471d84f4f99SDavid Howells 		return -ENOMEM;
6472d84f4f99SDavid Howells 
64731da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
64741da177e4SLinus Torvalds 	   performed during the actual operation (execve,
64751da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6476b8bff599SEric W. Biederman 	   operation.  See selinux_bprm_creds_for_exec for the execve
64771da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
64781da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
64790c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
6480d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
64811da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6482d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
64831da177e4SLinus Torvalds 		tsec->create_sid = sid;
6484d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
6485464c258aSOndrej Mosnacek 		if (sid) {
6486464c258aSOndrej Mosnacek 			error = avc_has_perm(&selinux_state, mysid, sid,
6487464c258aSOndrej Mosnacek 					     SECCLASS_KEY, KEY__CREATE, NULL);
64884eb582cfSMichael LeMay 			if (error)
6489d84f4f99SDavid Howells 				goto abort_change;
6490464c258aSOndrej Mosnacek 		}
64914eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6492d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
649342c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6494d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6495d84f4f99SDavid Howells 		error = -EINVAL;
64961da177e4SLinus Torvalds 		if (sid == 0)
6497d84f4f99SDavid Howells 			goto abort_change;
6498d9250deaSKaiGai Kohei 
6499d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6500d84f4f99SDavid Howells 		error = -EPERM;
65015bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6502aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6503aa8e712cSStephen Smalley 							    tsec->sid, sid);
6504d84f4f99SDavid Howells 			if (error)
6505d84f4f99SDavid Howells 				goto abort_change;
65061da177e4SLinus Torvalds 		}
65071da177e4SLinus Torvalds 
65081da177e4SLinus Torvalds 		/* Check permissions for the transition. */
65096b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
65106b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
65111da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
65121da177e4SLinus Torvalds 		if (error)
6513d84f4f99SDavid Howells 			goto abort_change;
65141da177e4SLinus Torvalds 
65151da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
65161da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6517be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
65180c6181cbSPaul Moore 		if (ptsid != 0) {
65196b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
65206b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6521d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6522d84f4f99SDavid Howells 			if (error)
6523d84f4f99SDavid Howells 				goto abort_change;
6524d84f4f99SDavid Howells 		}
6525d84f4f99SDavid Howells 
6526d84f4f99SDavid Howells 		tsec->sid = sid;
6527d84f4f99SDavid Howells 	} else {
6528d84f4f99SDavid Howells 		error = -EINVAL;
6529d84f4f99SDavid Howells 		goto abort_change;
6530d84f4f99SDavid Howells 	}
6531d84f4f99SDavid Howells 
6532d84f4f99SDavid Howells 	commit_creds(new);
65331da177e4SLinus Torvalds 	return size;
6534d84f4f99SDavid Howells 
6535d84f4f99SDavid Howells abort_change:
6536d84f4f99SDavid Howells 	abort_creds(new);
6537d84f4f99SDavid Howells 	return error;
65381da177e4SLinus Torvalds }
65391da177e4SLinus Torvalds 
6540746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6541746df9b5SDavid Quigley {
6542746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6543746df9b5SDavid Quigley }
6544746df9b5SDavid Quigley 
6545dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6546dc49c1f9SCatherine Zhang {
6547aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6548aa8e712cSStephen Smalley 				       secdata, seclen);
6549dc49c1f9SCatherine Zhang }
6550dc49c1f9SCatherine Zhang 
65517bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
655263cb3449SDavid Howells {
6553aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6554aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
655563cb3449SDavid Howells }
655663cb3449SDavid Howells 
6557dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6558dc49c1f9SCatherine Zhang {
6559dc49c1f9SCatherine Zhang 	kfree(secdata);
6560dc49c1f9SCatherine Zhang }
6561dc49c1f9SCatherine Zhang 
65626f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
65636f3be9f5SAndreas Gruenbacher {
656480788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
65656f3be9f5SAndreas Gruenbacher 
65669287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
65676f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
65689287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
65696f3be9f5SAndreas Gruenbacher }
65706f3be9f5SAndreas Gruenbacher 
65711ee65e37SDavid P. Quigley /*
65721ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
65731ee65e37SDavid P. Quigley  */
65741ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
65751ee65e37SDavid P. Quigley {
657653e0c2aaSOndrej Mosnacek 	int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
657753e0c2aaSOndrej Mosnacek 					   ctx, ctxlen, 0);
657853e0c2aaSOndrej Mosnacek 	/* Do not return error when suppressing label (SBLABEL_MNT not set). */
657953e0c2aaSOndrej Mosnacek 	return rc == -EOPNOTSUPP ? 0 : rc;
65801ee65e37SDavid P. Quigley }
65811ee65e37SDavid P. Quigley 
65821ee65e37SDavid P. Quigley /*
65831ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
65841ee65e37SDavid P. Quigley  */
65851ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
65861ee65e37SDavid P. Quigley {
6587c7c7a1a1STycho Andersen 	return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SELINUX,
6588c7c7a1a1STycho Andersen 				     ctx, ctxlen, 0);
65891ee65e37SDavid P. Quigley }
65901ee65e37SDavid P. Quigley 
65911ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
65921ee65e37SDavid P. Quigley {
65931ee65e37SDavid P. Quigley 	int len = 0;
659471bc356fSChristian Brauner 	len = selinux_inode_getsecurity(&init_user_ns, inode,
659571bc356fSChristian Brauner 					XATTR_SELINUX_SUFFIX, ctx, true);
65961ee65e37SDavid P. Quigley 	if (len < 0)
65971ee65e37SDavid P. Quigley 		return len;
65981ee65e37SDavid P. Quigley 	*ctxlen = len;
65991ee65e37SDavid P. Quigley 	return 0;
66001ee65e37SDavid P. Quigley }
6601d720024eSMichael LeMay #ifdef CONFIG_KEYS
6602d720024eSMichael LeMay 
6603d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
66047e047ef5SDavid Howells 			     unsigned long flags)
6605d720024eSMichael LeMay {
6606d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6607d720024eSMichael LeMay 	struct key_security_struct *ksec;
6608d720024eSMichael LeMay 
6609d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6610d720024eSMichael LeMay 	if (!ksec)
6611d720024eSMichael LeMay 		return -ENOMEM;
6612d720024eSMichael LeMay 
66130c6cfa62SCasey Schaufler 	tsec = selinux_cred(cred);
6614d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6615d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
66164eb582cfSMichael LeMay 	else
6617d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6618d720024eSMichael LeMay 
6619275bb41eSDavid Howells 	k->security = ksec;
6620d720024eSMichael LeMay 	return 0;
6621d720024eSMichael LeMay }
6622d720024eSMichael LeMay 
6623d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6624d720024eSMichael LeMay {
6625d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6626d720024eSMichael LeMay 
6627d720024eSMichael LeMay 	k->security = NULL;
6628d720024eSMichael LeMay 	kfree(ksec);
6629d720024eSMichael LeMay }
6630d720024eSMichael LeMay 
6631d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6632d84f4f99SDavid Howells 				  const struct cred *cred,
66338c0637e9SDavid Howells 				  enum key_need_perm need_perm)
6634d720024eSMichael LeMay {
6635d720024eSMichael LeMay 	struct key *key;
6636d720024eSMichael LeMay 	struct key_security_struct *ksec;
66378c0637e9SDavid Howells 	u32 perm, sid;
6638d720024eSMichael LeMay 
66398c0637e9SDavid Howells 	switch (need_perm) {
66408c0637e9SDavid Howells 	case KEY_NEED_VIEW:
66418c0637e9SDavid Howells 		perm = KEY__VIEW;
66428c0637e9SDavid Howells 		break;
66438c0637e9SDavid Howells 	case KEY_NEED_READ:
66448c0637e9SDavid Howells 		perm = KEY__READ;
66458c0637e9SDavid Howells 		break;
66468c0637e9SDavid Howells 	case KEY_NEED_WRITE:
66478c0637e9SDavid Howells 		perm = KEY__WRITE;
66488c0637e9SDavid Howells 		break;
66498c0637e9SDavid Howells 	case KEY_NEED_SEARCH:
66508c0637e9SDavid Howells 		perm = KEY__SEARCH;
66518c0637e9SDavid Howells 		break;
66528c0637e9SDavid Howells 	case KEY_NEED_LINK:
66538c0637e9SDavid Howells 		perm = KEY__LINK;
66548c0637e9SDavid Howells 		break;
66558c0637e9SDavid Howells 	case KEY_NEED_SETATTR:
66568c0637e9SDavid Howells 		perm = KEY__SETATTR;
66578c0637e9SDavid Howells 		break;
66588c0637e9SDavid Howells 	case KEY_NEED_UNLINK:
66598c0637e9SDavid Howells 	case KEY_SYSADMIN_OVERRIDE:
66608c0637e9SDavid Howells 	case KEY_AUTHTOKEN_OVERRIDE:
66618c0637e9SDavid Howells 	case KEY_DEFER_PERM_CHECK:
6662d720024eSMichael LeMay 		return 0;
66638c0637e9SDavid Howells 	default:
66648c0637e9SDavid Howells 		WARN_ON(1);
66658c0637e9SDavid Howells 		return -EPERM;
66668c0637e9SDavid Howells 
66678c0637e9SDavid Howells 	}
6668d720024eSMichael LeMay 
6669d84f4f99SDavid Howells 	sid = cred_sid(cred);
6670275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6671275bb41eSDavid Howells 	ksec = key->security;
6672275bb41eSDavid Howells 
66736b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
6674028db3e2SLinus Torvalds 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6675d720024eSMichael LeMay }
6676d720024eSMichael LeMay 
667770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
667870a5bb72SDavid Howells {
667970a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
668070a5bb72SDavid Howells 	char *context = NULL;
668170a5bb72SDavid Howells 	unsigned len;
668270a5bb72SDavid Howells 	int rc;
668370a5bb72SDavid Howells 
6684aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6685aa8e712cSStephen Smalley 				     &context, &len);
668670a5bb72SDavid Howells 	if (!rc)
668770a5bb72SDavid Howells 		rc = len;
668870a5bb72SDavid Howells 	*_buffer = context;
668970a5bb72SDavid Howells 	return rc;
669070a5bb72SDavid Howells }
66913e412cccSDavid Howells 
66923e412cccSDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
66933e412cccSDavid Howells static int selinux_watch_key(struct key *key)
66943e412cccSDavid Howells {
66953e412cccSDavid Howells 	struct key_security_struct *ksec = key->security;
66963e412cccSDavid Howells 	u32 sid = current_sid();
66973e412cccSDavid Howells 
66983e412cccSDavid Howells 	return avc_has_perm(&selinux_state,
66993e412cccSDavid Howells 			    sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
67003e412cccSDavid Howells }
67013e412cccSDavid Howells #endif
67023a976fa6SDaniel Jurgens #endif
670370a5bb72SDavid Howells 
67043a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6705cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6706cfc4d882SDaniel Jurgens {
6707cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6708cfc4d882SDaniel Jurgens 	int err;
6709cfc4d882SDaniel Jurgens 	u32 sid = 0;
6710cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6711cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6712cfc4d882SDaniel Jurgens 
6713409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6714cfc4d882SDaniel Jurgens 	if (err)
6715cfc4d882SDaniel Jurgens 		return err;
6716cfc4d882SDaniel Jurgens 
6717cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6718cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6719cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6720cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
67216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67226b6bc620SStephen Smalley 			    sec->sid, sid,
6723cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6724cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6725cfc4d882SDaniel Jurgens }
6726cfc4d882SDaniel Jurgens 
6727ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6728ab861dfcSDaniel Jurgens 					    u8 port_num)
6729ab861dfcSDaniel Jurgens {
6730ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6731ab861dfcSDaniel Jurgens 	int err;
6732ab861dfcSDaniel Jurgens 	u32 sid = 0;
6733ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6734ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6735ab861dfcSDaniel Jurgens 
6736aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6737aa8e712cSStephen Smalley 				      &sid);
6738ab861dfcSDaniel Jurgens 
6739ab861dfcSDaniel Jurgens 	if (err)
6740ab861dfcSDaniel Jurgens 		return err;
6741ab861dfcSDaniel Jurgens 
6742ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6743869cbeefSOndrej Mosnacek 	ibendport.dev_name = dev_name;
6744ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6745ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
67466b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67476b6bc620SStephen Smalley 			    sec->sid, sid,
6748ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6749ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6750ab861dfcSDaniel Jurgens }
6751ab861dfcSDaniel Jurgens 
67523a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
67533a976fa6SDaniel Jurgens {
67543a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
67553a976fa6SDaniel Jurgens 
67563a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
67573a976fa6SDaniel Jurgens 	if (!sec)
67583a976fa6SDaniel Jurgens 		return -ENOMEM;
67593a976fa6SDaniel Jurgens 	sec->sid = current_sid();
67603a976fa6SDaniel Jurgens 
67613a976fa6SDaniel Jurgens 	*ib_sec = sec;
67623a976fa6SDaniel Jurgens 	return 0;
67633a976fa6SDaniel Jurgens }
67643a976fa6SDaniel Jurgens 
67653a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
67663a976fa6SDaniel Jurgens {
67673a976fa6SDaniel Jurgens 	kfree(ib_sec);
67683a976fa6SDaniel Jurgens }
6769d720024eSMichael LeMay #endif
6770d720024eSMichael LeMay 
6771ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6772ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6773ec27c356SChenbo Feng 				     unsigned int size)
6774ec27c356SChenbo Feng {
6775ec27c356SChenbo Feng 	u32 sid = current_sid();
6776ec27c356SChenbo Feng 	int ret;
6777ec27c356SChenbo Feng 
6778ec27c356SChenbo Feng 	switch (cmd) {
6779ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
67806b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67816b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6782ec27c356SChenbo Feng 				   NULL);
6783ec27c356SChenbo Feng 		break;
6784ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
67856b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67866b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6787ec27c356SChenbo Feng 				   NULL);
6788ec27c356SChenbo Feng 		break;
6789ec27c356SChenbo Feng 	default:
6790ec27c356SChenbo Feng 		ret = 0;
6791ec27c356SChenbo Feng 		break;
6792ec27c356SChenbo Feng 	}
6793ec27c356SChenbo Feng 
6794ec27c356SChenbo Feng 	return ret;
6795ec27c356SChenbo Feng }
6796ec27c356SChenbo Feng 
6797ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6798ec27c356SChenbo Feng {
6799ec27c356SChenbo Feng 	u32 av = 0;
6800ec27c356SChenbo Feng 
6801ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6802ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6803ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6804ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6805ec27c356SChenbo Feng 	return av;
6806ec27c356SChenbo Feng }
6807ec27c356SChenbo Feng 
6808f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6809f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6810f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6811f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6812f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6813f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6814f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6815f66e448cSChenbo Feng  */
6816f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6817f66e448cSChenbo Feng {
6818f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6819f66e448cSChenbo Feng 	struct bpf_prog *prog;
6820f66e448cSChenbo Feng 	struct bpf_map *map;
6821f66e448cSChenbo Feng 	int ret;
6822f66e448cSChenbo Feng 
6823f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6824f66e448cSChenbo Feng 		map = file->private_data;
6825f66e448cSChenbo Feng 		bpfsec = map->security;
68266b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68276b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6828f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6829f66e448cSChenbo Feng 		if (ret)
6830f66e448cSChenbo Feng 			return ret;
6831f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6832f66e448cSChenbo Feng 		prog = file->private_data;
6833f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
68346b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68356b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6836f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6837f66e448cSChenbo Feng 		if (ret)
6838f66e448cSChenbo Feng 			return ret;
6839f66e448cSChenbo Feng 	}
6840f66e448cSChenbo Feng 	return 0;
6841f66e448cSChenbo Feng }
6842f66e448cSChenbo Feng 
6843ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6844ec27c356SChenbo Feng {
6845ec27c356SChenbo Feng 	u32 sid = current_sid();
6846ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6847ec27c356SChenbo Feng 
6848ec27c356SChenbo Feng 	bpfsec = map->security;
68496b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68506b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6851ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6852ec27c356SChenbo Feng }
6853ec27c356SChenbo Feng 
6854ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6855ec27c356SChenbo Feng {
6856ec27c356SChenbo Feng 	u32 sid = current_sid();
6857ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6858ec27c356SChenbo Feng 
6859ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
68606b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68616b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6862ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6863ec27c356SChenbo Feng }
6864ec27c356SChenbo Feng 
6865ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6866ec27c356SChenbo Feng {
6867ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6868ec27c356SChenbo Feng 
6869ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6870ec27c356SChenbo Feng 	if (!bpfsec)
6871ec27c356SChenbo Feng 		return -ENOMEM;
6872ec27c356SChenbo Feng 
6873ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6874ec27c356SChenbo Feng 	map->security = bpfsec;
6875ec27c356SChenbo Feng 
6876ec27c356SChenbo Feng 	return 0;
6877ec27c356SChenbo Feng }
6878ec27c356SChenbo Feng 
6879ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6880ec27c356SChenbo Feng {
6881ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6882ec27c356SChenbo Feng 
6883ec27c356SChenbo Feng 	map->security = NULL;
6884ec27c356SChenbo Feng 	kfree(bpfsec);
6885ec27c356SChenbo Feng }
6886ec27c356SChenbo Feng 
6887ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6888ec27c356SChenbo Feng {
6889ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6890ec27c356SChenbo Feng 
6891ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6892ec27c356SChenbo Feng 	if (!bpfsec)
6893ec27c356SChenbo Feng 		return -ENOMEM;
6894ec27c356SChenbo Feng 
6895ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6896ec27c356SChenbo Feng 	aux->security = bpfsec;
6897ec27c356SChenbo Feng 
6898ec27c356SChenbo Feng 	return 0;
6899ec27c356SChenbo Feng }
6900ec27c356SChenbo Feng 
6901ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6902ec27c356SChenbo Feng {
6903ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6904ec27c356SChenbo Feng 
6905ec27c356SChenbo Feng 	aux->security = NULL;
6906ec27c356SChenbo Feng 	kfree(bpfsec);
6907ec27c356SChenbo Feng }
6908ec27c356SChenbo Feng #endif
6909ec27c356SChenbo Feng 
6910bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6911bbd3662aSCasey Schaufler 	.lbs_cred = sizeof(struct task_security_struct),
691233bf60caSCasey Schaufler 	.lbs_file = sizeof(struct file_security_struct),
6913afb1cbe3SCasey Schaufler 	.lbs_inode = sizeof(struct inode_security_struct),
6914ecd5f82eSCasey Schaufler 	.lbs_ipc = sizeof(struct ipc_security_struct),
6915ecd5f82eSCasey Schaufler 	.lbs_msg_msg = sizeof(struct msg_security_struct),
69161aea7808SCasey Schaufler 	.lbs_superblock = sizeof(struct superblock_security_struct),
6917bbd3662aSCasey Schaufler };
6918bbd3662aSCasey Schaufler 
6919da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
6920da97e184SJoel Fernandes (Google) static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6921da97e184SJoel Fernandes (Google) {
6922da97e184SJoel Fernandes (Google) 	u32 requested, sid = current_sid();
6923da97e184SJoel Fernandes (Google) 
6924da97e184SJoel Fernandes (Google) 	if (type == PERF_SECURITY_OPEN)
6925da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__OPEN;
6926da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_CPU)
6927da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__CPU;
6928da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_KERNEL)
6929da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__KERNEL;
6930da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_TRACEPOINT)
6931da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__TRACEPOINT;
6932da97e184SJoel Fernandes (Google) 	else
6933da97e184SJoel Fernandes (Google) 		return -EINVAL;
6934da97e184SJoel Fernandes (Google) 
6935da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6936da97e184SJoel Fernandes (Google) 			    requested, NULL);
6937da97e184SJoel Fernandes (Google) }
6938da97e184SJoel Fernandes (Google) 
6939da97e184SJoel Fernandes (Google) static int selinux_perf_event_alloc(struct perf_event *event)
6940da97e184SJoel Fernandes (Google) {
6941da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec;
6942da97e184SJoel Fernandes (Google) 
6943da97e184SJoel Fernandes (Google) 	perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6944da97e184SJoel Fernandes (Google) 	if (!perfsec)
6945da97e184SJoel Fernandes (Google) 		return -ENOMEM;
6946da97e184SJoel Fernandes (Google) 
6947da97e184SJoel Fernandes (Google) 	perfsec->sid = current_sid();
6948da97e184SJoel Fernandes (Google) 	event->security = perfsec;
6949da97e184SJoel Fernandes (Google) 
6950da97e184SJoel Fernandes (Google) 	return 0;
6951da97e184SJoel Fernandes (Google) }
6952da97e184SJoel Fernandes (Google) 
6953da97e184SJoel Fernandes (Google) static void selinux_perf_event_free(struct perf_event *event)
6954da97e184SJoel Fernandes (Google) {
6955da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6956da97e184SJoel Fernandes (Google) 
6957da97e184SJoel Fernandes (Google) 	event->security = NULL;
6958da97e184SJoel Fernandes (Google) 	kfree(perfsec);
6959da97e184SJoel Fernandes (Google) }
6960da97e184SJoel Fernandes (Google) 
6961da97e184SJoel Fernandes (Google) static int selinux_perf_event_read(struct perf_event *event)
6962da97e184SJoel Fernandes (Google) {
6963da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6964da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
6965da97e184SJoel Fernandes (Google) 
6966da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
6967da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
6968da97e184SJoel Fernandes (Google) }
6969da97e184SJoel Fernandes (Google) 
6970da97e184SJoel Fernandes (Google) static int selinux_perf_event_write(struct perf_event *event)
6971da97e184SJoel Fernandes (Google) {
6972da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6973da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
6974da97e184SJoel Fernandes (Google) 
6975da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
6976da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
6977da97e184SJoel Fernandes (Google) }
6978da97e184SJoel Fernandes (Google) #endif
6979da97e184SJoel Fernandes (Google) 
6980740b0341SPaul Moore #ifdef CONFIG_IO_URING
6981740b0341SPaul Moore /**
6982740b0341SPaul Moore  * selinux_uring_override_creds - check the requested cred override
6983740b0341SPaul Moore  * @new: the target creds
6984740b0341SPaul Moore  *
6985740b0341SPaul Moore  * Check to see if the current task is allowed to override it's credentials
6986740b0341SPaul Moore  * to service an io_uring operation.
6987740b0341SPaul Moore  */
6988740b0341SPaul Moore static int selinux_uring_override_creds(const struct cred *new)
6989740b0341SPaul Moore {
6990740b0341SPaul Moore 	return avc_has_perm(&selinux_state, current_sid(), cred_sid(new),
6991740b0341SPaul Moore 			    SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
6992740b0341SPaul Moore }
6993740b0341SPaul Moore 
6994740b0341SPaul Moore /**
6995740b0341SPaul Moore  * selinux_uring_sqpoll - check if a io_uring polling thread can be created
6996740b0341SPaul Moore  *
6997740b0341SPaul Moore  * Check to see if the current task is allowed to create a new io_uring
6998740b0341SPaul Moore  * kernel polling thread.
6999740b0341SPaul Moore  */
7000740b0341SPaul Moore static int selinux_uring_sqpoll(void)
7001740b0341SPaul Moore {
7002740b0341SPaul Moore 	int sid = current_sid();
7003740b0341SPaul Moore 
7004740b0341SPaul Moore 	return avc_has_perm(&selinux_state, sid, sid,
7005740b0341SPaul Moore 			    SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
7006740b0341SPaul Moore }
7007740b0341SPaul Moore #endif /* CONFIG_IO_URING */
7008740b0341SPaul Moore 
7009cfff75d8SOndrej Mosnacek /*
7010cfff75d8SOndrej Mosnacek  * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7011cfff75d8SOndrej Mosnacek  * 1. any hooks that don't belong to (2.) or (3.) below,
7012cfff75d8SOndrej Mosnacek  * 2. hooks that both access structures allocated by other hooks, and allocate
7013cfff75d8SOndrej Mosnacek  *    structures that can be later accessed by other hooks (mostly "cloning"
7014cfff75d8SOndrej Mosnacek  *    hooks),
7015cfff75d8SOndrej Mosnacek  * 3. hooks that only allocate structures that can be later accessed by other
7016cfff75d8SOndrej Mosnacek  *    hooks ("allocating" hooks).
7017cfff75d8SOndrej Mosnacek  *
7018cfff75d8SOndrej Mosnacek  * Please follow block comment delimiters in the list to keep this order.
7019cfff75d8SOndrej Mosnacek  *
7020cfff75d8SOndrej Mosnacek  * This ordering is needed for SELinux runtime disable to work at least somewhat
7021cfff75d8SOndrej Mosnacek  * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7022cfff75d8SOndrej Mosnacek  * when disabling SELinux at runtime.
7023cfff75d8SOndrej Mosnacek  */
7024ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
7025e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7026e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7027e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7028e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
7029076c54c5SAhmed S. Darwish 
7030e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7031e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7032e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
7033e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
7034e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
7035e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
7036e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
7037e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
7038e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
703979af7307SStephen Smalley 
7040e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
70411da177e4SLinus Torvalds 
7042b8bff599SEric W. Biederman 	LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
7043e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7044e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
70451da177e4SLinus Torvalds 
7046204cc0ccSAl Viro 	LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
704769c4a42dSOlga Kornievskaia 	LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
7048e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7049e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7050e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7051e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7052e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
7053e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
7054e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7055e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
70561da177e4SLinus Torvalds 
705798aa0034SStephen Smalley 	LSM_HOOK_INIT(move_mount, selinux_move_mount),
705898aa0034SStephen Smalley 
7059e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
7060a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
7061e0007529SEric Paris 
7062e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7063e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
706429cd6591SDaniel Colascione 	LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
7065e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
7066e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
7067e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7068e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7069e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7070e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7071e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7072e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7073e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7074e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7075e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7076e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7077e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7078e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7079e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7080e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7081e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7082e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7083e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7084e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7085e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7086e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
708756909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
708819472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
7089ac5656d8SAaron Goidel 	LSM_HOOK_INIT(path_notify, selinux_path_notify),
70901da177e4SLinus Torvalds 
7091ec882da5SOndrej Mosnacek 	LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7092ec882da5SOndrej Mosnacek 
7093e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
7094e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
7095e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7096e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7097e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7098e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7099e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
7100e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7101e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7102e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7103e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
71041da177e4SLinus Torvalds 
7105e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
71061da177e4SLinus Torvalds 
7107a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
7108e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7109e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
71103ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
7111e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7112e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7113e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
7114c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
711561d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
7116e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7117e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7118e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
71196326948fSPaul Moore 	LSM_HOOK_INIT(current_getsecid_subj, selinux_current_getsecid_subj),
7120eb1231f7SPaul Moore 	LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
7121e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7122e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7123e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7124791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7125e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7126e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7127e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7128e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7129e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
7130e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7131788e7dd4SYuichi Nakamura 
7132e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7133e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
71341da177e4SLinus Torvalds 
7135e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7136e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7137e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7138e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
71391da177e4SLinus Torvalds 
7140e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7141e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7142e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
71431da177e4SLinus Torvalds 
7144e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7145e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7146e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
71471da177e4SLinus Torvalds 
7148e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
71491da177e4SLinus Torvalds 
7150e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7151e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
71521da177e4SLinus Torvalds 
7153e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7154e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7155e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
71566f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7157e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7158e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
71591da177e4SLinus Torvalds 
7160e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7161e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7162dc49c1f9SCatherine Zhang 
7163e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
7164e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
71650b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7166e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7167e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7168e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7169e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7170e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7171e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7172e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7173e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7174e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7175e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7176e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7177e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7178e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
7179e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
7180e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7181e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7182e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7183e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7184e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7185d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7186d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7187d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7188e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7189e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7190e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7191e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7192e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7193e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7194e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7195e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7196e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7197e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7198e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7199e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
72003a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
7201cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7202ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
7203ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
72043a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
72053a976fa6SDaniel Jurgens #endif
7206d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
7207e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7208e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7209e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7210e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7211e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7212e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7213e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
7214e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
72151da177e4SLinus Torvalds #endif
7216d720024eSMichael LeMay 
7217d720024eSMichael LeMay #ifdef CONFIG_KEYS
7218e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
7219e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
7220e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
72213e412cccSDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
72223e412cccSDavid Howells 	LSM_HOOK_INIT(watch_key, selinux_watch_key),
72233e412cccSDavid Howells #endif
7224d720024eSMichael LeMay #endif
72259d57a7f9SAhmed S. Darwish 
72269d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
7227e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7228e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7229e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
72309d57a7f9SAhmed S. Darwish #endif
7231ec27c356SChenbo Feng 
7232ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
7233ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
7234ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7235ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7236ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7237ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7238ec27c356SChenbo Feng #endif
7239da97e184SJoel Fernandes (Google) 
7240da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
7241da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7242da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7243da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7244da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7245da97e184SJoel Fernandes (Google) #endif
724659438b46SStephen Smalley 
7247740b0341SPaul Moore #ifdef CONFIG_IO_URING
7248740b0341SPaul Moore 	LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7249740b0341SPaul Moore 	LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
7250740b0341SPaul Moore #endif
7251cfff75d8SOndrej Mosnacek 
7252cfff75d8SOndrej Mosnacek 	/*
7253cfff75d8SOndrej Mosnacek 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7254cfff75d8SOndrej Mosnacek 	 */
7255cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7256cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7257cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7258cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7259cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7260cfff75d8SOndrej Mosnacek #endif
7261cfff75d8SOndrej Mosnacek 
7262cfff75d8SOndrej Mosnacek 	/*
7263cfff75d8SOndrej Mosnacek 	 * PUT "ALLOCATING" HOOKS HERE
7264cfff75d8SOndrej Mosnacek 	 */
7265cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7266cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_queue_alloc_security,
7267cfff75d8SOndrej Mosnacek 		      selinux_msg_queue_alloc_security),
7268cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7269cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7270cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7271cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7272cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7273cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7274cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7275cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7276cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_INFINIBAND
7277cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7278cfff75d8SOndrej Mosnacek #endif
7279cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7280cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7281cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7282cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7283cfff75d8SOndrej Mosnacek 		      selinux_xfrm_state_alloc_acquire),
7284cfff75d8SOndrej Mosnacek #endif
7285cfff75d8SOndrej Mosnacek #ifdef CONFIG_KEYS
7286cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7287cfff75d8SOndrej Mosnacek #endif
7288cfff75d8SOndrej Mosnacek #ifdef CONFIG_AUDIT
7289cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7290cfff75d8SOndrej Mosnacek #endif
7291cfff75d8SOndrej Mosnacek #ifdef CONFIG_BPF_SYSCALL
7292cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7293cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7294cfff75d8SOndrej Mosnacek #endif
7295cfff75d8SOndrej Mosnacek #ifdef CONFIG_PERF_EVENTS
7296cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7297cfff75d8SOndrej Mosnacek #endif
72981da177e4SLinus Torvalds };
72991da177e4SLinus Torvalds 
73001da177e4SLinus Torvalds static __init int selinux_init(void)
73011da177e4SLinus Torvalds {
7302c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
73031da177e4SLinus Torvalds 
7304aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
7305e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
73068861d0afSLakshmi Ramasubramanian 	checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
73076b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
73084b36cb77SOndrej Mosnacek 	mutex_init(&selinux_state.status_lock);
73099ff9abc4SStephen Smalley 	mutex_init(&selinux_state.policy_mutex);
7310aa8e712cSStephen Smalley 
73111da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
7312d84f4f99SDavid Howells 	cred_init_security();
73131da177e4SLinus Torvalds 
7314fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7315fcaaade1SStephen Smalley 
73161da177e4SLinus Torvalds 	avc_init();
73171da177e4SLinus Torvalds 
7318aa8e712cSStephen Smalley 	avtab_cache_init();
7319aa8e712cSStephen Smalley 
7320aa8e712cSStephen Smalley 	ebitmap_cache_init();
7321aa8e712cSStephen Smalley 
7322aa8e712cSStephen Smalley 	hashtab_cache_init();
7323aa8e712cSStephen Smalley 
7324d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
73251da177e4SLinus Torvalds 
7326615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7327615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7328615e51fdSPaul Moore 
73298f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
73308f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
73318f408ab6SDaniel Jurgens 
7332aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7333c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
7334828dfe1dSEric Paris 	else
7335c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
7336d720024eSMichael LeMay 
7337d7167b14SAl Viro 	fs_validate_description("selinux", selinux_fs_parameters);
7338442155c1SDavid Howells 
73391da177e4SLinus Torvalds 	return 0;
73401da177e4SLinus Torvalds }
73411da177e4SLinus Torvalds 
7342e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7343e8c26255SAl Viro {
7344204cc0ccSAl Viro 	selinux_set_mnt_opts(sb, NULL, 0, NULL);
7345e8c26255SAl Viro }
7346e8c26255SAl Viro 
73471da177e4SLinus Torvalds void selinux_complete_init(void)
73481da177e4SLinus Torvalds {
7349c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
73501da177e4SLinus Torvalds 
73511da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7352c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
7353e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
73541da177e4SLinus Torvalds }
73551da177e4SLinus Torvalds 
73561da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
73571da177e4SLinus Torvalds    all processes and objects when they are created. */
73583d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
735907aed2f2SKees Cook 	.name = "selinux",
736014bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
73616c5a682eSStephen Smalley 	.enabled = &selinux_enabled_boot,
7362bbd3662aSCasey Schaufler 	.blobs = &selinux_blob_sizes,
73633d6e5f6dSKees Cook 	.init = selinux_init,
73643d6e5f6dSKees Cook };
73651da177e4SLinus Torvalds 
7366c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
73671da177e4SLinus Torvalds 
7368591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7369effad8dfSPaul Moore 	{
73704342f705SFlorian Westphal 		.hook =		selinux_ip_postroute,
73712597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
73726e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
73731da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7374effad8dfSPaul Moore 	},
7375effad8dfSPaul Moore 	{
73764342f705SFlorian Westphal 		.hook =		selinux_ip_forward,
73772597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7378effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7379effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7380948bf85cSPaul Moore 	},
7381948bf85cSPaul Moore 	{
73824342f705SFlorian Westphal 		.hook =		selinux_ip_output,
73832597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7384948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7385948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
738625db6beaSJiri Pirko 	},
73871a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7388effad8dfSPaul Moore 	{
73894342f705SFlorian Westphal 		.hook =		selinux_ip_postroute,
73902597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
73916e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
73921da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7393effad8dfSPaul Moore 	},
7394effad8dfSPaul Moore 	{
73954342f705SFlorian Westphal 		.hook =		selinux_ip_forward,
73962597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7397effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7398effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
739925db6beaSJiri Pirko 	},
74002917f57bSHuw Davies 	{
74014342f705SFlorian Westphal 		.hook =		selinux_ip_output,
74022917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
74032917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
74042917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
74052917f57bSHuw Davies 	},
74061da177e4SLinus Torvalds #endif	/* IPV6 */
740725db6beaSJiri Pirko };
74081da177e4SLinus Torvalds 
74098e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
74108e71bf75SFlorian Westphal {
74118e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
74128e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
74138e71bf75SFlorian Westphal }
74148e71bf75SFlorian Westphal 
74158e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
74168e71bf75SFlorian Westphal {
74178e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
74188e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
74198e71bf75SFlorian Westphal }
74208e71bf75SFlorian Westphal 
74218e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
74228e71bf75SFlorian Westphal 	.init = selinux_nf_register,
74238e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
74248e71bf75SFlorian Westphal };
74258e71bf75SFlorian Westphal 
74261da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
74271da177e4SLinus Torvalds {
742825db6beaSJiri Pirko 	int err;
74291da177e4SLinus Torvalds 
74306c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
743125db6beaSJiri Pirko 		return 0;
74321da177e4SLinus Torvalds 
7433c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
74341da177e4SLinus Torvalds 
74358e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
74361da177e4SLinus Torvalds 	if (err)
74378e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
74381da177e4SLinus Torvalds 
743925db6beaSJiri Pirko 	return 0;
74401da177e4SLinus Torvalds }
74411da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
74421da177e4SLinus Torvalds 
74431da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
74441da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
74451da177e4SLinus Torvalds {
7446c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
74471da177e4SLinus Torvalds 
74488e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
74491da177e4SLinus Torvalds }
74501da177e4SLinus Torvalds #endif
74511da177e4SLinus Torvalds 
7452c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
74531da177e4SLinus Torvalds 
74541da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
74551da177e4SLinus Torvalds #define selinux_nf_ip_exit()
74561da177e4SLinus Torvalds #endif
74571da177e4SLinus Torvalds 
7458c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
74591da177e4SLinus Torvalds 
74601da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7461aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
74621da177e4SLinus Torvalds {
746365cddd50SOndrej Mosnacek 	if (selinux_initialized(state)) {
74641da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
74651da177e4SLinus Torvalds 		return -EINVAL;
74661da177e4SLinus Torvalds 	}
74671da177e4SLinus Torvalds 
746865cddd50SOndrej Mosnacek 	if (selinux_disabled(state)) {
74691da177e4SLinus Torvalds 		/* Only do this once. */
74701da177e4SLinus Torvalds 		return -EINVAL;
74711da177e4SLinus Torvalds 	}
74721da177e4SLinus Torvalds 
747365cddd50SOndrej Mosnacek 	selinux_mark_disabled(state);
7474aa8e712cSStephen Smalley 
7475c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
74761da177e4SLinus Torvalds 
7477cfff75d8SOndrej Mosnacek 	/*
7478cfff75d8SOndrej Mosnacek 	 * Unregister netfilter hooks.
7479cfff75d8SOndrej Mosnacek 	 * Must be done before security_delete_hooks() to avoid breaking
7480cfff75d8SOndrej Mosnacek 	 * runtime disable.
7481cfff75d8SOndrej Mosnacek 	 */
7482cfff75d8SOndrej Mosnacek 	selinux_nf_ip_exit();
7483cfff75d8SOndrej Mosnacek 
7484b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
74851da177e4SLinus Torvalds 
7486af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7487af8ff049SEric Paris 	avc_disable();
7488af8ff049SEric Paris 
74891da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
74901da177e4SLinus Torvalds 	exit_sel_fs();
74911da177e4SLinus Torvalds 
74921da177e4SLinus Torvalds 	return 0;
74931da177e4SLinus Torvalds }
74941da177e4SLinus Torvalds #endif
7495