xref: /openbmc/linux/security/selinux/hooks.c (revision 29cd6591ab6fee3125ea5c1bf350f5013bc615e1)
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  */
235275bb41eSDavid Howells static inline u32 task_sid(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;
325afb1cbe3SCasey Schaufler 	sbsec = inode->i_sb->s_security;
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 
3431da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3461da177e4SLinus Torvalds 	sb->s_security = NULL;
3471da177e4SLinus Torvalds 	kfree(sbsec);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350bd323655SAl Viro struct selinux_mnt_opts {
351bd323655SAl Viro 	const char *fscontext, *context, *rootcontext, *defcontext;
352bd323655SAl Viro };
353bd323655SAl Viro 
354204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts)
355204cc0ccSAl Viro {
356bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
357bd323655SAl Viro 	kfree(opts->fscontext);
358bd323655SAl Viro 	kfree(opts->context);
359bd323655SAl Viro 	kfree(opts->rootcontext);
360bd323655SAl Viro 	kfree(opts->defcontext);
361204cc0ccSAl Viro 	kfree(opts);
362204cc0ccSAl Viro }
363204cc0ccSAl Viro 
3641da177e4SLinus Torvalds enum {
36531e87930SEric Paris 	Opt_error = -1,
366442155c1SDavid Howells 	Opt_context = 0,
367442155c1SDavid Howells 	Opt_defcontext = 1,
3681da177e4SLinus Torvalds 	Opt_fscontext = 2,
369442155c1SDavid Howells 	Opt_rootcontext = 3,
370442155c1SDavid Howells 	Opt_seclabel = 4,
3711da177e4SLinus Torvalds };
3721da177e4SLinus Torvalds 
373da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
374169d68efSAl Viro static struct {
375169d68efSAl Viro 	const char *name;
376169d68efSAl Viro 	int len;
377169d68efSAl Viro 	int opt;
378169d68efSAl Viro 	bool has_arg;
379169d68efSAl Viro } tokens[] = {
380da3d76abSAl Viro 	A(context, true),
381da3d76abSAl Viro 	A(fscontext, true),
382da3d76abSAl Viro 	A(defcontext, true),
383da3d76abSAl Viro 	A(rootcontext, true),
384da3d76abSAl Viro 	A(seclabel, false),
3851da177e4SLinus Torvalds };
386169d68efSAl Viro #undef A
387169d68efSAl Viro 
388169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg)
389169d68efSAl Viro {
390169d68efSAl Viro 	int i;
391169d68efSAl Viro 
392169d68efSAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
393169d68efSAl Viro 		size_t len = tokens[i].len;
394169d68efSAl Viro 		if (len > l || memcmp(s, tokens[i].name, len))
395169d68efSAl Viro 			continue;
396169d68efSAl Viro 		if (tokens[i].has_arg) {
397169d68efSAl Viro 			if (len == l || s[len] != '=')
398169d68efSAl Viro 				continue;
399169d68efSAl Viro 			*arg = s + len + 1;
400169d68efSAl Viro 		} else if (len != l)
401169d68efSAl Viro 			continue;
402169d68efSAl Viro 		return tokens[i].opt;
403169d68efSAl Viro 	}
404169d68efSAl Viro 	return Opt_error;
405169d68efSAl Viro }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4081da177e4SLinus Torvalds 
409c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
410c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
411275bb41eSDavid Howells 			const struct cred *cred)
412c312feb2SEric Paris {
4130c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
414c312feb2SEric Paris 	int rc;
415c312feb2SEric Paris 
4166b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4176b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
418c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
419c312feb2SEric Paris 	if (rc)
420c312feb2SEric Paris 		return rc;
421c312feb2SEric Paris 
4226b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4236b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
424c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
425c312feb2SEric Paris 	return rc;
426c312feb2SEric Paris }
427c312feb2SEric Paris 
4280808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4290808925eSEric Paris 			struct superblock_security_struct *sbsec,
430275bb41eSDavid Howells 			const struct cred *cred)
4310808925eSEric Paris {
4320c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
4330808925eSEric Paris 	int rc;
4346b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4356b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4360808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
4370808925eSEric Paris 	if (rc)
4380808925eSEric Paris 		return rc;
4390808925eSEric Paris 
4406b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4416b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
4420808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4430808925eSEric Paris 	return rc;
4440808925eSEric Paris }
4450808925eSEric Paris 
446a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb)
447b43e725dSEric Paris {
448d5f3a5f6SMark Salyzyn 	/* Special handling. Genfs but also in-core setxattr handler */
449a83d6ddaSOndrej Mosnacek 	return	!strcmp(sb->s_type->name, "sysfs") ||
450d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
451d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
452a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
4532651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
454aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
4552651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
4562651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
457b43e725dSEric Paris }
458b43e725dSEric Paris 
459a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb)
460a83d6ddaSOndrej Mosnacek {
461a83d6ddaSOndrej Mosnacek 	struct superblock_security_struct *sbsec = sb->s_security;
462a83d6ddaSOndrej Mosnacek 
463a83d6ddaSOndrej Mosnacek 	/*
464a83d6ddaSOndrej Mosnacek 	 * IMPORTANT: Double-check logic in this function when adding a new
465a83d6ddaSOndrej Mosnacek 	 * SECURITY_FS_USE_* definition!
466a83d6ddaSOndrej Mosnacek 	 */
467a83d6ddaSOndrej Mosnacek 	BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
468a83d6ddaSOndrej Mosnacek 
469a83d6ddaSOndrej Mosnacek 	switch (sbsec->behavior) {
470a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_XATTR:
471a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TRANS:
472a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TASK:
473a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NATIVE:
474a83d6ddaSOndrej Mosnacek 		return 1;
475a83d6ddaSOndrej Mosnacek 
476a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_GENFS:
477a83d6ddaSOndrej Mosnacek 		return selinux_is_genfs_special_handling(sb);
478a83d6ddaSOndrej Mosnacek 
479a83d6ddaSOndrej Mosnacek 	/* Never allow relabeling on context mounts */
480a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_MNTPOINT:
481a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NONE:
482a83d6ddaSOndrej Mosnacek 	default:
483a83d6ddaSOndrej Mosnacek 		return 0;
484a83d6ddaSOndrej Mosnacek 	}
485a83d6ddaSOndrej Mosnacek }
486a83d6ddaSOndrej Mosnacek 
48708abe46bSOndrej Mosnacek static int sb_check_xattr_support(struct super_block *sb)
48808abe46bSOndrej Mosnacek {
48908abe46bSOndrej Mosnacek 	struct superblock_security_struct *sbsec = sb->s_security;
49008abe46bSOndrej Mosnacek 	struct dentry *root = sb->s_root;
49108abe46bSOndrej Mosnacek 	struct inode *root_inode = d_backing_inode(root);
49208abe46bSOndrej Mosnacek 	u32 sid;
49308abe46bSOndrej Mosnacek 	int rc;
49408abe46bSOndrej Mosnacek 
49508abe46bSOndrej Mosnacek 	/*
49608abe46bSOndrej Mosnacek 	 * Make sure that the xattr handler exists and that no
49708abe46bSOndrej Mosnacek 	 * error other than -ENODATA is returned by getxattr on
49808abe46bSOndrej Mosnacek 	 * the root directory.  -ENODATA is ok, as this may be
49908abe46bSOndrej Mosnacek 	 * the first boot of the SELinux kernel before we have
50008abe46bSOndrej Mosnacek 	 * assigned xattr values to the filesystem.
50108abe46bSOndrej Mosnacek 	 */
50208abe46bSOndrej Mosnacek 	if (!(root_inode->i_opflags & IOP_XATTR)) {
50308abe46bSOndrej Mosnacek 		pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
50408abe46bSOndrej Mosnacek 			sb->s_id, sb->s_type->name);
50508abe46bSOndrej Mosnacek 		goto fallback;
50608abe46bSOndrej Mosnacek 	}
50708abe46bSOndrej Mosnacek 
50808abe46bSOndrej Mosnacek 	rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
50908abe46bSOndrej Mosnacek 	if (rc < 0 && rc != -ENODATA) {
51008abe46bSOndrej Mosnacek 		if (rc == -EOPNOTSUPP) {
51108abe46bSOndrej Mosnacek 			pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
51208abe46bSOndrej Mosnacek 				sb->s_id, sb->s_type->name);
51308abe46bSOndrej Mosnacek 			goto fallback;
51408abe46bSOndrej Mosnacek 		} else {
51508abe46bSOndrej Mosnacek 			pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
51608abe46bSOndrej Mosnacek 				sb->s_id, sb->s_type->name, -rc);
51708abe46bSOndrej Mosnacek 			return rc;
51808abe46bSOndrej Mosnacek 		}
51908abe46bSOndrej Mosnacek 	}
52008abe46bSOndrej Mosnacek 	return 0;
52108abe46bSOndrej Mosnacek 
52208abe46bSOndrej Mosnacek fallback:
52308abe46bSOndrej Mosnacek 	/* No xattr support - try to fallback to genfs if possible. */
52408abe46bSOndrej Mosnacek 	rc = security_genfs_sid(&selinux_state, sb->s_type->name, "/",
52508abe46bSOndrej Mosnacek 				SECCLASS_DIR, &sid);
52608abe46bSOndrej Mosnacek 	if (rc)
52708abe46bSOndrej Mosnacek 		return -EOPNOTSUPP;
52808abe46bSOndrej Mosnacek 
52908abe46bSOndrej Mosnacek 	pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
53008abe46bSOndrej Mosnacek 		sb->s_id, sb->s_type->name);
53108abe46bSOndrej Mosnacek 	sbsec->behavior = SECURITY_FS_USE_GENFS;
53208abe46bSOndrej Mosnacek 	sbsec->sid = sid;
53308abe46bSOndrej Mosnacek 	return 0;
53408abe46bSOndrej Mosnacek }
53508abe46bSOndrej Mosnacek 
536c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5391da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
540c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5411da177e4SLinus Torvalds 	int rc = 0;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
54408abe46bSOndrej Mosnacek 		rc = sb_check_xattr_support(sb);
54508abe46bSOndrej Mosnacek 		if (rc)
54608abe46bSOndrej Mosnacek 			return rc;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
549eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5500b4d3452SScott Mayhew 
5510b4d3452SScott Mayhew 	/*
5520b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5530b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5540b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5550b4d3452SScott Mayhew 	 */
556b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
55712f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5580b4d3452SScott Mayhew 	else
5590b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
560ddd29ec6SDavid P. Quigley 
5611da177e4SLinus Torvalds 	/* Initialize the root inode. */
562c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5651da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5661da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5671da177e4SLinus Torvalds 	   populates itself. */
5681da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5698d64124aSAl Viro 	while (!list_empty(&sbsec->isec_head)) {
5701da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5718d64124aSAl Viro 				list_first_entry(&sbsec->isec_head,
5721da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5731da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
574923190d3SStephen Smalley 		list_del_init(&isec->list);
5751da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5761da177e4SLinus Torvalds 		inode = igrab(inode);
5771da177e4SLinus Torvalds 		if (inode) {
5781da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
579cb89e246SPaul Moore 				inode_doinit_with_dentry(inode, NULL);
5801da177e4SLinus Torvalds 			iput(inode);
5811da177e4SLinus Torvalds 		}
5821da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
585c9180a57SEric Paris 	return rc;
586c9180a57SEric Paris }
587c9180a57SEric Paris 
588c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
589c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
590c9180a57SEric Paris {
5910d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5920d90a7ecSDavid P. Quigley 
593c9180a57SEric Paris 	/* check if the old mount command had the same options */
5940d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
595c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
596c9180a57SEric Paris 		    (old_sid != new_sid))
597c9180a57SEric Paris 			return 1;
598c9180a57SEric Paris 
599c9180a57SEric Paris 	/* check if we were passed the same options twice,
600c9180a57SEric Paris 	 * aka someone passed context=a,context=b
601c9180a57SEric Paris 	 */
6020d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
6030d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
604c9180a57SEric Paris 			return 1;
605c9180a57SEric Paris 	return 0;
606c9180a57SEric Paris }
607e0007529SEric Paris 
608bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
609bd323655SAl Viro {
610bd323655SAl Viro 	int rc = security_context_str_to_sid(&selinux_state, s,
611bd323655SAl Viro 					     sid, GFP_KERNEL);
612bd323655SAl Viro 	if (rc)
613bd323655SAl Viro 		pr_warn("SELinux: security_context_str_to_sid"
614bd323655SAl Viro 		       "(%s) failed for (dev %s, type %s) errno=%d\n",
615bd323655SAl Viro 		       s, sb->s_id, sb->s_type->name, rc);
616bd323655SAl Viro 	return rc;
617bd323655SAl Viro }
618bd323655SAl Viro 
619c9180a57SEric Paris /*
620c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
621c9180a57SEric Paris  * labeling information.
622c9180a57SEric Paris  */
623e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
624204cc0ccSAl Viro 				void *mnt_opts,
625649f6e77SDavid Quigley 				unsigned long kern_flags,
626649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
627c9180a57SEric Paris {
628275bb41eSDavid Howells 	const struct cred *cred = current_cred();
629c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
630b159e86bSOndrej Mosnacek 	struct dentry *root = sb->s_root;
631bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
6322c97165bSPaul Moore 	struct inode_security_struct *root_isec;
633c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
634c9180a57SEric Paris 	u32 defcontext_sid = 0;
635bd323655SAl Viro 	int rc = 0;
636c9180a57SEric Paris 
637c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
638c9180a57SEric Paris 
63965cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
640bd323655SAl Viro 		if (!opts) {
641c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
642c9180a57SEric Paris 			   after the initial policy is loaded and the security
643c9180a57SEric Paris 			   server is ready to handle calls. */
644c9180a57SEric Paris 			goto out;
645c9180a57SEric Paris 		}
646c9180a57SEric Paris 		rc = -EINVAL;
647c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
648744ba35eSEric Paris 			"before the security server is initialized\n");
649c9180a57SEric Paris 		goto out;
650c9180a57SEric Paris 	}
651649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
652649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
653649f6e77SDavid Quigley 		 * place the results is not allowed */
654649f6e77SDavid Quigley 		rc = -EINVAL;
655649f6e77SDavid Quigley 		goto out;
656649f6e77SDavid Quigley 	}
657c9180a57SEric Paris 
658c9180a57SEric Paris 	/*
659e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
660e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
661e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
662e0007529SEric Paris 	 * we need to skip the double mount verification.
663e0007529SEric Paris 	 *
664e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
665e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
666e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
667e0007529SEric Paris 	 * will be used for both mounts)
668e0007529SEric Paris 	 */
6690d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
670bd323655SAl Viro 	    && !opts)
671e0007529SEric Paris 		goto out;
672e0007529SEric Paris 
6732c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
6742c97165bSPaul Moore 
675e0007529SEric Paris 	/*
676c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
677c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
678c9180a57SEric Paris 	 * than once with different security options.
679c9180a57SEric Paris 	 */
680bd323655SAl Viro 	if (opts) {
681bd323655SAl Viro 		if (opts->fscontext) {
682bd323655SAl Viro 			rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
683bd323655SAl Viro 			if (rc)
684c9180a57SEric Paris 				goto out;
685c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
686c9180a57SEric Paris 					fscontext_sid))
687c9180a57SEric Paris 				goto out_double_mount;
688c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
689bd323655SAl Viro 		}
690bd323655SAl Viro 		if (opts->context) {
691bd323655SAl Viro 			rc = parse_sid(sb, opts->context, &context_sid);
692bd323655SAl Viro 			if (rc)
693bd323655SAl Viro 				goto out;
694c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
695c9180a57SEric Paris 					context_sid))
696c9180a57SEric Paris 				goto out_double_mount;
697c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
698bd323655SAl Viro 		}
699bd323655SAl Viro 		if (opts->rootcontext) {
700bd323655SAl Viro 			rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
701bd323655SAl Viro 			if (rc)
702bd323655SAl Viro 				goto out;
703c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
704c9180a57SEric Paris 					rootcontext_sid))
705c9180a57SEric Paris 				goto out_double_mount;
706c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
707bd323655SAl Viro 		}
708bd323655SAl Viro 		if (opts->defcontext) {
709bd323655SAl Viro 			rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
710bd323655SAl Viro 			if (rc)
711bd323655SAl Viro 				goto out;
712c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
713c9180a57SEric Paris 					defcontext_sid))
714c9180a57SEric Paris 				goto out_double_mount;
715c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
716c9180a57SEric Paris 		}
717c9180a57SEric Paris 	}
718c9180a57SEric Paris 
7190d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
720c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
721bd323655SAl Viro 		if ((sbsec->flags & SE_MNTMASK) && !opts)
722c9180a57SEric Paris 			goto out_double_mount;
723c9180a57SEric Paris 		rc = 0;
724c9180a57SEric Paris 		goto out;
725c9180a57SEric Paris 	}
726c9180a57SEric Paris 
727089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
728134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
729134509d5SStephen Smalley 
7308e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
7316a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
732a20456aeSHridya Valsaraju 	    !strcmp(sb->s_type->name, "binder") ||
7334ca54d3dSConnor O'Brien 	    !strcmp(sb->s_type->name, "bpf") ||
734b754026bSOndrej Mosnacek 	    !strcmp(sb->s_type->name, "pstore"))
735b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS;
736b754026bSOndrej Mosnacek 
737b754026bSOndrej Mosnacek 	if (!strcmp(sb->s_type->name, "sysfs") ||
738901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
739901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
740b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
741c9180a57SEric Paris 
742eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
743eb9ae686SDavid Quigley 		/*
744eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
745eb9ae686SDavid Quigley 		 * filesystem type.
746eb9ae686SDavid Quigley 		 */
747aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
748c9180a57SEric Paris 		if (rc) {
749c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
750089be43eSJames Morris 					__func__, sb->s_type->name, rc);
751c9180a57SEric Paris 			goto out;
752c9180a57SEric Paris 		}
753eb9ae686SDavid Quigley 	}
754aad82892SSeth Forshee 
755aad82892SSeth Forshee 	/*
75601593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
75701593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
75801593d32SStephen Smalley 	 * line and security labels must be ignored.
759aad82892SSeth Forshee 	 */
76001593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
76101593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
76201593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
76301593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
764aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
765aad82892SSeth Forshee 		    defcontext_sid) {
766aad82892SSeth Forshee 			rc = -EACCES;
767aad82892SSeth Forshee 			goto out;
768aad82892SSeth Forshee 		}
769aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
770aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
771aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
772aa8e712cSStephen Smalley 						     current_sid(),
773aa8e712cSStephen Smalley 						     current_sid(),
774aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
775aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
776aad82892SSeth Forshee 			if (rc)
777aad82892SSeth Forshee 				goto out;
778aad82892SSeth Forshee 		}
779aad82892SSeth Forshee 		goto out_set_opts;
780aad82892SSeth Forshee 	}
781aad82892SSeth Forshee 
782c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
783c9180a57SEric Paris 	if (fscontext_sid) {
784275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
785c9180a57SEric Paris 		if (rc)
786c9180a57SEric Paris 			goto out;
787c9180a57SEric Paris 
788c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
789c9180a57SEric Paris 	}
790c9180a57SEric Paris 
791c9180a57SEric Paris 	/*
792c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
793c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
794c9180a57SEric Paris 	 * the superblock context if not already set.
795c9180a57SEric Paris 	 */
796eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
797eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
798eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
799eb9ae686SDavid Quigley 	}
800eb9ae686SDavid Quigley 
801c9180a57SEric Paris 	if (context_sid) {
802c9180a57SEric Paris 		if (!fscontext_sid) {
803275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
804275bb41eSDavid Howells 							  cred);
805c9180a57SEric Paris 			if (rc)
806c9180a57SEric Paris 				goto out;
807c9180a57SEric Paris 			sbsec->sid = context_sid;
808c9180a57SEric Paris 		} else {
809275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
810275bb41eSDavid Howells 							     cred);
811c9180a57SEric Paris 			if (rc)
812c9180a57SEric Paris 				goto out;
813c9180a57SEric Paris 		}
814c9180a57SEric Paris 		if (!rootcontext_sid)
815c9180a57SEric Paris 			rootcontext_sid = context_sid;
816c9180a57SEric Paris 
817c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
818c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
819c9180a57SEric Paris 	}
820c9180a57SEric Paris 
821c9180a57SEric Paris 	if (rootcontext_sid) {
822275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
823275bb41eSDavid Howells 						     cred);
824c9180a57SEric Paris 		if (rc)
825c9180a57SEric Paris 			goto out;
826c9180a57SEric Paris 
827c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
8286f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
829c9180a57SEric Paris 	}
830c9180a57SEric Paris 
831c9180a57SEric Paris 	if (defcontext_sid) {
832eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
833eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
834c9180a57SEric Paris 			rc = -EINVAL;
835c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
836c9180a57SEric Paris 			       "invalid for this filesystem type\n");
837c9180a57SEric Paris 			goto out;
838c9180a57SEric Paris 		}
839c9180a57SEric Paris 
840c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
841c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
842275bb41eSDavid Howells 							     sbsec, cred);
843c9180a57SEric Paris 			if (rc)
844c9180a57SEric Paris 				goto out;
845c9180a57SEric Paris 		}
846c9180a57SEric Paris 
847c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
848c9180a57SEric Paris 	}
849c9180a57SEric Paris 
850aad82892SSeth Forshee out_set_opts:
851c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
852c9180a57SEric Paris out:
853bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8541da177e4SLinus Torvalds 	return rc;
855c9180a57SEric Paris out_double_mount:
856c9180a57SEric Paris 	rc = -EINVAL;
857c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
858bd323655SAl Viro 	       "security settings for (dev %s, type %s)\n", sb->s_id,
859bd323655SAl Viro 	       sb->s_type->name);
860c9180a57SEric Paris 	goto out;
861c9180a57SEric Paris }
862c9180a57SEric Paris 
863094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
864094f7b69SJeff Layton 				    const struct super_block *newsb)
865094f7b69SJeff Layton {
866094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
867094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
868094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
869094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
870094f7b69SJeff Layton 
871094f7b69SJeff Layton 	if (oldflags != newflags)
872094f7b69SJeff Layton 		goto mismatch;
873094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
874094f7b69SJeff Layton 		goto mismatch;
875094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
876094f7b69SJeff Layton 		goto mismatch;
877094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
878094f7b69SJeff Layton 		goto mismatch;
879094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
88083da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
88183da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
882094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
883094f7b69SJeff Layton 			goto mismatch;
884094f7b69SJeff Layton 	}
885094f7b69SJeff Layton 	return 0;
886094f7b69SJeff Layton mismatch:
887c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
888094f7b69SJeff Layton 			    "different security settings for (dev %s, "
889094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
890094f7b69SJeff Layton 	return -EBUSY;
891094f7b69SJeff Layton }
892094f7b69SJeff Layton 
893094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
8940b4d3452SScott Mayhew 					struct super_block *newsb,
8950b4d3452SScott Mayhew 					unsigned long kern_flags,
8960b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
897c9180a57SEric Paris {
8980b4d3452SScott Mayhew 	int rc = 0;
899c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
900c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
901c9180a57SEric Paris 
902c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
903c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
904c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
905c9180a57SEric Paris 
9060f5e6420SEric Paris 	/*
9070f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
908e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
9090f5e6420SEric Paris 	 */
91065cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
911094f7b69SJeff Layton 		return 0;
912c9180a57SEric Paris 
9130b4d3452SScott Mayhew 	/*
9140b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
9150b4d3452SScott Mayhew 	 * place the results is not allowed.
9160b4d3452SScott Mayhew 	 */
9170b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
9180b4d3452SScott Mayhew 		return -EINVAL;
9190b4d3452SScott Mayhew 
920c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
9210d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
922c9180a57SEric Paris 
923094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
9243815a245SJ. Bruce Fields 	if (newsbsec->flags & SE_SBINITIALIZED) {
9253815a245SJ. Bruce Fields 		if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
9263815a245SJ. Bruce Fields 			*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
927094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
9283815a245SJ. Bruce Fields 	}
9295a552617SEric Paris 
930c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
931c9180a57SEric Paris 
932c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
933c9180a57SEric Paris 
934c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
935c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
936c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
937c9180a57SEric Paris 
9380b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
9390b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
940aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
9410b4d3452SScott Mayhew 		if (rc)
9420b4d3452SScott Mayhew 			goto out;
9430b4d3452SScott Mayhew 	}
9440b4d3452SScott Mayhew 
9450b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
9460b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
9470b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
9480b4d3452SScott Mayhew 	}
9490b4d3452SScott Mayhew 
950c9180a57SEric Paris 	if (set_context) {
951c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
952c9180a57SEric Paris 
953c9180a57SEric Paris 		if (!set_fscontext)
954c9180a57SEric Paris 			newsbsec->sid = sid;
955c9180a57SEric Paris 		if (!set_rootcontext) {
95683da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
957c9180a57SEric Paris 			newisec->sid = sid;
958c9180a57SEric Paris 		}
959c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
960c9180a57SEric Paris 	}
961c9180a57SEric Paris 	if (set_rootcontext) {
96283da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
96383da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
964c9180a57SEric Paris 
965c9180a57SEric Paris 		newisec->sid = oldisec->sid;
966c9180a57SEric Paris 	}
967c9180a57SEric Paris 
968c9180a57SEric Paris 	sb_finish_set_opts(newsb);
9690b4d3452SScott Mayhew out:
970c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
9710b4d3452SScott Mayhew 	return rc;
972c9180a57SEric Paris }
973c9180a57SEric Paris 
974ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts)
975c9180a57SEric Paris {
976ba641862SAl Viro 	struct selinux_mnt_opts *opts = *mnt_opts;
977c9180a57SEric Paris 
978da3d76abSAl Viro 	if (token == Opt_seclabel)	/* eaten and completely ignored */
979e0007529SEric Paris 		return 0;
980e0007529SEric Paris 
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;
986ba641862SAl Viro 	}
987ba641862SAl Viro 	if (!s)
988ba641862SAl Viro 		return -ENOMEM;
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:
1013ba641862SAl Viro 	pr_warn(SEL_MOUNT_FAIL_MSG);
1014ba641862SAl Viro 	return -EINVAL;
1015ba641862SAl Viro }
1016ba641862SAl Viro 
1017757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1018204cc0ccSAl Viro 			       void **mnt_opts)
1019c9180a57SEric Paris {
1020757cbe59SAl Viro 	int token = Opt_error;
1021757cbe59SAl Viro 	int rc, i;
1022c9180a57SEric Paris 
1023757cbe59SAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1024757cbe59SAl Viro 		if (strcmp(option, tokens[i].name) == 0) {
1025757cbe59SAl Viro 			token = tokens[i].opt;
1026757cbe59SAl Viro 			break;
1027757cbe59SAl Viro 		}
1028169d68efSAl Viro 	}
1029169d68efSAl Viro 
1030757cbe59SAl Viro 	if (token == Opt_error)
1031757cbe59SAl Viro 		return -EINVAL;
1032c9180a57SEric Paris 
1033e2e0e097SGen Zhang 	if (token != Opt_seclabel) {
1034757cbe59SAl Viro 		val = kmemdup_nul(val, len, GFP_KERNEL);
1035e2e0e097SGen Zhang 		if (!val) {
1036e2e0e097SGen Zhang 			rc = -ENOMEM;
1037e2e0e097SGen Zhang 			goto free_opt;
1038e2e0e097SGen Zhang 		}
1039e2e0e097SGen Zhang 	}
1040757cbe59SAl Viro 	rc = selinux_add_opt(token, val, mnt_opts);
1041757cbe59SAl Viro 	if (unlikely(rc)) {
1042757cbe59SAl Viro 		kfree(val);
1043e2e0e097SGen Zhang 		goto free_opt;
1044e2e0e097SGen Zhang 	}
1045e2e0e097SGen Zhang 	return rc;
1046e2e0e097SGen Zhang 
1047e2e0e097SGen Zhang free_opt:
1048757cbe59SAl Viro 	if (*mnt_opts) {
1049ba641862SAl Viro 		selinux_free_mnt_opts(*mnt_opts);
1050ba641862SAl Viro 		*mnt_opts = NULL;
1051757cbe59SAl Viro 	}
1052c9180a57SEric Paris 	return rc;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
1055e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid)
10562069f457SEric Paris {
1057e3489f89SAl Viro 	char *context = NULL;
1058e3489f89SAl Viro 	u32 len;
1059e3489f89SAl Viro 	int rc;
10602069f457SEric Paris 
1061e3489f89SAl Viro 	rc = security_sid_to_context(&selinux_state, sid,
1062e3489f89SAl Viro 					     &context, &len);
1063e3489f89SAl Viro 	if (!rc) {
1064e3489f89SAl Viro 		bool has_comma = context && strchr(context, ',');
106511689d47SDavid P. Quigley 
1066442155c1SDavid Howells 		seq_putc(m, '=');
10672069f457SEric Paris 		if (has_comma)
10682069f457SEric Paris 			seq_putc(m, '\"');
1069e3489f89SAl Viro 		seq_escape(m, context, "\"\n\\");
10702069f457SEric Paris 		if (has_comma)
10712069f457SEric Paris 			seq_putc(m, '\"');
10722069f457SEric Paris 	}
10731da177e4SLinus Torvalds 	kfree(context);
10741da177e4SLinus Torvalds 	return rc;
10751da177e4SLinus Torvalds }
10762069f457SEric Paris 
10772069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10782069f457SEric Paris {
1079e3489f89SAl Viro 	struct superblock_security_struct *sbsec = sb->s_security;
10802069f457SEric Paris 	int rc;
10812069f457SEric Paris 
1082e3489f89SAl Viro 	if (!(sbsec->flags & SE_SBINITIALIZED))
1083e3489f89SAl Viro 		return 0;
1084e3489f89SAl Viro 
108565cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
1086e3489f89SAl Viro 		return 0;
1087e3489f89SAl Viro 
1088e3489f89SAl Viro 	if (sbsec->flags & FSCONTEXT_MNT) {
1089e3489f89SAl Viro 		seq_putc(m, ',');
1090e3489f89SAl Viro 		seq_puts(m, FSCONTEXT_STR);
1091e3489f89SAl Viro 		rc = show_sid(m, sbsec->sid);
1092e3489f89SAl Viro 		if (rc)
10932069f457SEric Paris 			return rc;
1094383795c2SEric Paris 	}
1095e3489f89SAl Viro 	if (sbsec->flags & CONTEXT_MNT) {
1096e3489f89SAl Viro 		seq_putc(m, ',');
1097e3489f89SAl Viro 		seq_puts(m, CONTEXT_STR);
1098e3489f89SAl Viro 		rc = show_sid(m, sbsec->mntpoint_sid);
1099e3489f89SAl Viro 		if (rc)
11002069f457SEric Paris 			return rc;
11012069f457SEric Paris 	}
1102e3489f89SAl Viro 	if (sbsec->flags & DEFCONTEXT_MNT) {
1103e3489f89SAl Viro 		seq_putc(m, ',');
1104e3489f89SAl Viro 		seq_puts(m, DEFCONTEXT_STR);
1105e3489f89SAl Viro 		rc = show_sid(m, sbsec->def_sid);
1106e3489f89SAl Viro 		if (rc)
1107e3489f89SAl Viro 			return rc;
1108e3489f89SAl Viro 	}
1109e3489f89SAl Viro 	if (sbsec->flags & ROOTCONTEXT_MNT) {
1110b159e86bSOndrej Mosnacek 		struct dentry *root = sb->s_root;
1111e3489f89SAl Viro 		struct inode_security_struct *isec = backing_inode_security(root);
1112e3489f89SAl Viro 		seq_putc(m, ',');
1113e3489f89SAl Viro 		seq_puts(m, ROOTCONTEXT_STR);
1114e3489f89SAl Viro 		rc = show_sid(m, isec->sid);
1115e3489f89SAl Viro 		if (rc)
1116e3489f89SAl Viro 			return rc;
1117e3489f89SAl Viro 	}
1118e3489f89SAl Viro 	if (sbsec->flags & SBLABEL_MNT) {
1119e3489f89SAl Viro 		seq_putc(m, ',');
1120442155c1SDavid Howells 		seq_puts(m, SECLABEL_STR);
1121e3489f89SAl Viro 	}
1122e3489f89SAl Viro 	return 0;
1123e3489f89SAl Viro }
11242069f457SEric Paris 
11251da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11281da177e4SLinus Torvalds 	case S_IFSOCK:
11291da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11301da177e4SLinus Torvalds 	case S_IFLNK:
11311da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11321da177e4SLinus Torvalds 	case S_IFREG:
11331da177e4SLinus Torvalds 		return SECCLASS_FILE;
11341da177e4SLinus Torvalds 	case S_IFBLK:
11351da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11361da177e4SLinus Torvalds 	case S_IFDIR:
11371da177e4SLinus Torvalds 		return SECCLASS_DIR;
11381da177e4SLinus Torvalds 	case S_IFCHR:
11391da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11401da177e4SLinus Torvalds 	case S_IFIFO:
11411da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	return SECCLASS_FILE;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
114813402580SJames Morris static inline int default_protocol_stream(int protocol)
114913402580SJames Morris {
115095ca9072SPaolo Abeni 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
115195ca9072SPaolo Abeni 		protocol == IPPROTO_MPTCP);
115213402580SJames Morris }
115313402580SJames Morris 
115413402580SJames Morris static inline int default_protocol_dgram(int protocol)
115513402580SJames Morris {
115613402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
115713402580SJames Morris }
115813402580SJames Morris 
11591da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11601da177e4SLinus Torvalds {
1161aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1162da69a530SStephen Smalley 
11631da177e4SLinus Torvalds 	switch (family) {
11641da177e4SLinus Torvalds 	case PF_UNIX:
11651da177e4SLinus Torvalds 		switch (type) {
11661da177e4SLinus Torvalds 		case SOCK_STREAM:
11671da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11681da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11691da177e4SLinus Torvalds 		case SOCK_DGRAM:
11702a764b52SLuis Ressel 		case SOCK_RAW:
11711da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 		break;
11741da177e4SLinus Torvalds 	case PF_INET:
11751da177e4SLinus Torvalds 	case PF_INET6:
11761da177e4SLinus Torvalds 		switch (type) {
11771da177e4SLinus Torvalds 		case SOCK_STREAM:
1178da69a530SStephen Smalley 		case SOCK_SEQPACKET:
117913402580SJames Morris 			if (default_protocol_stream(protocol))
11801da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1181da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1182da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
118313402580SJames Morris 			else
118413402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11851da177e4SLinus Torvalds 		case SOCK_DGRAM:
118613402580SJames Morris 			if (default_protocol_dgram(protocol))
11871da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1188ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1189ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1190da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
119113402580SJames Morris 			else
119213402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11932ee92d46SJames Morris 		case SOCK_DCCP:
11942ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
119513402580SJames Morris 		default:
11961da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 		break;
11991da177e4SLinus Torvalds 	case PF_NETLINK:
12001da177e4SLinus Torvalds 		switch (protocol) {
12011da177e4SLinus Torvalds 		case NETLINK_ROUTE:
12021da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
12037f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
12041da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
12051da177e4SLinus Torvalds 		case NETLINK_NFLOG:
12061da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
12071da177e4SLinus Torvalds 		case NETLINK_XFRM:
12081da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
12091da177e4SLinus Torvalds 		case NETLINK_SELINUX:
12101da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
12116c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
12126c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
12131da177e4SLinus Torvalds 		case NETLINK_AUDIT:
12141da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
12156c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
12166c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
12176c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
12186c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
12196c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
12206c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
12211da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12221da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12230c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12240c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12256c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
12266c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
12276c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
12286c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
12296c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
12306c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
12316c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
12326c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
12331da177e4SLinus Torvalds 		default:
12341da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12351da177e4SLinus Torvalds 		}
12361da177e4SLinus Torvalds 	case PF_PACKET:
12371da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12381da177e4SLinus Torvalds 	case PF_KEY:
12391da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12403e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12413e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
1244da69a530SStephen Smalley 	if (extsockclass) {
1245da69a530SStephen Smalley 		switch (family) {
1246da69a530SStephen Smalley 		case PF_AX25:
1247da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1248da69a530SStephen Smalley 		case PF_IPX:
1249da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1250da69a530SStephen Smalley 		case PF_NETROM:
1251da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1252da69a530SStephen Smalley 		case PF_ATMPVC:
1253da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1254da69a530SStephen Smalley 		case PF_X25:
1255da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1256da69a530SStephen Smalley 		case PF_ROSE:
1257da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1258da69a530SStephen Smalley 		case PF_DECnet:
1259da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1260da69a530SStephen Smalley 		case PF_ATMSVC:
1261da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1262da69a530SStephen Smalley 		case PF_RDS:
1263da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1264da69a530SStephen Smalley 		case PF_IRDA:
1265da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1266da69a530SStephen Smalley 		case PF_PPPOX:
1267da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1268da69a530SStephen Smalley 		case PF_LLC:
1269da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1270da69a530SStephen Smalley 		case PF_CAN:
1271da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1272da69a530SStephen Smalley 		case PF_TIPC:
1273da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1274da69a530SStephen Smalley 		case PF_BLUETOOTH:
1275da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1276da69a530SStephen Smalley 		case PF_IUCV:
1277da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1278da69a530SStephen Smalley 		case PF_RXRPC:
1279da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1280da69a530SStephen Smalley 		case PF_ISDN:
1281da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1282da69a530SStephen Smalley 		case PF_PHONET:
1283da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1284da69a530SStephen Smalley 		case PF_IEEE802154:
1285da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1286da69a530SStephen Smalley 		case PF_CAIF:
1287da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1288da69a530SStephen Smalley 		case PF_ALG:
1289da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1290da69a530SStephen Smalley 		case PF_NFC:
1291da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1292da69a530SStephen Smalley 		case PF_VSOCK:
1293da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1294da69a530SStephen Smalley 		case PF_KCM:
1295da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1296da69a530SStephen Smalley 		case PF_QIPCRTR:
1297da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
12983051bf36SLinus Torvalds 		case PF_SMC:
12993051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
130068e8b849SBjörn Töpel 		case PF_XDP:
130168e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
130268e8b849SBjörn Töpel #if PF_MAX > 45
1303da69a530SStephen Smalley #error New address family defined, please update this function.
1304da69a530SStephen Smalley #endif
1305da69a530SStephen Smalley 		}
1306da69a530SStephen Smalley 	}
1307da69a530SStephen Smalley 
13081da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
1311134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
13121da177e4SLinus Torvalds 				 u16 tclass,
1313134509d5SStephen Smalley 				 u16 flags,
13141da177e4SLinus Torvalds 				 u32 *sid)
13151da177e4SLinus Torvalds {
13168e6c9693SLucian Adrian Grijincu 	int rc;
1317fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
13188e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
13211da177e4SLinus Torvalds 	if (!buffer)
13221da177e4SLinus Torvalds 		return -ENOMEM;
13231da177e4SLinus Torvalds 
13248e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
13258e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
13268e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
13278e6c9693SLucian Adrian Grijincu 	else {
1328134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
13298e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
13308e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
13318e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
13328e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
13338e6c9693SLucian Adrian Grijincu 				path[1] = '/';
13348e6c9693SLucian Adrian Grijincu 				path++;
13351da177e4SLinus Torvalds 			}
1336134509d5SStephen Smalley 		}
1337aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1338aa8e712cSStephen Smalley 					path, tclass, sid);
13397bb185edSStephen Smalley 		if (rc == -ENOENT) {
13407bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
13417bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
13427bb185edSStephen Smalley 			rc = 0;
13437bb185edSStephen Smalley 		}
13448e6c9693SLucian Adrian Grijincu 	}
13451da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
13461da177e4SLinus Torvalds 	return rc;
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
1349b754026bSOndrej Mosnacek static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1350b754026bSOndrej Mosnacek 				  u32 def_sid, u32 *sid)
1351b754026bSOndrej Mosnacek {
1352b754026bSOndrej Mosnacek #define INITCONTEXTLEN 255
1353b754026bSOndrej Mosnacek 	char *context;
1354b754026bSOndrej Mosnacek 	unsigned int len;
1355b754026bSOndrej Mosnacek 	int rc;
1356b754026bSOndrej Mosnacek 
1357b754026bSOndrej Mosnacek 	len = INITCONTEXTLEN;
1358b754026bSOndrej Mosnacek 	context = kmalloc(len + 1, GFP_NOFS);
1359b754026bSOndrej Mosnacek 	if (!context)
1360b754026bSOndrej Mosnacek 		return -ENOMEM;
1361b754026bSOndrej Mosnacek 
1362b754026bSOndrej Mosnacek 	context[len] = '\0';
1363b754026bSOndrej Mosnacek 	rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1364b754026bSOndrej Mosnacek 	if (rc == -ERANGE) {
1365b754026bSOndrej Mosnacek 		kfree(context);
1366b754026bSOndrej Mosnacek 
1367b754026bSOndrej Mosnacek 		/* Need a larger buffer.  Query for the right size. */
1368b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1369b754026bSOndrej Mosnacek 		if (rc < 0)
1370b754026bSOndrej Mosnacek 			return rc;
1371b754026bSOndrej Mosnacek 
1372b754026bSOndrej Mosnacek 		len = rc;
1373b754026bSOndrej Mosnacek 		context = kmalloc(len + 1, GFP_NOFS);
1374b754026bSOndrej Mosnacek 		if (!context)
1375b754026bSOndrej Mosnacek 			return -ENOMEM;
1376b754026bSOndrej Mosnacek 
1377b754026bSOndrej Mosnacek 		context[len] = '\0';
1378b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1379b754026bSOndrej Mosnacek 				    context, len);
1380b754026bSOndrej Mosnacek 	}
1381b754026bSOndrej Mosnacek 	if (rc < 0) {
1382b754026bSOndrej Mosnacek 		kfree(context);
1383b754026bSOndrej Mosnacek 		if (rc != -ENODATA) {
1384b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  getxattr returned %d for dev=%s ino=%ld\n",
1385b754026bSOndrej Mosnacek 				__func__, -rc, inode->i_sb->s_id, inode->i_ino);
1386b754026bSOndrej Mosnacek 			return rc;
1387b754026bSOndrej Mosnacek 		}
1388b754026bSOndrej Mosnacek 		*sid = def_sid;
1389b754026bSOndrej Mosnacek 		return 0;
1390b754026bSOndrej Mosnacek 	}
1391b754026bSOndrej Mosnacek 
1392b754026bSOndrej Mosnacek 	rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1393b754026bSOndrej Mosnacek 					     def_sid, GFP_NOFS);
1394b754026bSOndrej Mosnacek 	if (rc) {
1395b754026bSOndrej Mosnacek 		char *dev = inode->i_sb->s_id;
1396b754026bSOndrej Mosnacek 		unsigned long ino = inode->i_ino;
1397b754026bSOndrej Mosnacek 
1398b754026bSOndrej Mosnacek 		if (rc == -EINVAL) {
1399b754026bSOndrej 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",
1400b754026bSOndrej Mosnacek 					      ino, dev, context);
1401b754026bSOndrej Mosnacek 		} else {
1402b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1403b754026bSOndrej Mosnacek 				__func__, context, -rc, dev, ino);
1404b754026bSOndrej Mosnacek 		}
1405b754026bSOndrej Mosnacek 	}
1406b754026bSOndrej Mosnacek 	kfree(context);
1407b754026bSOndrej Mosnacek 	return 0;
1408b754026bSOndrej Mosnacek }
1409b754026bSOndrej Mosnacek 
14101da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
14111da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
141480788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
14159287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
14169287aed2SAndreas Gruenbacher 	u16 sclass;
14171da177e4SLinus Torvalds 	struct dentry *dentry;
14181da177e4SLinus Torvalds 	int rc = 0;
14191da177e4SLinus Torvalds 
14206f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
142113457d07SAndreas Gruenbacher 		return 0;
14221da177e4SLinus Torvalds 
14239287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
14246f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
142523970741SEric Paris 		goto out_unlock;
14261da177e4SLinus Torvalds 
142713457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
142813457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
142913457d07SAndreas Gruenbacher 
14301da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
14310d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
14321da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
14331da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
14341da177e4SLinus Torvalds 		   server is ready to handle calls. */
14351da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
14361da177e4SLinus Torvalds 		if (list_empty(&isec->list))
14371da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
14381da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
143923970741SEric Paris 		goto out_unlock;
14401da177e4SLinus Torvalds 	}
14411da177e4SLinus Torvalds 
14429287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
14439287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
14449287aed2SAndreas Gruenbacher 	sid = isec->sid;
14459287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
14469287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
14479287aed2SAndreas Gruenbacher 
14481da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1449eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1450eb9ae686SDavid Quigley 		break;
14511da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
14525d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
14539287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
14541da177e4SLinus Torvalds 			break;
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
14571da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
14581da177e4SLinus Torvalds 		if (opt_dentry) {
14591da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
14601da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
14611da177e4SLinus Torvalds 		} else {
1462b127125dSAl Viro 			/*
1463b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1464b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1465b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1466b127125dSAl Viro 			 * two, depending upon that...
1467b127125dSAl Viro 			 */
14681da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1469b127125dSAl Viro 			if (!dentry)
1470b127125dSAl Viro 				dentry = d_find_any_alias(inode);
14711da177e4SLinus Torvalds 		}
14721da177e4SLinus Torvalds 		if (!dentry) {
1473df7f54c0SEric Paris 			/*
1474df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1475df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1476df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1477df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1478df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1479df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1480df7f54c0SEric Paris 			 * be used again by userspace.
1481df7f54c0SEric Paris 			 */
1482200ea5a2SPaul Moore 			goto out_invalid;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 
1485b754026bSOndrej Mosnacek 		rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1486b754026bSOndrej Mosnacek 					    &sid);
14871da177e4SLinus Torvalds 		dput(dentry);
1488b754026bSOndrej Mosnacek 		if (rc)
14899287aed2SAndreas Gruenbacher 			goto out;
14901da177e4SLinus Torvalds 		break;
14911da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
14929287aed2SAndreas Gruenbacher 		sid = task_sid;
14931da177e4SLinus Torvalds 		break;
14941da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
14951da177e4SLinus Torvalds 		/* Default to the fs SID. */
14969287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1499aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1500aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
15011da177e4SLinus Torvalds 		if (rc)
15029287aed2SAndreas Gruenbacher 			goto out;
15031da177e4SLinus Torvalds 		break;
1504c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
15059287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1506c312feb2SEric Paris 		break;
15071da177e4SLinus Torvalds 	default:
1508c312feb2SEric Paris 		/* Default to the fs superblock SID. */
15099287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
15101da177e4SLinus Torvalds 
15117470d0d1SChristian Göttsche 		if ((sbsec->flags & SE_SBGENFS) &&
15127470d0d1SChristian Göttsche 		     (!S_ISLNK(inode->i_mode) ||
15137470d0d1SChristian Göttsche 		      selinux_policycap_genfs_seclabel_symlinks())) {
1514f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1515f64410ecSPaul Moore 			 * procfs inodes */
1516b127125dSAl Viro 			if (opt_dentry) {
1517f64410ecSPaul Moore 				/* Called from d_instantiate or
1518f64410ecSPaul Moore 				 * d_splice_alias. */
1519f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1520b127125dSAl Viro 			} else {
1521f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1522b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1523b127125dSAl Viro 				 * a connected one, so try that first.
1524b127125dSAl Viro 				 */
1525f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1526b127125dSAl Viro 				if (!dentry)
1527b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1528b127125dSAl Viro 			}
1529f64410ecSPaul Moore 			/*
1530f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1531f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1532f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1533f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1534f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1535f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1536f64410ecSPaul Moore 			 * could be used again by userspace.
1537f64410ecSPaul Moore 			 */
1538f64410ecSPaul Moore 			if (!dentry)
1539200ea5a2SPaul Moore 				goto out_invalid;
15409287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1541134509d5SStephen Smalley 						   sbsec->flags, &sid);
1542b754026bSOndrej Mosnacek 			if (rc) {
1543f64410ecSPaul Moore 				dput(dentry);
15449287aed2SAndreas Gruenbacher 				goto out;
15451da177e4SLinus Torvalds 			}
1546b754026bSOndrej Mosnacek 
1547b754026bSOndrej Mosnacek 			if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1548b754026bSOndrej Mosnacek 			    (inode->i_opflags & IOP_XATTR)) {
1549b754026bSOndrej Mosnacek 				rc = inode_doinit_use_xattr(inode, dentry,
1550b754026bSOndrej Mosnacek 							    sid, &sid);
1551b754026bSOndrej Mosnacek 				if (rc) {
1552b754026bSOndrej Mosnacek 					dput(dentry);
1553b754026bSOndrej Mosnacek 					goto out;
1554b754026bSOndrej Mosnacek 				}
1555b754026bSOndrej Mosnacek 			}
1556b754026bSOndrej Mosnacek 			dput(dentry);
1557b754026bSOndrej Mosnacek 		}
15581da177e4SLinus Torvalds 		break;
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 
15619287aed2SAndreas Gruenbacher out:
15629287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15639287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
1564200ea5a2SPaul Moore 		if (rc) {
15659287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
15669287aed2SAndreas Gruenbacher 			goto out_unlock;
15679287aed2SAndreas Gruenbacher 		}
15686f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
15699287aed2SAndreas Gruenbacher 		isec->sid = sid;
15709287aed2SAndreas Gruenbacher 	}
15711da177e4SLinus Torvalds 
157223970741SEric Paris out_unlock:
15739287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15741da177e4SLinus Torvalds 	return rc;
1575200ea5a2SPaul Moore 
1576200ea5a2SPaul Moore out_invalid:
1577200ea5a2SPaul Moore 	spin_lock(&isec->lock);
1578200ea5a2SPaul Moore 	if (isec->initialized == LABEL_PENDING) {
1579200ea5a2SPaul Moore 		isec->initialized = LABEL_INVALID;
1580200ea5a2SPaul Moore 		isec->sid = sid;
1581200ea5a2SPaul Moore 	}
1582200ea5a2SPaul Moore 	spin_unlock(&isec->lock);
1583200ea5a2SPaul Moore 	return 0;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
15871da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	u32 perm = 0;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	switch (sig) {
15921da177e4SLinus Torvalds 	case SIGCHLD:
15931da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
15941da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
15951da177e4SLinus Torvalds 		break;
15961da177e4SLinus Torvalds 	case SIGKILL:
15971da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
15981da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
15991da177e4SLinus Torvalds 		break;
16001da177e4SLinus Torvalds 	case SIGSTOP:
16011da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
16021da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
16031da177e4SLinus Torvalds 		break;
16041da177e4SLinus Torvalds 	default:
16051da177e4SLinus Torvalds 		/* All other signals. */
16061da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
16071da177e4SLinus Torvalds 		break;
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	return perm;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
1613b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1614b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1615b68e418cSStephen Smalley #endif
1616b68e418cSStephen Smalley 
16171da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
16186a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
1619c1a85a00SMicah Morton 			       int cap, unsigned int opts, bool initns)
16201da177e4SLinus Torvalds {
16212bf49690SThomas Liu 	struct common_audit_data ad;
162206112163SEric Paris 	struct av_decision avd;
1623b68e418cSStephen Smalley 	u16 sclass;
16243699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1625b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
162606112163SEric Paris 	int rc;
16271da177e4SLinus Torvalds 
162850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
16291da177e4SLinus Torvalds 	ad.u.cap = cap;
16301da177e4SLinus Torvalds 
1631b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1632b68e418cSStephen Smalley 	case 0:
16338e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1634b68e418cSStephen Smalley 		break;
1635b68e418cSStephen Smalley 	case 1:
16368e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1637b68e418cSStephen Smalley 		break;
1638b68e418cSStephen Smalley 	default:
1639c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1640b68e418cSStephen Smalley 		BUG();
1641a35c6c83SEric Paris 		return -EINVAL;
1642b68e418cSStephen Smalley 	}
164306112163SEric Paris 
16446b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
16456b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
1646c1a85a00SMicah Morton 	if (!(opts & CAP_OPT_NOAUDIT)) {
16476b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
16486b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
16499ade0cf4SEric Paris 		if (rc2)
16509ade0cf4SEric Paris 			return rc2;
16519ade0cf4SEric Paris 	}
165206112163SEric Paris 	return rc;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
16561da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
16571da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
165888e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
16591da177e4SLinus Torvalds 			  struct inode *inode,
16601da177e4SLinus Torvalds 			  u32 perms,
166119e49834SLinus Torvalds 			  struct common_audit_data *adp)
16621da177e4SLinus Torvalds {
16631da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1664275bb41eSDavid Howells 	u32 sid;
16651da177e4SLinus Torvalds 
1666e0e81739SDavid Howells 	validate_creds(cred);
1667e0e81739SDavid Howells 
1668bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1669bbaca6c2SStephen Smalley 		return 0;
1670bbaca6c2SStephen Smalley 
167188e67f3bSDavid Howells 	sid = cred_sid(cred);
167280788c22SCasey Schaufler 	isec = selinux_inode(inode);
16731da177e4SLinus Torvalds 
16746b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
16756b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
16761da177e4SLinus Torvalds }
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16791da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16801da177e4SLinus Torvalds    pathname if needed. */
168188e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16821da177e4SLinus Torvalds 				  struct dentry *dentry,
16831da177e4SLinus Torvalds 				  u32 av)
16841da177e4SLinus Torvalds {
1685c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16862bf49690SThomas Liu 	struct common_audit_data ad;
168788e67f3bSDavid Howells 
168850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16892875fa00SEric Paris 	ad.u.dentry = dentry;
16905d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
169119e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16922875fa00SEric Paris }
16932875fa00SEric Paris 
16942875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16952875fa00SEric Paris    the path to help the auditing code to more easily generate the
16962875fa00SEric Paris    pathname if needed. */
16972875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
16983f7036a0SAl Viro 				const struct path *path,
16992875fa00SEric Paris 				u32 av)
17002875fa00SEric Paris {
1701c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
17022875fa00SEric Paris 	struct common_audit_data ad;
17032875fa00SEric Paris 
170450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
17052875fa00SEric Paris 	ad.u.path = *path;
17065d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
170719e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
171013f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
171113f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
171213f8e981SDavid Howells 				     struct file *file,
171313f8e981SDavid Howells 				     u32 av)
171413f8e981SDavid Howells {
171513f8e981SDavid Howells 	struct common_audit_data ad;
171613f8e981SDavid Howells 
171743af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
171843af5de7SVivek Goyal 	ad.u.file = file;
171919e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
172013f8e981SDavid Howells }
172113f8e981SDavid Howells 
1722f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1723f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1724f66e448cSChenbo Feng #endif
1725f66e448cSChenbo Feng 
17261da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
17271da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
17281da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
17291da177e4SLinus Torvalds    check a particular permission to the file.
17301da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
17311da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
17321da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
17331da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
173488e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
17351da177e4SLinus Torvalds 			 struct file *file,
17361da177e4SLinus Torvalds 			 u32 av)
17371da177e4SLinus Torvalds {
1738bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
1739496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17402bf49690SThomas Liu 	struct common_audit_data ad;
174188e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17421da177e4SLinus Torvalds 	int rc;
17431da177e4SLinus Torvalds 
174443af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
174543af5de7SVivek Goyal 	ad.u.file = file;
17461da177e4SLinus Torvalds 
1747275bb41eSDavid Howells 	if (sid != fsec->sid) {
17486b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
17496b6bc620SStephen Smalley 				  sid, fsec->sid,
17501da177e4SLinus Torvalds 				  SECCLASS_FD,
17511da177e4SLinus Torvalds 				  FD__USE,
17521da177e4SLinus Torvalds 				  &ad);
17531da177e4SLinus Torvalds 		if (rc)
175488e67f3bSDavid Howells 			goto out;
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 
1757f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1758f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1759f66e448cSChenbo Feng 	if (rc)
1760f66e448cSChenbo Feng 		return rc;
1761f66e448cSChenbo Feng #endif
1762f66e448cSChenbo Feng 
17631da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
176488e67f3bSDavid Howells 	rc = 0;
17651da177e4SLinus Torvalds 	if (av)
176619e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
17671da177e4SLinus Torvalds 
176888e67f3bSDavid Howells out:
176988e67f3bSDavid Howells 	return rc;
17701da177e4SLinus Torvalds }
17711da177e4SLinus Torvalds 
1772c3c188b2SDavid Howells /*
1773c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1774c3c188b2SDavid Howells  */
1775c957f6dfSVivek Goyal static int
1776c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1777c957f6dfSVivek Goyal 				 struct inode *dir,
1778c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1779c3c188b2SDavid Howells 				 u32 *_new_isid)
1780c3c188b2SDavid Howells {
1781c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1782c3c188b2SDavid Howells 
1783c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1784c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1785c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1786c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1787c3c188b2SDavid Howells 		   tsec->create_sid) {
1788c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1789c3c188b2SDavid Howells 	} else {
179020cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1791aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1792aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1793c3c188b2SDavid Howells 					       name, _new_isid);
1794c3c188b2SDavid Howells 	}
1795c3c188b2SDavid Howells 
1796c3c188b2SDavid Howells 	return 0;
1797c3c188b2SDavid Howells }
1798c3c188b2SDavid Howells 
17991da177e4SLinus Torvalds /* Check whether a task can create a file. */
18001da177e4SLinus Torvalds static int may_create(struct inode *dir,
18011da177e4SLinus Torvalds 		      struct dentry *dentry,
18021da177e4SLinus Torvalds 		      u16 tclass)
18031da177e4SLinus Torvalds {
18040c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
18051da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
18061da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1807275bb41eSDavid Howells 	u32 sid, newsid;
18082bf49690SThomas Liu 	struct common_audit_data ad;
18091da177e4SLinus Torvalds 	int rc;
18101da177e4SLinus Torvalds 
181183da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
18121da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
18131da177e4SLinus Torvalds 
1814275bb41eSDavid Howells 	sid = tsec->sid;
1815275bb41eSDavid Howells 
181650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1817a269434dSEric Paris 	ad.u.dentry = dentry;
18181da177e4SLinus Torvalds 
18196b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18206b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
18211da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
18221da177e4SLinus Torvalds 			  &ad);
18231da177e4SLinus Torvalds 	if (rc)
18241da177e4SLinus Torvalds 		return rc;
18251da177e4SLinus Torvalds 
1826210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1827210a2928SYang Guo 					   &newsid);
18281da177e4SLinus Torvalds 	if (rc)
18291da177e4SLinus Torvalds 		return rc;
18301da177e4SLinus Torvalds 
18316b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18326b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
18331da177e4SLinus Torvalds 	if (rc)
18341da177e4SLinus Torvalds 		return rc;
18351da177e4SLinus Torvalds 
18366b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18376b6bc620SStephen Smalley 			    newsid, sbsec->sid,
18381da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
18391da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds #define MAY_LINK	0
18431da177e4SLinus Torvalds #define MAY_UNLINK	1
18441da177e4SLinus Torvalds #define MAY_RMDIR	2
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
18471da177e4SLinus Torvalds static int may_link(struct inode *dir,
18481da177e4SLinus Torvalds 		    struct dentry *dentry,
18491da177e4SLinus Torvalds 		    int kind)
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds {
18521da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
18532bf49690SThomas Liu 	struct common_audit_data ad;
1854275bb41eSDavid Howells 	u32 sid = current_sid();
18551da177e4SLinus Torvalds 	u32 av;
18561da177e4SLinus Torvalds 	int rc;
18571da177e4SLinus Torvalds 
185883da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
185983da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
18601da177e4SLinus Torvalds 
186150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1862a269434dSEric Paris 	ad.u.dentry = dentry;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	av = DIR__SEARCH;
18651da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
18666b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18676b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
18681da177e4SLinus Torvalds 	if (rc)
18691da177e4SLinus Torvalds 		return rc;
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	switch (kind) {
18721da177e4SLinus Torvalds 	case MAY_LINK:
18731da177e4SLinus Torvalds 		av = FILE__LINK;
18741da177e4SLinus Torvalds 		break;
18751da177e4SLinus Torvalds 	case MAY_UNLINK:
18761da177e4SLinus Torvalds 		av = FILE__UNLINK;
18771da177e4SLinus Torvalds 		break;
18781da177e4SLinus Torvalds 	case MAY_RMDIR:
18791da177e4SLinus Torvalds 		av = DIR__RMDIR;
18801da177e4SLinus Torvalds 		break;
18811da177e4SLinus Torvalds 	default:
1882c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
1883744ba35eSEric Paris 			__func__, kind);
18841da177e4SLinus Torvalds 		return 0;
18851da177e4SLinus Torvalds 	}
18861da177e4SLinus Torvalds 
18876b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18886b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
18891da177e4SLinus Torvalds 	return rc;
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
18931da177e4SLinus Torvalds 			     struct dentry *old_dentry,
18941da177e4SLinus Torvalds 			     struct inode *new_dir,
18951da177e4SLinus Torvalds 			     struct dentry *new_dentry)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
18982bf49690SThomas Liu 	struct common_audit_data ad;
1899275bb41eSDavid Howells 	u32 sid = current_sid();
19001da177e4SLinus Torvalds 	u32 av;
19011da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
19021da177e4SLinus Torvalds 	int rc;
19031da177e4SLinus Torvalds 
190483da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
190583da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
1906e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
190783da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
19081da177e4SLinus Torvalds 
190950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
19101da177e4SLinus Torvalds 
1911a269434dSEric Paris 	ad.u.dentry = old_dentry;
19126b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19136b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
19141da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
19151da177e4SLinus Torvalds 	if (rc)
19161da177e4SLinus Torvalds 		return rc;
19176b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19186b6bc620SStephen Smalley 			  sid, old_isec->sid,
19191da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
19201da177e4SLinus Torvalds 	if (rc)
19211da177e4SLinus Torvalds 		return rc;
19221da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
19236b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19246b6bc620SStephen Smalley 				  sid, old_isec->sid,
19251da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
19261da177e4SLinus Torvalds 		if (rc)
19271da177e4SLinus Torvalds 			return rc;
19281da177e4SLinus Torvalds 	}
19291da177e4SLinus Torvalds 
1930a269434dSEric Paris 	ad.u.dentry = new_dentry;
19311da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
19322c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
19331da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
19346b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19356b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
19361da177e4SLinus Torvalds 	if (rc)
19371da177e4SLinus Torvalds 		return rc;
19382c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
193983da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
1940e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
19416b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19426b6bc620SStephen Smalley 				  sid, new_isec->sid,
19431da177e4SLinus Torvalds 				  new_isec->sclass,
19441da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
19451da177e4SLinus Torvalds 		if (rc)
19461da177e4SLinus Torvalds 			return rc;
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	return 0;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
195388e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
19541da177e4SLinus Torvalds 			       struct super_block *sb,
19551da177e4SLinus Torvalds 			       u32 perms,
19562bf49690SThomas Liu 			       struct common_audit_data *ad)
19571da177e4SLinus Torvalds {
19581da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
195988e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	sbsec = sb->s_security;
19626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19636b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
19671da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
19681da177e4SLinus Torvalds {
19691da177e4SLinus Torvalds 	u32 av = 0;
19701da177e4SLinus Torvalds 
1971dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
19721da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19731da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
19741da177e4SLinus Torvalds 		if (mask & MAY_READ)
19751da177e4SLinus Torvalds 			av |= FILE__READ;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
19781da177e4SLinus Torvalds 			av |= FILE__APPEND;
19791da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
19801da177e4SLinus Torvalds 			av |= FILE__WRITE;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	} else {
19831da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19841da177e4SLinus Torvalds 			av |= DIR__SEARCH;
19851da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
19861da177e4SLinus Torvalds 			av |= DIR__WRITE;
19871da177e4SLinus Torvalds 		if (mask & MAY_READ)
19881da177e4SLinus Torvalds 			av |= DIR__READ;
19891da177e4SLinus Torvalds 	}
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	return av;
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
19951da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
19961da177e4SLinus Torvalds {
19971da177e4SLinus Torvalds 	u32 av = 0;
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
20001da177e4SLinus Torvalds 		av |= FILE__READ;
20011da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
20021da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
20031da177e4SLinus Torvalds 			av |= FILE__APPEND;
20041da177e4SLinus Torvalds 		else
20051da177e4SLinus Torvalds 			av |= FILE__WRITE;
20061da177e4SLinus Torvalds 	}
20070794c66dSStephen Smalley 	if (!av) {
20080794c66dSStephen Smalley 		/*
20090794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
20100794c66dSStephen Smalley 		 */
20110794c66dSStephen Smalley 		av = FILE__IOCTL;
20120794c66dSStephen Smalley 	}
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	return av;
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
20178b6a5a37SEric Paris /*
2018c76a2f9eSRandy Dunlap  * Convert a file to an access vector and include the correct
20198b6a5a37SEric Paris  * open permission.
20208b6a5a37SEric Paris  */
20218b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
20228b6a5a37SEric Paris {
20238b6a5a37SEric Paris 	u32 av = file_to_av(file);
2024ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
20258b6a5a37SEric Paris 
2026aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
2027aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
20288b6a5a37SEric Paris 		av |= FILE__OPEN;
202949b7b8deSEric Paris 
20308b6a5a37SEric Paris 	return av;
20318b6a5a37SEric Paris }
20328b6a5a37SEric Paris 
20331da177e4SLinus Torvalds /* Hook functions begin here. */
20341da177e4SLinus Torvalds 
203579af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
203679af7307SStephen Smalley {
203779af7307SStephen Smalley 	u32 mysid = current_sid();
203879af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
203979af7307SStephen Smalley 
20406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20416b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
204279af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
204379af7307SStephen Smalley }
204479af7307SStephen Smalley 
204579af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
204679af7307SStephen Smalley 				      struct task_struct *to)
204779af7307SStephen Smalley {
204879af7307SStephen Smalley 	u32 mysid = current_sid();
204979af7307SStephen Smalley 	u32 fromsid = task_sid(from);
205079af7307SStephen Smalley 	u32 tosid = task_sid(to);
205179af7307SStephen Smalley 	int rc;
205279af7307SStephen Smalley 
205379af7307SStephen Smalley 	if (mysid != fromsid) {
20546b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20556b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
205679af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
205779af7307SStephen Smalley 		if (rc)
205879af7307SStephen Smalley 			return rc;
205979af7307SStephen Smalley 	}
206079af7307SStephen Smalley 
20616b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20626b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
206379af7307SStephen Smalley 			    NULL);
206479af7307SStephen Smalley }
206579af7307SStephen Smalley 
206679af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
206779af7307SStephen Smalley 					  struct task_struct *to)
206879af7307SStephen Smalley {
206979af7307SStephen Smalley 	u32 fromsid = task_sid(from);
207079af7307SStephen Smalley 	u32 tosid = task_sid(to);
207179af7307SStephen Smalley 
20726b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20736b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
207479af7307SStephen Smalley 			    NULL);
207579af7307SStephen Smalley }
207679af7307SStephen Smalley 
207779af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
207879af7307SStephen Smalley 					struct task_struct *to,
207979af7307SStephen Smalley 					struct file *file)
208079af7307SStephen Smalley {
208179af7307SStephen Smalley 	u32 sid = task_sid(to);
2082bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
208383da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
208420cdef8dSPaul Moore 	struct inode_security_struct *isec;
208579af7307SStephen Smalley 	struct common_audit_data ad;
208679af7307SStephen Smalley 	int rc;
208779af7307SStephen Smalley 
208879af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
208979af7307SStephen Smalley 	ad.u.path = file->f_path;
209079af7307SStephen Smalley 
209179af7307SStephen Smalley 	if (sid != fsec->sid) {
20926b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20936b6bc620SStephen Smalley 				  sid, fsec->sid,
209479af7307SStephen Smalley 				  SECCLASS_FD,
209579af7307SStephen Smalley 				  FD__USE,
209679af7307SStephen Smalley 				  &ad);
209779af7307SStephen Smalley 		if (rc)
209879af7307SStephen Smalley 			return rc;
209979af7307SStephen Smalley 	}
210079af7307SStephen Smalley 
2101f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2102f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2103f66e448cSChenbo Feng 	if (rc)
2104f66e448cSChenbo Feng 		return rc;
2105f66e448cSChenbo Feng #endif
2106f66e448cSChenbo Feng 
210783da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
210879af7307SStephen Smalley 		return 0;
210979af7307SStephen Smalley 
211020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
21116b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21126b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
211379af7307SStephen Smalley 			    &ad);
211479af7307SStephen Smalley }
211579af7307SStephen Smalley 
21169e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2117006ebb40SStephen Smalley 				     unsigned int mode)
21181da177e4SLinus Torvalds {
2119275bb41eSDavid Howells 	u32 sid = current_sid();
2120275bb41eSDavid Howells 	u32 csid = task_sid(child);
2121006ebb40SStephen Smalley 
2122be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
21236b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21246b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2125be0554c9SStephen Smalley 
21266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21276b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
21285cd9c58fSDavid Howells }
21295cd9c58fSDavid Howells 
21305cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
21315cd9c58fSDavid Howells {
21326b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21336b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2134be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
21381da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
21391da177e4SLinus Torvalds {
21406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21416b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2142be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds 
2145d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2146d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
214715a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
214815a2460eSDavid Howells 			  const kernel_cap_t *permitted)
21491da177e4SLinus Torvalds {
21506b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21516b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2152be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
21555626d3e8SJames Morris /*
21565626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
21575626d3e8SJames Morris  * which was removed).
21585626d3e8SJames Morris  *
21595626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
21605626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
21615626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
21625626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
21635626d3e8SJames Morris  */
21645626d3e8SJames Morris 
21656a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2166c1a85a00SMicah Morton 			   int cap, unsigned int opts)
21671da177e4SLinus Torvalds {
2168c1a85a00SMicah Morton 	return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
21721da177e4SLinus Torvalds {
217388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
21741da177e4SLinus Torvalds 	int rc = 0;
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	if (!sb)
21771da177e4SLinus Torvalds 		return 0;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	switch (cmds) {
21801da177e4SLinus Torvalds 	case Q_SYNC:
21811da177e4SLinus Torvalds 	case Q_QUOTAON:
21821da177e4SLinus Torvalds 	case Q_QUOTAOFF:
21831da177e4SLinus Torvalds 	case Q_SETINFO:
21841da177e4SLinus Torvalds 	case Q_SETQUOTA:
2185e4cfa05eSRichard Haines 	case Q_XQUOTAOFF:
2186e4cfa05eSRichard Haines 	case Q_XQUOTAON:
2187e4cfa05eSRichard Haines 	case Q_XSETQLIM:
218888e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
21891da177e4SLinus Torvalds 		break;
21901da177e4SLinus Torvalds 	case Q_GETFMT:
21911da177e4SLinus Torvalds 	case Q_GETINFO:
21921da177e4SLinus Torvalds 	case Q_GETQUOTA:
2193e4cfa05eSRichard Haines 	case Q_XGETQUOTA:
2194e4cfa05eSRichard Haines 	case Q_XGETQSTAT:
2195e4cfa05eSRichard Haines 	case Q_XGETQSTATV:
2196e4cfa05eSRichard Haines 	case Q_XGETNEXTQUOTA:
219788e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
21981da177e4SLinus Torvalds 		break;
21991da177e4SLinus Torvalds 	default:
22001da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
22011da177e4SLinus Torvalds 		break;
22021da177e4SLinus Torvalds 	}
22031da177e4SLinus Torvalds 	return rc;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
22071da177e4SLinus Torvalds {
220888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
220988e67f3bSDavid Howells 
22102875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
221312b3052cSEric Paris static int selinux_syslog(int type)
22141da177e4SLinus Torvalds {
22151da177e4SLinus Torvalds 	switch (type) {
2216d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2217d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
22186b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22196b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2220be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2221d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2222d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2223d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2224d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
22256b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22266b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2227be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2228be0554c9SStephen Smalley 				    NULL);
22291da177e4SLinus Torvalds 	}
2230be0554c9SStephen Smalley 	/* All other syslog types */
22316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22326b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2233be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds /*
22371da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
22381da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
22391da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
22401da177e4SLinus Torvalds  *
22411da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
22421da177e4SLinus Torvalds  * processes that allocate mappings.
22431da177e4SLinus Torvalds  */
224434b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22451da177e4SLinus Torvalds {
22461da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
22471da177e4SLinus Torvalds 
2248b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2249c1a85a00SMicah Morton 				 CAP_OPT_NOAUDIT, true);
22501da177e4SLinus Torvalds 	if (rc == 0)
22511da177e4SLinus Torvalds 		cap_sys_admin = 1;
22521da177e4SLinus Torvalds 
2253b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /* binprm security operations */
22571da177e4SLinus Torvalds 
2258be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
22590c6181cbSPaul Moore {
22600c6181cbSPaul Moore 	u32 sid = 0;
22610c6181cbSPaul Moore 	struct task_struct *tracer;
22620c6181cbSPaul Moore 
22630c6181cbSPaul Moore 	rcu_read_lock();
2264be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
22650c6181cbSPaul Moore 	if (tracer)
22660c6181cbSPaul Moore 		sid = task_sid(tracer);
22670c6181cbSPaul Moore 	rcu_read_unlock();
22680c6181cbSPaul Moore 
22690c6181cbSPaul Moore 	return sid;
22700c6181cbSPaul Moore }
22710c6181cbSPaul Moore 
22727b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
22737b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
22747b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
22757b0d0b40SStephen Smalley {
22767b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2277380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
22787b0d0b40SStephen Smalley 	int rc;
2279af63f419SStephen Smalley 	u32 av;
22807b0d0b40SStephen Smalley 
22817b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
22827b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
22837b0d0b40SStephen Smalley 
22847b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
22857b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
22867b0d0b40SStephen Smalley 
22877b0d0b40SStephen Smalley 	/*
2288af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2289af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2290af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2291af63f419SStephen Smalley 	 * the old and new contexts.
2292af63f419SStephen Smalley 	 */
2293aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2294af63f419SStephen Smalley 		av = 0;
2295af63f419SStephen Smalley 		if (nnp)
2296af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2297af63f419SStephen Smalley 		if (nosuid)
2298af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
22996b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23006b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2301af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2302af63f419SStephen Smalley 		if (!rc)
2303af63f419SStephen Smalley 			return 0;
2304af63f419SStephen Smalley 	}
2305af63f419SStephen Smalley 
2306af63f419SStephen Smalley 	/*
2307af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2308af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2309af63f419SStephen Smalley 	 * of the permissions of the current SID.
23107b0d0b40SStephen Smalley 	 */
2311aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2312aa8e712cSStephen Smalley 					 new_tsec->sid);
2313af63f419SStephen Smalley 	if (!rc)
2314af63f419SStephen Smalley 		return 0;
2315af63f419SStephen Smalley 
23167b0d0b40SStephen Smalley 	/*
23177b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
23187b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
23197b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
23207b0d0b40SStephen Smalley 	 */
23217b0d0b40SStephen Smalley 	if (nnp)
23227b0d0b40SStephen Smalley 		return -EPERM;
23237b0d0b40SStephen Smalley 	return -EACCES;
23247b0d0b40SStephen Smalley }
23257b0d0b40SStephen Smalley 
2326b8bff599SEric W. Biederman static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
23271da177e4SLinus Torvalds {
2328a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2329a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23301da177e4SLinus Torvalds 	struct inode_security_struct *isec;
23312bf49690SThomas Liu 	struct common_audit_data ad;
2332496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
23331da177e4SLinus Torvalds 	int rc;
23341da177e4SLinus Torvalds 
2335a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2336a6f76f23SDavid Howells 	 * the script interpreter */
23371da177e4SLinus Torvalds 
23380c6cfa62SCasey Schaufler 	old_tsec = selinux_cred(current_cred());
23390c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
234083da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	/* Default to the current task SID. */
2343a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2344a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
23451da177e4SLinus Torvalds 
234628eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2347a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2348a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2349a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
23501da177e4SLinus Torvalds 
2351a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2352a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
23531da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2354a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2355259e5e6cSAndy Lutomirski 
23567b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
23577b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23587b0d0b40SStephen Smalley 		if (rc)
23597b0d0b40SStephen Smalley 			return rc;
23601da177e4SLinus Torvalds 	} else {
23611da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2362aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2363aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2364652bb9b0SEric Paris 					     &new_tsec->sid);
23651da177e4SLinus Torvalds 		if (rc)
23661da177e4SLinus Torvalds 			return rc;
23677b0d0b40SStephen Smalley 
23687b0d0b40SStephen Smalley 		/*
23697b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
23707b0d0b40SStephen Smalley 		 * transition.
23717b0d0b40SStephen Smalley 		 */
23727b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23737b0d0b40SStephen Smalley 		if (rc)
23747b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
23751da177e4SLinus Torvalds 	}
23761da177e4SLinus Torvalds 
237743af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
237843af5de7SVivek Goyal 	ad.u.file = bprm->file;
23791da177e4SLinus Torvalds 
2380a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
23816b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23826b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
23831da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
23841da177e4SLinus Torvalds 		if (rc)
23851da177e4SLinus Torvalds 			return rc;
23861da177e4SLinus Torvalds 	} else {
23871da177e4SLinus Torvalds 		/* Check permissions for the transition. */
23886b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23896b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
23901da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
23911da177e4SLinus Torvalds 		if (rc)
23921da177e4SLinus Torvalds 			return rc;
23931da177e4SLinus Torvalds 
23946b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23956b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
23961da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
23971da177e4SLinus Torvalds 		if (rc)
23981da177e4SLinus Torvalds 			return rc;
23991da177e4SLinus Torvalds 
2400a6f76f23SDavid Howells 		/* Check for shared state */
2401a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
24026b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
24036b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2404a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2405a6f76f23SDavid Howells 					  NULL);
2406a6f76f23SDavid Howells 			if (rc)
2407a6f76f23SDavid Howells 				return -EPERM;
24081da177e4SLinus Torvalds 		}
24091da177e4SLinus Torvalds 
2410a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2411a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
24129227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2413be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2414a6f76f23SDavid Howells 			if (ptsid != 0) {
24156b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
24166b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2417a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2418a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2419a6f76f23SDavid Howells 				if (rc)
2420a6f76f23SDavid Howells 					return -EPERM;
2421a6f76f23SDavid Howells 			}
2422a6f76f23SDavid Howells 		}
2423a6f76f23SDavid Howells 
2424a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2425a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2426a6f76f23SDavid Howells 
24271da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
24281da177e4SLinus Torvalds 		   the noatsecure permission is granted between
24291da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
24306b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
24316b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
243262874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
243362874c3aSKees Cook 				  NULL);
243462874c3aSKees Cook 		bprm->secureexec |= !!rc;
24351da177e4SLinus Torvalds 	}
24361da177e4SLinus Torvalds 
243762874c3aSKees Cook 	return 0;
24381da177e4SLinus Torvalds }
24391da177e4SLinus Torvalds 
2440c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2441c3c073f8SAl Viro {
2442c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2443c3c073f8SAl Viro }
2444c3c073f8SAl Viro 
24451da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2446745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2447745ca247SDavid Howells 					    struct files_struct *files)
24481da177e4SLinus Torvalds {
24491da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2450b20c8122SStephen Smalley 	struct tty_struct *tty;
245124ec839cSPeter Zijlstra 	int drop_tty = 0;
2452c3c073f8SAl Viro 	unsigned n;
24531da177e4SLinus Torvalds 
245424ec839cSPeter Zijlstra 	tty = get_current_tty();
24551da177e4SLinus Torvalds 	if (tty) {
24564a510969SPeter Hurley 		spin_lock(&tty->files_lock);
245737dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2458d996b62aSNick Piggin 			struct tty_file_private *file_priv;
245937dd0bd0SEric Paris 
24601da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
246113f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
246213f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
246313f8e981SDavid Howells 			   open file may belong to another process and we are
246413f8e981SDavid Howells 			   only interested in the inode-based check here. */
2465d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2466d996b62aSNick Piggin 						struct tty_file_private, list);
2467d996b62aSNick Piggin 			file = file_priv->file;
246813f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
246924ec839cSPeter Zijlstra 				drop_tty = 1;
24701da177e4SLinus Torvalds 		}
24714a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2472452a00d2SAlan Cox 		tty_kref_put(tty);
24731da177e4SLinus Torvalds 	}
247498a27ba4SEric W. Biederman 	/* Reset controlling tty. */
247598a27ba4SEric W. Biederman 	if (drop_tty)
247698a27ba4SEric W. Biederman 		no_tty();
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2479c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2480c3c073f8SAl Viro 	if (!n) /* none found? */
2481c3c073f8SAl Viro 		return;
24821da177e4SLinus Torvalds 
2483c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
248445525b26SAl Viro 	if (IS_ERR(devnull))
248545525b26SAl Viro 		devnull = NULL;
2486c3c073f8SAl Viro 	/* replace all the matching ones with this */
2487c3c073f8SAl Viro 	do {
248845525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2489c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
249045525b26SAl Viro 	if (devnull)
2491c3c073f8SAl Viro 		fput(devnull);
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds /*
2495a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
24961da177e4SLinus Torvalds  */
2497a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
24981da177e4SLinus Torvalds {
2499a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
25001da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
25011da177e4SLinus Torvalds 	int rc, i;
25021da177e4SLinus Torvalds 
25030c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
2504a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
25051da177e4SLinus Torvalds 		return;
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2508a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
25091da177e4SLinus Torvalds 
2510a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2511a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2512a6f76f23SDavid Howells 
2513a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2514a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2515a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2516a6f76f23SDavid Howells 	 *
2517a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2518a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2519a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2520a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2521a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2522a6f76f23SDavid Howells 	 */
25236b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25246b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2525a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2526a6f76f23SDavid Howells 	if (rc) {
2527eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2528eb2d55a3SOleg Nesterov 		task_lock(current);
2529a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2530a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2531a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2532a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2533a6f76f23SDavid Howells 		}
2534eb2d55a3SOleg Nesterov 		task_unlock(current);
2535baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2536eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2537a6f76f23SDavid Howells 	}
2538a6f76f23SDavid Howells }
2539a6f76f23SDavid Howells 
2540a6f76f23SDavid Howells /*
2541a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2542a6f76f23SDavid Howells  * due to exec
2543a6f76f23SDavid Howells  */
2544a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2545a6f76f23SDavid Howells {
25460c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
2547a6f76f23SDavid Howells 	u32 osid, sid;
2548ddbc7d06SArnd Bergmann 	int rc;
2549a6f76f23SDavid Howells 
2550a6f76f23SDavid Howells 	osid = tsec->osid;
2551a6f76f23SDavid Howells 	sid = tsec->sid;
2552a6f76f23SDavid Howells 
2553a6f76f23SDavid Howells 	if (sid == osid)
2554a6f76f23SDavid Howells 		return;
2555a6f76f23SDavid Howells 
2556a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2557a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2558a6f76f23SDavid Howells 	 * flush and unblock signals.
2559a6f76f23SDavid Howells 	 *
2560a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2561a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2562a6f76f23SDavid Howells 	 */
25636b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25646b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
25651da177e4SLinus Torvalds 	if (rc) {
2566ddbc7d06SArnd Bergmann 		clear_itimer();
2567ddbc7d06SArnd Bergmann 
25681da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
25699e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
25709e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
25719e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
25721da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
25731da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
25749e7c8f8cSOleg Nesterov 			recalc_sigpending();
25753bcac026SDavid Howells 		}
25761da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
25771da177e4SLinus Torvalds 	}
25781da177e4SLinus Torvalds 
2579a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2580a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2581ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
25820b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2583ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds /* superblock security operations */
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
25891da177e4SLinus Torvalds {
2590cb89e246SPaul Moore 	struct superblock_security_struct *sbsec;
2591cb89e246SPaul Moore 
2592cb89e246SPaul Moore 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2593cb89e246SPaul Moore 	if (!sbsec)
2594cb89e246SPaul Moore 		return -ENOMEM;
2595cb89e246SPaul Moore 
2596cb89e246SPaul Moore 	mutex_init(&sbsec->lock);
2597cb89e246SPaul Moore 	INIT_LIST_HEAD(&sbsec->isec_head);
2598cb89e246SPaul Moore 	spin_lock_init(&sbsec->isec_lock);
2599cb89e246SPaul Moore 	sbsec->sid = SECINITSID_UNLABELED;
2600cb89e246SPaul Moore 	sbsec->def_sid = SECINITSID_FILE;
2601cb89e246SPaul Moore 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2602cb89e246SPaul Moore 	sb->s_security = sbsec;
2603cb89e246SPaul Moore 
2604cb89e246SPaul Moore 	return 0;
26051da177e4SLinus Torvalds }
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
26081da177e4SLinus Torvalds {
26091da177e4SLinus Torvalds 	superblock_free_security(sb);
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds 
261299dbbb59SAl Viro static inline int opt_len(const char *s)
26131da177e4SLinus Torvalds {
261499dbbb59SAl Viro 	bool open_quote = false;
261599dbbb59SAl Viro 	int len;
261699dbbb59SAl Viro 	char c;
26171da177e4SLinus Torvalds 
261899dbbb59SAl Viro 	for (len = 0; (c = s[len]) != '\0'; len++) {
261999dbbb59SAl Viro 		if (c == '"')
26203528a953SCory Olmo 			open_quote = !open_quote;
262199dbbb59SAl Viro 		if (c == ',' && !open_quote)
262299dbbb59SAl Viro 			break;
26231da177e4SLinus Torvalds 	}
262499dbbb59SAl Viro 	return len;
26251da177e4SLinus Torvalds }
26261da177e4SLinus Torvalds 
2627204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26285b400239SAl Viro {
262999dbbb59SAl Viro 	char *from = options;
263099dbbb59SAl Viro 	char *to = options;
263199dbbb59SAl Viro 	bool first = true;
2632fec63753SGen Zhang 	int rc;
26335b400239SAl Viro 
263499dbbb59SAl Viro 	while (1) {
263599dbbb59SAl Viro 		int len = opt_len(from);
2636fec63753SGen Zhang 		int token;
263799dbbb59SAl Viro 		char *arg = NULL;
263899dbbb59SAl Viro 
263999dbbb59SAl Viro 		token = match_opt_prefix(from, len, &arg);
264099dbbb59SAl Viro 
264199dbbb59SAl Viro 		if (token != Opt_error) {
264299dbbb59SAl Viro 			char *p, *q;
264399dbbb59SAl Viro 
264499dbbb59SAl Viro 			/* strip quotes */
264599dbbb59SAl Viro 			if (arg) {
264699dbbb59SAl Viro 				for (p = q = arg; p < from + len; p++) {
264799dbbb59SAl Viro 					char c = *p;
264899dbbb59SAl Viro 					if (c != '"')
264999dbbb59SAl Viro 						*q++ = c;
265099dbbb59SAl Viro 				}
265199dbbb59SAl Viro 				arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2652fec63753SGen Zhang 				if (!arg) {
2653fec63753SGen Zhang 					rc = -ENOMEM;
2654fec63753SGen Zhang 					goto free_opt;
2655fec63753SGen Zhang 				}
265699dbbb59SAl Viro 			}
265799dbbb59SAl Viro 			rc = selinux_add_opt(token, arg, mnt_opts);
265899dbbb59SAl Viro 			if (unlikely(rc)) {
265999dbbb59SAl Viro 				kfree(arg);
2660fec63753SGen Zhang 				goto free_opt;
26611da177e4SLinus Torvalds 			}
266299dbbb59SAl Viro 		} else {
266399dbbb59SAl Viro 			if (!first) {	// copy with preceding comma
266499dbbb59SAl Viro 				from--;
266599dbbb59SAl Viro 				len++;
266699dbbb59SAl Viro 			}
266799dbbb59SAl Viro 			if (to != from)
266899dbbb59SAl Viro 				memmove(to, from, len);
266999dbbb59SAl Viro 			to += len;
267099dbbb59SAl Viro 			first = false;
267199dbbb59SAl Viro 		}
267299dbbb59SAl Viro 		if (!from[len])
267399dbbb59SAl Viro 			break;
267499dbbb59SAl Viro 		from += len + 1;
267599dbbb59SAl Viro 	}
267699dbbb59SAl Viro 	*to = '\0';
267799dbbb59SAl Viro 	return 0;
2678fec63753SGen Zhang 
2679fec63753SGen Zhang free_opt:
2680fec63753SGen Zhang 	if (*mnt_opts) {
2681fec63753SGen Zhang 		selinux_free_mnt_opts(*mnt_opts);
2682fec63753SGen Zhang 		*mnt_opts = NULL;
2683fec63753SGen Zhang 	}
2684fec63753SGen Zhang 	return rc;
26855b400239SAl Viro }
26861da177e4SLinus Torvalds 
2687204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2688026eb167SEric Paris {
2689bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
2690026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2691bd323655SAl Viro 	u32 sid;
2692bd323655SAl Viro 	int rc;
2693026eb167SEric Paris 
2694026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2695026eb167SEric Paris 		return 0;
2696026eb167SEric Paris 
2697204cc0ccSAl Viro 	if (!opts)
2698026eb167SEric Paris 		return 0;
2699026eb167SEric Paris 
2700bd323655SAl Viro 	if (opts->fscontext) {
2701bd323655SAl Viro 		rc = parse_sid(sb, opts->fscontext, &sid);
2702026eb167SEric Paris 		if (rc)
2703c039bc3cSAl Viro 			return rc;
2704026eb167SEric Paris 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2705026eb167SEric Paris 			goto out_bad_option;
2706bd323655SAl Viro 	}
2707bd323655SAl Viro 	if (opts->context) {
2708bd323655SAl Viro 		rc = parse_sid(sb, opts->context, &sid);
2709bd323655SAl Viro 		if (rc)
2710bd323655SAl Viro 			return rc;
2711026eb167SEric Paris 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2712026eb167SEric Paris 			goto out_bad_option;
2713bd323655SAl Viro 	}
2714bd323655SAl Viro 	if (opts->rootcontext) {
2715026eb167SEric Paris 		struct inode_security_struct *root_isec;
271683da53c5SAndreas Gruenbacher 		root_isec = backing_inode_security(sb->s_root);
2717bd323655SAl Viro 		rc = parse_sid(sb, opts->rootcontext, &sid);
2718bd323655SAl Viro 		if (rc)
2719bd323655SAl Viro 			return rc;
2720026eb167SEric Paris 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2721026eb167SEric Paris 			goto out_bad_option;
2722026eb167SEric Paris 	}
2723bd323655SAl Viro 	if (opts->defcontext) {
2724bd323655SAl Viro 		rc = parse_sid(sb, opts->defcontext, &sid);
2725bd323655SAl Viro 		if (rc)
2726bd323655SAl Viro 			return rc;
2727026eb167SEric Paris 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2728026eb167SEric Paris 			goto out_bad_option;
2729026eb167SEric Paris 	}
2730c039bc3cSAl Viro 	return 0;
2731026eb167SEric Paris 
2732026eb167SEric Paris out_bad_option:
2733c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
273429b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
273529b1deb2SLinus Torvalds 	       sb->s_type->name);
2736c039bc3cSAl Viro 	return -EINVAL;
2737026eb167SEric Paris }
2738026eb167SEric Paris 
2739a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb)
27401da177e4SLinus Torvalds {
274188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27422bf49690SThomas Liu 	struct common_audit_data ad;
274374192246SJames Morris 
274450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2745a269434dSEric Paris 	ad.u.dentry = sb->s_root;
274688e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
2749726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
27501da177e4SLinus Torvalds {
275188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27522bf49690SThomas Liu 	struct common_audit_data ad;
27531da177e4SLinus Torvalds 
275450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2755a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
275688e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
2759808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
27608a04c43bSAl Viro 			 const struct path *path,
2761808d4e3cSAl Viro 			 const char *type,
27621da177e4SLinus Torvalds 			 unsigned long flags,
27631da177e4SLinus Torvalds 			 void *data)
27641da177e4SLinus Torvalds {
276588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2768d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
27691da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
27701da177e4SLinus Torvalds 	else
27712875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
277498aa0034SStephen Smalley static int selinux_move_mount(const struct path *from_path,
277598aa0034SStephen Smalley 			      const struct path *to_path)
277698aa0034SStephen Smalley {
277798aa0034SStephen Smalley 	const struct cred *cred = current_cred();
277898aa0034SStephen Smalley 
277998aa0034SStephen Smalley 	return path_has_perm(cred, to_path, FILE__MOUNTON);
278098aa0034SStephen Smalley }
278198aa0034SStephen Smalley 
27821da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27831da177e4SLinus Torvalds {
278488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27851da177e4SLinus Torvalds 
278688e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27871da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27881da177e4SLinus Torvalds }
27891da177e4SLinus Torvalds 
27900b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc,
27910b52075eSAl Viro 				  struct fs_context *src_fc)
27920b52075eSAl Viro {
27930b52075eSAl Viro 	const struct selinux_mnt_opts *src = src_fc->security;
27940b52075eSAl Viro 	struct selinux_mnt_opts *opts;
27950b52075eSAl Viro 
27960b52075eSAl Viro 	if (!src)
27970b52075eSAl Viro 		return 0;
27980b52075eSAl Viro 
27990b52075eSAl Viro 	fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
28000b52075eSAl Viro 	if (!fc->security)
28010b52075eSAl Viro 		return -ENOMEM;
28020b52075eSAl Viro 
28030b52075eSAl Viro 	opts = fc->security;
28040b52075eSAl Viro 
28050b52075eSAl Viro 	if (src->fscontext) {
28060b52075eSAl Viro 		opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
28070b52075eSAl Viro 		if (!opts->fscontext)
28080b52075eSAl Viro 			return -ENOMEM;
28090b52075eSAl Viro 	}
28100b52075eSAl Viro 	if (src->context) {
28110b52075eSAl Viro 		opts->context = kstrdup(src->context, GFP_KERNEL);
28120b52075eSAl Viro 		if (!opts->context)
28130b52075eSAl Viro 			return -ENOMEM;
28140b52075eSAl Viro 	}
28150b52075eSAl Viro 	if (src->rootcontext) {
28160b52075eSAl Viro 		opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
28170b52075eSAl Viro 		if (!opts->rootcontext)
28180b52075eSAl Viro 			return -ENOMEM;
28190b52075eSAl Viro 	}
28200b52075eSAl Viro 	if (src->defcontext) {
28210b52075eSAl Viro 		opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
28220b52075eSAl Viro 		if (!opts->defcontext)
28230b52075eSAl Viro 			return -ENOMEM;
28240b52075eSAl Viro 	}
28250b52075eSAl Viro 	return 0;
28260b52075eSAl Viro }
28270b52075eSAl Viro 
2828d7167b14SAl Viro static const struct fs_parameter_spec selinux_fs_parameters[] = {
2829442155c1SDavid Howells 	fsparam_string(CONTEXT_STR,	Opt_context),
2830442155c1SDavid Howells 	fsparam_string(DEFCONTEXT_STR,	Opt_defcontext),
2831442155c1SDavid Howells 	fsparam_string(FSCONTEXT_STR,	Opt_fscontext),
2832442155c1SDavid Howells 	fsparam_string(ROOTCONTEXT_STR,	Opt_rootcontext),
2833442155c1SDavid Howells 	fsparam_flag  (SECLABEL_STR,	Opt_seclabel),
2834442155c1SDavid Howells 	{}
2835442155c1SDavid Howells };
2836442155c1SDavid Howells 
2837442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc,
2838442155c1SDavid Howells 					  struct fs_parameter *param)
2839442155c1SDavid Howells {
2840442155c1SDavid Howells 	struct fs_parse_result result;
2841442155c1SDavid Howells 	int opt, rc;
2842442155c1SDavid Howells 
2843d7167b14SAl Viro 	opt = fs_parse(fc, selinux_fs_parameters, param, &result);
2844442155c1SDavid Howells 	if (opt < 0)
2845442155c1SDavid Howells 		return opt;
2846442155c1SDavid Howells 
2847442155c1SDavid Howells 	rc = selinux_add_opt(opt, param->string, &fc->security);
2848442155c1SDavid Howells 	if (!rc) {
2849442155c1SDavid Howells 		param->string = NULL;
2850442155c1SDavid Howells 		rc = 1;
2851442155c1SDavid Howells 	}
2852442155c1SDavid Howells 	return rc;
2853442155c1SDavid Howells }
2854442155c1SDavid Howells 
28551da177e4SLinus Torvalds /* inode security operations */
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
28581da177e4SLinus Torvalds {
2859cb89e246SPaul Moore 	struct inode_security_struct *isec = selinux_inode(inode);
2860cb89e246SPaul Moore 	u32 sid = current_sid();
2861cb89e246SPaul Moore 
2862cb89e246SPaul Moore 	spin_lock_init(&isec->lock);
2863cb89e246SPaul Moore 	INIT_LIST_HEAD(&isec->list);
2864cb89e246SPaul Moore 	isec->inode = inode;
2865cb89e246SPaul Moore 	isec->sid = SECINITSID_UNLABELED;
2866cb89e246SPaul Moore 	isec->sclass = SECCLASS_FILE;
2867cb89e246SPaul Moore 	isec->task_sid = sid;
2868cb89e246SPaul Moore 	isec->initialized = LABEL_INVALID;
2869cb89e246SPaul Moore 
2870cb89e246SPaul Moore 	return 0;
28711da177e4SLinus Torvalds }
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
28741da177e4SLinus Torvalds {
28751da177e4SLinus Torvalds 	inode_free_security(inode);
28761da177e4SLinus Torvalds }
28771da177e4SLinus Torvalds 
2878d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
28794f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2880d47be3dfSDavid Quigley 					u32 *ctxlen)
2881d47be3dfSDavid Quigley {
2882d47be3dfSDavid Quigley 	u32 newsid;
2883d47be3dfSDavid Quigley 	int rc;
2884d47be3dfSDavid Quigley 
28850c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2886c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2887d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2888d47be3dfSDavid Quigley 					   &newsid);
2889c3c188b2SDavid Howells 	if (rc)
2890d47be3dfSDavid Quigley 		return rc;
2891d47be3dfSDavid Quigley 
2892aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2893aa8e712cSStephen Smalley 				       ctxlen);
2894d47be3dfSDavid Quigley }
2895d47be3dfSDavid Quigley 
2896a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2897a518b0a5SVivek Goyal 					  struct qstr *name,
2898a518b0a5SVivek Goyal 					  const struct cred *old,
2899a518b0a5SVivek Goyal 					  struct cred *new)
2900a518b0a5SVivek Goyal {
2901a518b0a5SVivek Goyal 	u32 newsid;
2902a518b0a5SVivek Goyal 	int rc;
2903a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2904a518b0a5SVivek Goyal 
29050c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(old),
2906a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
2907a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
2908a518b0a5SVivek Goyal 					   &newsid);
2909a518b0a5SVivek Goyal 	if (rc)
2910a518b0a5SVivek Goyal 		return rc;
2911a518b0a5SVivek Goyal 
29120c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
2913a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
2914a518b0a5SVivek Goyal 	return 0;
2915a518b0a5SVivek Goyal }
2916a518b0a5SVivek Goyal 
29175e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
29189548906bSTetsuo Handa 				       const struct qstr *qstr,
29199548906bSTetsuo Handa 				       const char **name,
29202a7dba39SEric Paris 				       void **value, size_t *len)
29215e41ff9eSStephen Smalley {
29220c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
29235e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2924c0d4f464SCorentin LABBE 	u32 newsid, clen;
29255e41ff9eSStephen Smalley 	int rc;
29269548906bSTetsuo Handa 	char *context;
29275e41ff9eSStephen Smalley 
29285e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
29295e41ff9eSStephen Smalley 
29305e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2931275bb41eSDavid Howells 
2932210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, qstr,
29335e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
2934c3c188b2SDavid Howells 		&newsid);
2935c3c188b2SDavid Howells 	if (rc)
29365e41ff9eSStephen Smalley 		return rc;
29375e41ff9eSStephen Smalley 
2938296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
29390d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
294080788c22SCasey Schaufler 		struct inode_security_struct *isec = selinux_inode(inode);
2941296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2942296fddf7SEric Paris 		isec->sid = newsid;
29436f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
2944296fddf7SEric Paris 	}
29455e41ff9eSStephen Smalley 
294665cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state) ||
294765cddd50SOndrej Mosnacek 	    !(sbsec->flags & SBLABEL_MNT))
294825a74f3bSStephen Smalley 		return -EOPNOTSUPP;
294925a74f3bSStephen Smalley 
29509548906bSTetsuo Handa 	if (name)
29519548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
29525e41ff9eSStephen Smalley 
2953570bc1c2SStephen Smalley 	if (value && len) {
2954aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
2955aa8e712cSStephen Smalley 						   &context, &clen);
29569548906bSTetsuo Handa 		if (rc)
29575e41ff9eSStephen Smalley 			return rc;
29585e41ff9eSStephen Smalley 		*value = context;
2959570bc1c2SStephen Smalley 		*len = clen;
2960570bc1c2SStephen Smalley 	}
29615e41ff9eSStephen Smalley 
29625e41ff9eSStephen Smalley 	return 0;
29635e41ff9eSStephen Smalley }
29645e41ff9eSStephen Smalley 
2965*29cd6591SDaniel Colascione static int selinux_inode_init_security_anon(struct inode *inode,
2966*29cd6591SDaniel Colascione 					    const struct qstr *name,
2967*29cd6591SDaniel Colascione 					    const struct inode *context_inode)
2968*29cd6591SDaniel Colascione {
2969*29cd6591SDaniel Colascione 	const struct task_security_struct *tsec = selinux_cred(current_cred());
2970*29cd6591SDaniel Colascione 	struct common_audit_data ad;
2971*29cd6591SDaniel Colascione 	struct inode_security_struct *isec;
2972*29cd6591SDaniel Colascione 	int rc;
2973*29cd6591SDaniel Colascione 
2974*29cd6591SDaniel Colascione 	if (unlikely(!selinux_initialized(&selinux_state)))
2975*29cd6591SDaniel Colascione 		return 0;
2976*29cd6591SDaniel Colascione 
2977*29cd6591SDaniel Colascione 	isec = selinux_inode(inode);
2978*29cd6591SDaniel Colascione 
2979*29cd6591SDaniel Colascione 	/*
2980*29cd6591SDaniel Colascione 	 * We only get here once per ephemeral inode.  The inode has
2981*29cd6591SDaniel Colascione 	 * been initialized via inode_alloc_security but is otherwise
2982*29cd6591SDaniel Colascione 	 * untouched.
2983*29cd6591SDaniel Colascione 	 */
2984*29cd6591SDaniel Colascione 
2985*29cd6591SDaniel Colascione 	if (context_inode) {
2986*29cd6591SDaniel Colascione 		struct inode_security_struct *context_isec =
2987*29cd6591SDaniel Colascione 			selinux_inode(context_inode);
2988*29cd6591SDaniel Colascione 		if (context_isec->initialized != LABEL_INITIALIZED) {
2989*29cd6591SDaniel Colascione 			pr_err("SELinux:  context_inode is not initialized");
2990*29cd6591SDaniel Colascione 			return -EACCES;
2991*29cd6591SDaniel Colascione 		}
2992*29cd6591SDaniel Colascione 
2993*29cd6591SDaniel Colascione 		isec->sclass = context_isec->sclass;
2994*29cd6591SDaniel Colascione 		isec->sid = context_isec->sid;
2995*29cd6591SDaniel Colascione 	} else {
2996*29cd6591SDaniel Colascione 		isec->sclass = SECCLASS_ANON_INODE;
2997*29cd6591SDaniel Colascione 		rc = security_transition_sid(
2998*29cd6591SDaniel Colascione 			&selinux_state, tsec->sid, tsec->sid,
2999*29cd6591SDaniel Colascione 			isec->sclass, name, &isec->sid);
3000*29cd6591SDaniel Colascione 		if (rc)
3001*29cd6591SDaniel Colascione 			return rc;
3002*29cd6591SDaniel Colascione 	}
3003*29cd6591SDaniel Colascione 
3004*29cd6591SDaniel Colascione 	isec->initialized = LABEL_INITIALIZED;
3005*29cd6591SDaniel Colascione 	/*
3006*29cd6591SDaniel Colascione 	 * Now that we've initialized security, check whether we're
3007*29cd6591SDaniel Colascione 	 * allowed to actually create this type of anonymous inode.
3008*29cd6591SDaniel Colascione 	 */
3009*29cd6591SDaniel Colascione 
3010*29cd6591SDaniel Colascione 	ad.type = LSM_AUDIT_DATA_INODE;
3011*29cd6591SDaniel Colascione 	ad.u.inode = inode;
3012*29cd6591SDaniel Colascione 
3013*29cd6591SDaniel Colascione 	return avc_has_perm(&selinux_state,
3014*29cd6591SDaniel Colascione 			    tsec->sid,
3015*29cd6591SDaniel Colascione 			    isec->sid,
3016*29cd6591SDaniel Colascione 			    isec->sclass,
3017*29cd6591SDaniel Colascione 			    FILE__CREATE,
3018*29cd6591SDaniel Colascione 			    &ad);
3019*29cd6591SDaniel Colascione }
3020*29cd6591SDaniel Colascione 
30214acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
30221da177e4SLinus Torvalds {
30231da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30271da177e4SLinus Torvalds {
30281da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
30291da177e4SLinus Torvalds }
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
30321da177e4SLinus Torvalds {
30331da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
30341da177e4SLinus Torvalds }
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
30371da177e4SLinus Torvalds {
30381da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
30391da177e4SLinus Torvalds }
30401da177e4SLinus Torvalds 
304118bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
30421da177e4SLinus Torvalds {
30431da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
30471da177e4SLinus Torvalds {
30481da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
30491da177e4SLinus Torvalds }
30501da177e4SLinus Torvalds 
30511a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
30521da177e4SLinus Torvalds {
30531da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
30571da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
30581da177e4SLinus Torvalds {
30591da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
30601da177e4SLinus Torvalds }
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
30631da177e4SLinus Torvalds {
306488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
306588e67f3bSDavid Howells 
30662875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
30671da177e4SLinus Torvalds }
30681da177e4SLinus Torvalds 
3069bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3070bda0be7aSNeilBrown 				     bool rcu)
30711da177e4SLinus Torvalds {
307288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3073bda0be7aSNeilBrown 	struct common_audit_data ad;
3074bda0be7aSNeilBrown 	struct inode_security_struct *isec;
3075bda0be7aSNeilBrown 	u32 sid;
30761da177e4SLinus Torvalds 
3077bda0be7aSNeilBrown 	validate_creds(cred);
3078bda0be7aSNeilBrown 
3079bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
3080bda0be7aSNeilBrown 	ad.u.dentry = dentry;
3081bda0be7aSNeilBrown 	sid = cred_sid(cred);
30825d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
30835d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
30845d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
3085bda0be7aSNeilBrown 
30861a37079cSStephen Smalley 	return avc_has_perm_flags(&selinux_state,
30871a37079cSStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
30881a37079cSStephen Smalley 				  rcu ? MAY_NOT_BLOCK : 0);
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
3091d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
3092d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
30930188d5c0SStephen Smalley 					   int result)
3094d4cf970dSEric Paris {
3095d4cf970dSEric Paris 	struct common_audit_data ad;
309680788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
3097d4cf970dSEric Paris 	int rc;
3098d4cf970dSEric Paris 
309950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
3100d4cf970dSEric Paris 	ad.u.inode = inode;
3101d4cf970dSEric Paris 
31026b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
31036b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
31040188d5c0SStephen Smalley 			    audited, denied, result, &ad);
3105d4cf970dSEric Paris 	if (rc)
3106d4cf970dSEric Paris 		return rc;
3107d4cf970dSEric Paris 	return 0;
3108d4cf970dSEric Paris }
3109d4cf970dSEric Paris 
3110e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
31111da177e4SLinus Torvalds {
311288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3113b782e0a6SEric Paris 	u32 perms;
3114b782e0a6SEric Paris 	bool from_access;
31155298d0b9SStephen Smalley 	bool no_block = mask & MAY_NOT_BLOCK;
31162e334057SEric Paris 	struct inode_security_struct *isec;
31172e334057SEric Paris 	u32 sid;
31182e334057SEric Paris 	struct av_decision avd;
31192e334057SEric Paris 	int rc, rc2;
31202e334057SEric Paris 	u32 audited, denied;
31211da177e4SLinus Torvalds 
3122b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3123d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3124d09ca739SEric Paris 
31251da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3126b782e0a6SEric Paris 	if (!mask)
31271da177e4SLinus Torvalds 		return 0;
31281da177e4SLinus Torvalds 
31292e334057SEric Paris 	validate_creds(cred);
3130b782e0a6SEric Paris 
31312e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
31322e334057SEric Paris 		return 0;
3133b782e0a6SEric Paris 
3134b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3135b782e0a6SEric Paris 
31362e334057SEric Paris 	sid = cred_sid(cred);
31375298d0b9SStephen Smalley 	isec = inode_security_rcu(inode, no_block);
31385d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31395d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
31402e334057SEric Paris 
31416b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
31423a28cff3SStephen Smalley 				  sid, isec->sid, isec->sclass, perms,
31435298d0b9SStephen Smalley 				  no_block ? AVC_NONBLOCKING : 0,
31443a28cff3SStephen Smalley 				  &avd);
31452e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
31462e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
31472e334057SEric Paris 				     &denied);
31482e334057SEric Paris 	if (likely(!audited))
31492e334057SEric Paris 		return rc;
31502e334057SEric Paris 
31510188d5c0SStephen Smalley 	/* fall back to ref-walk if we have to generate audit */
31525298d0b9SStephen Smalley 	if (no_block)
31530188d5c0SStephen Smalley 		return -ECHILD;
31540188d5c0SStephen Smalley 
31550188d5c0SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
31562e334057SEric Paris 	if (rc2)
31572e334057SEric Paris 		return rc2;
31582e334057SEric Paris 	return rc;
31591da177e4SLinus Torvalds }
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
31621da177e4SLinus Torvalds {
316388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3164ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3165bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
316695dbf739SEric Paris 	__u32 av = FILE__WRITE;
31671da177e4SLinus Torvalds 
3168bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3169bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3170bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3171bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3172bc6a6008SAmerigo Wang 		if (!ia_valid)
31731da177e4SLinus Torvalds 			return 0;
3174bc6a6008SAmerigo Wang 	}
31751da177e4SLinus Torvalds 
3176bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3177bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
31782875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
31791da177e4SLinus Torvalds 
3180aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3181ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3182ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3183ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
318495dbf739SEric Paris 		av |= FILE__OPEN;
318595dbf739SEric Paris 
318695dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
31871da177e4SLinus Torvalds }
31881da177e4SLinus Torvalds 
31893f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
31901da177e4SLinus Torvalds {
31913f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
31921da177e4SLinus Torvalds }
31931da177e4SLinus Torvalds 
3194db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3195db59000aSStephen Smalley {
3196db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3197c1a85a00SMicah Morton 	unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3198db59000aSStephen Smalley 
3199c1a85a00SMicah Morton 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3200db59000aSStephen Smalley 		return false;
3201c1a85a00SMicah Morton 	if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3202db59000aSStephen Smalley 		return false;
3203db59000aSStephen Smalley 	return true;
3204db59000aSStephen Smalley }
3205db59000aSStephen Smalley 
32068f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
32078f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
32081da177e4SLinus Torvalds {
3209c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
321020cdef8dSPaul Moore 	struct inode_security_struct *isec;
32111da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
32122bf49690SThomas Liu 	struct common_audit_data ad;
3213275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
32141da177e4SLinus Torvalds 	int rc = 0;
32151da177e4SLinus Torvalds 
32166b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32176b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
32186b240306SEric W. Biederman 		if (rc)
32196b240306SEric W. Biederman 			return rc;
32206b240306SEric W. Biederman 
32216b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
32226b240306SEric W. Biederman 		   ordinary setattr permission. */
32236b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32246b240306SEric W. Biederman 	}
32251da177e4SLinus Torvalds 
322665cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
32273e3e24b4SJonathan Lebon 		return (inode_owner_or_capable(inode) ? 0 : -EPERM);
32283e3e24b4SJonathan Lebon 
32291da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
323012f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
32311da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32321da177e4SLinus Torvalds 
32332e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
32341da177e4SLinus Torvalds 		return -EPERM;
32351da177e4SLinus Torvalds 
323650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3237a269434dSEric Paris 	ad.u.dentry = dentry;
32381da177e4SLinus Torvalds 
323920cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
32406b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32416b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
32421da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
32431da177e4SLinus Torvalds 	if (rc)
32441da177e4SLinus Torvalds 		return rc;
32451da177e4SLinus Torvalds 
3246aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3247aa8e712cSStephen Smalley 				     GFP_KERNEL);
324812b29f34SStephen Smalley 	if (rc == -EINVAL) {
3249db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3250d6ea83ecSEric Paris 			struct audit_buffer *ab;
3251d6ea83ecSEric Paris 			size_t audit_size;
3252d6ea83ecSEric Paris 
3253d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3254d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3255e3fea3f7SAl Viro 			if (value) {
3256add24372SColin Ian King 				const char *str = value;
3257add24372SColin Ian King 
3258d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3259d6ea83ecSEric Paris 					audit_size = size - 1;
3260d6ea83ecSEric Paris 				else
3261d6ea83ecSEric Paris 					audit_size = size;
3262e3fea3f7SAl Viro 			} else {
3263e3fea3f7SAl Viro 				audit_size = 0;
3264e3fea3f7SAl Viro 			}
3265cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3266cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3267d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3268d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3269d6ea83ecSEric Paris 			audit_log_end(ab);
3270d6ea83ecSEric Paris 
327112b29f34SStephen Smalley 			return rc;
3272d6ea83ecSEric Paris 		}
3273aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3274aa8e712cSStephen Smalley 						   size, &newsid);
327512b29f34SStephen Smalley 	}
32761da177e4SLinus Torvalds 	if (rc)
32771da177e4SLinus Torvalds 		return rc;
32781da177e4SLinus Torvalds 
32796b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32806b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
32811da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
32821da177e4SLinus Torvalds 	if (rc)
32831da177e4SLinus Torvalds 		return rc;
32841da177e4SLinus Torvalds 
3285aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3286aa8e712cSStephen Smalley 					  sid, isec->sclass);
32871da177e4SLinus Torvalds 	if (rc)
32881da177e4SLinus Torvalds 		return rc;
32891da177e4SLinus Torvalds 
32906b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
32916b6bc620SStephen Smalley 			    newsid,
32921da177e4SLinus Torvalds 			    sbsec->sid,
32931da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
32941da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
32951da177e4SLinus Torvalds 			    &ad);
32961da177e4SLinus Torvalds }
32971da177e4SLinus Torvalds 
32988f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
32998f0cfa52SDavid Howells 					const void *value, size_t size,
33008f0cfa52SDavid Howells 					int flags)
33011da177e4SLinus Torvalds {
3302c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
330320cdef8dSPaul Moore 	struct inode_security_struct *isec;
33041da177e4SLinus Torvalds 	u32 newsid;
33051da177e4SLinus Torvalds 	int rc;
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33081da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
33091da177e4SLinus Torvalds 		return;
33101da177e4SLinus Torvalds 	}
33111da177e4SLinus Torvalds 
331265cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
33133e3e24b4SJonathan Lebon 		/* If we haven't even been initialized, then we can't validate
33143e3e24b4SJonathan Lebon 		 * against a policy, so leave the label as invalid. It may
33153e3e24b4SJonathan Lebon 		 * resolve to a valid label on the next revalidation try if
33163e3e24b4SJonathan Lebon 		 * we've since initialized.
33173e3e24b4SJonathan Lebon 		 */
33183e3e24b4SJonathan Lebon 		return;
33193e3e24b4SJonathan Lebon 	}
33203e3e24b4SJonathan Lebon 
3321aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3322aa8e712cSStephen Smalley 					   &newsid);
33231da177e4SLinus Torvalds 	if (rc) {
3324c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
332512b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
332612b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
33271da177e4SLinus Torvalds 		return;
33281da177e4SLinus Torvalds 	}
33291da177e4SLinus Torvalds 
333020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
33319287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3332aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
33331da177e4SLinus Torvalds 	isec->sid = newsid;
33346f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
33359287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3336aa9c2669SDavid Quigley 
33371da177e4SLinus Torvalds 	return;
33381da177e4SLinus Torvalds }
33391da177e4SLinus Torvalds 
33408f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
33411da177e4SLinus Torvalds {
334288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
334388e67f3bSDavid Howells 
33442875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33451da177e4SLinus Torvalds }
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
33481da177e4SLinus Torvalds {
334988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
335088e67f3bSDavid Howells 
33512875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33521da177e4SLinus Torvalds }
33531da177e4SLinus Torvalds 
33548f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
33551da177e4SLinus Torvalds {
33566b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33576b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
33586b240306SEric W. Biederman 		if (rc)
33596b240306SEric W. Biederman 			return rc;
33606b240306SEric W. Biederman 
33616b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
33626b240306SEric W. Biederman 		   ordinary setattr permission. */
33636b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
33646b240306SEric W. Biederman 	}
33651da177e4SLinus Torvalds 
33669530a3e0SStephen Smalley 	if (!selinux_initialized(&selinux_state))
33679530a3e0SStephen Smalley 		return 0;
33689530a3e0SStephen Smalley 
33691da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
33701da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
33711da177e4SLinus Torvalds 	return -EACCES;
33721da177e4SLinus Torvalds }
33731da177e4SLinus Torvalds 
3374ac5656d8SAaron Goidel static int selinux_path_notify(const struct path *path, u64 mask,
3375ac5656d8SAaron Goidel 						unsigned int obj_type)
3376ac5656d8SAaron Goidel {
3377ac5656d8SAaron Goidel 	int ret;
3378ac5656d8SAaron Goidel 	u32 perm;
3379ac5656d8SAaron Goidel 
3380ac5656d8SAaron Goidel 	struct common_audit_data ad;
3381ac5656d8SAaron Goidel 
3382ac5656d8SAaron Goidel 	ad.type = LSM_AUDIT_DATA_PATH;
3383ac5656d8SAaron Goidel 	ad.u.path = *path;
3384ac5656d8SAaron Goidel 
3385ac5656d8SAaron Goidel 	/*
3386ac5656d8SAaron Goidel 	 * Set permission needed based on the type of mark being set.
3387ac5656d8SAaron Goidel 	 * Performs an additional check for sb watches.
3388ac5656d8SAaron Goidel 	 */
3389ac5656d8SAaron Goidel 	switch (obj_type) {
3390ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3391ac5656d8SAaron Goidel 		perm = FILE__WATCH_MOUNT;
3392ac5656d8SAaron Goidel 		break;
3393ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_SB:
3394ac5656d8SAaron Goidel 		perm = FILE__WATCH_SB;
3395ac5656d8SAaron Goidel 		ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3396ac5656d8SAaron Goidel 						FILESYSTEM__WATCH, &ad);
3397ac5656d8SAaron Goidel 		if (ret)
3398ac5656d8SAaron Goidel 			return ret;
3399ac5656d8SAaron Goidel 		break;
3400ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_INODE:
3401ac5656d8SAaron Goidel 		perm = FILE__WATCH;
3402ac5656d8SAaron Goidel 		break;
3403ac5656d8SAaron Goidel 	default:
3404ac5656d8SAaron Goidel 		return -EINVAL;
3405ac5656d8SAaron Goidel 	}
3406ac5656d8SAaron Goidel 
3407ac5656d8SAaron Goidel 	/* blocking watches require the file:watch_with_perm permission */
3408ac5656d8SAaron Goidel 	if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3409ac5656d8SAaron Goidel 		perm |= FILE__WATCH_WITH_PERM;
3410ac5656d8SAaron Goidel 
3411ac5656d8SAaron Goidel 	/* watches on read-like events need the file:watch_reads permission */
3412ac5656d8SAaron Goidel 	if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3413ac5656d8SAaron Goidel 		perm |= FILE__WATCH_READS;
3414ac5656d8SAaron Goidel 
3415ac5656d8SAaron Goidel 	return path_has_perm(current_cred(), path, perm);
3416ac5656d8SAaron Goidel }
3417ac5656d8SAaron Goidel 
3418d381d8a9SJames Morris /*
3419abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3420d381d8a9SJames Morris  *
3421d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3422d381d8a9SJames Morris  */
3423ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
34241da177e4SLinus Torvalds {
342542492594SDavid P. Quigley 	u32 size;
342642492594SDavid P. Quigley 	int error;
342742492594SDavid P. Quigley 	char *context = NULL;
342820cdef8dSPaul Moore 	struct inode_security_struct *isec;
34291da177e4SLinus Torvalds 
3430c8e22261SJonathan Lebon 	/*
3431c8e22261SJonathan Lebon 	 * If we're not initialized yet, then we can't validate contexts, so
3432c8e22261SJonathan Lebon 	 * just let vfs_getxattr fall back to using the on-disk xattr.
3433c8e22261SJonathan Lebon 	 */
3434c8e22261SJonathan Lebon 	if (!selinux_initialized(&selinux_state) ||
3435c8e22261SJonathan Lebon 	    strcmp(name, XATTR_SELINUX_SUFFIX))
34368c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
34371da177e4SLinus Torvalds 
3438abc69bb6SStephen Smalley 	/*
3439abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3440abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3441abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3442abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3443abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3444abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3445abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3446abc69bb6SStephen Smalley 	 */
344720cdef8dSPaul Moore 	isec = inode_security(inode);
3448db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3449aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3450aa8e712cSStephen Smalley 						      isec->sid, &context,
3451abc69bb6SStephen Smalley 						      &size);
3452abc69bb6SStephen Smalley 	else
3453aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3454aa8e712cSStephen Smalley 						&context, &size);
345542492594SDavid P. Quigley 	if (error)
345642492594SDavid P. Quigley 		return error;
345742492594SDavid P. Quigley 	error = size;
345842492594SDavid P. Quigley 	if (alloc) {
345942492594SDavid P. Quigley 		*buffer = context;
346042492594SDavid P. Quigley 		goto out_nofree;
346142492594SDavid P. Quigley 	}
346242492594SDavid P. Quigley 	kfree(context);
346342492594SDavid P. Quigley out_nofree:
346442492594SDavid P. Quigley 	return error;
34651da177e4SLinus Torvalds }
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
34681da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
34691da177e4SLinus Torvalds {
34702c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
347153e0c2aaSOndrej Mosnacek 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
34721da177e4SLinus Torvalds 	u32 newsid;
34731da177e4SLinus Torvalds 	int rc;
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34761da177e4SLinus Torvalds 		return -EOPNOTSUPP;
34771da177e4SLinus Torvalds 
347853e0c2aaSOndrej Mosnacek 	if (!(sbsec->flags & SBLABEL_MNT))
347953e0c2aaSOndrej Mosnacek 		return -EOPNOTSUPP;
348053e0c2aaSOndrej Mosnacek 
34811da177e4SLinus Torvalds 	if (!value || !size)
34821da177e4SLinus Torvalds 		return -EACCES;
34831da177e4SLinus Torvalds 
3484aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3485aa8e712cSStephen Smalley 				     GFP_KERNEL);
34861da177e4SLinus Torvalds 	if (rc)
34871da177e4SLinus Torvalds 		return rc;
34881da177e4SLinus Torvalds 
34899287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3490aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
34911da177e4SLinus Torvalds 	isec->sid = newsid;
34926f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
34939287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
34941da177e4SLinus Torvalds 	return 0;
34951da177e4SLinus Torvalds }
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
34981da177e4SLinus Torvalds {
34991da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
3500a9ffe682SAmir Goldstein 
3501a9ffe682SAmir Goldstein 	if (!selinux_initialized(&selinux_state))
3502a9ffe682SAmir Goldstein 		return 0;
3503a9ffe682SAmir Goldstein 
35041da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
35051da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
35061da177e4SLinus Torvalds 	return len;
35071da177e4SLinus Torvalds }
35081da177e4SLinus Torvalds 
3509d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3510713a04aeSAhmed S. Darwish {
3511e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3512713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3513713a04aeSAhmed S. Darwish }
3514713a04aeSAhmed S. Darwish 
351556909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
351656909eb3SVivek Goyal {
351756909eb3SVivek Goyal 	u32 sid;
351856909eb3SVivek Goyal 	struct task_security_struct *tsec;
351956909eb3SVivek Goyal 	struct cred *new_creds = *new;
352056909eb3SVivek Goyal 
352156909eb3SVivek Goyal 	if (new_creds == NULL) {
352256909eb3SVivek Goyal 		new_creds = prepare_creds();
352356909eb3SVivek Goyal 		if (!new_creds)
352456909eb3SVivek Goyal 			return -ENOMEM;
352556909eb3SVivek Goyal 	}
352656909eb3SVivek Goyal 
35270c6cfa62SCasey Schaufler 	tsec = selinux_cred(new_creds);
352856909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
352956909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
353056909eb3SVivek Goyal 	tsec->create_sid = sid;
353156909eb3SVivek Goyal 	*new = new_creds;
353256909eb3SVivek Goyal 	return 0;
353356909eb3SVivek Goyal }
353456909eb3SVivek Goyal 
353519472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
353619472b69SVivek Goyal {
353719472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
353819472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
353919472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
354019472b69SVivek Goyal 	 */
354119472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
354219472b69SVivek Goyal 		return 1; /* Discard */
354319472b69SVivek Goyal 	/*
354419472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
354519472b69SVivek Goyal 	 * by selinux.
354619472b69SVivek Goyal 	 */
354719472b69SVivek Goyal 	return -EOPNOTSUPP;
354819472b69SVivek Goyal }
354919472b69SVivek Goyal 
3550ec882da5SOndrej Mosnacek /* kernfs node operations */
3551ec882da5SOndrej Mosnacek 
3552c72c4cdeSYueHaibing static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3553ec882da5SOndrej Mosnacek 					struct kernfs_node *kn)
3554ec882da5SOndrej Mosnacek {
3555169ce0c0SStephen Smalley 	const struct task_security_struct *tsec = selinux_cred(current_cred());
3556ec882da5SOndrej Mosnacek 	u32 parent_sid, newsid, clen;
3557ec882da5SOndrej Mosnacek 	int rc;
3558ec882da5SOndrej Mosnacek 	char *context;
3559ec882da5SOndrej Mosnacek 
35601537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3561ec882da5SOndrej Mosnacek 	if (rc == -ENODATA)
3562ec882da5SOndrej Mosnacek 		return 0;
3563ec882da5SOndrej Mosnacek 	else if (rc < 0)
3564ec882da5SOndrej Mosnacek 		return rc;
3565ec882da5SOndrej Mosnacek 
3566ec882da5SOndrej Mosnacek 	clen = (u32)rc;
3567ec882da5SOndrej Mosnacek 	context = kmalloc(clen, GFP_KERNEL);
3568ec882da5SOndrej Mosnacek 	if (!context)
3569ec882da5SOndrej Mosnacek 		return -ENOMEM;
3570ec882da5SOndrej Mosnacek 
35711537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3572ec882da5SOndrej Mosnacek 	if (rc < 0) {
3573ec882da5SOndrej Mosnacek 		kfree(context);
3574ec882da5SOndrej Mosnacek 		return rc;
3575ec882da5SOndrej Mosnacek 	}
3576ec882da5SOndrej Mosnacek 
3577ec882da5SOndrej Mosnacek 	rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3578ec882da5SOndrej Mosnacek 				     GFP_KERNEL);
3579ec882da5SOndrej Mosnacek 	kfree(context);
3580ec882da5SOndrej Mosnacek 	if (rc)
3581ec882da5SOndrej Mosnacek 		return rc;
3582ec882da5SOndrej Mosnacek 
3583ec882da5SOndrej Mosnacek 	if (tsec->create_sid) {
3584ec882da5SOndrej Mosnacek 		newsid = tsec->create_sid;
3585ec882da5SOndrej Mosnacek 	} else {
3586ec882da5SOndrej Mosnacek 		u16 secclass = inode_mode_to_security_class(kn->mode);
3587ec882da5SOndrej Mosnacek 		struct qstr q;
3588ec882da5SOndrej Mosnacek 
3589ec882da5SOndrej Mosnacek 		q.name = kn->name;
3590ec882da5SOndrej Mosnacek 		q.hash_len = hashlen_string(kn_dir, kn->name);
3591ec882da5SOndrej Mosnacek 
3592ec882da5SOndrej Mosnacek 		rc = security_transition_sid(&selinux_state, tsec->sid,
3593ec882da5SOndrej Mosnacek 					     parent_sid, secclass, &q,
3594ec882da5SOndrej Mosnacek 					     &newsid);
3595ec882da5SOndrej Mosnacek 		if (rc)
3596ec882da5SOndrej Mosnacek 			return rc;
3597ec882da5SOndrej Mosnacek 	}
3598ec882da5SOndrej Mosnacek 
3599ec882da5SOndrej Mosnacek 	rc = security_sid_to_context_force(&selinux_state, newsid,
3600ec882da5SOndrej Mosnacek 					   &context, &clen);
3601ec882da5SOndrej Mosnacek 	if (rc)
3602ec882da5SOndrej Mosnacek 		return rc;
3603ec882da5SOndrej Mosnacek 
36041537ad15SOndrej Mosnacek 	rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3605ec882da5SOndrej Mosnacek 			      XATTR_CREATE);
3606ec882da5SOndrej Mosnacek 	kfree(context);
3607ec882da5SOndrej Mosnacek 	return rc;
3608ec882da5SOndrej Mosnacek }
3609ec882da5SOndrej Mosnacek 
3610ec882da5SOndrej Mosnacek 
36111da177e4SLinus Torvalds /* file security operations */
36121da177e4SLinus Torvalds 
3613788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
36141da177e4SLinus Torvalds {
361588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3616496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
36191da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
36201da177e4SLinus Torvalds 		mask |= MAY_APPEND;
36211da177e4SLinus Torvalds 
3622389fb800SPaul Moore 	return file_has_perm(cred, file,
36231da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
36241da177e4SLinus Torvalds }
36251da177e4SLinus Torvalds 
3626788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3627788e7dd4SYuichi Nakamura {
3628496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3629bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3630b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
363120dda18bSStephen Smalley 	u32 sid = current_sid();
363220dda18bSStephen Smalley 
3633389fb800SPaul Moore 	if (!mask)
3634788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3635788e7dd4SYuichi Nakamura 		return 0;
3636788e7dd4SYuichi Nakamura 
3637b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
363820dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
36396b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
364083d49856SEric Paris 		/* No change since file_open check. */
364120dda18bSStephen Smalley 		return 0;
364220dda18bSStephen Smalley 
3643788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3644788e7dd4SYuichi Nakamura }
3645788e7dd4SYuichi Nakamura 
36461da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
36471da177e4SLinus Torvalds {
3648cb89e246SPaul Moore 	struct file_security_struct *fsec = selinux_file(file);
3649cb89e246SPaul Moore 	u32 sid = current_sid();
3650cb89e246SPaul Moore 
3651cb89e246SPaul Moore 	fsec->sid = sid;
3652cb89e246SPaul Moore 	fsec->fown_sid = sid;
3653cb89e246SPaul Moore 
3654cb89e246SPaul Moore 	return 0;
36551da177e4SLinus Torvalds }
36561da177e4SLinus Torvalds 
3657fa1aa143SJeff Vander Stoep /*
3658fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3659fa1aa143SJeff Vander Stoep  * operation to an inode.
3660fa1aa143SJeff Vander Stoep  */
36611d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3662fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3663fa1aa143SJeff Vander Stoep {
3664fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3665bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3666fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
366720cdef8dSPaul Moore 	struct inode_security_struct *isec;
3668fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3669fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3670fa1aa143SJeff Vander Stoep 	int rc;
3671fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3672fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3673fa1aa143SJeff Vander Stoep 
3674fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3675fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3676fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3677fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3678fa1aa143SJeff Vander Stoep 
3679fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
36806b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36816b6bc620SStephen Smalley 				  ssid, fsec->sid,
3682fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3683fa1aa143SJeff Vander Stoep 				FD__USE,
3684fa1aa143SJeff Vander Stoep 				&ad);
3685fa1aa143SJeff Vander Stoep 		if (rc)
3686fa1aa143SJeff Vander Stoep 			goto out;
3687fa1aa143SJeff Vander Stoep 	}
3688fa1aa143SJeff Vander Stoep 
3689fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3690fa1aa143SJeff Vander Stoep 		return 0;
3691fa1aa143SJeff Vander Stoep 
369220cdef8dSPaul Moore 	isec = inode_security(inode);
36936b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
36946b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3695fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3696fa1aa143SJeff Vander Stoep out:
3697fa1aa143SJeff Vander Stoep 	return rc;
3698fa1aa143SJeff Vander Stoep }
3699fa1aa143SJeff Vander Stoep 
37001da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
37011da177e4SLinus Torvalds 			      unsigned long arg)
37021da177e4SLinus Torvalds {
370388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
37040b24dcb7SEric Paris 	int error = 0;
37051da177e4SLinus Torvalds 
37060b24dcb7SEric Paris 	switch (cmd) {
37070b24dcb7SEric Paris 	case FIONREAD:
37080b24dcb7SEric Paris 	case FIBMAP:
37090b24dcb7SEric Paris 	case FIGETBSZ:
37102f99c369SAl Viro 	case FS_IOC_GETFLAGS:
37112f99c369SAl Viro 	case FS_IOC_GETVERSION:
37120b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
37130b24dcb7SEric Paris 		break;
37141da177e4SLinus Torvalds 
37152f99c369SAl Viro 	case FS_IOC_SETFLAGS:
37162f99c369SAl Viro 	case FS_IOC_SETVERSION:
37170b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
37180b24dcb7SEric Paris 		break;
37190b24dcb7SEric Paris 
37200b24dcb7SEric Paris 	/* sys_ioctl() checks */
37210b24dcb7SEric Paris 	case FIONBIO:
37220b24dcb7SEric Paris 	case FIOASYNC:
37230b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
37240b24dcb7SEric Paris 		break;
37250b24dcb7SEric Paris 
37260b24dcb7SEric Paris 	case KDSKBENT:
37270b24dcb7SEric Paris 	case KDSKBSENT:
37286a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3729c1a85a00SMicah Morton 					    CAP_OPT_NONE, true);
37300b24dcb7SEric Paris 		break;
37310b24dcb7SEric Paris 
37320b24dcb7SEric Paris 	/* default case assumes that the command will go
37330b24dcb7SEric Paris 	 * to the file's ioctl() function.
37340b24dcb7SEric Paris 	 */
37350b24dcb7SEric Paris 	default:
3736fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
37370b24dcb7SEric Paris 	}
37380b24dcb7SEric Paris 	return error;
37391da177e4SLinus Torvalds }
37401da177e4SLinus Torvalds 
3741b78b7d59SStephen Smalley static int default_noexec __ro_after_init;
3742fcaaade1SStephen Smalley 
37431da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
37441da177e4SLinus Torvalds {
374588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3746be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3747d84f4f99SDavid Howells 	int rc = 0;
374888e67f3bSDavid Howells 
3749fcaaade1SStephen Smalley 	if (default_noexec &&
3750892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3751892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
37521da177e4SLinus Torvalds 		/*
37531da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
37541da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
37551da177e4SLinus Torvalds 		 * This has an additional check.
37561da177e4SLinus Torvalds 		 */
37576b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
37586b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3759be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
37601da177e4SLinus Torvalds 		if (rc)
3761d84f4f99SDavid Howells 			goto error;
37621da177e4SLinus Torvalds 	}
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 	if (file) {
37651da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
37661da177e4SLinus Torvalds 		u32 av = FILE__READ;
37671da177e4SLinus Torvalds 
37681da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
37691da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
37701da177e4SLinus Torvalds 			av |= FILE__WRITE;
37711da177e4SLinus Torvalds 
37721da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
37731da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
37741da177e4SLinus Torvalds 
377588e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
37761da177e4SLinus Torvalds 	}
3777d84f4f99SDavid Howells 
3778d84f4f99SDavid Howells error:
3779d84f4f99SDavid Howells 	return rc;
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
3782e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
37831da177e4SLinus Torvalds {
3784b1d9e6b0SCasey Schaufler 	int rc = 0;
378598883bfdSPaul Moore 
378698883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
378798883bfdSPaul Moore 		u32 sid = current_sid();
37886b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
37896b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
379098883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
379198883bfdSPaul Moore 	}
379298883bfdSPaul Moore 
379398883bfdSPaul Moore 	return rc;
3794e5467859SAl Viro }
37951da177e4SLinus Torvalds 
3796e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3797e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3798e5467859SAl Viro {
37993ba4bf5fSStephen Smalley 	struct common_audit_data ad;
38003ba4bf5fSStephen Smalley 	int rc;
38013ba4bf5fSStephen Smalley 
38023ba4bf5fSStephen Smalley 	if (file) {
38033ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
38043ba4bf5fSStephen Smalley 		ad.u.file = file;
38053ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
38063ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
38073ba4bf5fSStephen Smalley 		if (rc)
38083ba4bf5fSStephen Smalley 			return rc;
38093ba4bf5fSStephen Smalley 	}
38103ba4bf5fSStephen Smalley 
38118861d0afSLakshmi Ramasubramanian 	if (checkreqprot_get(&selinux_state))
38121da177e4SLinus Torvalds 		prot = reqprot;
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
38151da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
38161da177e4SLinus Torvalds }
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
38191da177e4SLinus Torvalds 				 unsigned long reqprot,
38201da177e4SLinus Torvalds 				 unsigned long prot)
38211da177e4SLinus Torvalds {
382288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3823be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
38241da177e4SLinus Torvalds 
38258861d0afSLakshmi Ramasubramanian 	if (checkreqprot_get(&selinux_state))
38261da177e4SLinus Torvalds 		prot = reqprot;
38271da177e4SLinus Torvalds 
3828fcaaade1SStephen Smalley 	if (default_noexec &&
3829fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3830d541bbeeSJames Morris 		int rc = 0;
3831db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3832db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
38336b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
38346b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3835be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3836db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3837c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3838c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3839d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
38406b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
38416b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3842be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3843db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3844db4c9641SStephen Smalley 			/*
3845db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3846db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3847db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3848db4c9641SStephen Smalley 			 * modified content.  This typically should only
3849db4c9641SStephen Smalley 			 * occur for text relocations.
3850db4c9641SStephen Smalley 			 */
3851d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3852db4c9641SStephen Smalley 		}
38536b992197SLorenzo Hernandez García-Hierro 		if (rc)
38546b992197SLorenzo Hernandez García-Hierro 			return rc;
38556b992197SLorenzo Hernandez García-Hierro 	}
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
38581da177e4SLinus Torvalds }
38591da177e4SLinus Torvalds 
38601da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
38611da177e4SLinus Torvalds {
386288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
386388e67f3bSDavid Howells 
386488e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
38671da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
38681da177e4SLinus Torvalds 			      unsigned long arg)
38691da177e4SLinus Torvalds {
387088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
38711da177e4SLinus Torvalds 	int err = 0;
38721da177e4SLinus Torvalds 
38731da177e4SLinus Torvalds 	switch (cmd) {
38741da177e4SLinus Torvalds 	case F_SETFL:
38751da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
387688e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
38771da177e4SLinus Torvalds 			break;
38781da177e4SLinus Torvalds 		}
3879df561f66SGustavo A. R. Silva 		fallthrough;
38801da177e4SLinus Torvalds 	case F_SETOWN:
38811da177e4SLinus Torvalds 	case F_SETSIG:
38821da177e4SLinus Torvalds 	case F_GETFL:
38831da177e4SLinus Torvalds 	case F_GETOWN:
38841da177e4SLinus Torvalds 	case F_GETSIG:
38851d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
38861da177e4SLinus Torvalds 		/* Just check FD__USE permission */
388788e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
38881da177e4SLinus Torvalds 		break;
38891da177e4SLinus Torvalds 	case F_GETLK:
38901da177e4SLinus Torvalds 	case F_SETLK:
38911da177e4SLinus Torvalds 	case F_SETLKW:
38920d3f7a2dSJeff Layton 	case F_OFD_GETLK:
38930d3f7a2dSJeff Layton 	case F_OFD_SETLK:
38940d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
38951da177e4SLinus Torvalds #if BITS_PER_LONG == 32
38961da177e4SLinus Torvalds 	case F_GETLK64:
38971da177e4SLinus Torvalds 	case F_SETLK64:
38981da177e4SLinus Torvalds 	case F_SETLKW64:
38991da177e4SLinus Torvalds #endif
390088e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
39011da177e4SLinus Torvalds 		break;
39021da177e4SLinus Torvalds 	}
39031da177e4SLinus Torvalds 
39041da177e4SLinus Torvalds 	return err;
39051da177e4SLinus Torvalds }
39061da177e4SLinus Torvalds 
3907e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
39081da177e4SLinus Torvalds {
39091da177e4SLinus Torvalds 	struct file_security_struct *fsec;
39101da177e4SLinus Torvalds 
3911bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
3912275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
39131da177e4SLinus Torvalds }
39141da177e4SLinus Torvalds 
39151da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
39161da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
39171da177e4SLinus Torvalds {
39181da177e4SLinus Torvalds 	struct file *file;
391965c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
39201da177e4SLinus Torvalds 	u32 perm;
39211da177e4SLinus Torvalds 	struct file_security_struct *fsec;
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3924b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
39251da177e4SLinus Torvalds 
3926bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
39271da177e4SLinus Torvalds 
39281da177e4SLinus Torvalds 	if (!signum)
39291da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
39301da177e4SLinus Torvalds 	else
39311da177e4SLinus Torvalds 		perm = signal_to_av(signum);
39321da177e4SLinus Torvalds 
39336b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39346b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
39351da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
39391da177e4SLinus Torvalds {
394088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
394188e67f3bSDavid Howells 
394288e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
39431da177e4SLinus Torvalds }
39441da177e4SLinus Torvalds 
394594817692SAl Viro static int selinux_file_open(struct file *file)
3946788e7dd4SYuichi Nakamura {
3947788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3948788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3949d84f4f99SDavid Howells 
3950bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
395183da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3952788e7dd4SYuichi Nakamura 	/*
3953788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3954788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3955788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3956788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3957788e7dd4SYuichi Nakamura 	 * struct as its SID.
3958788e7dd4SYuichi Nakamura 	 */
3959788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
39606b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3961788e7dd4SYuichi Nakamura 	/*
3962788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3963788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3964788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3965788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3966788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3967788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3968788e7dd4SYuichi Nakamura 	 */
396994817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3970788e7dd4SYuichi Nakamura }
3971788e7dd4SYuichi Nakamura 
39721da177e4SLinus Torvalds /* task security operations */
39731da177e4SLinus Torvalds 
3974a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3975a79be238STetsuo Handa 			      unsigned long clone_flags)
39761da177e4SLinus Torvalds {
3977be0554c9SStephen Smalley 	u32 sid = current_sid();
3978be0554c9SStephen Smalley 
39796b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39806b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
39811da177e4SLinus Torvalds }
39821da177e4SLinus Torvalds 
3983f1752eecSDavid Howells /*
3984d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3985d84f4f99SDavid Howells  */
3986d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3987d84f4f99SDavid Howells 				gfp_t gfp)
3988d84f4f99SDavid Howells {
3989bbd3662aSCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
3990bbd3662aSCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
3991d84f4f99SDavid Howells 
3992bbd3662aSCasey Schaufler 	*tsec = *old_tsec;
3993d84f4f99SDavid Howells 	return 0;
3994d84f4f99SDavid Howells }
3995d84f4f99SDavid Howells 
3996d84f4f99SDavid Howells /*
3997ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3998ee18d64cSDavid Howells  */
3999ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
4000ee18d64cSDavid Howells {
40010c6cfa62SCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
40020c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
4003ee18d64cSDavid Howells 
4004ee18d64cSDavid Howells 	*tsec = *old_tsec;
4005ee18d64cSDavid Howells }
4006ee18d64cSDavid Howells 
40073ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
40083ec30113SMatthew Garrett {
40093ec30113SMatthew Garrett 	*secid = cred_sid(c);
40103ec30113SMatthew Garrett }
40113ec30113SMatthew Garrett 
4012ee18d64cSDavid Howells /*
40133a3b7ce9SDavid Howells  * set the security data for a kernel service
40143a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
40153a3b7ce9SDavid Howells  */
40163a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
40173a3b7ce9SDavid Howells {
40180c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
40193a3b7ce9SDavid Howells 	u32 sid = current_sid();
40203a3b7ce9SDavid Howells 	int ret;
40213a3b7ce9SDavid Howells 
40226b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
40236b6bc620SStephen Smalley 			   sid, secid,
40243a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
40253a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
40263a3b7ce9SDavid Howells 			   NULL);
40273a3b7ce9SDavid Howells 	if (ret == 0) {
40283a3b7ce9SDavid Howells 		tsec->sid = secid;
40293a3b7ce9SDavid Howells 		tsec->create_sid = 0;
40303a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
40313a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
40323a3b7ce9SDavid Howells 	}
40333a3b7ce9SDavid Howells 	return ret;
40343a3b7ce9SDavid Howells }
40353a3b7ce9SDavid Howells 
40363a3b7ce9SDavid Howells /*
40373a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
40383a3b7ce9SDavid Howells  * objective context of the specified inode
40393a3b7ce9SDavid Howells  */
40403a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
40413a3b7ce9SDavid Howells {
404283da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
40430c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
40443a3b7ce9SDavid Howells 	u32 sid = current_sid();
40453a3b7ce9SDavid Howells 	int ret;
40463a3b7ce9SDavid Howells 
40476b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
40486b6bc620SStephen Smalley 			   sid, isec->sid,
40493a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
40503a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
40513a3b7ce9SDavid Howells 			   NULL);
40523a3b7ce9SDavid Howells 
40533a3b7ce9SDavid Howells 	if (ret == 0)
40543a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
4055ef57471aSDavid Howells 	return ret;
40563a3b7ce9SDavid Howells }
40573a3b7ce9SDavid Howells 
4058dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
405925354c4fSEric Paris {
4060dd8dbf2eSEric Paris 	struct common_audit_data ad;
4061dd8dbf2eSEric Paris 
406250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
4063dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
4064dd8dbf2eSEric Paris 
40656b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40666b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4067dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
406825354c4fSEric Paris }
406925354c4fSEric Paris 
407061d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
407161d612eaSJeff Vander Stoep {
407261d612eaSJeff Vander Stoep 	struct common_audit_data ad;
407361d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
407461d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
407561d612eaSJeff Vander Stoep 	u32 sid = current_sid();
407661d612eaSJeff Vander Stoep 	int rc;
407761d612eaSJeff Vander Stoep 
407861d612eaSJeff Vander Stoep 	/* init_module */
407961d612eaSJeff Vander Stoep 	if (file == NULL)
40806b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
40816b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
408261d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
408361d612eaSJeff Vander Stoep 
408461d612eaSJeff Vander Stoep 	/* finit_module */
408520cdef8dSPaul Moore 
408643af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
408743af5de7SVivek Goyal 	ad.u.file = file;
408861d612eaSJeff Vander Stoep 
4089bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
409061d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
40916b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
40926b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
409361d612eaSJeff Vander Stoep 		if (rc)
409461d612eaSJeff Vander Stoep 			return rc;
409561d612eaSJeff Vander Stoep 	}
409661d612eaSJeff Vander Stoep 
409720cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
40986b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40996b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
410061d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
410161d612eaSJeff Vander Stoep }
410261d612eaSJeff Vander Stoep 
410361d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
41042039bda1SKees Cook 				    enum kernel_read_file_id id,
41052039bda1SKees Cook 				    bool contents)
410661d612eaSJeff Vander Stoep {
410761d612eaSJeff Vander Stoep 	int rc = 0;
410861d612eaSJeff Vander Stoep 
410961d612eaSJeff Vander Stoep 	switch (id) {
411061d612eaSJeff Vander Stoep 	case READING_MODULE:
41112039bda1SKees Cook 		rc = selinux_kernel_module_from_file(contents ? file : NULL);
411261d612eaSJeff Vander Stoep 		break;
411361d612eaSJeff Vander Stoep 	default:
411461d612eaSJeff Vander Stoep 		break;
411561d612eaSJeff Vander Stoep 	}
411661d612eaSJeff Vander Stoep 
411761d612eaSJeff Vander Stoep 	return rc;
411861d612eaSJeff Vander Stoep }
411961d612eaSJeff Vander Stoep 
4120b64fcae7SKees Cook static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
4121c77b8cdfSMimi Zohar {
4122c77b8cdfSMimi Zohar 	int rc = 0;
4123c77b8cdfSMimi Zohar 
4124c77b8cdfSMimi Zohar 	switch (id) {
4125c77b8cdfSMimi Zohar 	case LOADING_MODULE:
4126c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
4127b2d99bcbSGustavo A. R. Silva 		break;
4128c77b8cdfSMimi Zohar 	default:
4129c77b8cdfSMimi Zohar 		break;
4130c77b8cdfSMimi Zohar 	}
4131c77b8cdfSMimi Zohar 
4132c77b8cdfSMimi Zohar 	return rc;
4133c77b8cdfSMimi Zohar }
4134c77b8cdfSMimi Zohar 
41351da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
41361da177e4SLinus Torvalds {
41376b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41386b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4139be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
41401da177e4SLinus Torvalds }
41411da177e4SLinus Torvalds 
41421da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
41431da177e4SLinus Torvalds {
41446b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41456b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4146be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
41471da177e4SLinus Torvalds }
41481da177e4SLinus Torvalds 
41491da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
41501da177e4SLinus Torvalds {
41516b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41526b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4153be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
41541da177e4SLinus Torvalds }
41551da177e4SLinus Torvalds 
4156f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4157f9008e4cSDavid Quigley {
4158275bb41eSDavid Howells 	*secid = task_sid(p);
4159f9008e4cSDavid Quigley }
4160f9008e4cSDavid Quigley 
41611da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
41621da177e4SLinus Torvalds {
41636b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41646b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4165be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41661da177e4SLinus Torvalds }
41671da177e4SLinus Torvalds 
416803e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
416903e68060SJames Morris {
41706b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41716b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4172be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
417303e68060SJames Morris }
417403e68060SJames Morris 
4175a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
4176a1836a42SDavid Quigley {
41776b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41786b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4179be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
4180a1836a42SDavid Quigley }
4181a1836a42SDavid Quigley 
41824298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4183791ec491SStephen Smalley 				unsigned int flags)
4184791ec491SStephen Smalley {
4185791ec491SStephen Smalley 	u32 av = 0;
4186791ec491SStephen Smalley 
418784e6885eSStephen Smalley 	if (!flags)
418884e6885eSStephen Smalley 		return 0;
4189791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
4190791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
4191791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
4192791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
41936b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41946b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
4195791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
4196791ec491SStephen Smalley }
4197791ec491SStephen Smalley 
41988fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
41998fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
42001da177e4SLinus Torvalds {
42018fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
42041da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
42051da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
4206d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
42071da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
42086b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
42096b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
4210be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
42111da177e4SLinus Torvalds 
42121da177e4SLinus Torvalds 	return 0;
42131da177e4SLinus Torvalds }
42141da177e4SLinus Torvalds 
4215b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
42161da177e4SLinus Torvalds {
42176b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42186b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4219be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
42201da177e4SLinus Torvalds }
42211da177e4SLinus Torvalds 
42221da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
42231da177e4SLinus Torvalds {
42246b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42256b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4226be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
42271da177e4SLinus Torvalds }
42281da177e4SLinus Torvalds 
422935601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
423035601547SDavid Quigley {
42316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42326b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4233be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
423435601547SDavid Quigley }
423535601547SDavid Quigley 
4236ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
42376b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
42381da177e4SLinus Torvalds {
42396b4f3d01SStephen Smalley 	u32 secid;
42401da177e4SLinus Torvalds 	u32 perm;
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds 	if (!sig)
42431da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
42441da177e4SLinus Torvalds 	else
42451da177e4SLinus Torvalds 		perm = signal_to_av(sig);
42466b4f3d01SStephen Smalley 	if (!cred)
4247be0554c9SStephen Smalley 		secid = current_sid();
42486b4f3d01SStephen Smalley 	else
42496b4f3d01SStephen Smalley 		secid = cred_sid(cred);
42506b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42516b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
42521da177e4SLinus Torvalds }
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
42551da177e4SLinus Torvalds 				  struct inode *inode)
42561da177e4SLinus Torvalds {
425780788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
4258275bb41eSDavid Howells 	u32 sid = task_sid(p);
42591da177e4SLinus Torvalds 
42609287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4261db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4262275bb41eSDavid Howells 	isec->sid = sid;
42636f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
42649287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
42651da177e4SLinus Torvalds }
42661da177e4SLinus Torvalds 
42671da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
426867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
42692bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
42701da177e4SLinus Torvalds {
42711da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
42721da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
42731da177e4SLinus Torvalds 
4274bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
42751da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
42761da177e4SLinus Torvalds 	if (ih == NULL)
42771da177e4SLinus Torvalds 		goto out;
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
42801da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
42811da177e4SLinus Torvalds 		goto out;
42821da177e4SLinus Torvalds 
428348c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
428448c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
42851da177e4SLinus Torvalds 	ret = 0;
42861da177e4SLinus Torvalds 
428767f83cbfSVenkat Yekkirala 	if (proto)
428867f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
428967f83cbfSVenkat Yekkirala 
42901da177e4SLinus Torvalds 	switch (ih->protocol) {
42911da177e4SLinus Torvalds 	case IPPROTO_TCP: {
42921da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
42951da177e4SLinus Torvalds 			break;
42961da177e4SLinus Torvalds 
42971da177e4SLinus Torvalds 		offset += ihlen;
42981da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
42991da177e4SLinus Torvalds 		if (th == NULL)
43001da177e4SLinus Torvalds 			break;
43011da177e4SLinus Torvalds 
430248c62af6SEric Paris 		ad->u.net->sport = th->source;
430348c62af6SEric Paris 		ad->u.net->dport = th->dest;
43041da177e4SLinus Torvalds 		break;
43051da177e4SLinus Torvalds 	}
43061da177e4SLinus Torvalds 
43071da177e4SLinus Torvalds 	case IPPROTO_UDP: {
43081da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
43091da177e4SLinus Torvalds 
43101da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
43111da177e4SLinus Torvalds 			break;
43121da177e4SLinus Torvalds 
43131da177e4SLinus Torvalds 		offset += ihlen;
43141da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
43151da177e4SLinus Torvalds 		if (uh == NULL)
43161da177e4SLinus Torvalds 			break;
43171da177e4SLinus Torvalds 
431848c62af6SEric Paris 		ad->u.net->sport = uh->source;
431948c62af6SEric Paris 		ad->u.net->dport = uh->dest;
43201da177e4SLinus Torvalds 		break;
43211da177e4SLinus Torvalds 	}
43221da177e4SLinus Torvalds 
43232ee92d46SJames Morris 	case IPPROTO_DCCP: {
43242ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
43252ee92d46SJames Morris 
43262ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
43272ee92d46SJames Morris 			break;
43282ee92d46SJames Morris 
43292ee92d46SJames Morris 		offset += ihlen;
43302ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
43312ee92d46SJames Morris 		if (dh == NULL)
43322ee92d46SJames Morris 			break;
43332ee92d46SJames Morris 
433448c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
433548c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
43362ee92d46SJames Morris 		break;
43372ee92d46SJames Morris 	}
43382ee92d46SJames Morris 
4339d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4340d452930fSRichard Haines 	case IPPROTO_SCTP: {
4341d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4342d452930fSRichard Haines 
4343d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4344d452930fSRichard Haines 			break;
4345d452930fSRichard Haines 
4346d452930fSRichard Haines 		offset += ihlen;
4347d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4348d452930fSRichard Haines 		if (sh == NULL)
4349d452930fSRichard Haines 			break;
4350d452930fSRichard Haines 
4351d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4352d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4353d452930fSRichard Haines 		break;
4354d452930fSRichard Haines 	}
4355d452930fSRichard Haines #endif
43561da177e4SLinus Torvalds 	default:
43571da177e4SLinus Torvalds 		break;
43581da177e4SLinus Torvalds 	}
43591da177e4SLinus Torvalds out:
43601da177e4SLinus Torvalds 	return ret;
43611da177e4SLinus Torvalds }
43621da177e4SLinus Torvalds 
43631a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
43641da177e4SLinus Torvalds 
43651da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
436667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
43672bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
43681da177e4SLinus Torvalds {
43691da177e4SLinus Torvalds 	u8 nexthdr;
43701da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
43711da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
437275f2811cSJesse Gross 	__be16 frag_off;
43731da177e4SLinus Torvalds 
4374bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
43751da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
43761da177e4SLinus Torvalds 	if (ip6 == NULL)
43771da177e4SLinus Torvalds 		goto out;
43781da177e4SLinus Torvalds 
437948c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
438048c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
43811da177e4SLinus Torvalds 	ret = 0;
43821da177e4SLinus Torvalds 
43831da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
43841da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
438575f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
43861da177e4SLinus Torvalds 	if (offset < 0)
43871da177e4SLinus Torvalds 		goto out;
43881da177e4SLinus Torvalds 
438967f83cbfSVenkat Yekkirala 	if (proto)
439067f83cbfSVenkat Yekkirala 		*proto = nexthdr;
439167f83cbfSVenkat Yekkirala 
43921da177e4SLinus Torvalds 	switch (nexthdr) {
43931da177e4SLinus Torvalds 	case IPPROTO_TCP: {
43941da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
43951da177e4SLinus Torvalds 
43961da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
43971da177e4SLinus Torvalds 		if (th == NULL)
43981da177e4SLinus Torvalds 			break;
43991da177e4SLinus Torvalds 
440048c62af6SEric Paris 		ad->u.net->sport = th->source;
440148c62af6SEric Paris 		ad->u.net->dport = th->dest;
44021da177e4SLinus Torvalds 		break;
44031da177e4SLinus Torvalds 	}
44041da177e4SLinus Torvalds 
44051da177e4SLinus Torvalds 	case IPPROTO_UDP: {
44061da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
44071da177e4SLinus Torvalds 
44081da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
44091da177e4SLinus Torvalds 		if (uh == NULL)
44101da177e4SLinus Torvalds 			break;
44111da177e4SLinus Torvalds 
441248c62af6SEric Paris 		ad->u.net->sport = uh->source;
441348c62af6SEric Paris 		ad->u.net->dport = uh->dest;
44141da177e4SLinus Torvalds 		break;
44151da177e4SLinus Torvalds 	}
44161da177e4SLinus Torvalds 
44172ee92d46SJames Morris 	case IPPROTO_DCCP: {
44182ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
44192ee92d46SJames Morris 
44202ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
44212ee92d46SJames Morris 		if (dh == NULL)
44222ee92d46SJames Morris 			break;
44232ee92d46SJames Morris 
442448c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
442548c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
44262ee92d46SJames Morris 		break;
44272ee92d46SJames Morris 	}
44282ee92d46SJames Morris 
4429d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4430d452930fSRichard Haines 	case IPPROTO_SCTP: {
4431d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4432d452930fSRichard Haines 
4433d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4434d452930fSRichard Haines 		if (sh == NULL)
4435d452930fSRichard Haines 			break;
4436d452930fSRichard Haines 
4437d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4438d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4439d452930fSRichard Haines 		break;
4440d452930fSRichard Haines 	}
4441d452930fSRichard Haines #endif
44421da177e4SLinus Torvalds 	/* includes fragments */
44431da177e4SLinus Torvalds 	default:
44441da177e4SLinus Torvalds 		break;
44451da177e4SLinus Torvalds 	}
44461da177e4SLinus Torvalds out:
44471da177e4SLinus Torvalds 	return ret;
44481da177e4SLinus Torvalds }
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds #endif /* IPV6 */
44511da177e4SLinus Torvalds 
44522bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4453cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
44541da177e4SLinus Torvalds {
4455cf9481e2SDavid Howells 	char *addrp;
4456cf9481e2SDavid Howells 	int ret;
44571da177e4SLinus Torvalds 
445848c62af6SEric Paris 	switch (ad->u.net->family) {
44591da177e4SLinus Torvalds 	case PF_INET:
446067f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4461cf9481e2SDavid Howells 		if (ret)
4462cf9481e2SDavid Howells 			goto parse_error;
446348c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
446448c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4465cf9481e2SDavid Howells 		goto okay;
44661da177e4SLinus Torvalds 
44671a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
44681da177e4SLinus Torvalds 	case PF_INET6:
446967f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4470cf9481e2SDavid Howells 		if (ret)
4471cf9481e2SDavid Howells 			goto parse_error;
447248c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
447348c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4474cf9481e2SDavid Howells 		goto okay;
44751da177e4SLinus Torvalds #endif	/* IPV6 */
44761da177e4SLinus Torvalds 	default:
4477cf9481e2SDavid Howells 		addrp = NULL;
4478cf9481e2SDavid Howells 		goto okay;
44791da177e4SLinus Torvalds 	}
44801da177e4SLinus Torvalds 
4481cf9481e2SDavid Howells parse_error:
4482c103a91eSpeter enderborg 	pr_warn(
448371f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
448471f1cb05SPaul Moore 	       " unable to parse packet\n");
44851da177e4SLinus Torvalds 	return ret;
4486cf9481e2SDavid Howells 
4487cf9481e2SDavid Howells okay:
4488cf9481e2SDavid Howells 	if (_addrp)
4489cf9481e2SDavid Howells 		*_addrp = addrp;
4490cf9481e2SDavid Howells 	return 0;
44911da177e4SLinus Torvalds }
44921da177e4SLinus Torvalds 
44934f6a993fSPaul Moore /**
4494220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
44954f6a993fSPaul Moore  * @skb: the packet
449675e22910SPaul Moore  * @family: protocol family
4497220deb96SPaul Moore  * @sid: the packet's peer label SID
44984f6a993fSPaul Moore  *
44994f6a993fSPaul Moore  * Description:
4500220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4501220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4502220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4503220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4504220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4505220deb96SPaul Moore  * peer labels.
45064f6a993fSPaul Moore  *
45074f6a993fSPaul Moore  */
4508220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
45094f6a993fSPaul Moore {
451071f1cb05SPaul Moore 	int err;
45114f6a993fSPaul Moore 	u32 xfrm_sid;
45124f6a993fSPaul Moore 	u32 nlbl_sid;
4513220deb96SPaul Moore 	u32 nlbl_type;
45144f6a993fSPaul Moore 
4515817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4516bed4d7efSPaul Moore 	if (unlikely(err))
4517bed4d7efSPaul Moore 		return -EACCES;
4518bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4519bed4d7efSPaul Moore 	if (unlikely(err))
4520bed4d7efSPaul Moore 		return -EACCES;
4521220deb96SPaul Moore 
4522aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4523aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
452471f1cb05SPaul Moore 	if (unlikely(err)) {
4525c103a91eSpeter enderborg 		pr_warn(
452671f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
452771f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4528220deb96SPaul Moore 		return -EACCES;
452971f1cb05SPaul Moore 	}
4530220deb96SPaul Moore 
4531220deb96SPaul Moore 	return 0;
45324f6a993fSPaul Moore }
45334f6a993fSPaul Moore 
4534446b8024SPaul Moore /**
4535446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4536446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4537446b8024SPaul Moore  * @skb_sid: the packet's SID
4538446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4539446b8024SPaul Moore  *
4540446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4541446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4542c76a2f9eSRandy Dunlap  * @conn_sid.  If @skb_sid is not valid then @conn_sid is simply a copy
4543446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4544446b8024SPaul Moore  *
4545446b8024SPaul Moore  */
4546446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4547446b8024SPaul Moore {
4548446b8024SPaul Moore 	int err = 0;
4549446b8024SPaul Moore 
4550446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4551aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4552aa8e712cSStephen Smalley 					    conn_sid);
4553446b8024SPaul Moore 	else
4554446b8024SPaul Moore 		*conn_sid = sk_sid;
4555446b8024SPaul Moore 
4556446b8024SPaul Moore 	return err;
4557446b8024SPaul Moore }
4558446b8024SPaul Moore 
45591da177e4SLinus Torvalds /* socket security operations */
4560d4f2d978SPaul Moore 
45612ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
45622ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4563d4f2d978SPaul Moore {
45642ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
45652ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
45662ad18bdfSHarry Ciao 		return 0;
45672ad18bdfSHarry Ciao 	}
45682ad18bdfSHarry Ciao 
4569aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4570aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4571d4f2d978SPaul Moore }
4572d4f2d978SPaul Moore 
4573be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
45741da177e4SLinus Torvalds {
4575253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45762bf49690SThomas Liu 	struct common_audit_data ad;
457748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
45781da177e4SLinus Torvalds 
4579253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4580253bfae6SPaul Moore 		return 0;
45811da177e4SLinus Torvalds 
458250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
458348c62af6SEric Paris 	ad.u.net = &net;
458448c62af6SEric Paris 	ad.u.net->sk = sk;
45851da177e4SLinus Torvalds 
45866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
45876b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4588be0554c9SStephen Smalley 			    &ad);
45891da177e4SLinus Torvalds }
45901da177e4SLinus Torvalds 
45911da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
45921da177e4SLinus Torvalds 				 int protocol, int kern)
45931da177e4SLinus Torvalds {
45940c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
4595d4f2d978SPaul Moore 	u32 newsid;
4596275bb41eSDavid Howells 	u16 secclass;
45972ad18bdfSHarry Ciao 	int rc;
45981da177e4SLinus Torvalds 
45991da177e4SLinus Torvalds 	if (kern)
4600d4f2d978SPaul Moore 		return 0;
46011da177e4SLinus Torvalds 
4602275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
46032ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
46042ad18bdfSHarry Ciao 	if (rc)
46052ad18bdfSHarry Ciao 		return rc;
46062ad18bdfSHarry Ciao 
46076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
46086b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
46091da177e4SLinus Torvalds }
46101da177e4SLinus Torvalds 
46117420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
46121da177e4SLinus Torvalds 				      int type, int protocol, int kern)
46131da177e4SLinus Torvalds {
46140c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
46155d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4616892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
46179287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
46189287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4619275bb41eSDavid Howells 	int err = 0;
4620275bb41eSDavid Howells 
46219287aed2SAndreas Gruenbacher 	if (!kern) {
46229287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
46232ad18bdfSHarry Ciao 		if (err)
46242ad18bdfSHarry Ciao 			return err;
46252ad18bdfSHarry Ciao 	}
4626275bb41eSDavid Howells 
46279287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
46289287aed2SAndreas Gruenbacher 	isec->sid = sid;
46296f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
46301da177e4SLinus Torvalds 
4631892c141eSVenkat Yekkirala 	if (sock->sk) {
4632892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
46339287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
46349287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4635d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4636d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4637d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4638d452930fSRichard Haines 
4639389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4640892c141eSVenkat Yekkirala 	}
4641892c141eSVenkat Yekkirala 
46427420ed23SVenkat Yekkirala 	return err;
46431da177e4SLinus Torvalds }
46441da177e4SLinus Torvalds 
46450b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
46460b811db2SDavid Herrmann 				     struct socket *sockb)
46470b811db2SDavid Herrmann {
46480b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
46490b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
46500b811db2SDavid Herrmann 
46510b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
46520b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
46530b811db2SDavid Herrmann 
46540b811db2SDavid Herrmann 	return 0;
46550b811db2SDavid Herrmann }
46560b811db2SDavid Herrmann 
46571da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
46581da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
46591da177e4SLinus Torvalds    permission check between the socket and the port number. */
46601da177e4SLinus Torvalds 
46611da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
46621da177e4SLinus Torvalds {
4663253bfae6SPaul Moore 	struct sock *sk = sock->sk;
46640f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
46651da177e4SLinus Torvalds 	u16 family;
46661da177e4SLinus Torvalds 	int err;
46671da177e4SLinus Torvalds 
4668be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
46691da177e4SLinus Torvalds 	if (err)
46701da177e4SLinus Torvalds 		goto out;
46711da177e4SLinus Torvalds 
4672d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4673253bfae6SPaul Moore 	family = sk->sk_family;
46741da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
46751da177e4SLinus Torvalds 		char *addrp;
46762bf49690SThomas Liu 		struct common_audit_data ad;
467748c62af6SEric Paris 		struct lsm_network_audit net = {0,};
46781da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
46791da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
4680c750e692STetsuo Handa 		u16 family_sa;
46811da177e4SLinus Torvalds 		unsigned short snum;
4682e399f982SJames Morris 		u32 sid, node_perm;
46831da177e4SLinus Torvalds 
4684d452930fSRichard Haines 		/*
4685d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4686d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4687d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4688d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4689d452930fSRichard Haines 		 */
4690c750e692STetsuo Handa 		if (addrlen < offsetofend(struct sockaddr, sa_family))
4691c750e692STetsuo Handa 			return -EINVAL;
4692c750e692STetsuo Handa 		family_sa = address->sa_family;
46930f8db8ccSAlexey Kodanev 		switch (family_sa) {
46940f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
469568741a8aSRichard Haines 		case AF_INET:
469668741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
469768741a8aSRichard Haines 				return -EINVAL;
46981da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
46990f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
47000f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
47010f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
47020f8db8ccSAlexey Kodanev 				 */
47030f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
47040f8db8ccSAlexey Kodanev 					goto err_af;
47050f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
47060f8db8ccSAlexey Kodanev 			}
47071da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
47081da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
470968741a8aSRichard Haines 			break;
471068741a8aSRichard Haines 		case AF_INET6:
471168741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
471268741a8aSRichard Haines 				return -EINVAL;
47131da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
47141da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
47151da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
471668741a8aSRichard Haines 			break;
471768741a8aSRichard Haines 		default:
47180f8db8ccSAlexey Kodanev 			goto err_af;
47191da177e4SLinus Torvalds 		}
47201da177e4SLinus Torvalds 
472188b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
472288b7d370SAlexey Kodanev 		ad.u.net = &net;
472388b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
472488b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
472588b7d370SAlexey Kodanev 
4726227b60f5SStephen Hemminger 		if (snum) {
4727227b60f5SStephen Hemminger 			int low, high;
4728227b60f5SStephen Hemminger 
47290bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4730227b60f5SStephen Hemminger 
473182f31ebfSMaciej Żenczykowski 			if (inet_port_requires_bind_service(sock_net(sk), snum) ||
473282f31ebfSMaciej Żenczykowski 			    snum < low || snum > high) {
47333e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
47343e112172SPaul Moore 						      snum, &sid);
47351da177e4SLinus Torvalds 				if (err)
47361da177e4SLinus Torvalds 					goto out;
47376b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
47386b6bc620SStephen Smalley 						   sksec->sid, sid,
4739253bfae6SPaul Moore 						   sksec->sclass,
47401da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
47411da177e4SLinus Torvalds 				if (err)
47421da177e4SLinus Torvalds 					goto out;
47431da177e4SLinus Torvalds 			}
4744227b60f5SStephen Hemminger 		}
47451da177e4SLinus Torvalds 
4746253bfae6SPaul Moore 		switch (sksec->sclass) {
474713402580SJames Morris 		case SECCLASS_TCP_SOCKET:
47481da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
47491da177e4SLinus Torvalds 			break;
47501da177e4SLinus Torvalds 
475113402580SJames Morris 		case SECCLASS_UDP_SOCKET:
47521da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
47531da177e4SLinus Torvalds 			break;
47541da177e4SLinus Torvalds 
47552ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
47562ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
47572ee92d46SJames Morris 			break;
47582ee92d46SJames Morris 
4759d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4760d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4761d452930fSRichard Haines 			break;
4762d452930fSRichard Haines 
47631da177e4SLinus Torvalds 		default:
47641da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
47651da177e4SLinus Torvalds 			break;
47661da177e4SLinus Torvalds 		}
47671da177e4SLinus Torvalds 
476888b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
47691da177e4SLinus Torvalds 		if (err)
47701da177e4SLinus Torvalds 			goto out;
47711da177e4SLinus Torvalds 
47720f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
477348c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
47741da177e4SLinus Torvalds 		else
477548c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
47761da177e4SLinus Torvalds 
47776b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
47786b6bc620SStephen Smalley 				   sksec->sid, sid,
4779253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
47801da177e4SLinus Torvalds 		if (err)
47811da177e4SLinus Torvalds 			goto out;
47821da177e4SLinus Torvalds 	}
47831da177e4SLinus Torvalds out:
47841da177e4SLinus Torvalds 	return err;
47850f8db8ccSAlexey Kodanev err_af:
47860f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
47870f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
47880f8db8ccSAlexey Kodanev 		return -EINVAL;
47890f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
47901da177e4SLinus Torvalds }
47911da177e4SLinus Torvalds 
4792d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4793d61330c6SKees Cook  * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4794d452930fSRichard Haines  */
4795d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4796d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
47971da177e4SLinus Torvalds {
4798014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4799253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
48001da177e4SLinus Torvalds 	int err;
48011da177e4SLinus Torvalds 
4802be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
48031da177e4SLinus Torvalds 	if (err)
48041da177e4SLinus Torvalds 		return err;
480505174c95SPaolo Abeni 	if (addrlen < offsetofend(struct sockaddr, sa_family))
480605174c95SPaolo Abeni 		return -EINVAL;
480705174c95SPaolo Abeni 
480805174c95SPaolo Abeni 	/* connect(AF_UNSPEC) has special handling, as it is a documented
480905174c95SPaolo Abeni 	 * way to disconnect the socket
481005174c95SPaolo Abeni 	 */
481105174c95SPaolo Abeni 	if (address->sa_family == AF_UNSPEC)
481205174c95SPaolo Abeni 		return 0;
48131da177e4SLinus Torvalds 
48141da177e4SLinus Torvalds 	/*
4815d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4816d452930fSRichard Haines 	 * for the port.
48171da177e4SLinus Torvalds 	 */
4818253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4819d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4820d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
48212bf49690SThomas Liu 		struct common_audit_data ad;
482248c62af6SEric Paris 		struct lsm_network_audit net = {0,};
48231da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
48241da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
48251da177e4SLinus Torvalds 		unsigned short snum;
48262ee92d46SJames Morris 		u32 sid, perm;
48271da177e4SLinus Torvalds 
4828d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4829d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4830d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4831d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4832d452930fSRichard Haines 		 */
483368741a8aSRichard Haines 		switch (address->sa_family) {
483468741a8aSRichard Haines 		case AF_INET:
48351da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4836911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
48371da177e4SLinus Torvalds 				return -EINVAL;
48381da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
483968741a8aSRichard Haines 			break;
484068741a8aSRichard Haines 		case AF_INET6:
48411da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4842911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
48431da177e4SLinus Torvalds 				return -EINVAL;
48441da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
484568741a8aSRichard Haines 			break;
484668741a8aSRichard Haines 		default:
484768741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
484868741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
484968741a8aSRichard Haines 			 */
485068741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
485168741a8aSRichard Haines 				return -EINVAL;
485268741a8aSRichard Haines 			else
485368741a8aSRichard Haines 				return -EAFNOSUPPORT;
48541da177e4SLinus Torvalds 		}
48551da177e4SLinus Torvalds 
48563e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
48571da177e4SLinus Torvalds 		if (err)
4858d452930fSRichard Haines 			return err;
48591da177e4SLinus Torvalds 
4860d452930fSRichard Haines 		switch (sksec->sclass) {
4861d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4862d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4863d452930fSRichard Haines 			break;
4864d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4865d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4866d452930fSRichard Haines 			break;
4867d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4868d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4869d452930fSRichard Haines 			break;
4870d452930fSRichard Haines 		}
48712ee92d46SJames Morris 
487250c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
487348c62af6SEric Paris 		ad.u.net = &net;
487448c62af6SEric Paris 		ad.u.net->dport = htons(snum);
487588b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
48766b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48776b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
48781da177e4SLinus Torvalds 		if (err)
4879d452930fSRichard Haines 			return err;
48801da177e4SLinus Torvalds 	}
48811da177e4SLinus Torvalds 
4882d452930fSRichard Haines 	return 0;
4883d452930fSRichard Haines }
4884014ab19aSPaul Moore 
4885d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4886d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4887d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4888d452930fSRichard Haines {
4889d452930fSRichard Haines 	int err;
4890d452930fSRichard Haines 	struct sock *sk = sock->sk;
4891d452930fSRichard Haines 
4892d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4893d452930fSRichard Haines 	if (err)
48941da177e4SLinus Torvalds 		return err;
4895d452930fSRichard Haines 
4896d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
48971da177e4SLinus Torvalds }
48981da177e4SLinus Torvalds 
48991da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
49001da177e4SLinus Torvalds {
4901be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
49021da177e4SLinus Torvalds }
49031da177e4SLinus Torvalds 
49041da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
49051da177e4SLinus Torvalds {
49061da177e4SLinus Torvalds 	int err;
49071da177e4SLinus Torvalds 	struct inode_security_struct *isec;
49081da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
49099287aed2SAndreas Gruenbacher 	u16 sclass;
49109287aed2SAndreas Gruenbacher 	u32 sid;
49111da177e4SLinus Torvalds 
4912be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
49131da177e4SLinus Torvalds 	if (err)
49141da177e4SLinus Torvalds 		return err;
49151da177e4SLinus Torvalds 
49165d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
49179287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
49189287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
49199287aed2SAndreas Gruenbacher 	sid = isec->sid;
49209287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
49219287aed2SAndreas Gruenbacher 
49229287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
49239287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
49249287aed2SAndreas Gruenbacher 	newisec->sid = sid;
49256f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
49261da177e4SLinus Torvalds 
49271da177e4SLinus Torvalds 	return 0;
49281da177e4SLinus Torvalds }
49291da177e4SLinus Torvalds 
49301da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
49311da177e4SLinus Torvalds 				  int size)
49321da177e4SLinus Torvalds {
4933be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
49341da177e4SLinus Torvalds }
49351da177e4SLinus Torvalds 
49361da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
49371da177e4SLinus Torvalds 				  int size, int flags)
49381da177e4SLinus Torvalds {
4939be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
49401da177e4SLinus Torvalds }
49411da177e4SLinus Torvalds 
49421da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
49431da177e4SLinus Torvalds {
4944be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
49451da177e4SLinus Torvalds }
49461da177e4SLinus Torvalds 
49471da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
49481da177e4SLinus Torvalds {
4949be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
49501da177e4SLinus Torvalds }
49511da177e4SLinus Torvalds 
49521da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
49531da177e4SLinus Torvalds {
4954f8687afeSPaul Moore 	int err;
4955f8687afeSPaul Moore 
4956be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4957f8687afeSPaul Moore 	if (err)
4958f8687afeSPaul Moore 		return err;
4959f8687afeSPaul Moore 
4960f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
49611da177e4SLinus Torvalds }
49621da177e4SLinus Torvalds 
49631da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
49641da177e4SLinus Torvalds 				     int optname)
49651da177e4SLinus Torvalds {
4966be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
49671da177e4SLinus Torvalds }
49681da177e4SLinus Torvalds 
49691da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
49701da177e4SLinus Torvalds {
4971be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
49721da177e4SLinus Torvalds }
49731da177e4SLinus Torvalds 
49743610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
49753610cda5SDavid S. Miller 					      struct sock *other,
49761da177e4SLinus Torvalds 					      struct sock *newsk)
49771da177e4SLinus Torvalds {
49783610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
49793610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
49804d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
49812bf49690SThomas Liu 	struct common_audit_data ad;
498248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49831da177e4SLinus Torvalds 	int err;
49841da177e4SLinus Torvalds 
498550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
498648c62af6SEric Paris 	ad.u.net = &net;
498748c62af6SEric Paris 	ad.u.net->sk = other;
49881da177e4SLinus Torvalds 
49896b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
49906b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
49914d1e2451SPaul Moore 			   sksec_other->sclass,
49921da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
49931da177e4SLinus Torvalds 	if (err)
49941da177e4SLinus Torvalds 		return err;
49951da177e4SLinus Torvalds 
49961da177e4SLinus Torvalds 	/* server child socket */
49974d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4998aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4999aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
50004d1e2451SPaul Moore 	if (err)
50014237c75cSVenkat Yekkirala 		return err;
50024d1e2451SPaul Moore 
50034d1e2451SPaul Moore 	/* connecting socket */
50044d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
50054d1e2451SPaul Moore 
50064d1e2451SPaul Moore 	return 0;
50071da177e4SLinus Torvalds }
50081da177e4SLinus Torvalds 
50091da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
50101da177e4SLinus Torvalds 					struct socket *other)
50111da177e4SLinus Torvalds {
5012253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
5013253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
50142bf49690SThomas Liu 	struct common_audit_data ad;
501548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
50161da177e4SLinus Torvalds 
501750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
501848c62af6SEric Paris 	ad.u.net = &net;
501948c62af6SEric Paris 	ad.u.net->sk = other->sk;
50201da177e4SLinus Torvalds 
50216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
50226b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
5023253bfae6SPaul Moore 			    &ad);
50241da177e4SLinus Torvalds }
50251da177e4SLinus Torvalds 
5026cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
5027cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
50282bf49690SThomas Liu 				    struct common_audit_data *ad)
5029effad8dfSPaul Moore {
5030effad8dfSPaul Moore 	int err;
5031effad8dfSPaul Moore 	u32 if_sid;
5032effad8dfSPaul Moore 	u32 node_sid;
5033effad8dfSPaul Moore 
5034cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
5035effad8dfSPaul Moore 	if (err)
5036effad8dfSPaul Moore 		return err;
50376b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
50386b6bc620SStephen Smalley 			   peer_sid, if_sid,
5039effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
5040effad8dfSPaul Moore 	if (err)
5041effad8dfSPaul Moore 		return err;
5042effad8dfSPaul Moore 
5043effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
5044effad8dfSPaul Moore 	if (err)
5045effad8dfSPaul Moore 		return err;
50466b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
50476b6bc620SStephen Smalley 			    peer_sid, node_sid,
5048effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
5049effad8dfSPaul Moore }
5050effad8dfSPaul Moore 
5051220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5052d8395c87SPaul Moore 				       u16 family)
5053220deb96SPaul Moore {
5054277d342fSPaul Moore 	int err = 0;
5055220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5056220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
50572bf49690SThomas Liu 	struct common_audit_data ad;
505848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5059d8395c87SPaul Moore 	char *addrp;
5060d8395c87SPaul Moore 
506150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
506248c62af6SEric Paris 	ad.u.net = &net;
506348c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
506448c62af6SEric Paris 	ad.u.net->family = family;
5065d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5066d8395c87SPaul Moore 	if (err)
5067d8395c87SPaul Moore 		return err;
5068220deb96SPaul Moore 
506958bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
50706b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50716b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5072d8395c87SPaul Moore 				   PACKET__RECV, &ad);
5073220deb96SPaul Moore 		if (err)
5074220deb96SPaul Moore 			return err;
507558bfbb51SPaul Moore 	}
5076220deb96SPaul Moore 
5077d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5078220deb96SPaul Moore 	if (err)
5079220deb96SPaul Moore 		return err;
5080d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5081220deb96SPaul Moore 
50824e5ab4cbSJames Morris 	return err;
50834e5ab4cbSJames Morris }
5084d28d1e08STrent Jaeger 
50854e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
50864e5ab4cbSJames Morris {
5087220deb96SPaul Moore 	int err;
50884237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
5089220deb96SPaul Moore 	u16 family = sk->sk_family;
5090220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
50912bf49690SThomas Liu 	struct common_audit_data ad;
509248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5093220deb96SPaul Moore 	char *addrp;
5094d8395c87SPaul Moore 	u8 secmark_active;
5095d8395c87SPaul Moore 	u8 peerlbl_active;
50964e5ab4cbSJames Morris 
50974e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
5098220deb96SPaul Moore 		return 0;
50994e5ab4cbSJames Morris 
51004e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
510187fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
51024e5ab4cbSJames Morris 		family = PF_INET;
51034e5ab4cbSJames Morris 
5104d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5105d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
5106d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5107d8395c87SPaul Moore 	 * as fast and as clean as possible. */
5108aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5109d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
5110d8395c87SPaul Moore 
5111d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
51122be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5113d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
5114d8395c87SPaul Moore 		return 0;
5115d8395c87SPaul Moore 
511650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
511748c62af6SEric Paris 	ad.u.net = &net;
511848c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
511948c62af6SEric Paris 	ad.u.net->family = family;
5120224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
51214e5ab4cbSJames Morris 	if (err)
5122220deb96SPaul Moore 		return err;
51234e5ab4cbSJames Morris 
5124d8395c87SPaul Moore 	if (peerlbl_active) {
5125d621d35eSPaul Moore 		u32 peer_sid;
5126220deb96SPaul Moore 
5127220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5128220deb96SPaul Moore 		if (err)
5129220deb96SPaul Moore 			return err;
5130cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5131cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5132dfaebe98SPaul Moore 		if (err) {
5133a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
5134effad8dfSPaul Moore 			return err;
5135dfaebe98SPaul Moore 		}
51366b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
51376b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
5138d621d35eSPaul Moore 				   PEER__RECV, &ad);
513946d01d63SChad Hanson 		if (err) {
5140a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
514146d01d63SChad Hanson 			return err;
514246d01d63SChad Hanson 		}
5143d621d35eSPaul Moore 	}
5144d621d35eSPaul Moore 
5145d8395c87SPaul Moore 	if (secmark_active) {
51466b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
51476b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5148effad8dfSPaul Moore 				   PACKET__RECV, &ad);
5149effad8dfSPaul Moore 		if (err)
5150effad8dfSPaul Moore 			return err;
5151effad8dfSPaul Moore 	}
5152effad8dfSPaul Moore 
5153d621d35eSPaul Moore 	return err;
51541da177e4SLinus Torvalds }
51551da177e4SLinus Torvalds 
51562c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
51571da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
51581da177e4SLinus Torvalds {
51591da177e4SLinus Torvalds 	int err = 0;
51601da177e4SLinus Torvalds 	char *scontext;
51611da177e4SLinus Torvalds 	u32 scontext_len;
5162253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
51633de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
51641da177e4SLinus Torvalds 
5165253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5166d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
5167d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
5168dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
5169253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
5170253bfae6SPaul Moore 		return -ENOPROTOOPT;
51711da177e4SLinus Torvalds 
5172aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5173aa8e712cSStephen Smalley 				      &scontext_len);
51741da177e4SLinus Torvalds 	if (err)
5175253bfae6SPaul Moore 		return err;
51761da177e4SLinus Torvalds 
51771da177e4SLinus Torvalds 	if (scontext_len > len) {
51781da177e4SLinus Torvalds 		err = -ERANGE;
51791da177e4SLinus Torvalds 		goto out_len;
51801da177e4SLinus Torvalds 	}
51811da177e4SLinus Torvalds 
51821da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
51831da177e4SLinus Torvalds 		err = -EFAULT;
51841da177e4SLinus Torvalds 
51851da177e4SLinus Torvalds out_len:
51861da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
51871da177e4SLinus Torvalds 		err = -EFAULT;
51881da177e4SLinus Torvalds 	kfree(scontext);
51891da177e4SLinus Torvalds 	return err;
51901da177e4SLinus Torvalds }
51911da177e4SLinus Torvalds 
5192dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
51932c7946a7SCatherine Zhang {
5194dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
519575e22910SPaul Moore 	u16 family;
5196899134f2SPaul Moore 	struct inode_security_struct *isec;
5197877ce7c1SCatherine Zhang 
5198aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
5199aa862900SPaul Moore 		family = PF_INET;
5200aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
5201aa862900SPaul Moore 		family = PF_INET6;
5202aa862900SPaul Moore 	else if (sock)
520375e22910SPaul Moore 		family = sock->sk->sk_family;
520475e22910SPaul Moore 	else
520575e22910SPaul Moore 		goto out;
520675e22910SPaul Moore 
5207899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
5208899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
5209899134f2SPaul Moore 		peer_secid = isec->sid;
5210899134f2SPaul Moore 	} else if (skb)
5211220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
52122c7946a7SCatherine Zhang 
521375e22910SPaul Moore out:
5214dc49c1f9SCatherine Zhang 	*secid = peer_secid;
521575e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
521675e22910SPaul Moore 		return -EINVAL;
521775e22910SPaul Moore 	return 0;
52182c7946a7SCatherine Zhang }
52192c7946a7SCatherine Zhang 
52207d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
52211da177e4SLinus Torvalds {
522284914b7eSPaul Moore 	struct sk_security_struct *sksec;
522384914b7eSPaul Moore 
522484914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
522584914b7eSPaul Moore 	if (!sksec)
522684914b7eSPaul Moore 		return -ENOMEM;
522784914b7eSPaul Moore 
522884914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
522984914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
52305dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
523184914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
523284914b7eSPaul Moore 	sk->sk_security = sksec;
523384914b7eSPaul Moore 
523484914b7eSPaul Moore 	return 0;
52351da177e4SLinus Torvalds }
52361da177e4SLinus Torvalds 
52371da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
52381da177e4SLinus Torvalds {
523984914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
524084914b7eSPaul Moore 
524184914b7eSPaul Moore 	sk->sk_security = NULL;
524284914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
524384914b7eSPaul Moore 	kfree(sksec);
52441da177e4SLinus Torvalds }
52451da177e4SLinus Torvalds 
5246892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5247892c141eSVenkat Yekkirala {
5248dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5249dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5250892c141eSVenkat Yekkirala 
5251dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5252dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5253dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
525499f59ed0SPaul Moore 
5255dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5256892c141eSVenkat Yekkirala }
5257892c141eSVenkat Yekkirala 
5258beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5259d28d1e08STrent Jaeger {
5260d28d1e08STrent Jaeger 	if (!sk)
5261beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5262892c141eSVenkat Yekkirala 	else {
5263892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5264d28d1e08STrent Jaeger 
5265beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5266892c141eSVenkat Yekkirala 	}
5267d28d1e08STrent Jaeger }
5268d28d1e08STrent Jaeger 
52699a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
52704237c75cSVenkat Yekkirala {
52715d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
52725d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
52734237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52744237c75cSVenkat Yekkirala 
52752873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
52762873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
52774237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5278220deb96SPaul Moore 	sksec->sclass = isec->sclass;
52794237c75cSVenkat Yekkirala }
52804237c75cSVenkat Yekkirala 
5281d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5282d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5283d452930fSRichard Haines  * already present).
5284d452930fSRichard Haines  */
5285d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5286d452930fSRichard Haines 				      struct sk_buff *skb)
5287d452930fSRichard Haines {
5288d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5289d452930fSRichard Haines 	struct common_audit_data ad;
5290d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5291d452930fSRichard Haines 	u8 peerlbl_active;
5292d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5293d452930fSRichard Haines 	u32 conn_sid;
5294d452930fSRichard Haines 	int err = 0;
5295d452930fSRichard Haines 
5296aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5297d452930fSRichard Haines 		return 0;
5298d452930fSRichard Haines 
5299d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5300d452930fSRichard Haines 
5301d452930fSRichard Haines 	if (peerlbl_active) {
5302d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5303d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5304d452930fSRichard Haines 		 */
5305d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5306d452930fSRichard Haines 					      &peer_sid);
5307d452930fSRichard Haines 		if (err)
5308d452930fSRichard Haines 			return err;
5309d452930fSRichard Haines 
5310d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5311d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5312d452930fSRichard Haines 	}
5313d452930fSRichard Haines 
5314d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5315d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5316d452930fSRichard Haines 
5317d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5318d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5319d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5320d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5321d452930fSRichard Haines 		 */
5322d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5323d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5324d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5325d452930fSRichard Haines 		 * consistency among the peer SIDs.
5326d452930fSRichard Haines 		 */
5327d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5328d452930fSRichard Haines 		ad.u.net = &net;
5329d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
53306b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
53316b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5332d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5333d452930fSRichard Haines 		if (err)
5334d452930fSRichard Haines 			return err;
5335d452930fSRichard Haines 	}
5336d452930fSRichard Haines 
5337d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5338d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5339d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5340d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5341d452930fSRichard Haines 	 * plug this into the new socket.
5342d452930fSRichard Haines 	 */
5343d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5344d452930fSRichard Haines 	if (err)
5345d452930fSRichard Haines 		return err;
5346d452930fSRichard Haines 
5347d452930fSRichard Haines 	ep->secid = conn_sid;
5348d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5349d452930fSRichard Haines 
5350d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5351d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5352d452930fSRichard Haines }
5353d452930fSRichard Haines 
5354d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5355d452930fSRichard Haines  * based on their @optname.
5356d452930fSRichard Haines  */
5357d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5358d452930fSRichard Haines 				     struct sockaddr *address,
5359d452930fSRichard Haines 				     int addrlen)
5360d452930fSRichard Haines {
5361d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5362d452930fSRichard Haines 	void *addr_buf;
5363d452930fSRichard Haines 	struct sockaddr *addr;
5364d452930fSRichard Haines 	struct socket *sock;
5365d452930fSRichard Haines 
5366aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5367d452930fSRichard Haines 		return 0;
5368d452930fSRichard Haines 
5369d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5370d452930fSRichard Haines 	sock = sk->sk_socket;
5371d452930fSRichard Haines 	addr_buf = address;
5372d452930fSRichard Haines 
5373d452930fSRichard Haines 	while (walk_size < addrlen) {
5374c138325fSOndrej Mosnacek 		if (walk_size + sizeof(sa_family_t) > addrlen)
5375c138325fSOndrej Mosnacek 			return -EINVAL;
5376c138325fSOndrej Mosnacek 
5377d452930fSRichard Haines 		addr = addr_buf;
5378d452930fSRichard Haines 		switch (addr->sa_family) {
53794152dc91SAlexey Kodanev 		case AF_UNSPEC:
5380d452930fSRichard Haines 		case AF_INET:
5381d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5382d452930fSRichard Haines 			break;
5383d452930fSRichard Haines 		case AF_INET6:
5384d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5385d452930fSRichard Haines 			break;
5386d452930fSRichard Haines 		default:
53874152dc91SAlexey Kodanev 			return -EINVAL;
5388d452930fSRichard Haines 		}
5389d452930fSRichard Haines 
5390292c997aSXin Long 		if (walk_size + len > addrlen)
5391292c997aSXin Long 			return -EINVAL;
5392292c997aSXin Long 
5393d452930fSRichard Haines 		err = -EINVAL;
5394d452930fSRichard Haines 		switch (optname) {
5395d452930fSRichard Haines 		/* Bind checks */
5396d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5397d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5398d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5399d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5400d452930fSRichard Haines 			break;
5401d452930fSRichard Haines 		/* Connect checks */
5402d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5403d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5404d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5405d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5406d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5407d452930fSRichard Haines 			if (err)
5408d452930fSRichard Haines 				return err;
5409d452930fSRichard Haines 
5410d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5411d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5412c76a2f9eSRandy Dunlap 			 * therefore selinux_netlbl_socket_connect_locked()
5413d452930fSRichard Haines 			 * is called here. The situations handled are:
5414d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5415d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5416d452930fSRichard Haines 			 * primary address is selected.
5417d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5418d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5419d452930fSRichard Haines 			 * selinux_socket_connect().
5420d452930fSRichard Haines 			 */
5421d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5422d452930fSRichard Haines 			break;
5423d452930fSRichard Haines 		}
5424d452930fSRichard Haines 
5425d452930fSRichard Haines 		if (err)
5426d452930fSRichard Haines 			return err;
5427d452930fSRichard Haines 
5428d452930fSRichard Haines 		addr_buf += len;
5429d452930fSRichard Haines 		walk_size += len;
5430d452930fSRichard Haines 	}
5431d452930fSRichard Haines 
5432d452930fSRichard Haines 	return 0;
5433d452930fSRichard Haines }
5434d452930fSRichard Haines 
5435d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5436d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5437d452930fSRichard Haines 				  struct sock *newsk)
5438d452930fSRichard Haines {
5439d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5440d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5441d452930fSRichard Haines 
5442d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5443d452930fSRichard Haines 	 * the non-sctp clone version.
5444d452930fSRichard Haines 	 */
5445aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5446d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5447d452930fSRichard Haines 
5448d452930fSRichard Haines 	newsksec->sid = ep->secid;
5449d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5450d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5451d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5452d452930fSRichard Haines }
5453d452930fSRichard Haines 
545441dd9596SFlorian Westphal static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
54554237c75cSVenkat Yekkirala 				     struct request_sock *req)
54564237c75cSVenkat Yekkirala {
54574237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
54584237c75cSVenkat Yekkirala 	int err;
54590b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5460446b8024SPaul Moore 	u32 connsid;
54614237c75cSVenkat Yekkirala 	u32 peersid;
54624237c75cSVenkat Yekkirala 
5463aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5464220deb96SPaul Moore 	if (err)
5465220deb96SPaul Moore 		return err;
5466446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
54674237c75cSVenkat Yekkirala 	if (err)
54684237c75cSVenkat Yekkirala 		return err;
5469446b8024SPaul Moore 	req->secid = connsid;
54706b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5471389fb800SPaul Moore 
5472389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
54734237c75cSVenkat Yekkirala }
54744237c75cSVenkat Yekkirala 
54759a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
54769a673e56SAdrian Bunk 				   const struct request_sock *req)
54774237c75cSVenkat Yekkirala {
54784237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
54794237c75cSVenkat Yekkirala 
54804237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
54816b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
54824237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
54834237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
54844237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
54854237c75cSVenkat Yekkirala 	   time it will have been created and available. */
548699f59ed0SPaul Moore 
54879f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
54889f2ad665SPaul Moore 	 * thread with access to newsksec */
5489389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
54904237c75cSVenkat Yekkirala }
54914237c75cSVenkat Yekkirala 
5492014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
54936b877699SVenkat Yekkirala {
5494aa862900SPaul Moore 	u16 family = sk->sk_family;
54956b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
54966b877699SVenkat Yekkirala 
5497aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5498aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5499aa862900SPaul Moore 		family = PF_INET;
5500aa862900SPaul Moore 
5501aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
55026b877699SVenkat Yekkirala }
55036b877699SVenkat Yekkirala 
55042606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
55052606fd1fSEric Paris {
55062606fd1fSEric Paris 	const struct task_security_struct *__tsec;
55072606fd1fSEric Paris 	u32 tsid;
55082606fd1fSEric Paris 
55090c6cfa62SCasey Schaufler 	__tsec = selinux_cred(current_cred());
55102606fd1fSEric Paris 	tsid = __tsec->sid;
55112606fd1fSEric Paris 
55126b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55136b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
55146b6bc620SStephen Smalley 			    NULL);
55152606fd1fSEric Paris }
55162606fd1fSEric Paris 
55172606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
55182606fd1fSEric Paris {
55192606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
55202606fd1fSEric Paris }
55212606fd1fSEric Paris 
55222606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
55232606fd1fSEric Paris {
55242606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
55252606fd1fSEric Paris }
55262606fd1fSEric Paris 
55279a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
55283df98d79SPaul Moore 				      struct flowi_common *flic)
55294237c75cSVenkat Yekkirala {
55303df98d79SPaul Moore 	flic->flowic_secid = req->secid;
55314237c75cSVenkat Yekkirala }
55324237c75cSVenkat Yekkirala 
55335dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
55345dbbaf2dSPaul Moore {
55355dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
55365dbbaf2dSPaul Moore 
55375dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
55385dbbaf2dSPaul Moore 	if (!tunsec)
55395dbbaf2dSPaul Moore 		return -ENOMEM;
55405dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
55415dbbaf2dSPaul Moore 
55425dbbaf2dSPaul Moore 	*security = tunsec;
55435dbbaf2dSPaul Moore 	return 0;
55445dbbaf2dSPaul Moore }
55455dbbaf2dSPaul Moore 
55465dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
55475dbbaf2dSPaul Moore {
55485dbbaf2dSPaul Moore 	kfree(security);
55495dbbaf2dSPaul Moore }
55505dbbaf2dSPaul Moore 
5551ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5552ed6d76e4SPaul Moore {
5553ed6d76e4SPaul Moore 	u32 sid = current_sid();
5554ed6d76e4SPaul Moore 
5555ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5556ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5557ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5558ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5559ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5560ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5561ed6d76e4SPaul Moore 
55626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55636b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5564ed6d76e4SPaul Moore 			    NULL);
5565ed6d76e4SPaul Moore }
5566ed6d76e4SPaul Moore 
55675dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5568ed6d76e4SPaul Moore {
55695dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
55705dbbaf2dSPaul Moore 
55716b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55726b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
55735dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
55745dbbaf2dSPaul Moore }
55755dbbaf2dSPaul Moore 
55765dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
55775dbbaf2dSPaul Moore {
55785dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5579ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5580ed6d76e4SPaul Moore 
5581ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5582ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5583ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5584ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5585ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5586ed6d76e4SPaul Moore 	 * protocols were being used */
5587ed6d76e4SPaul Moore 
55885dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5589ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
55905dbbaf2dSPaul Moore 
55915dbbaf2dSPaul Moore 	return 0;
5592ed6d76e4SPaul Moore }
5593ed6d76e4SPaul Moore 
55945dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5595ed6d76e4SPaul Moore {
55965dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5597ed6d76e4SPaul Moore 	u32 sid = current_sid();
5598ed6d76e4SPaul Moore 	int err;
5599ed6d76e4SPaul Moore 
56006b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
56016b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5602ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5603ed6d76e4SPaul Moore 	if (err)
5604ed6d76e4SPaul Moore 		return err;
56056b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
56066b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5607ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5608ed6d76e4SPaul Moore 	if (err)
5609ed6d76e4SPaul Moore 		return err;
56105dbbaf2dSPaul Moore 	tunsec->sid = sid;
5611ed6d76e4SPaul Moore 
5612ed6d76e4SPaul Moore 	return 0;
5613ed6d76e4SPaul Moore }
5614ed6d76e4SPaul Moore 
56151da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
56161da177e4SLinus Torvalds 
5617cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5618cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5619effad8dfSPaul Moore 				       u16 family)
56201da177e4SLinus Torvalds {
5621dfaebe98SPaul Moore 	int err;
5622effad8dfSPaul Moore 	char *addrp;
5623effad8dfSPaul Moore 	u32 peer_sid;
56242bf49690SThomas Liu 	struct common_audit_data ad;
562548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5626effad8dfSPaul Moore 	u8 secmark_active;
5627948bf85cSPaul Moore 	u8 netlbl_active;
5628effad8dfSPaul Moore 	u8 peerlbl_active;
56294237c75cSVenkat Yekkirala 
5630aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5631effad8dfSPaul Moore 		return NF_ACCEPT;
56324237c75cSVenkat Yekkirala 
5633effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5634948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
56352be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5636effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5637effad8dfSPaul Moore 		return NF_ACCEPT;
56384237c75cSVenkat Yekkirala 
5639d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5640d8395c87SPaul Moore 		return NF_DROP;
5641d8395c87SPaul Moore 
564250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
564348c62af6SEric Paris 	ad.u.net = &net;
5644cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
564548c62af6SEric Paris 	ad.u.net->family = family;
5646effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5647effad8dfSPaul Moore 		return NF_DROP;
56481da177e4SLinus Torvalds 
5649dfaebe98SPaul Moore 	if (peerlbl_active) {
5650cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5651cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5652dfaebe98SPaul Moore 		if (err) {
5653a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5654effad8dfSPaul Moore 			return NF_DROP;
5655dfaebe98SPaul Moore 		}
5656dfaebe98SPaul Moore 	}
5657effad8dfSPaul Moore 
5658effad8dfSPaul Moore 	if (secmark_active)
56596b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56606b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5661effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5662effad8dfSPaul Moore 			return NF_DROP;
5663effad8dfSPaul Moore 
5664948bf85cSPaul Moore 	if (netlbl_active)
5665948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5666948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5667948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5668948bf85cSPaul Moore 		 * protection */
5669948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5670948bf85cSPaul Moore 			return NF_DROP;
5671948bf85cSPaul Moore 
5672effad8dfSPaul Moore 	return NF_ACCEPT;
5673effad8dfSPaul Moore }
5674effad8dfSPaul Moore 
567506198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5676effad8dfSPaul Moore 					 struct sk_buff *skb,
5677238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5678effad8dfSPaul Moore {
5679238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5680effad8dfSPaul Moore }
5681effad8dfSPaul Moore 
56821a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
568306198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5684effad8dfSPaul Moore 					 struct sk_buff *skb,
5685238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5686effad8dfSPaul Moore {
5687238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5688effad8dfSPaul Moore }
5689effad8dfSPaul Moore #endif	/* IPV6 */
5690effad8dfSPaul Moore 
5691948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5692948bf85cSPaul Moore 				      u16 family)
5693948bf85cSPaul Moore {
569447180068SPaul Moore 	struct sock *sk;
5695948bf85cSPaul Moore 	u32 sid;
5696948bf85cSPaul Moore 
5697948bf85cSPaul Moore 	if (!netlbl_enabled())
5698948bf85cSPaul Moore 		return NF_ACCEPT;
5699948bf85cSPaul Moore 
5700948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5701948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5702948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
570347180068SPaul Moore 	sk = skb->sk;
570447180068SPaul Moore 	if (sk) {
570547180068SPaul Moore 		struct sk_security_struct *sksec;
570647180068SPaul Moore 
5707e446f9dfSEric Dumazet 		if (sk_listener(sk))
570847180068SPaul Moore 			/* if the socket is the listening state then this
570947180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
571047180068SPaul Moore 			 * be labeled based on the connection/request_sock and
571147180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
571247180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
571347180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
571447180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
571547180068SPaul Moore 			 * as any IP option based labeling should be copied
571647180068SPaul Moore 			 * from the initial connection request (in the IP
571747180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
571847180068SPaul Moore 			 * security label in the packet itself this is the
571947180068SPaul Moore 			 * best we can do. */
572047180068SPaul Moore 			return NF_ACCEPT;
572147180068SPaul Moore 
572247180068SPaul Moore 		/* standard practice, label using the parent socket */
572347180068SPaul Moore 		sksec = sk->sk_security;
5724948bf85cSPaul Moore 		sid = sksec->sid;
5725948bf85cSPaul Moore 	} else
5726948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5727948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5728948bf85cSPaul Moore 		return NF_DROP;
5729948bf85cSPaul Moore 
5730948bf85cSPaul Moore 	return NF_ACCEPT;
5731948bf85cSPaul Moore }
5732948bf85cSPaul Moore 
573306198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5734948bf85cSPaul Moore 					struct sk_buff *skb,
5735238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5736948bf85cSPaul Moore {
5737948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5738948bf85cSPaul Moore }
5739948bf85cSPaul Moore 
57401a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
57412917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
57422917f57bSHuw Davies 					struct sk_buff *skb,
57432917f57bSHuw Davies 					const struct nf_hook_state *state)
57442917f57bSHuw Davies {
57452917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
57462917f57bSHuw Davies }
57472917f57bSHuw Davies #endif	/* IPV6 */
57482917f57bSHuw Davies 
5749effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5750effad8dfSPaul Moore 						int ifindex,
5751d8395c87SPaul Moore 						u16 family)
57524e5ab4cbSJames Morris {
575354abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
57544237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
57552bf49690SThomas Liu 	struct common_audit_data ad;
575648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5757d8395c87SPaul Moore 	char *addrp;
5758d8395c87SPaul Moore 	u8 proto;
57594e5ab4cbSJames Morris 
5760effad8dfSPaul Moore 	if (sk == NULL)
5761effad8dfSPaul Moore 		return NF_ACCEPT;
57624237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
57634e5ab4cbSJames Morris 
576450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
576548c62af6SEric Paris 	ad.u.net = &net;
576648c62af6SEric Paris 	ad.u.net->netif = ifindex;
576748c62af6SEric Paris 	ad.u.net->family = family;
5768d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5769d8395c87SPaul Moore 		return NF_DROP;
5770d8395c87SPaul Moore 
577158bfbb51SPaul Moore 	if (selinux_secmark_enabled())
57726b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57736b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5774d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
57752fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
57761da177e4SLinus Torvalds 
5777d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
57782fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5779effad8dfSPaul Moore 
5780effad8dfSPaul Moore 	return NF_ACCEPT;
5781effad8dfSPaul Moore }
5782effad8dfSPaul Moore 
5783cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5784cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5785effad8dfSPaul Moore 					 u16 family)
5786effad8dfSPaul Moore {
5787effad8dfSPaul Moore 	u32 secmark_perm;
5788effad8dfSPaul Moore 	u32 peer_sid;
5789cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5790effad8dfSPaul Moore 	struct sock *sk;
57912bf49690SThomas Liu 	struct common_audit_data ad;
579248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5793effad8dfSPaul Moore 	char *addrp;
5794effad8dfSPaul Moore 	u8 secmark_active;
5795effad8dfSPaul Moore 	u8 peerlbl_active;
5796effad8dfSPaul Moore 
5797effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5798effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5799effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5800effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5801aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5802d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5803c0828e50SPaul Moore 
5804effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
58052be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5806effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5807effad8dfSPaul Moore 		return NF_ACCEPT;
5808effad8dfSPaul Moore 
580954abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5810c0828e50SPaul Moore 
5811effad8dfSPaul Moore #ifdef CONFIG_XFRM
5812effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5813effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5814effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5815effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5816effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5817c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5818c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5819c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5820c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5821c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5822c0828e50SPaul Moore 	 *       connection. */
5823c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5824e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5825effad8dfSPaul Moore 		return NF_ACCEPT;
5826effad8dfSPaul Moore #endif
5827effad8dfSPaul Moore 
5828d8395c87SPaul Moore 	if (sk == NULL) {
5829446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5830446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5831446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5832446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
58334a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5834d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5835d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
583604f6d70fSEric Paris 				return NF_DROP;
58374a7ab3dcSSteffen Klassert 		} else {
58384a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5839d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
58404a7ab3dcSSteffen Klassert 		}
5841e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5842446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5843446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5844446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5845446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5846446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5847446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5848446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5849446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5850446b8024SPaul Moore 		 * for similar problems. */
5851446b8024SPaul Moore 		u32 skb_sid;
5852e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5853e446f9dfSEric Dumazet 
5854e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5855446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5856446b8024SPaul Moore 			return NF_DROP;
5857c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5858c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5859c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5860c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5861c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5862c0828e50SPaul Moore 		 * pass the packet. */
5863c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5864c0828e50SPaul Moore 			switch (family) {
5865c0828e50SPaul Moore 			case PF_INET:
5866c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5867c0828e50SPaul Moore 					return NF_ACCEPT;
5868c0828e50SPaul Moore 				break;
5869c0828e50SPaul Moore 			case PF_INET6:
5870c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5871c0828e50SPaul Moore 					return NF_ACCEPT;
5872a7a91a19SPaul Moore 				break;
5873c0828e50SPaul Moore 			default:
5874c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5875c0828e50SPaul Moore 			}
5876c0828e50SPaul Moore 		}
5877446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5878446b8024SPaul Moore 			return NF_DROP;
5879446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5880d8395c87SPaul Moore 	} else {
5881446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5882446b8024SPaul Moore 		 * associated socket. */
5883effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5884effad8dfSPaul Moore 		peer_sid = sksec->sid;
5885effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5886effad8dfSPaul Moore 	}
5887effad8dfSPaul Moore 
588850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
588948c62af6SEric Paris 	ad.u.net = &net;
589048c62af6SEric Paris 	ad.u.net->netif = ifindex;
589148c62af6SEric Paris 	ad.u.net->family = family;
5892d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
589304f6d70fSEric Paris 		return NF_DROP;
5894d8395c87SPaul Moore 
5895effad8dfSPaul Moore 	if (secmark_active)
58966b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58976b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5898effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
58991f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5900effad8dfSPaul Moore 
5901effad8dfSPaul Moore 	if (peerlbl_active) {
5902effad8dfSPaul Moore 		u32 if_sid;
5903effad8dfSPaul Moore 		u32 node_sid;
5904effad8dfSPaul Moore 
5905cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
590604f6d70fSEric Paris 			return NF_DROP;
59076b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
59086b6bc620SStephen Smalley 				 peer_sid, if_sid,
5909effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
59101f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5911effad8dfSPaul Moore 
5912effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
591304f6d70fSEric Paris 			return NF_DROP;
59146b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
59156b6bc620SStephen Smalley 				 peer_sid, node_sid,
5916effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
59171f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5918effad8dfSPaul Moore 	}
5919effad8dfSPaul Moore 
5920effad8dfSPaul Moore 	return NF_ACCEPT;
5921effad8dfSPaul Moore }
5922effad8dfSPaul Moore 
592306198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5924a224be76SDavid S. Miller 					   struct sk_buff *skb,
5925238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
59261da177e4SLinus Torvalds {
5927238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
59281da177e4SLinus Torvalds }
59291da177e4SLinus Torvalds 
59301a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
593106198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5932a224be76SDavid S. Miller 					   struct sk_buff *skb,
5933238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
59341da177e4SLinus Torvalds {
5935238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
59361da177e4SLinus Torvalds }
59371da177e4SLinus Torvalds #endif	/* IPV6 */
59381da177e4SLinus Torvalds 
59391da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
59401da177e4SLinus Torvalds 
59411da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
59421da177e4SLinus Torvalds {
5943fb739741SPaul Moore 	int rc = 0;
5944fb739741SPaul Moore 	unsigned int msg_len;
5945fb739741SPaul Moore 	unsigned int data_len = skb->len;
5946fb739741SPaul Moore 	unsigned char *data = skb->data;
5947df4779b5SHuaisheng Ye 	struct nlmsghdr *nlh;
5948df4779b5SHuaisheng Ye 	struct sk_security_struct *sksec = sk->sk_security;
5949fb739741SPaul Moore 	u16 sclass = sksec->sclass;
5950fb739741SPaul Moore 	u32 perm;
5951df4779b5SHuaisheng Ye 
5952fb739741SPaul Moore 	while (data_len >= nlmsg_total_size(0)) {
5953fb739741SPaul Moore 		nlh = (struct nlmsghdr *)data;
5954df4779b5SHuaisheng Ye 
5955fb739741SPaul Moore 		/* NOTE: the nlmsg_len field isn't reliably set by some netlink
5956fb739741SPaul Moore 		 *       users which means we can't reject skb's with bogus
5957fb739741SPaul Moore 		 *       length fields; our solution is to follow what
5958fb739741SPaul Moore 		 *       netlink_rcv_skb() does and simply skip processing at
5959fb739741SPaul Moore 		 *       messages with length fields that are clearly junk
5960fb739741SPaul Moore 		 */
5961fb739741SPaul Moore 		if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5962fb739741SPaul Moore 			return 0;
5963fb739741SPaul Moore 
5964fb739741SPaul Moore 		rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5965fb739741SPaul Moore 		if (rc == 0) {
5966fb739741SPaul Moore 			rc = sock_has_perm(sk, perm);
5967fb739741SPaul Moore 			if (rc)
5968fb739741SPaul Moore 				return rc;
5969fb739741SPaul Moore 		} else if (rc == -EINVAL) {
5970fb739741SPaul Moore 			/* -EINVAL is a missing msg/perm mapping */
5971df4779b5SHuaisheng Ye 			pr_warn_ratelimited("SELinux: unrecognized netlink"
5972df4779b5SHuaisheng Ye 				" message: protocol=%hu nlmsg_type=%hu sclass=%s"
5973df4779b5SHuaisheng Ye 				" pid=%d comm=%s\n",
5974df4779b5SHuaisheng Ye 				sk->sk_protocol, nlh->nlmsg_type,
5975fb739741SPaul Moore 				secclass_map[sclass - 1].name,
5976df4779b5SHuaisheng Ye 				task_pid_nr(current), current->comm);
5977fb739741SPaul Moore 			if (enforcing_enabled(&selinux_state) &&
5978fb739741SPaul Moore 			    !security_get_allow_unknown(&selinux_state))
5979fb739741SPaul Moore 				return rc;
5980fb739741SPaul Moore 			rc = 0;
5981fb739741SPaul Moore 		} else if (rc == -ENOENT) {
5982fb739741SPaul Moore 			/* -ENOENT is a missing socket/class mapping, ignore */
5983fb739741SPaul Moore 			rc = 0;
5984fb739741SPaul Moore 		} else {
5985fb739741SPaul Moore 			return rc;
5986df4779b5SHuaisheng Ye 		}
5987df4779b5SHuaisheng Ye 
5988fb739741SPaul Moore 		/* move to the next message after applying netlink padding */
5989fb739741SPaul Moore 		msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5990fb739741SPaul Moore 		if (msg_len >= data_len)
5991fb739741SPaul Moore 			return 0;
5992fb739741SPaul Moore 		data_len -= msg_len;
5993fb739741SPaul Moore 		data += msg_len;
5994df4779b5SHuaisheng Ye 	}
5995df4779b5SHuaisheng Ye 
5996fb739741SPaul Moore 	return rc;
59971da177e4SLinus Torvalds }
59981da177e4SLinus Torvalds 
5999ecd5f82eSCasey Schaufler static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
60001da177e4SLinus Torvalds {
60011da177e4SLinus Torvalds 	isec->sclass = sclass;
6002be0554c9SStephen Smalley 	isec->sid = current_sid();
60031da177e4SLinus Torvalds }
60041da177e4SLinus Torvalds 
60051da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
60066af963f1SStephen Smalley 			u32 perms)
60071da177e4SLinus Torvalds {
60081da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60092bf49690SThomas Liu 	struct common_audit_data ad;
6010275bb41eSDavid Howells 	u32 sid = current_sid();
60111da177e4SLinus Torvalds 
60127c653828SCasey Schaufler 	isec = selinux_ipc(ipc_perms);
60131da177e4SLinus Torvalds 
601450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
60151da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
60161da177e4SLinus Torvalds 
60176b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60186b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
60191da177e4SLinus Torvalds }
60201da177e4SLinus Torvalds 
60211da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
60221da177e4SLinus Torvalds {
6023b82f3f68SHuaisheng Ye 	struct msg_security_struct *msec;
6024b82f3f68SHuaisheng Ye 
6025b82f3f68SHuaisheng Ye 	msec = selinux_msg_msg(msg);
6026b82f3f68SHuaisheng Ye 	msec->sid = SECINITSID_UNLABELED;
6027b82f3f68SHuaisheng Ye 
6028b82f3f68SHuaisheng Ye 	return 0;
60291da177e4SLinus Torvalds }
60301da177e4SLinus Torvalds 
60311da177e4SLinus Torvalds /* message queue security operations */
6032d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
60331da177e4SLinus Torvalds {
60341da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60352bf49690SThomas Liu 	struct common_audit_data ad;
6036275bb41eSDavid Howells 	u32 sid = current_sid();
60371da177e4SLinus Torvalds 	int rc;
60381da177e4SLinus Torvalds 
6039ecd5f82eSCasey Schaufler 	isec = selinux_ipc(msq);
6040ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_MSGQ);
60411da177e4SLinus Torvalds 
604250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6043d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60441da177e4SLinus Torvalds 
60456b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60466b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
60471da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
60481da177e4SLinus Torvalds 	return rc;
60491da177e4SLinus Torvalds }
60501da177e4SLinus Torvalds 
6051d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
60521da177e4SLinus Torvalds {
60531da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60542bf49690SThomas Liu 	struct common_audit_data ad;
6055275bb41eSDavid Howells 	u32 sid = current_sid();
60561da177e4SLinus Torvalds 
60577c653828SCasey Schaufler 	isec = selinux_ipc(msq);
60581da177e4SLinus Torvalds 
605950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6060d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60611da177e4SLinus Torvalds 
60626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60636b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
60641da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
60651da177e4SLinus Torvalds }
60661da177e4SLinus Torvalds 
6067d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
60681da177e4SLinus Torvalds {
60691da177e4SLinus Torvalds 	int err;
60701da177e4SLinus Torvalds 	int perms;
60711da177e4SLinus Torvalds 
60721da177e4SLinus Torvalds 	switch (cmd) {
60731da177e4SLinus Torvalds 	case IPC_INFO:
60741da177e4SLinus Torvalds 	case MSG_INFO:
60751da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60766b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60776b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6078be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60791da177e4SLinus Torvalds 	case IPC_STAT:
60801da177e4SLinus Torvalds 	case MSG_STAT:
608123c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
60821da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
60831da177e4SLinus Torvalds 		break;
60841da177e4SLinus Torvalds 	case IPC_SET:
60851da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
60861da177e4SLinus Torvalds 		break;
60871da177e4SLinus Torvalds 	case IPC_RMID:
60881da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
60891da177e4SLinus Torvalds 		break;
60901da177e4SLinus Torvalds 	default:
60911da177e4SLinus Torvalds 		return 0;
60921da177e4SLinus Torvalds 	}
60931da177e4SLinus Torvalds 
6094d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
60951da177e4SLinus Torvalds 	return err;
60961da177e4SLinus Torvalds }
60971da177e4SLinus Torvalds 
6098d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
60991da177e4SLinus Torvalds {
61001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61011da177e4SLinus Torvalds 	struct msg_security_struct *msec;
61022bf49690SThomas Liu 	struct common_audit_data ad;
6103275bb41eSDavid Howells 	u32 sid = current_sid();
61041da177e4SLinus Torvalds 	int rc;
61051da177e4SLinus Torvalds 
61067c653828SCasey Schaufler 	isec = selinux_ipc(msq);
61077c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
61081da177e4SLinus Torvalds 
61091da177e4SLinus Torvalds 	/*
61101da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
61111da177e4SLinus Torvalds 	 */
61121da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
61131da177e4SLinus Torvalds 		/*
61141da177e4SLinus Torvalds 		 * Compute new sid based on current process and
61151da177e4SLinus Torvalds 		 * message queue this message will be stored in
61161da177e4SLinus Torvalds 		 */
6117aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
6118aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
61191da177e4SLinus Torvalds 		if (rc)
61201da177e4SLinus Torvalds 			return rc;
61211da177e4SLinus Torvalds 	}
61221da177e4SLinus Torvalds 
612350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6124d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
61251da177e4SLinus Torvalds 
61261da177e4SLinus Torvalds 	/* Can this process write to the queue? */
61276b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61286b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
61291da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
61301da177e4SLinus Torvalds 	if (!rc)
61311da177e4SLinus Torvalds 		/* Can this process send the message */
61326b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61336b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
6134275bb41eSDavid Howells 				  MSG__SEND, &ad);
61351da177e4SLinus Torvalds 	if (!rc)
61361da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
61376b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61386b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
6139275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
61401da177e4SLinus Torvalds 
61411da177e4SLinus Torvalds 	return rc;
61421da177e4SLinus Torvalds }
61431da177e4SLinus Torvalds 
6144d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
61451da177e4SLinus Torvalds 				    struct task_struct *target,
61461da177e4SLinus Torvalds 				    long type, int mode)
61471da177e4SLinus Torvalds {
61481da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61491da177e4SLinus Torvalds 	struct msg_security_struct *msec;
61502bf49690SThomas Liu 	struct common_audit_data ad;
6151275bb41eSDavid Howells 	u32 sid = task_sid(target);
61521da177e4SLinus Torvalds 	int rc;
61531da177e4SLinus Torvalds 
61547c653828SCasey Schaufler 	isec = selinux_ipc(msq);
61557c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
61561da177e4SLinus Torvalds 
615750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6158d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
61591da177e4SLinus Torvalds 
61606b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61616b6bc620SStephen Smalley 			  sid, isec->sid,
61621da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
61631da177e4SLinus Torvalds 	if (!rc)
61646b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61656b6bc620SStephen Smalley 				  sid, msec->sid,
61661da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
61671da177e4SLinus Torvalds 	return rc;
61681da177e4SLinus Torvalds }
61691da177e4SLinus Torvalds 
61701da177e4SLinus Torvalds /* Shared Memory security operations */
61717191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
61721da177e4SLinus Torvalds {
61731da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61742bf49690SThomas Liu 	struct common_audit_data ad;
6175275bb41eSDavid Howells 	u32 sid = current_sid();
61761da177e4SLinus Torvalds 	int rc;
61771da177e4SLinus Torvalds 
6178ecd5f82eSCasey Schaufler 	isec = selinux_ipc(shp);
6179ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SHM);
61801da177e4SLinus Torvalds 
618150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61827191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61831da177e4SLinus Torvalds 
61846b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61856b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
61861da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
61871da177e4SLinus Torvalds 	return rc;
61881da177e4SLinus Torvalds }
61891da177e4SLinus Torvalds 
61907191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
61911da177e4SLinus Torvalds {
61921da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61932bf49690SThomas Liu 	struct common_audit_data ad;
6194275bb41eSDavid Howells 	u32 sid = current_sid();
61951da177e4SLinus Torvalds 
61967c653828SCasey Schaufler 	isec = selinux_ipc(shp);
61971da177e4SLinus Torvalds 
619850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61997191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
62001da177e4SLinus Torvalds 
62016b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
62026b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
62031da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
62041da177e4SLinus Torvalds }
62051da177e4SLinus Torvalds 
62061da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
62077191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
62081da177e4SLinus Torvalds {
62091da177e4SLinus Torvalds 	int perms;
62101da177e4SLinus Torvalds 	int err;
62111da177e4SLinus Torvalds 
62121da177e4SLinus Torvalds 	switch (cmd) {
62131da177e4SLinus Torvalds 	case IPC_INFO:
62141da177e4SLinus Torvalds 	case SHM_INFO:
62151da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
62166b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
62176b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6218be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
62191da177e4SLinus Torvalds 	case IPC_STAT:
62201da177e4SLinus Torvalds 	case SHM_STAT:
6221c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
62221da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
62231da177e4SLinus Torvalds 		break;
62241da177e4SLinus Torvalds 	case IPC_SET:
62251da177e4SLinus Torvalds 		perms = SHM__SETATTR;
62261da177e4SLinus Torvalds 		break;
62271da177e4SLinus Torvalds 	case SHM_LOCK:
62281da177e4SLinus Torvalds 	case SHM_UNLOCK:
62291da177e4SLinus Torvalds 		perms = SHM__LOCK;
62301da177e4SLinus Torvalds 		break;
62311da177e4SLinus Torvalds 	case IPC_RMID:
62321da177e4SLinus Torvalds 		perms = SHM__DESTROY;
62331da177e4SLinus Torvalds 		break;
62341da177e4SLinus Torvalds 	default:
62351da177e4SLinus Torvalds 		return 0;
62361da177e4SLinus Torvalds 	}
62371da177e4SLinus Torvalds 
62387191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
62391da177e4SLinus Torvalds 	return err;
62401da177e4SLinus Torvalds }
62411da177e4SLinus Torvalds 
62427191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
62431da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
62441da177e4SLinus Torvalds {
62451da177e4SLinus Torvalds 	u32 perms;
62461da177e4SLinus Torvalds 
62471da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
62481da177e4SLinus Torvalds 		perms = SHM__READ;
62491da177e4SLinus Torvalds 	else
62501da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
62511da177e4SLinus Torvalds 
62527191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
62531da177e4SLinus Torvalds }
62541da177e4SLinus Torvalds 
62551da177e4SLinus Torvalds /* Semaphore security operations */
6256aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
62571da177e4SLinus Torvalds {
62581da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62592bf49690SThomas Liu 	struct common_audit_data ad;
6260275bb41eSDavid Howells 	u32 sid = current_sid();
62611da177e4SLinus Torvalds 	int rc;
62621da177e4SLinus Torvalds 
6263ecd5f82eSCasey Schaufler 	isec = selinux_ipc(sma);
6264ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SEM);
62651da177e4SLinus Torvalds 
626650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6267aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62681da177e4SLinus Torvalds 
62696b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
62706b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
62711da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
62721da177e4SLinus Torvalds 	return rc;
62731da177e4SLinus Torvalds }
62741da177e4SLinus Torvalds 
6275aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
62761da177e4SLinus Torvalds {
62771da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62782bf49690SThomas Liu 	struct common_audit_data ad;
6279275bb41eSDavid Howells 	u32 sid = current_sid();
62801da177e4SLinus Torvalds 
62817c653828SCasey Schaufler 	isec = selinux_ipc(sma);
62821da177e4SLinus Torvalds 
628350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6284aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62851da177e4SLinus Torvalds 
62866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
62876b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
62881da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
62891da177e4SLinus Torvalds }
62901da177e4SLinus Torvalds 
62911da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6292aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
62931da177e4SLinus Torvalds {
62941da177e4SLinus Torvalds 	int err;
62951da177e4SLinus Torvalds 	u32 perms;
62961da177e4SLinus Torvalds 
62971da177e4SLinus Torvalds 	switch (cmd) {
62981da177e4SLinus Torvalds 	case IPC_INFO:
62991da177e4SLinus Torvalds 	case SEM_INFO:
63001da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
63016b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
63026b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6303be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
63041da177e4SLinus Torvalds 	case GETPID:
63051da177e4SLinus Torvalds 	case GETNCNT:
63061da177e4SLinus Torvalds 	case GETZCNT:
63071da177e4SLinus Torvalds 		perms = SEM__GETATTR;
63081da177e4SLinus Torvalds 		break;
63091da177e4SLinus Torvalds 	case GETVAL:
63101da177e4SLinus Torvalds 	case GETALL:
63111da177e4SLinus Torvalds 		perms = SEM__READ;
63121da177e4SLinus Torvalds 		break;
63131da177e4SLinus Torvalds 	case SETVAL:
63141da177e4SLinus Torvalds 	case SETALL:
63151da177e4SLinus Torvalds 		perms = SEM__WRITE;
63161da177e4SLinus Torvalds 		break;
63171da177e4SLinus Torvalds 	case IPC_RMID:
63181da177e4SLinus Torvalds 		perms = SEM__DESTROY;
63191da177e4SLinus Torvalds 		break;
63201da177e4SLinus Torvalds 	case IPC_SET:
63211da177e4SLinus Torvalds 		perms = SEM__SETATTR;
63221da177e4SLinus Torvalds 		break;
63231da177e4SLinus Torvalds 	case IPC_STAT:
63241da177e4SLinus Torvalds 	case SEM_STAT:
6325a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
63261da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
63271da177e4SLinus Torvalds 		break;
63281da177e4SLinus Torvalds 	default:
63291da177e4SLinus Torvalds 		return 0;
63301da177e4SLinus Torvalds 	}
63311da177e4SLinus Torvalds 
6332aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
63331da177e4SLinus Torvalds 	return err;
63341da177e4SLinus Torvalds }
63351da177e4SLinus Torvalds 
6336aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
63371da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
63381da177e4SLinus Torvalds {
63391da177e4SLinus Torvalds 	u32 perms;
63401da177e4SLinus Torvalds 
63411da177e4SLinus Torvalds 	if (alter)
63421da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
63431da177e4SLinus Torvalds 	else
63441da177e4SLinus Torvalds 		perms = SEM__READ;
63451da177e4SLinus Torvalds 
6346aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
63471da177e4SLinus Torvalds }
63481da177e4SLinus Torvalds 
63491da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
63501da177e4SLinus Torvalds {
63511da177e4SLinus Torvalds 	u32 av = 0;
63521da177e4SLinus Torvalds 
63531da177e4SLinus Torvalds 	av = 0;
63541da177e4SLinus Torvalds 	if (flag & S_IRUGO)
63551da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
63561da177e4SLinus Torvalds 	if (flag & S_IWUGO)
63571da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
63581da177e4SLinus Torvalds 
63591da177e4SLinus Torvalds 	if (av == 0)
63601da177e4SLinus Torvalds 		return 0;
63611da177e4SLinus Torvalds 
63626af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
63631da177e4SLinus Torvalds }
63641da177e4SLinus Torvalds 
6365713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6366713a04aeSAhmed S. Darwish {
63677c653828SCasey Schaufler 	struct ipc_security_struct *isec = selinux_ipc(ipcp);
6368713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6369713a04aeSAhmed S. Darwish }
6370713a04aeSAhmed S. Darwish 
63711da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
63721da177e4SLinus Torvalds {
63731da177e4SLinus Torvalds 	if (inode)
63741da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
63751da177e4SLinus Torvalds }
63761da177e4SLinus Torvalds 
63771da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
637804ff9708SAl Viro 			       char *name, char **value)
63791da177e4SLinus Torvalds {
6380275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
63818c8570fbSDustin Kirkland 	u32 sid;
63821da177e4SLinus Torvalds 	int error;
638304ff9708SAl Viro 	unsigned len;
63841da177e4SLinus Torvalds 
6385275bb41eSDavid Howells 	rcu_read_lock();
63860c6cfa62SCasey Schaufler 	__tsec = selinux_cred(__task_cred(p));
63871da177e4SLinus Torvalds 
6388be0554c9SStephen Smalley 	if (current != p) {
63896b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63906b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6391be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6392be0554c9SStephen Smalley 		if (error)
6393be0554c9SStephen Smalley 			goto bad;
6394be0554c9SStephen Smalley 	}
6395be0554c9SStephen Smalley 
63961da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6397275bb41eSDavid Howells 		sid = __tsec->sid;
63981da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6399275bb41eSDavid Howells 		sid = __tsec->osid;
64001da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6401275bb41eSDavid Howells 		sid = __tsec->exec_sid;
64021da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6403275bb41eSDavid Howells 		sid = __tsec->create_sid;
64044eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6405275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
640642c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6407275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6408be0554c9SStephen Smalley 	else {
6409be0554c9SStephen Smalley 		error = -EINVAL;
6410be0554c9SStephen Smalley 		goto bad;
6411be0554c9SStephen Smalley 	}
6412275bb41eSDavid Howells 	rcu_read_unlock();
64131da177e4SLinus Torvalds 
64141da177e4SLinus Torvalds 	if (!sid)
64151da177e4SLinus Torvalds 		return 0;
64161da177e4SLinus Torvalds 
6417aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
641804ff9708SAl Viro 	if (error)
641904ff9708SAl Viro 		return error;
642004ff9708SAl Viro 	return len;
6421275bb41eSDavid Howells 
6422be0554c9SStephen Smalley bad:
6423275bb41eSDavid Howells 	rcu_read_unlock();
6424be0554c9SStephen Smalley 	return error;
64251da177e4SLinus Torvalds }
64261da177e4SLinus Torvalds 
6427b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
64281da177e4SLinus Torvalds {
64291da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6430d84f4f99SDavid Howells 	struct cred *new;
6431be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
64321da177e4SLinus Torvalds 	int error;
64331da177e4SLinus Torvalds 	char *str = value;
64341da177e4SLinus Torvalds 
64351da177e4SLinus Torvalds 	/*
64361da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
64371da177e4SLinus Torvalds 	 */
64381da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
64396b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64406b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6441be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
64421da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
64436b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64446b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6445be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
64464eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
64476b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64486b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6449be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
645042c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
64516b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64526b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6453be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
64541da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
64556b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64566b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6457be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
64581da177e4SLinus Torvalds 	else
64591da177e4SLinus Torvalds 		error = -EINVAL;
64601da177e4SLinus Torvalds 	if (error)
64611da177e4SLinus Torvalds 		return error;
64621da177e4SLinus Torvalds 
64631da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6464a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
64651da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
64661da177e4SLinus Torvalds 			str[size-1] = 0;
64671da177e4SLinus Torvalds 			size--;
64681da177e4SLinus Torvalds 		}
6469aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6470aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
647112b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6472db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6473d6ea83ecSEric Paris 				struct audit_buffer *ab;
6474d6ea83ecSEric Paris 				size_t audit_size;
6475d6ea83ecSEric Paris 
6476d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6477d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6478d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6479d6ea83ecSEric Paris 					audit_size = size - 1;
6480d6ea83ecSEric Paris 				else
6481d6ea83ecSEric Paris 					audit_size = size;
6482cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6483cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6484cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6485d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6486d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6487d6ea83ecSEric Paris 				audit_log_end(ab);
6488d6ea83ecSEric Paris 
648912b29f34SStephen Smalley 				return error;
6490d6ea83ecSEric Paris 			}
6491aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6492aa8e712cSStephen Smalley 						      &selinux_state,
6493aa8e712cSStephen Smalley 						      value, size, &sid);
649412b29f34SStephen Smalley 		}
64951da177e4SLinus Torvalds 		if (error)
64961da177e4SLinus Torvalds 			return error;
64971da177e4SLinus Torvalds 	}
64981da177e4SLinus Torvalds 
6499d84f4f99SDavid Howells 	new = prepare_creds();
6500d84f4f99SDavid Howells 	if (!new)
6501d84f4f99SDavid Howells 		return -ENOMEM;
6502d84f4f99SDavid Howells 
65031da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
65041da177e4SLinus Torvalds 	   performed during the actual operation (execve,
65051da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6506b8bff599SEric W. Biederman 	   operation.  See selinux_bprm_creds_for_exec for the execve
65071da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
65081da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
65090c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
6510d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
65111da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6512d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
65131da177e4SLinus Torvalds 		tsec->create_sid = sid;
6514d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
6515464c258aSOndrej Mosnacek 		if (sid) {
6516464c258aSOndrej Mosnacek 			error = avc_has_perm(&selinux_state, mysid, sid,
6517464c258aSOndrej Mosnacek 					     SECCLASS_KEY, KEY__CREATE, NULL);
65184eb582cfSMichael LeMay 			if (error)
6519d84f4f99SDavid Howells 				goto abort_change;
6520464c258aSOndrej Mosnacek 		}
65214eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6522d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
652342c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6524d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6525d84f4f99SDavid Howells 		error = -EINVAL;
65261da177e4SLinus Torvalds 		if (sid == 0)
6527d84f4f99SDavid Howells 			goto abort_change;
6528d9250deaSKaiGai Kohei 
6529d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6530d84f4f99SDavid Howells 		error = -EPERM;
65315bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6532aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6533aa8e712cSStephen Smalley 							    tsec->sid, sid);
6534d84f4f99SDavid Howells 			if (error)
6535d84f4f99SDavid Howells 				goto abort_change;
65361da177e4SLinus Torvalds 		}
65371da177e4SLinus Torvalds 
65381da177e4SLinus Torvalds 		/* Check permissions for the transition. */
65396b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
65406b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
65411da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
65421da177e4SLinus Torvalds 		if (error)
6543d84f4f99SDavid Howells 			goto abort_change;
65441da177e4SLinus Torvalds 
65451da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
65461da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6547be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
65480c6181cbSPaul Moore 		if (ptsid != 0) {
65496b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
65506b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6551d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6552d84f4f99SDavid Howells 			if (error)
6553d84f4f99SDavid Howells 				goto abort_change;
6554d84f4f99SDavid Howells 		}
6555d84f4f99SDavid Howells 
6556d84f4f99SDavid Howells 		tsec->sid = sid;
6557d84f4f99SDavid Howells 	} else {
6558d84f4f99SDavid Howells 		error = -EINVAL;
6559d84f4f99SDavid Howells 		goto abort_change;
6560d84f4f99SDavid Howells 	}
6561d84f4f99SDavid Howells 
6562d84f4f99SDavid Howells 	commit_creds(new);
65631da177e4SLinus Torvalds 	return size;
6564d84f4f99SDavid Howells 
6565d84f4f99SDavid Howells abort_change:
6566d84f4f99SDavid Howells 	abort_creds(new);
6567d84f4f99SDavid Howells 	return error;
65681da177e4SLinus Torvalds }
65691da177e4SLinus Torvalds 
6570746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6571746df9b5SDavid Quigley {
6572746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6573746df9b5SDavid Quigley }
6574746df9b5SDavid Quigley 
6575dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6576dc49c1f9SCatherine Zhang {
6577aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6578aa8e712cSStephen Smalley 				       secdata, seclen);
6579dc49c1f9SCatherine Zhang }
6580dc49c1f9SCatherine Zhang 
65817bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
658263cb3449SDavid Howells {
6583aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6584aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
658563cb3449SDavid Howells }
658663cb3449SDavid Howells 
6587dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6588dc49c1f9SCatherine Zhang {
6589dc49c1f9SCatherine Zhang 	kfree(secdata);
6590dc49c1f9SCatherine Zhang }
6591dc49c1f9SCatherine Zhang 
65926f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
65936f3be9f5SAndreas Gruenbacher {
659480788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
65956f3be9f5SAndreas Gruenbacher 
65969287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
65976f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
65989287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
65996f3be9f5SAndreas Gruenbacher }
66006f3be9f5SAndreas Gruenbacher 
66011ee65e37SDavid P. Quigley /*
66021ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
66031ee65e37SDavid P. Quigley  */
66041ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
66051ee65e37SDavid P. Quigley {
660653e0c2aaSOndrej Mosnacek 	int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
660753e0c2aaSOndrej Mosnacek 					   ctx, ctxlen, 0);
660853e0c2aaSOndrej Mosnacek 	/* Do not return error when suppressing label (SBLABEL_MNT not set). */
660953e0c2aaSOndrej Mosnacek 	return rc == -EOPNOTSUPP ? 0 : rc;
66101ee65e37SDavid P. Quigley }
66111ee65e37SDavid P. Quigley 
66121ee65e37SDavid P. Quigley /*
66131ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
66141ee65e37SDavid P. Quigley  */
66151ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
66161ee65e37SDavid P. Quigley {
66171ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
66181ee65e37SDavid P. Quigley }
66191ee65e37SDavid P. Quigley 
66201ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
66211ee65e37SDavid P. Quigley {
66221ee65e37SDavid P. Quigley 	int len = 0;
66231ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
66241ee65e37SDavid P. Quigley 						ctx, true);
66251ee65e37SDavid P. Quigley 	if (len < 0)
66261ee65e37SDavid P. Quigley 		return len;
66271ee65e37SDavid P. Quigley 	*ctxlen = len;
66281ee65e37SDavid P. Quigley 	return 0;
66291ee65e37SDavid P. Quigley }
6630d720024eSMichael LeMay #ifdef CONFIG_KEYS
6631d720024eSMichael LeMay 
6632d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
66337e047ef5SDavid Howells 			     unsigned long flags)
6634d720024eSMichael LeMay {
6635d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6636d720024eSMichael LeMay 	struct key_security_struct *ksec;
6637d720024eSMichael LeMay 
6638d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6639d720024eSMichael LeMay 	if (!ksec)
6640d720024eSMichael LeMay 		return -ENOMEM;
6641d720024eSMichael LeMay 
66420c6cfa62SCasey Schaufler 	tsec = selinux_cred(cred);
6643d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6644d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
66454eb582cfSMichael LeMay 	else
6646d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6647d720024eSMichael LeMay 
6648275bb41eSDavid Howells 	k->security = ksec;
6649d720024eSMichael LeMay 	return 0;
6650d720024eSMichael LeMay }
6651d720024eSMichael LeMay 
6652d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6653d720024eSMichael LeMay {
6654d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6655d720024eSMichael LeMay 
6656d720024eSMichael LeMay 	k->security = NULL;
6657d720024eSMichael LeMay 	kfree(ksec);
6658d720024eSMichael LeMay }
6659d720024eSMichael LeMay 
6660d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6661d84f4f99SDavid Howells 				  const struct cred *cred,
66628c0637e9SDavid Howells 				  enum key_need_perm need_perm)
6663d720024eSMichael LeMay {
6664d720024eSMichael LeMay 	struct key *key;
6665d720024eSMichael LeMay 	struct key_security_struct *ksec;
66668c0637e9SDavid Howells 	u32 perm, sid;
6667d720024eSMichael LeMay 
66688c0637e9SDavid Howells 	switch (need_perm) {
66698c0637e9SDavid Howells 	case KEY_NEED_VIEW:
66708c0637e9SDavid Howells 		perm = KEY__VIEW;
66718c0637e9SDavid Howells 		break;
66728c0637e9SDavid Howells 	case KEY_NEED_READ:
66738c0637e9SDavid Howells 		perm = KEY__READ;
66748c0637e9SDavid Howells 		break;
66758c0637e9SDavid Howells 	case KEY_NEED_WRITE:
66768c0637e9SDavid Howells 		perm = KEY__WRITE;
66778c0637e9SDavid Howells 		break;
66788c0637e9SDavid Howells 	case KEY_NEED_SEARCH:
66798c0637e9SDavid Howells 		perm = KEY__SEARCH;
66808c0637e9SDavid Howells 		break;
66818c0637e9SDavid Howells 	case KEY_NEED_LINK:
66828c0637e9SDavid Howells 		perm = KEY__LINK;
66838c0637e9SDavid Howells 		break;
66848c0637e9SDavid Howells 	case KEY_NEED_SETATTR:
66858c0637e9SDavid Howells 		perm = KEY__SETATTR;
66868c0637e9SDavid Howells 		break;
66878c0637e9SDavid Howells 	case KEY_NEED_UNLINK:
66888c0637e9SDavid Howells 	case KEY_SYSADMIN_OVERRIDE:
66898c0637e9SDavid Howells 	case KEY_AUTHTOKEN_OVERRIDE:
66908c0637e9SDavid Howells 	case KEY_DEFER_PERM_CHECK:
6691d720024eSMichael LeMay 		return 0;
66928c0637e9SDavid Howells 	default:
66938c0637e9SDavid Howells 		WARN_ON(1);
66948c0637e9SDavid Howells 		return -EPERM;
66958c0637e9SDavid Howells 
66968c0637e9SDavid Howells 	}
6697d720024eSMichael LeMay 
6698d84f4f99SDavid Howells 	sid = cred_sid(cred);
6699275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6700275bb41eSDavid Howells 	ksec = key->security;
6701275bb41eSDavid Howells 
67026b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
6703028db3e2SLinus Torvalds 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6704d720024eSMichael LeMay }
6705d720024eSMichael LeMay 
670670a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
670770a5bb72SDavid Howells {
670870a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
670970a5bb72SDavid Howells 	char *context = NULL;
671070a5bb72SDavid Howells 	unsigned len;
671170a5bb72SDavid Howells 	int rc;
671270a5bb72SDavid Howells 
6713aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6714aa8e712cSStephen Smalley 				     &context, &len);
671570a5bb72SDavid Howells 	if (!rc)
671670a5bb72SDavid Howells 		rc = len;
671770a5bb72SDavid Howells 	*_buffer = context;
671870a5bb72SDavid Howells 	return rc;
671970a5bb72SDavid Howells }
67203e412cccSDavid Howells 
67213e412cccSDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
67223e412cccSDavid Howells static int selinux_watch_key(struct key *key)
67233e412cccSDavid Howells {
67243e412cccSDavid Howells 	struct key_security_struct *ksec = key->security;
67253e412cccSDavid Howells 	u32 sid = current_sid();
67263e412cccSDavid Howells 
67273e412cccSDavid Howells 	return avc_has_perm(&selinux_state,
67283e412cccSDavid Howells 			    sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
67293e412cccSDavid Howells }
67303e412cccSDavid Howells #endif
67313a976fa6SDaniel Jurgens #endif
673270a5bb72SDavid Howells 
67333a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6734cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6735cfc4d882SDaniel Jurgens {
6736cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6737cfc4d882SDaniel Jurgens 	int err;
6738cfc4d882SDaniel Jurgens 	u32 sid = 0;
6739cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6740cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6741cfc4d882SDaniel Jurgens 
6742409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6743cfc4d882SDaniel Jurgens 	if (err)
6744cfc4d882SDaniel Jurgens 		return err;
6745cfc4d882SDaniel Jurgens 
6746cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6747cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6748cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6749cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
67506b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67516b6bc620SStephen Smalley 			    sec->sid, sid,
6752cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6753cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6754cfc4d882SDaniel Jurgens }
6755cfc4d882SDaniel Jurgens 
6756ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6757ab861dfcSDaniel Jurgens 					    u8 port_num)
6758ab861dfcSDaniel Jurgens {
6759ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6760ab861dfcSDaniel Jurgens 	int err;
6761ab861dfcSDaniel Jurgens 	u32 sid = 0;
6762ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6763ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6764ab861dfcSDaniel Jurgens 
6765aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6766aa8e712cSStephen Smalley 				      &sid);
6767ab861dfcSDaniel Jurgens 
6768ab861dfcSDaniel Jurgens 	if (err)
6769ab861dfcSDaniel Jurgens 		return err;
6770ab861dfcSDaniel Jurgens 
6771ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6772ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6773ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6774ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
67756b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67766b6bc620SStephen Smalley 			    sec->sid, sid,
6777ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6778ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6779ab861dfcSDaniel Jurgens }
6780ab861dfcSDaniel Jurgens 
67813a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
67823a976fa6SDaniel Jurgens {
67833a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
67843a976fa6SDaniel Jurgens 
67853a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
67863a976fa6SDaniel Jurgens 	if (!sec)
67873a976fa6SDaniel Jurgens 		return -ENOMEM;
67883a976fa6SDaniel Jurgens 	sec->sid = current_sid();
67893a976fa6SDaniel Jurgens 
67903a976fa6SDaniel Jurgens 	*ib_sec = sec;
67913a976fa6SDaniel Jurgens 	return 0;
67923a976fa6SDaniel Jurgens }
67933a976fa6SDaniel Jurgens 
67943a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
67953a976fa6SDaniel Jurgens {
67963a976fa6SDaniel Jurgens 	kfree(ib_sec);
67973a976fa6SDaniel Jurgens }
6798d720024eSMichael LeMay #endif
6799d720024eSMichael LeMay 
6800ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6801ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6802ec27c356SChenbo Feng 				     unsigned int size)
6803ec27c356SChenbo Feng {
6804ec27c356SChenbo Feng 	u32 sid = current_sid();
6805ec27c356SChenbo Feng 	int ret;
6806ec27c356SChenbo Feng 
6807ec27c356SChenbo Feng 	switch (cmd) {
6808ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
68096b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68106b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6811ec27c356SChenbo Feng 				   NULL);
6812ec27c356SChenbo Feng 		break;
6813ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
68146b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68156b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6816ec27c356SChenbo Feng 				   NULL);
6817ec27c356SChenbo Feng 		break;
6818ec27c356SChenbo Feng 	default:
6819ec27c356SChenbo Feng 		ret = 0;
6820ec27c356SChenbo Feng 		break;
6821ec27c356SChenbo Feng 	}
6822ec27c356SChenbo Feng 
6823ec27c356SChenbo Feng 	return ret;
6824ec27c356SChenbo Feng }
6825ec27c356SChenbo Feng 
6826ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6827ec27c356SChenbo Feng {
6828ec27c356SChenbo Feng 	u32 av = 0;
6829ec27c356SChenbo Feng 
6830ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6831ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6832ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6833ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6834ec27c356SChenbo Feng 	return av;
6835ec27c356SChenbo Feng }
6836ec27c356SChenbo Feng 
6837f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6838f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6839f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6840f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6841f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6842f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6843f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6844f66e448cSChenbo Feng  */
6845f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6846f66e448cSChenbo Feng {
6847f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6848f66e448cSChenbo Feng 	struct bpf_prog *prog;
6849f66e448cSChenbo Feng 	struct bpf_map *map;
6850f66e448cSChenbo Feng 	int ret;
6851f66e448cSChenbo Feng 
6852f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6853f66e448cSChenbo Feng 		map = file->private_data;
6854f66e448cSChenbo Feng 		bpfsec = map->security;
68556b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68566b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6857f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6858f66e448cSChenbo Feng 		if (ret)
6859f66e448cSChenbo Feng 			return ret;
6860f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6861f66e448cSChenbo Feng 		prog = file->private_data;
6862f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
68636b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68646b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6865f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6866f66e448cSChenbo Feng 		if (ret)
6867f66e448cSChenbo Feng 			return ret;
6868f66e448cSChenbo Feng 	}
6869f66e448cSChenbo Feng 	return 0;
6870f66e448cSChenbo Feng }
6871f66e448cSChenbo Feng 
6872ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6873ec27c356SChenbo Feng {
6874ec27c356SChenbo Feng 	u32 sid = current_sid();
6875ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6876ec27c356SChenbo Feng 
6877ec27c356SChenbo Feng 	bpfsec = map->security;
68786b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68796b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6880ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6881ec27c356SChenbo Feng }
6882ec27c356SChenbo Feng 
6883ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6884ec27c356SChenbo Feng {
6885ec27c356SChenbo Feng 	u32 sid = current_sid();
6886ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6887ec27c356SChenbo Feng 
6888ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
68896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68906b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6891ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6892ec27c356SChenbo Feng }
6893ec27c356SChenbo Feng 
6894ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6895ec27c356SChenbo Feng {
6896ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6897ec27c356SChenbo Feng 
6898ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6899ec27c356SChenbo Feng 	if (!bpfsec)
6900ec27c356SChenbo Feng 		return -ENOMEM;
6901ec27c356SChenbo Feng 
6902ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6903ec27c356SChenbo Feng 	map->security = bpfsec;
6904ec27c356SChenbo Feng 
6905ec27c356SChenbo Feng 	return 0;
6906ec27c356SChenbo Feng }
6907ec27c356SChenbo Feng 
6908ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6909ec27c356SChenbo Feng {
6910ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6911ec27c356SChenbo Feng 
6912ec27c356SChenbo Feng 	map->security = NULL;
6913ec27c356SChenbo Feng 	kfree(bpfsec);
6914ec27c356SChenbo Feng }
6915ec27c356SChenbo Feng 
6916ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6917ec27c356SChenbo Feng {
6918ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6919ec27c356SChenbo Feng 
6920ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6921ec27c356SChenbo Feng 	if (!bpfsec)
6922ec27c356SChenbo Feng 		return -ENOMEM;
6923ec27c356SChenbo Feng 
6924ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6925ec27c356SChenbo Feng 	aux->security = bpfsec;
6926ec27c356SChenbo Feng 
6927ec27c356SChenbo Feng 	return 0;
6928ec27c356SChenbo Feng }
6929ec27c356SChenbo Feng 
6930ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6931ec27c356SChenbo Feng {
6932ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6933ec27c356SChenbo Feng 
6934ec27c356SChenbo Feng 	aux->security = NULL;
6935ec27c356SChenbo Feng 	kfree(bpfsec);
6936ec27c356SChenbo Feng }
6937ec27c356SChenbo Feng #endif
6938ec27c356SChenbo Feng 
693959438b46SStephen Smalley static int selinux_lockdown(enum lockdown_reason what)
694059438b46SStephen Smalley {
694159438b46SStephen Smalley 	struct common_audit_data ad;
694259438b46SStephen Smalley 	u32 sid = current_sid();
694359438b46SStephen Smalley 	int invalid_reason = (what <= LOCKDOWN_NONE) ||
694459438b46SStephen Smalley 			     (what == LOCKDOWN_INTEGRITY_MAX) ||
694559438b46SStephen Smalley 			     (what >= LOCKDOWN_CONFIDENTIALITY_MAX);
694659438b46SStephen Smalley 
694759438b46SStephen Smalley 	if (WARN(invalid_reason, "Invalid lockdown reason")) {
694859438b46SStephen Smalley 		audit_log(audit_context(),
694959438b46SStephen Smalley 			  GFP_ATOMIC, AUDIT_SELINUX_ERR,
695059438b46SStephen Smalley 			  "lockdown_reason=invalid");
695159438b46SStephen Smalley 		return -EINVAL;
695259438b46SStephen Smalley 	}
695359438b46SStephen Smalley 
695459438b46SStephen Smalley 	ad.type = LSM_AUDIT_DATA_LOCKDOWN;
695559438b46SStephen Smalley 	ad.u.reason = what;
695659438b46SStephen Smalley 
695759438b46SStephen Smalley 	if (what <= LOCKDOWN_INTEGRITY_MAX)
695859438b46SStephen Smalley 		return avc_has_perm(&selinux_state,
695959438b46SStephen Smalley 				    sid, sid, SECCLASS_LOCKDOWN,
696059438b46SStephen Smalley 				    LOCKDOWN__INTEGRITY, &ad);
696159438b46SStephen Smalley 	else
696259438b46SStephen Smalley 		return avc_has_perm(&selinux_state,
696359438b46SStephen Smalley 				    sid, sid, SECCLASS_LOCKDOWN,
696459438b46SStephen Smalley 				    LOCKDOWN__CONFIDENTIALITY, &ad);
696559438b46SStephen Smalley }
696659438b46SStephen Smalley 
6967bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6968bbd3662aSCasey Schaufler 	.lbs_cred = sizeof(struct task_security_struct),
696933bf60caSCasey Schaufler 	.lbs_file = sizeof(struct file_security_struct),
6970afb1cbe3SCasey Schaufler 	.lbs_inode = sizeof(struct inode_security_struct),
6971ecd5f82eSCasey Schaufler 	.lbs_ipc = sizeof(struct ipc_security_struct),
6972ecd5f82eSCasey Schaufler 	.lbs_msg_msg = sizeof(struct msg_security_struct),
6973bbd3662aSCasey Schaufler };
6974bbd3662aSCasey Schaufler 
6975da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
6976da97e184SJoel Fernandes (Google) static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6977da97e184SJoel Fernandes (Google) {
6978da97e184SJoel Fernandes (Google) 	u32 requested, sid = current_sid();
6979da97e184SJoel Fernandes (Google) 
6980da97e184SJoel Fernandes (Google) 	if (type == PERF_SECURITY_OPEN)
6981da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__OPEN;
6982da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_CPU)
6983da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__CPU;
6984da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_KERNEL)
6985da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__KERNEL;
6986da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_TRACEPOINT)
6987da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__TRACEPOINT;
6988da97e184SJoel Fernandes (Google) 	else
6989da97e184SJoel Fernandes (Google) 		return -EINVAL;
6990da97e184SJoel Fernandes (Google) 
6991da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6992da97e184SJoel Fernandes (Google) 			    requested, NULL);
6993da97e184SJoel Fernandes (Google) }
6994da97e184SJoel Fernandes (Google) 
6995da97e184SJoel Fernandes (Google) static int selinux_perf_event_alloc(struct perf_event *event)
6996da97e184SJoel Fernandes (Google) {
6997da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec;
6998da97e184SJoel Fernandes (Google) 
6999da97e184SJoel Fernandes (Google) 	perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
7000da97e184SJoel Fernandes (Google) 	if (!perfsec)
7001da97e184SJoel Fernandes (Google) 		return -ENOMEM;
7002da97e184SJoel Fernandes (Google) 
7003da97e184SJoel Fernandes (Google) 	perfsec->sid = current_sid();
7004da97e184SJoel Fernandes (Google) 	event->security = perfsec;
7005da97e184SJoel Fernandes (Google) 
7006da97e184SJoel Fernandes (Google) 	return 0;
7007da97e184SJoel Fernandes (Google) }
7008da97e184SJoel Fernandes (Google) 
7009da97e184SJoel Fernandes (Google) static void selinux_perf_event_free(struct perf_event *event)
7010da97e184SJoel Fernandes (Google) {
7011da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
7012da97e184SJoel Fernandes (Google) 
7013da97e184SJoel Fernandes (Google) 	event->security = NULL;
7014da97e184SJoel Fernandes (Google) 	kfree(perfsec);
7015da97e184SJoel Fernandes (Google) }
7016da97e184SJoel Fernandes (Google) 
7017da97e184SJoel Fernandes (Google) static int selinux_perf_event_read(struct perf_event *event)
7018da97e184SJoel Fernandes (Google) {
7019da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
7020da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
7021da97e184SJoel Fernandes (Google) 
7022da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
7023da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
7024da97e184SJoel Fernandes (Google) }
7025da97e184SJoel Fernandes (Google) 
7026da97e184SJoel Fernandes (Google) static int selinux_perf_event_write(struct perf_event *event)
7027da97e184SJoel Fernandes (Google) {
7028da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
7029da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
7030da97e184SJoel Fernandes (Google) 
7031da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
7032da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
7033da97e184SJoel Fernandes (Google) }
7034da97e184SJoel Fernandes (Google) #endif
7035da97e184SJoel Fernandes (Google) 
7036cfff75d8SOndrej Mosnacek /*
7037cfff75d8SOndrej Mosnacek  * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7038cfff75d8SOndrej Mosnacek  * 1. any hooks that don't belong to (2.) or (3.) below,
7039cfff75d8SOndrej Mosnacek  * 2. hooks that both access structures allocated by other hooks, and allocate
7040cfff75d8SOndrej Mosnacek  *    structures that can be later accessed by other hooks (mostly "cloning"
7041cfff75d8SOndrej Mosnacek  *    hooks),
7042cfff75d8SOndrej Mosnacek  * 3. hooks that only allocate structures that can be later accessed by other
7043cfff75d8SOndrej Mosnacek  *    hooks ("allocating" hooks).
7044cfff75d8SOndrej Mosnacek  *
7045cfff75d8SOndrej Mosnacek  * Please follow block comment delimiters in the list to keep this order.
7046cfff75d8SOndrej Mosnacek  *
7047cfff75d8SOndrej Mosnacek  * This ordering is needed for SELinux runtime disable to work at least somewhat
7048cfff75d8SOndrej Mosnacek  * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7049cfff75d8SOndrej Mosnacek  * when disabling SELinux at runtime.
7050cfff75d8SOndrej Mosnacek  */
7051ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
7052e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7053e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7054e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7055e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
7056076c54c5SAhmed S. Darwish 
7057e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7058e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7059e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
7060e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
7061e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
7062e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
7063e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
7064e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
7065e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
706679af7307SStephen Smalley 
7067e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
70681da177e4SLinus Torvalds 
7069b8bff599SEric W. Biederman 	LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
7070e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7071e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
70721da177e4SLinus Torvalds 
7073e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
7074204cc0ccSAl Viro 	LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
7075e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7076e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7077e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7078e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7079e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
7080e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
7081e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7082e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
70831da177e4SLinus Torvalds 
708498aa0034SStephen Smalley 	LSM_HOOK_INIT(move_mount, selinux_move_mount),
708598aa0034SStephen Smalley 
7086e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
7087a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
7088e0007529SEric Paris 
7089e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7090e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
7091*29cd6591SDaniel Colascione 	LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
7092e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
7093e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
7094e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7095e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7096e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7097e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7098e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7099e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7100e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7101e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7102e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7103e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7104e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7105e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7106e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7107e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7108e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7109e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7110e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7111e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7112e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7113e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
711456909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
711519472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
7116ac5656d8SAaron Goidel 	LSM_HOOK_INIT(path_notify, selinux_path_notify),
71171da177e4SLinus Torvalds 
7118ec882da5SOndrej Mosnacek 	LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7119ec882da5SOndrej Mosnacek 
7120e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
7121e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
7122e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7123e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7124e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7125e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7126e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
7127e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7128e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7129e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7130e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
71311da177e4SLinus Torvalds 
7132e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
71331da177e4SLinus Torvalds 
7134a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
7135e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7136e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
71373ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
7138e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7139e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7140e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
7141c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
714261d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
7143e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7144e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7145e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
7146e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
7147e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7148e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7149e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7150791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7151e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7152e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7153e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7154e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7155e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
7156e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7157788e7dd4SYuichi Nakamura 
7158e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7159e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
71601da177e4SLinus Torvalds 
7161e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7162e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7163e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7164e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
71651da177e4SLinus Torvalds 
7166e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7167e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7168e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
71691da177e4SLinus Torvalds 
7170e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7171e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7172e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
71731da177e4SLinus Torvalds 
7174e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
71751da177e4SLinus Torvalds 
7176e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7177e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
71781da177e4SLinus Torvalds 
7179e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7180e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7181e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
71826f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7183e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7184e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
71851da177e4SLinus Torvalds 
7186e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7187e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7188dc49c1f9SCatherine Zhang 
7189e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
7190e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
71910b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7192e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7193e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7194e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7195e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7196e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7197e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7198e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7199e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7200e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7201e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7202e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7203e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7204e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
7205e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
7206e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7207e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7208e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7209e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7210e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7211d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7212d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7213d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7214e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7215e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7216e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7217e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7218e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7219e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7220e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7221e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7222e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7223e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7224e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7225e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
72263a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
7227cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7228ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
7229ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
72303a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
72313a976fa6SDaniel Jurgens #endif
7232d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
7233e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7234e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7235e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7236e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7237e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7238e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7239e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
7240e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
72411da177e4SLinus Torvalds #endif
7242d720024eSMichael LeMay 
7243d720024eSMichael LeMay #ifdef CONFIG_KEYS
7244e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
7245e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
7246e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
72473e412cccSDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
72483e412cccSDavid Howells 	LSM_HOOK_INIT(watch_key, selinux_watch_key),
72493e412cccSDavid Howells #endif
7250d720024eSMichael LeMay #endif
72519d57a7f9SAhmed S. Darwish 
72529d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
7253e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7254e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7255e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
72569d57a7f9SAhmed S. Darwish #endif
7257ec27c356SChenbo Feng 
7258ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
7259ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
7260ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7261ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7262ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7263ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7264ec27c356SChenbo Feng #endif
7265da97e184SJoel Fernandes (Google) 
7266da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
7267da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7268da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7269da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7270da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7271da97e184SJoel Fernandes (Google) #endif
727259438b46SStephen Smalley 
727359438b46SStephen Smalley 	LSM_HOOK_INIT(locked_down, selinux_lockdown),
7274cfff75d8SOndrej Mosnacek 
7275cfff75d8SOndrej Mosnacek 	/*
7276cfff75d8SOndrej Mosnacek 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7277cfff75d8SOndrej Mosnacek 	 */
7278cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7279cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7280cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7281cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
7282cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7283cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7284cfff75d8SOndrej Mosnacek #endif
7285cfff75d8SOndrej Mosnacek 
7286cfff75d8SOndrej Mosnacek 	/*
7287cfff75d8SOndrej Mosnacek 	 * PUT "ALLOCATING" HOOKS HERE
7288cfff75d8SOndrej Mosnacek 	 */
7289cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7290cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_queue_alloc_security,
7291cfff75d8SOndrej Mosnacek 		      selinux_msg_queue_alloc_security),
7292cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7293cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7294cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7295cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7296cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7297cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7298cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7299cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7300cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_INFINIBAND
7301cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7302cfff75d8SOndrej Mosnacek #endif
7303cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7304cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7305cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7306cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7307cfff75d8SOndrej Mosnacek 		      selinux_xfrm_state_alloc_acquire),
7308cfff75d8SOndrej Mosnacek #endif
7309cfff75d8SOndrej Mosnacek #ifdef CONFIG_KEYS
7310cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7311cfff75d8SOndrej Mosnacek #endif
7312cfff75d8SOndrej Mosnacek #ifdef CONFIG_AUDIT
7313cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7314cfff75d8SOndrej Mosnacek #endif
7315cfff75d8SOndrej Mosnacek #ifdef CONFIG_BPF_SYSCALL
7316cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7317cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7318cfff75d8SOndrej Mosnacek #endif
7319cfff75d8SOndrej Mosnacek #ifdef CONFIG_PERF_EVENTS
7320cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7321cfff75d8SOndrej Mosnacek #endif
73221da177e4SLinus Torvalds };
73231da177e4SLinus Torvalds 
73241da177e4SLinus Torvalds static __init int selinux_init(void)
73251da177e4SLinus Torvalds {
7326c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
73271da177e4SLinus Torvalds 
7328aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
7329e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
73308861d0afSLakshmi Ramasubramanian 	checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
73316b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
73324b36cb77SOndrej Mosnacek 	mutex_init(&selinux_state.status_lock);
73339ff9abc4SStephen Smalley 	mutex_init(&selinux_state.policy_mutex);
7334aa8e712cSStephen Smalley 
73351da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
7336d84f4f99SDavid Howells 	cred_init_security();
73371da177e4SLinus Torvalds 
7338fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7339fcaaade1SStephen Smalley 
73401da177e4SLinus Torvalds 	avc_init();
73411da177e4SLinus Torvalds 
7342aa8e712cSStephen Smalley 	avtab_cache_init();
7343aa8e712cSStephen Smalley 
7344aa8e712cSStephen Smalley 	ebitmap_cache_init();
7345aa8e712cSStephen Smalley 
7346aa8e712cSStephen Smalley 	hashtab_cache_init();
7347aa8e712cSStephen Smalley 
7348d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
73491da177e4SLinus Torvalds 
7350615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7351615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7352615e51fdSPaul Moore 
73538f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
73548f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
73558f408ab6SDaniel Jurgens 
7356aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7357c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
7358828dfe1dSEric Paris 	else
7359c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
7360d720024eSMichael LeMay 
7361d7167b14SAl Viro 	fs_validate_description("selinux", selinux_fs_parameters);
7362442155c1SDavid Howells 
73631da177e4SLinus Torvalds 	return 0;
73641da177e4SLinus Torvalds }
73651da177e4SLinus Torvalds 
7366e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7367e8c26255SAl Viro {
7368204cc0ccSAl Viro 	selinux_set_mnt_opts(sb, NULL, 0, NULL);
7369e8c26255SAl Viro }
7370e8c26255SAl Viro 
73711da177e4SLinus Torvalds void selinux_complete_init(void)
73721da177e4SLinus Torvalds {
7373c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
73741da177e4SLinus Torvalds 
73751da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7376c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
7377e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
73781da177e4SLinus Torvalds }
73791da177e4SLinus Torvalds 
73801da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
73811da177e4SLinus Torvalds    all processes and objects when they are created. */
73823d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
738307aed2f2SKees Cook 	.name = "selinux",
738414bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
73856c5a682eSStephen Smalley 	.enabled = &selinux_enabled_boot,
7386bbd3662aSCasey Schaufler 	.blobs = &selinux_blob_sizes,
73873d6e5f6dSKees Cook 	.init = selinux_init,
73883d6e5f6dSKees Cook };
73891da177e4SLinus Torvalds 
7390c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
73911da177e4SLinus Torvalds 
7392591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7393effad8dfSPaul Moore 	{
7394effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
73952597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
73966e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
73971da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7398effad8dfSPaul Moore 	},
7399effad8dfSPaul Moore 	{
7400effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
74012597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7402effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7403effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7404948bf85cSPaul Moore 	},
7405948bf85cSPaul Moore 	{
7406948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
74072597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7408948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7409948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
741025db6beaSJiri Pirko 	},
74111a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7412effad8dfSPaul Moore 	{
7413effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
74142597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
74156e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
74161da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7417effad8dfSPaul Moore 	},
7418effad8dfSPaul Moore 	{
7419effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
74202597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7421effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7422effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
742325db6beaSJiri Pirko 	},
74242917f57bSHuw Davies 	{
74252917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
74262917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
74272917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
74282917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
74292917f57bSHuw Davies 	},
74301da177e4SLinus Torvalds #endif	/* IPV6 */
743125db6beaSJiri Pirko };
74321da177e4SLinus Torvalds 
74338e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
74348e71bf75SFlorian Westphal {
74358e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
74368e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
74378e71bf75SFlorian Westphal }
74388e71bf75SFlorian Westphal 
74398e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
74408e71bf75SFlorian Westphal {
74418e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
74428e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
74438e71bf75SFlorian Westphal }
74448e71bf75SFlorian Westphal 
74458e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
74468e71bf75SFlorian Westphal 	.init = selinux_nf_register,
74478e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
74488e71bf75SFlorian Westphal };
74498e71bf75SFlorian Westphal 
74501da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
74511da177e4SLinus Torvalds {
745225db6beaSJiri Pirko 	int err;
74531da177e4SLinus Torvalds 
74546c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
745525db6beaSJiri Pirko 		return 0;
74561da177e4SLinus Torvalds 
7457c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
74581da177e4SLinus Torvalds 
74598e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
74601da177e4SLinus Torvalds 	if (err)
74618e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
74621da177e4SLinus Torvalds 
746325db6beaSJiri Pirko 	return 0;
74641da177e4SLinus Torvalds }
74651da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
74661da177e4SLinus Torvalds 
74671da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
74681da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
74691da177e4SLinus Torvalds {
7470c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
74711da177e4SLinus Torvalds 
74728e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
74731da177e4SLinus Torvalds }
74741da177e4SLinus Torvalds #endif
74751da177e4SLinus Torvalds 
7476c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
74771da177e4SLinus Torvalds 
74781da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
74791da177e4SLinus Torvalds #define selinux_nf_ip_exit()
74801da177e4SLinus Torvalds #endif
74811da177e4SLinus Torvalds 
7482c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
74831da177e4SLinus Torvalds 
74841da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7485aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
74861da177e4SLinus Torvalds {
748765cddd50SOndrej Mosnacek 	if (selinux_initialized(state)) {
74881da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
74891da177e4SLinus Torvalds 		return -EINVAL;
74901da177e4SLinus Torvalds 	}
74911da177e4SLinus Torvalds 
749265cddd50SOndrej Mosnacek 	if (selinux_disabled(state)) {
74931da177e4SLinus Torvalds 		/* Only do this once. */
74941da177e4SLinus Torvalds 		return -EINVAL;
74951da177e4SLinus Torvalds 	}
74961da177e4SLinus Torvalds 
749765cddd50SOndrej Mosnacek 	selinux_mark_disabled(state);
7498aa8e712cSStephen Smalley 
7499c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
75001da177e4SLinus Torvalds 
7501cfff75d8SOndrej Mosnacek 	/*
7502cfff75d8SOndrej Mosnacek 	 * Unregister netfilter hooks.
7503cfff75d8SOndrej Mosnacek 	 * Must be done before security_delete_hooks() to avoid breaking
7504cfff75d8SOndrej Mosnacek 	 * runtime disable.
7505cfff75d8SOndrej Mosnacek 	 */
7506cfff75d8SOndrej Mosnacek 	selinux_nf_ip_exit();
7507cfff75d8SOndrej Mosnacek 
7508b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
75091da177e4SLinus Torvalds 
7510af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7511af8ff049SEric Paris 	avc_disable();
7512af8ff049SEric Paris 
75131da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
75141da177e4SLinus Torvalds 	exit_sel_fs();
75151da177e4SLinus Torvalds 
75161da177e4SLinus Torvalds 	return 0;
75171da177e4SLinus Torvalds }
75181da177e4SLinus Torvalds #endif
7519