xref: /openbmc/linux/security/selinux/hooks.c (revision cb89e2465896f30d4247ac9ff47d30522e39745a)
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>
270d094efeSRoland McGrath #include <linux/tracehook.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
293f07c014SIngo Molnar #include <linux/sched/signal.h>
3029930025SIngo Molnar #include <linux/sched/task.h>
313c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
321da177e4SLinus Torvalds #include <linux/xattr.h>
331da177e4SLinus Torvalds #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/unistd.h>
351da177e4SLinus Torvalds #include <linux/mm.h>
361da177e4SLinus Torvalds #include <linux/mman.h>
371da177e4SLinus Torvalds #include <linux/slab.h>
381da177e4SLinus Torvalds #include <linux/pagemap.h>
390b24dcb7SEric Paris #include <linux/proc_fs.h>
401da177e4SLinus Torvalds #include <linux/swap.h>
411da177e4SLinus Torvalds #include <linux/spinlock.h>
421da177e4SLinus Torvalds #include <linux/syscalls.h>
432a7dba39SEric Paris #include <linux/dcache.h>
441da177e4SLinus Torvalds #include <linux/file.h>
459f3acc31SAl Viro #include <linux/fdtable.h>
461da177e4SLinus Torvalds #include <linux/namei.h>
471da177e4SLinus Torvalds #include <linux/mount.h>
48442155c1SDavid Howells #include <linux/fs_context.h>
49442155c1SDavid Howells #include <linux/fs_parser.h>
501da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
511da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
521da177e4SLinus Torvalds #include <linux/tty.h>
531da177e4SLinus Torvalds #include <net/icmp.h>
54227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
551da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5647180068SPaul Moore #include <net/inet_connection_sock.h>
57220deb96SPaul Moore #include <net/net_namespace.h>
58d621d35eSPaul Moore #include <net/netlabel.h>
59f5269710SEric Paris #include <linux/uaccess.h>
601da177e4SLinus Torvalds #include <asm/ioctls.h>
6160063497SArun Sharma #include <linux/atomic.h>
621da177e4SLinus Torvalds #include <linux/bitops.h>
631da177e4SLinus Torvalds #include <linux/interrupt.h>
641da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6577954983SHong zhi guo #include <net/netlink.h>
661da177e4SLinus Torvalds #include <linux/tcp.h>
671da177e4SLinus Torvalds #include <linux/udp.h>
682ee92d46SJames Morris #include <linux/dccp.h>
69d452930fSRichard Haines #include <linux/sctp.h>
70d452930fSRichard Haines #include <net/sctp/structs.h>
711da177e4SLinus Torvalds #include <linux/quota.h>
721da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
731da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
741da177e4SLinus Torvalds #include <linux/parser.h>
751da177e4SLinus Torvalds #include <linux/nfs_mount.h>
761da177e4SLinus Torvalds #include <net/ipv6.h>
771da177e4SLinus Torvalds #include <linux/hugetlb.h>
781da177e4SLinus Torvalds #include <linux/personality.h>
791da177e4SLinus Torvalds #include <linux/audit.h>
806931dfc9SEric Paris #include <linux/string.h>
8123970741SEric Paris #include <linux/mutex.h>
82f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8300234592SKees Cook #include <linux/syslog.h>
843486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8544fc7ea0SPaul Gortmaker #include <linux/export.h>
8640401530SAl Viro #include <linux/msg.h>
8740401530SAl Viro #include <linux/shm.h>
88ec27c356SChenbo Feng #include <linux/bpf.h>
89ec882da5SOndrej Mosnacek #include <linux/kernfs.h>
90ec882da5SOndrej Mosnacek #include <linux/stringhash.h>	/* for hashlen_string() */
91e262e32dSDavid Howells #include <uapi/linux/mount.h>
92ac5656d8SAaron Goidel #include <linux/fsnotify.h>
93ac5656d8SAaron Goidel #include <linux/fanotify.h>
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #include "avc.h"
961da177e4SLinus Torvalds #include "objsec.h"
971da177e4SLinus Torvalds #include "netif.h"
98224dfbd8SPaul Moore #include "netnode.h"
993e112172SPaul Moore #include "netport.h"
100409dcf31SDaniel Jurgens #include "ibpkey.h"
101d28d1e08STrent Jaeger #include "xfrm.h"
102c60475bfSPaul Moore #include "netlabel.h"
1039d57a7f9SAhmed S. Darwish #include "audit.h"
1047b98a585SJames Morris #include "avc_ss.h"
1051da177e4SLinus Torvalds 
106aa8e712cSStephen Smalley struct selinux_state selinux_state;
107aa8e712cSStephen Smalley 
108d621d35eSPaul Moore /* SECMARK reference count */
10956a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
110d621d35eSPaul Moore 
1111da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1126c5a682eSStephen Smalley static int selinux_enforcing_boot __initdata;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1151da177e4SLinus Torvalds {
116f5269710SEric Paris 	unsigned long enforcing;
11729707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
118aa8e712cSStephen Smalley 		selinux_enforcing_boot = enforcing ? 1 : 0;
1191da177e4SLinus Torvalds 	return 1;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
122aa8e712cSStephen Smalley #else
123aa8e712cSStephen Smalley #define selinux_enforcing_boot 1
1241da177e4SLinus Torvalds #endif
1251da177e4SLinus Torvalds 
1266c5a682eSStephen Smalley int selinux_enabled_boot __initdata = 1;
1271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1281da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1291da177e4SLinus Torvalds {
130f5269710SEric Paris 	unsigned long enabled;
13129707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
1326c5a682eSStephen Smalley 		selinux_enabled_boot = enabled ? 1 : 0;
1331da177e4SLinus Torvalds 	return 1;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
1361da177e4SLinus Torvalds #endif
1371da177e4SLinus Torvalds 
138aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot =
139aa8e712cSStephen Smalley 	CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
140aa8e712cSStephen Smalley 
141aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str)
142aa8e712cSStephen Smalley {
143aa8e712cSStephen Smalley 	unsigned long checkreqprot;
144aa8e712cSStephen Smalley 
145aa8e712cSStephen Smalley 	if (!kstrtoul(str, 0, &checkreqprot))
146aa8e712cSStephen Smalley 		selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
147aa8e712cSStephen Smalley 	return 1;
148aa8e712cSStephen Smalley }
149aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup);
150aa8e712cSStephen Smalley 
151d621d35eSPaul Moore /**
152d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
153d621d35eSPaul Moore  *
154d621d35eSPaul Moore  * Description:
155d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
156d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
157d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1582be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1592be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
160d621d35eSPaul Moore  *
161d621d35eSPaul Moore  */
162d621d35eSPaul Moore static int selinux_secmark_enabled(void)
163d621d35eSPaul Moore {
164aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
165aa8e712cSStephen Smalley 		atomic_read(&selinux_secmark_refcount));
1662be4d74fSChris PeBenito }
1672be4d74fSChris PeBenito 
1682be4d74fSChris PeBenito /**
1692be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1702be4d74fSChris PeBenito  *
1712be4d74fSChris PeBenito  * Description:
1722be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1732be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1742be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1752be4d74fSChris PeBenito  * is always considered enabled.
1762be4d74fSChris PeBenito  *
1772be4d74fSChris PeBenito  */
1782be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1792be4d74fSChris PeBenito {
180aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
181aa8e712cSStephen Smalley 		netlbl_enabled() || selinux_xfrm_enabled());
182d621d35eSPaul Moore }
183d621d35eSPaul Moore 
184615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
185615e51fdSPaul Moore {
186615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
187615e51fdSPaul Moore 		sel_netif_flush();
188615e51fdSPaul Moore 		sel_netnode_flush();
189615e51fdSPaul Moore 		sel_netport_flush();
190615e51fdSPaul Moore 		synchronize_net();
191615e51fdSPaul Moore 	}
192615e51fdSPaul Moore 	return 0;
193615e51fdSPaul Moore }
194615e51fdSPaul Moore 
1958f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event)
1968f408ab6SDaniel Jurgens {
197409dcf31SDaniel Jurgens 	if (event == AVC_CALLBACK_RESET) {
198409dcf31SDaniel Jurgens 		sel_ib_pkey_flush();
19942df744cSJanne Karhunen 		call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
200409dcf31SDaniel Jurgens 	}
2018f408ab6SDaniel Jurgens 
2028f408ab6SDaniel Jurgens 	return 0;
2038f408ab6SDaniel Jurgens }
2048f408ab6SDaniel Jurgens 
205d84f4f99SDavid Howells /*
206d84f4f99SDavid Howells  * initialise the security for the init task
207d84f4f99SDavid Howells  */
208d84f4f99SDavid Howells static void cred_init_security(void)
2091da177e4SLinus Torvalds {
2103b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
2111da177e4SLinus Torvalds 	struct task_security_struct *tsec;
2121da177e4SLinus Torvalds 
213bbd3662aSCasey Schaufler 	tsec = selinux_cred(cred);
214d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
217275bb41eSDavid Howells /*
21888e67f3bSDavid Howells  * get the security ID of a set of credentials
21988e67f3bSDavid Howells  */
22088e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
22188e67f3bSDavid Howells {
22288e67f3bSDavid Howells 	const struct task_security_struct *tsec;
22388e67f3bSDavid Howells 
2240c6cfa62SCasey Schaufler 	tsec = selinux_cred(cred);
22588e67f3bSDavid Howells 	return tsec->sid;
22688e67f3bSDavid Howells }
22788e67f3bSDavid Howells 
22888e67f3bSDavid Howells /*
2293b11a1deSDavid Howells  * get the objective security ID of a task
230275bb41eSDavid Howells  */
231275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
232275bb41eSDavid Howells {
233275bb41eSDavid Howells 	u32 sid;
234275bb41eSDavid Howells 
235275bb41eSDavid Howells 	rcu_read_lock();
23688e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
237275bb41eSDavid Howells 	rcu_read_unlock();
238275bb41eSDavid Howells 	return sid;
239275bb41eSDavid Howells }
240275bb41eSDavid Howells 
2415d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2425d226df4SAndreas Gruenbacher 
2435d226df4SAndreas Gruenbacher /*
2445d226df4SAndreas Gruenbacher  * Try reloading inode security labels that have been marked as invalid.  The
2455d226df4SAndreas Gruenbacher  * @may_sleep parameter indicates when sleeping and thus reloading labels is
24642059112SAndreas Gruenbacher  * allowed; when set to false, returns -ECHILD when the label is
247e9193288SAl Viro  * invalid.  The @dentry parameter should be set to a dentry of the inode.
2485d226df4SAndreas Gruenbacher  */
2495d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode,
250e9193288SAl Viro 				       struct dentry *dentry,
2515d226df4SAndreas Gruenbacher 				       bool may_sleep)
2525d226df4SAndreas Gruenbacher {
25380788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
2545d226df4SAndreas Gruenbacher 
2555d226df4SAndreas Gruenbacher 	might_sleep_if(may_sleep);
2565d226df4SAndreas Gruenbacher 
25765cddd50SOndrej Mosnacek 	if (selinux_initialized(&selinux_state) &&
258aa8e712cSStephen Smalley 	    isec->initialized != LABEL_INITIALIZED) {
2595d226df4SAndreas Gruenbacher 		if (!may_sleep)
2605d226df4SAndreas Gruenbacher 			return -ECHILD;
2615d226df4SAndreas Gruenbacher 
2625d226df4SAndreas Gruenbacher 		/*
2635d226df4SAndreas Gruenbacher 		 * Try reloading the inode security label.  This will fail if
2645d226df4SAndreas Gruenbacher 		 * @opt_dentry is NULL and no dentry for this inode can be
2655d226df4SAndreas Gruenbacher 		 * found; in that case, continue using the old label.
2665d226df4SAndreas Gruenbacher 		 */
267e9193288SAl Viro 		inode_doinit_with_dentry(inode, dentry);
2685d226df4SAndreas Gruenbacher 	}
2695d226df4SAndreas Gruenbacher 	return 0;
2705d226df4SAndreas Gruenbacher }
2715d226df4SAndreas Gruenbacher 
2725d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
2735d226df4SAndreas Gruenbacher {
27480788c22SCasey Schaufler 	return selinux_inode(inode);
2755d226df4SAndreas Gruenbacher }
2765d226df4SAndreas Gruenbacher 
2775d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
2785d226df4SAndreas Gruenbacher {
2795d226df4SAndreas Gruenbacher 	int error;
2805d226df4SAndreas Gruenbacher 
2815d226df4SAndreas Gruenbacher 	error = __inode_security_revalidate(inode, NULL, !rcu);
2825d226df4SAndreas Gruenbacher 	if (error)
2835d226df4SAndreas Gruenbacher 		return ERR_PTR(error);
28480788c22SCasey Schaufler 	return selinux_inode(inode);
2855d226df4SAndreas Gruenbacher }
2865d226df4SAndreas Gruenbacher 
28783da53c5SAndreas Gruenbacher /*
28883da53c5SAndreas Gruenbacher  * Get the security label of an inode.
28983da53c5SAndreas Gruenbacher  */
29083da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode)
29183da53c5SAndreas Gruenbacher {
2925d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, NULL, true);
29380788c22SCasey Schaufler 	return selinux_inode(inode);
29483da53c5SAndreas Gruenbacher }
29583da53c5SAndreas Gruenbacher 
2962c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
2972c97165bSPaul Moore {
2982c97165bSPaul Moore 	struct inode *inode = d_backing_inode(dentry);
2992c97165bSPaul Moore 
30080788c22SCasey Schaufler 	return selinux_inode(inode);
3012c97165bSPaul Moore }
3022c97165bSPaul Moore 
30383da53c5SAndreas Gruenbacher /*
30483da53c5SAndreas Gruenbacher  * Get the security label of a dentry's backing inode.
30583da53c5SAndreas Gruenbacher  */
30683da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
30783da53c5SAndreas Gruenbacher {
30883da53c5SAndreas Gruenbacher 	struct inode *inode = d_backing_inode(dentry);
30983da53c5SAndreas Gruenbacher 
3105d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
31180788c22SCasey Schaufler 	return selinux_inode(inode);
31283da53c5SAndreas Gruenbacher }
31383da53c5SAndreas Gruenbacher 
3141da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
3151da177e4SLinus Torvalds {
31680788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
317afb1cbe3SCasey Schaufler 	struct superblock_security_struct *sbsec;
3181da177e4SLinus Torvalds 
319afb1cbe3SCasey Schaufler 	if (!isec)
320afb1cbe3SCasey Schaufler 		return;
321afb1cbe3SCasey Schaufler 	sbsec = inode->i_sb->s_security;
3229629d04aSWaiman Long 	/*
3239629d04aSWaiman Long 	 * As not all inode security structures are in a list, we check for
3249629d04aSWaiman Long 	 * empty list outside of the lock to make sure that we won't waste
3259629d04aSWaiman Long 	 * time taking a lock doing nothing.
3269629d04aSWaiman Long 	 *
3279629d04aSWaiman Long 	 * The list_del_init() function can be safely called more than once.
3289629d04aSWaiman Long 	 * It should not be possible for this function to be called with
3299629d04aSWaiman Long 	 * concurrent list_add(), but for better safety against future changes
3309629d04aSWaiman Long 	 * in the code, we use list_empty_careful() here.
3319629d04aSWaiman Long 	 */
3329629d04aSWaiman Long 	if (!list_empty_careful(&isec->list)) {
3331da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
3341da177e4SLinus Torvalds 		list_del_init(&isec->list);
3351da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
3369629d04aSWaiman Long 	}
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3421da177e4SLinus Torvalds 	sb->s_security = NULL;
3431da177e4SLinus Torvalds 	kfree(sbsec);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
346bd323655SAl Viro struct selinux_mnt_opts {
347bd323655SAl Viro 	const char *fscontext, *context, *rootcontext, *defcontext;
348bd323655SAl Viro };
349bd323655SAl Viro 
350204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts)
351204cc0ccSAl Viro {
352bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
353bd323655SAl Viro 	kfree(opts->fscontext);
354bd323655SAl Viro 	kfree(opts->context);
355bd323655SAl Viro 	kfree(opts->rootcontext);
356bd323655SAl Viro 	kfree(opts->defcontext);
357204cc0ccSAl Viro 	kfree(opts);
358204cc0ccSAl Viro }
359204cc0ccSAl Viro 
3601da177e4SLinus Torvalds enum {
36131e87930SEric Paris 	Opt_error = -1,
362442155c1SDavid Howells 	Opt_context = 0,
363442155c1SDavid Howells 	Opt_defcontext = 1,
3641da177e4SLinus Torvalds 	Opt_fscontext = 2,
365442155c1SDavid Howells 	Opt_rootcontext = 3,
366442155c1SDavid Howells 	Opt_seclabel = 4,
3671da177e4SLinus Torvalds };
3681da177e4SLinus Torvalds 
369da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
370169d68efSAl Viro static struct {
371169d68efSAl Viro 	const char *name;
372169d68efSAl Viro 	int len;
373169d68efSAl Viro 	int opt;
374169d68efSAl Viro 	bool has_arg;
375169d68efSAl Viro } tokens[] = {
376da3d76abSAl Viro 	A(context, true),
377da3d76abSAl Viro 	A(fscontext, true),
378da3d76abSAl Viro 	A(defcontext, true),
379da3d76abSAl Viro 	A(rootcontext, true),
380da3d76abSAl Viro 	A(seclabel, false),
3811da177e4SLinus Torvalds };
382169d68efSAl Viro #undef A
383169d68efSAl Viro 
384169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg)
385169d68efSAl Viro {
386169d68efSAl Viro 	int i;
387169d68efSAl Viro 
388169d68efSAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
389169d68efSAl Viro 		size_t len = tokens[i].len;
390169d68efSAl Viro 		if (len > l || memcmp(s, tokens[i].name, len))
391169d68efSAl Viro 			continue;
392169d68efSAl Viro 		if (tokens[i].has_arg) {
393169d68efSAl Viro 			if (len == l || s[len] != '=')
394169d68efSAl Viro 				continue;
395169d68efSAl Viro 			*arg = s + len + 1;
396169d68efSAl Viro 		} else if (len != l)
397169d68efSAl Viro 			continue;
398169d68efSAl Viro 		return tokens[i].opt;
399169d68efSAl Viro 	}
400169d68efSAl Viro 	return Opt_error;
401169d68efSAl Viro }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4041da177e4SLinus Torvalds 
405c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
406c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
407275bb41eSDavid Howells 			const struct cred *cred)
408c312feb2SEric Paris {
4090c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
410c312feb2SEric Paris 	int rc;
411c312feb2SEric Paris 
4126b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4136b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
414c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
415c312feb2SEric Paris 	if (rc)
416c312feb2SEric Paris 		return rc;
417c312feb2SEric Paris 
4186b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4196b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
420c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
421c312feb2SEric Paris 	return rc;
422c312feb2SEric Paris }
423c312feb2SEric Paris 
4240808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4250808925eSEric Paris 			struct superblock_security_struct *sbsec,
426275bb41eSDavid Howells 			const struct cred *cred)
4270808925eSEric Paris {
4280c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(cred);
4290808925eSEric Paris 	int rc;
4306b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4316b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4320808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
4330808925eSEric Paris 	if (rc)
4340808925eSEric Paris 		return rc;
4350808925eSEric Paris 
4366b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4376b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
4380808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4390808925eSEric Paris 	return rc;
4400808925eSEric Paris }
4410808925eSEric Paris 
442a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb)
443b43e725dSEric Paris {
444d5f3a5f6SMark Salyzyn 	/* Special handling. Genfs but also in-core setxattr handler */
445a83d6ddaSOndrej Mosnacek 	return	!strcmp(sb->s_type->name, "sysfs") ||
446d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
447d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
448a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
4492651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
450aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
4512651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
4522651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
453b43e725dSEric Paris }
454b43e725dSEric Paris 
455a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb)
456a83d6ddaSOndrej Mosnacek {
457a83d6ddaSOndrej Mosnacek 	struct superblock_security_struct *sbsec = sb->s_security;
458a83d6ddaSOndrej Mosnacek 
459a83d6ddaSOndrej Mosnacek 	/*
460a83d6ddaSOndrej Mosnacek 	 * IMPORTANT: Double-check logic in this function when adding a new
461a83d6ddaSOndrej Mosnacek 	 * SECURITY_FS_USE_* definition!
462a83d6ddaSOndrej Mosnacek 	 */
463a83d6ddaSOndrej Mosnacek 	BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
464a83d6ddaSOndrej Mosnacek 
465a83d6ddaSOndrej Mosnacek 	switch (sbsec->behavior) {
466a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_XATTR:
467a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TRANS:
468a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_TASK:
469a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NATIVE:
470a83d6ddaSOndrej Mosnacek 		return 1;
471a83d6ddaSOndrej Mosnacek 
472a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_GENFS:
473a83d6ddaSOndrej Mosnacek 		return selinux_is_genfs_special_handling(sb);
474a83d6ddaSOndrej Mosnacek 
475a83d6ddaSOndrej Mosnacek 	/* Never allow relabeling on context mounts */
476a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_MNTPOINT:
477a83d6ddaSOndrej Mosnacek 	case SECURITY_FS_USE_NONE:
478a83d6ddaSOndrej Mosnacek 	default:
479a83d6ddaSOndrej Mosnacek 		return 0;
480a83d6ddaSOndrej Mosnacek 	}
481a83d6ddaSOndrej Mosnacek }
482a83d6ddaSOndrej Mosnacek 
483c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4861da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
487c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
4881da177e4SLinus Torvalds 	int rc = 0;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
4911da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
4921da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
4931da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
4941da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
4951da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
4965d6c3191SAndreas Gruenbacher 		if (!(root_inode->i_opflags & IOP_XATTR)) {
497c103a91eSpeter enderborg 			pr_warn("SELinux: (dev %s, type %s) has no "
49829b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
4991da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5001da177e4SLinus Torvalds 			goto out;
5011da177e4SLinus Torvalds 		}
5025d6c3191SAndreas Gruenbacher 
5035d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
5041da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5051da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
506c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
50729b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
50829b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5091da177e4SLinus Torvalds 			else
510c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
51129b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
51229b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
5131da177e4SLinus Torvalds 			goto out;
5141da177e4SLinus Torvalds 		}
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
517eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5180b4d3452SScott Mayhew 
5190b4d3452SScott Mayhew 	/*
5200b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5210b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5220b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5230b4d3452SScott Mayhew 	 */
524b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
52512f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5260b4d3452SScott Mayhew 	else
5270b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
528ddd29ec6SDavid P. Quigley 
5291da177e4SLinus Torvalds 	/* Initialize the root inode. */
530c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5331da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5341da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5351da177e4SLinus Torvalds 	   populates itself. */
5361da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5378d64124aSAl Viro 	while (!list_empty(&sbsec->isec_head)) {
5381da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5398d64124aSAl Viro 				list_first_entry(&sbsec->isec_head,
5401da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5411da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
542923190d3SStephen Smalley 		list_del_init(&isec->list);
5431da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5441da177e4SLinus Torvalds 		inode = igrab(inode);
5451da177e4SLinus Torvalds 		if (inode) {
5461da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
547*cb89e246SPaul Moore 				inode_doinit_with_dentry(inode, NULL);
5481da177e4SLinus Torvalds 			iput(inode);
5491da177e4SLinus Torvalds 		}
5501da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
5531da177e4SLinus Torvalds out:
554c9180a57SEric Paris 	return rc;
555c9180a57SEric Paris }
556c9180a57SEric Paris 
557c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
558c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
559c9180a57SEric Paris {
5600d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5610d90a7ecSDavid P. Quigley 
562c9180a57SEric Paris 	/* check if the old mount command had the same options */
5630d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
564c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
565c9180a57SEric Paris 		    (old_sid != new_sid))
566c9180a57SEric Paris 			return 1;
567c9180a57SEric Paris 
568c9180a57SEric Paris 	/* check if we were passed the same options twice,
569c9180a57SEric Paris 	 * aka someone passed context=a,context=b
570c9180a57SEric Paris 	 */
5710d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5720d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
573c9180a57SEric Paris 			return 1;
574c9180a57SEric Paris 	return 0;
575c9180a57SEric Paris }
576e0007529SEric Paris 
577bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
578bd323655SAl Viro {
579bd323655SAl Viro 	int rc = security_context_str_to_sid(&selinux_state, s,
580bd323655SAl Viro 					     sid, GFP_KERNEL);
581bd323655SAl Viro 	if (rc)
582bd323655SAl Viro 		pr_warn("SELinux: security_context_str_to_sid"
583bd323655SAl Viro 		       "(%s) failed for (dev %s, type %s) errno=%d\n",
584bd323655SAl Viro 		       s, sb->s_id, sb->s_type->name, rc);
585bd323655SAl Viro 	return rc;
586bd323655SAl Viro }
587bd323655SAl Viro 
588c9180a57SEric Paris /*
589c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
590c9180a57SEric Paris  * labeling information.
591c9180a57SEric Paris  */
592e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
593204cc0ccSAl Viro 				void *mnt_opts,
594649f6e77SDavid Quigley 				unsigned long kern_flags,
595649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
596c9180a57SEric Paris {
597275bb41eSDavid Howells 	const struct cred *cred = current_cred();
598c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
59983da53c5SAndreas Gruenbacher 	struct dentry *root = sbsec->sb->s_root;
600bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
6012c97165bSPaul Moore 	struct inode_security_struct *root_isec;
602c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
603c9180a57SEric Paris 	u32 defcontext_sid = 0;
604bd323655SAl Viro 	int rc = 0;
605c9180a57SEric Paris 
606c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
607c9180a57SEric Paris 
60865cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
609bd323655SAl Viro 		if (!opts) {
610c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
611c9180a57SEric Paris 			   after the initial policy is loaded and the security
612c9180a57SEric Paris 			   server is ready to handle calls. */
613c9180a57SEric Paris 			goto out;
614c9180a57SEric Paris 		}
615c9180a57SEric Paris 		rc = -EINVAL;
616c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
617744ba35eSEric Paris 			"before the security server is initialized\n");
618c9180a57SEric Paris 		goto out;
619c9180a57SEric Paris 	}
620649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
621649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
622649f6e77SDavid Quigley 		 * place the results is not allowed */
623649f6e77SDavid Quigley 		rc = -EINVAL;
624649f6e77SDavid Quigley 		goto out;
625649f6e77SDavid Quigley 	}
626c9180a57SEric Paris 
627c9180a57SEric Paris 	/*
628e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
629e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
630e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
631e0007529SEric Paris 	 * we need to skip the double mount verification.
632e0007529SEric Paris 	 *
633e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
634e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
635e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
636e0007529SEric Paris 	 * will be used for both mounts)
637e0007529SEric Paris 	 */
6380d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
639bd323655SAl Viro 	    && !opts)
640e0007529SEric Paris 		goto out;
641e0007529SEric Paris 
6422c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
6432c97165bSPaul Moore 
644e0007529SEric Paris 	/*
645c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
646c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
647c9180a57SEric Paris 	 * than once with different security options.
648c9180a57SEric Paris 	 */
649bd323655SAl Viro 	if (opts) {
650bd323655SAl Viro 		if (opts->fscontext) {
651bd323655SAl Viro 			rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
652bd323655SAl Viro 			if (rc)
653c9180a57SEric Paris 				goto out;
654c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
655c9180a57SEric Paris 					fscontext_sid))
656c9180a57SEric Paris 				goto out_double_mount;
657c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
658bd323655SAl Viro 		}
659bd323655SAl Viro 		if (opts->context) {
660bd323655SAl Viro 			rc = parse_sid(sb, opts->context, &context_sid);
661bd323655SAl Viro 			if (rc)
662bd323655SAl Viro 				goto out;
663c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
664c9180a57SEric Paris 					context_sid))
665c9180a57SEric Paris 				goto out_double_mount;
666c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
667bd323655SAl Viro 		}
668bd323655SAl Viro 		if (opts->rootcontext) {
669bd323655SAl Viro 			rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
670bd323655SAl Viro 			if (rc)
671bd323655SAl Viro 				goto out;
672c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
673c9180a57SEric Paris 					rootcontext_sid))
674c9180a57SEric Paris 				goto out_double_mount;
675c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
676bd323655SAl Viro 		}
677bd323655SAl Viro 		if (opts->defcontext) {
678bd323655SAl Viro 			rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
679bd323655SAl Viro 			if (rc)
680bd323655SAl Viro 				goto out;
681c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
682c9180a57SEric Paris 					defcontext_sid))
683c9180a57SEric Paris 				goto out_double_mount;
684c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
685c9180a57SEric Paris 		}
686c9180a57SEric Paris 	}
687c9180a57SEric Paris 
6880d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
689c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
690bd323655SAl Viro 		if ((sbsec->flags & SE_MNTMASK) && !opts)
691c9180a57SEric Paris 			goto out_double_mount;
692c9180a57SEric Paris 		rc = 0;
693c9180a57SEric Paris 		goto out;
694c9180a57SEric Paris 	}
695c9180a57SEric Paris 
696089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
697134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
698134509d5SStephen Smalley 
6998e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
7006a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
7017a4b5194SHridya Valsaraju 	    !strcmp(sb->s_type->name, "binderfs") ||
702b754026bSOndrej Mosnacek 	    !strcmp(sb->s_type->name, "pstore"))
703b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS;
704b754026bSOndrej Mosnacek 
705b754026bSOndrej Mosnacek 	if (!strcmp(sb->s_type->name, "sysfs") ||
706901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
707901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
708b754026bSOndrej Mosnacek 		sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
709c9180a57SEric Paris 
710eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
711eb9ae686SDavid Quigley 		/*
712eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
713eb9ae686SDavid Quigley 		 * filesystem type.
714eb9ae686SDavid Quigley 		 */
715aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
716c9180a57SEric Paris 		if (rc) {
717c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
718089be43eSJames Morris 					__func__, sb->s_type->name, rc);
719c9180a57SEric Paris 			goto out;
720c9180a57SEric Paris 		}
721eb9ae686SDavid Quigley 	}
722aad82892SSeth Forshee 
723aad82892SSeth Forshee 	/*
72401593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
72501593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
72601593d32SStephen Smalley 	 * line and security labels must be ignored.
727aad82892SSeth Forshee 	 */
72801593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
72901593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
73001593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
73101593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
732aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
733aad82892SSeth Forshee 		    defcontext_sid) {
734aad82892SSeth Forshee 			rc = -EACCES;
735aad82892SSeth Forshee 			goto out;
736aad82892SSeth Forshee 		}
737aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
738aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
739aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
740aa8e712cSStephen Smalley 						     current_sid(),
741aa8e712cSStephen Smalley 						     current_sid(),
742aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
743aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
744aad82892SSeth Forshee 			if (rc)
745aad82892SSeth Forshee 				goto out;
746aad82892SSeth Forshee 		}
747aad82892SSeth Forshee 		goto out_set_opts;
748aad82892SSeth Forshee 	}
749aad82892SSeth Forshee 
750c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
751c9180a57SEric Paris 	if (fscontext_sid) {
752275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
753c9180a57SEric Paris 		if (rc)
754c9180a57SEric Paris 			goto out;
755c9180a57SEric Paris 
756c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
757c9180a57SEric Paris 	}
758c9180a57SEric Paris 
759c9180a57SEric Paris 	/*
760c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
761c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
762c9180a57SEric Paris 	 * the superblock context if not already set.
763c9180a57SEric Paris 	 */
764eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
765eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
766eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
767eb9ae686SDavid Quigley 	}
768eb9ae686SDavid Quigley 
769c9180a57SEric Paris 	if (context_sid) {
770c9180a57SEric Paris 		if (!fscontext_sid) {
771275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
772275bb41eSDavid Howells 							  cred);
773c9180a57SEric Paris 			if (rc)
774c9180a57SEric Paris 				goto out;
775c9180a57SEric Paris 			sbsec->sid = context_sid;
776c9180a57SEric Paris 		} else {
777275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
778275bb41eSDavid Howells 							     cred);
779c9180a57SEric Paris 			if (rc)
780c9180a57SEric Paris 				goto out;
781c9180a57SEric Paris 		}
782c9180a57SEric Paris 		if (!rootcontext_sid)
783c9180a57SEric Paris 			rootcontext_sid = context_sid;
784c9180a57SEric Paris 
785c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
786c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
787c9180a57SEric Paris 	}
788c9180a57SEric Paris 
789c9180a57SEric Paris 	if (rootcontext_sid) {
790275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
791275bb41eSDavid Howells 						     cred);
792c9180a57SEric Paris 		if (rc)
793c9180a57SEric Paris 			goto out;
794c9180a57SEric Paris 
795c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
7966f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
797c9180a57SEric Paris 	}
798c9180a57SEric Paris 
799c9180a57SEric Paris 	if (defcontext_sid) {
800eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
801eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
802c9180a57SEric Paris 			rc = -EINVAL;
803c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
804c9180a57SEric Paris 			       "invalid for this filesystem type\n");
805c9180a57SEric Paris 			goto out;
806c9180a57SEric Paris 		}
807c9180a57SEric Paris 
808c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
809c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
810275bb41eSDavid Howells 							     sbsec, cred);
811c9180a57SEric Paris 			if (rc)
812c9180a57SEric Paris 				goto out;
813c9180a57SEric Paris 		}
814c9180a57SEric Paris 
815c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
816c9180a57SEric Paris 	}
817c9180a57SEric Paris 
818aad82892SSeth Forshee out_set_opts:
819c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
820c9180a57SEric Paris out:
821bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8221da177e4SLinus Torvalds 	return rc;
823c9180a57SEric Paris out_double_mount:
824c9180a57SEric Paris 	rc = -EINVAL;
825c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
826bd323655SAl Viro 	       "security settings for (dev %s, type %s)\n", sb->s_id,
827bd323655SAl Viro 	       sb->s_type->name);
828c9180a57SEric Paris 	goto out;
829c9180a57SEric Paris }
830c9180a57SEric Paris 
831094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
832094f7b69SJeff Layton 				    const struct super_block *newsb)
833094f7b69SJeff Layton {
834094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
835094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
836094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
837094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
838094f7b69SJeff Layton 
839094f7b69SJeff Layton 	if (oldflags != newflags)
840094f7b69SJeff Layton 		goto mismatch;
841094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
842094f7b69SJeff Layton 		goto mismatch;
843094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
844094f7b69SJeff Layton 		goto mismatch;
845094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
846094f7b69SJeff Layton 		goto mismatch;
847094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
84883da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
84983da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
850094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
851094f7b69SJeff Layton 			goto mismatch;
852094f7b69SJeff Layton 	}
853094f7b69SJeff Layton 	return 0;
854094f7b69SJeff Layton mismatch:
855c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
856094f7b69SJeff Layton 			    "different security settings for (dev %s, "
857094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
858094f7b69SJeff Layton 	return -EBUSY;
859094f7b69SJeff Layton }
860094f7b69SJeff Layton 
861094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
8620b4d3452SScott Mayhew 					struct super_block *newsb,
8630b4d3452SScott Mayhew 					unsigned long kern_flags,
8640b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
865c9180a57SEric Paris {
8660b4d3452SScott Mayhew 	int rc = 0;
867c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
868c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
869c9180a57SEric Paris 
870c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
871c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
872c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
873c9180a57SEric Paris 
8740f5e6420SEric Paris 	/*
8750f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
876e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
8770f5e6420SEric Paris 	 */
87865cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
879094f7b69SJeff Layton 		return 0;
880c9180a57SEric Paris 
8810b4d3452SScott Mayhew 	/*
8820b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
8830b4d3452SScott Mayhew 	 * place the results is not allowed.
8840b4d3452SScott Mayhew 	 */
8850b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
8860b4d3452SScott Mayhew 		return -EINVAL;
8870b4d3452SScott Mayhew 
888c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8890d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
890c9180a57SEric Paris 
891094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
8923815a245SJ. Bruce Fields 	if (newsbsec->flags & SE_SBINITIALIZED) {
8933815a245SJ. Bruce Fields 		if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
8943815a245SJ. Bruce Fields 			*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
895094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
8963815a245SJ. Bruce Fields 	}
8975a552617SEric Paris 
898c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
899c9180a57SEric Paris 
900c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
901c9180a57SEric Paris 
902c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
903c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
904c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
905c9180a57SEric Paris 
9060b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
9070b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
908aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
9090b4d3452SScott Mayhew 		if (rc)
9100b4d3452SScott Mayhew 			goto out;
9110b4d3452SScott Mayhew 	}
9120b4d3452SScott Mayhew 
9130b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
9140b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
9150b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
9160b4d3452SScott Mayhew 	}
9170b4d3452SScott Mayhew 
918c9180a57SEric Paris 	if (set_context) {
919c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
920c9180a57SEric Paris 
921c9180a57SEric Paris 		if (!set_fscontext)
922c9180a57SEric Paris 			newsbsec->sid = sid;
923c9180a57SEric Paris 		if (!set_rootcontext) {
92483da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
925c9180a57SEric Paris 			newisec->sid = sid;
926c9180a57SEric Paris 		}
927c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
928c9180a57SEric Paris 	}
929c9180a57SEric Paris 	if (set_rootcontext) {
93083da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
93183da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
932c9180a57SEric Paris 
933c9180a57SEric Paris 		newisec->sid = oldisec->sid;
934c9180a57SEric Paris 	}
935c9180a57SEric Paris 
936c9180a57SEric Paris 	sb_finish_set_opts(newsb);
9370b4d3452SScott Mayhew out:
938c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
9390b4d3452SScott Mayhew 	return rc;
940c9180a57SEric Paris }
941c9180a57SEric Paris 
942ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts)
943c9180a57SEric Paris {
944ba641862SAl Viro 	struct selinux_mnt_opts *opts = *mnt_opts;
945c9180a57SEric Paris 
946da3d76abSAl Viro 	if (token == Opt_seclabel)	/* eaten and completely ignored */
947e0007529SEric Paris 		return 0;
948e0007529SEric Paris 
949ba641862SAl Viro 	if (!opts) {
950ba641862SAl Viro 		opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
951ba641862SAl Viro 		if (!opts)
952ba641862SAl Viro 			return -ENOMEM;
953ba641862SAl Viro 		*mnt_opts = opts;
954ba641862SAl Viro 	}
955ba641862SAl Viro 	if (!s)
956ba641862SAl Viro 		return -ENOMEM;
957ba641862SAl Viro 	switch (token) {
958ba641862SAl Viro 	case Opt_context:
959ba641862SAl Viro 		if (opts->context || opts->defcontext)
960ba641862SAl Viro 			goto Einval;
961ba641862SAl Viro 		opts->context = s;
962ba641862SAl Viro 		break;
963ba641862SAl Viro 	case Opt_fscontext:
964ba641862SAl Viro 		if (opts->fscontext)
965ba641862SAl Viro 			goto Einval;
966ba641862SAl Viro 		opts->fscontext = s;
967ba641862SAl Viro 		break;
968ba641862SAl Viro 	case Opt_rootcontext:
969ba641862SAl Viro 		if (opts->rootcontext)
970ba641862SAl Viro 			goto Einval;
971ba641862SAl Viro 		opts->rootcontext = s;
972ba641862SAl Viro 		break;
973ba641862SAl Viro 	case Opt_defcontext:
974ba641862SAl Viro 		if (opts->context || opts->defcontext)
975ba641862SAl Viro 			goto Einval;
976ba641862SAl Viro 		opts->defcontext = s;
977ba641862SAl Viro 		break;
978ba641862SAl Viro 	}
979ba641862SAl Viro 	return 0;
980ba641862SAl Viro Einval:
981ba641862SAl Viro 	pr_warn(SEL_MOUNT_FAIL_MSG);
982ba641862SAl Viro 	return -EINVAL;
983ba641862SAl Viro }
984ba641862SAl Viro 
985757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len,
986204cc0ccSAl Viro 			       void **mnt_opts)
987c9180a57SEric Paris {
988757cbe59SAl Viro 	int token = Opt_error;
989757cbe59SAl Viro 	int rc, i;
990c9180a57SEric Paris 
991757cbe59SAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
992757cbe59SAl Viro 		if (strcmp(option, tokens[i].name) == 0) {
993757cbe59SAl Viro 			token = tokens[i].opt;
994757cbe59SAl Viro 			break;
995757cbe59SAl Viro 		}
996169d68efSAl Viro 	}
997169d68efSAl Viro 
998757cbe59SAl Viro 	if (token == Opt_error)
999757cbe59SAl Viro 		return -EINVAL;
1000c9180a57SEric Paris 
1001e2e0e097SGen Zhang 	if (token != Opt_seclabel) {
1002757cbe59SAl Viro 		val = kmemdup_nul(val, len, GFP_KERNEL);
1003e2e0e097SGen Zhang 		if (!val) {
1004e2e0e097SGen Zhang 			rc = -ENOMEM;
1005e2e0e097SGen Zhang 			goto free_opt;
1006e2e0e097SGen Zhang 		}
1007e2e0e097SGen Zhang 	}
1008757cbe59SAl Viro 	rc = selinux_add_opt(token, val, mnt_opts);
1009757cbe59SAl Viro 	if (unlikely(rc)) {
1010757cbe59SAl Viro 		kfree(val);
1011e2e0e097SGen Zhang 		goto free_opt;
1012e2e0e097SGen Zhang 	}
1013e2e0e097SGen Zhang 	return rc;
1014e2e0e097SGen Zhang 
1015e2e0e097SGen Zhang free_opt:
1016757cbe59SAl Viro 	if (*mnt_opts) {
1017ba641862SAl Viro 		selinux_free_mnt_opts(*mnt_opts);
1018ba641862SAl Viro 		*mnt_opts = NULL;
1019757cbe59SAl Viro 	}
1020c9180a57SEric Paris 	return rc;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
1023e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid)
10242069f457SEric Paris {
1025e3489f89SAl Viro 	char *context = NULL;
1026e3489f89SAl Viro 	u32 len;
1027e3489f89SAl Viro 	int rc;
10282069f457SEric Paris 
1029e3489f89SAl Viro 	rc = security_sid_to_context(&selinux_state, sid,
1030e3489f89SAl Viro 					     &context, &len);
1031e3489f89SAl Viro 	if (!rc) {
1032e3489f89SAl Viro 		bool has_comma = context && strchr(context, ',');
103311689d47SDavid P. Quigley 
1034442155c1SDavid Howells 		seq_putc(m, '=');
10352069f457SEric Paris 		if (has_comma)
10362069f457SEric Paris 			seq_putc(m, '\"');
1037e3489f89SAl Viro 		seq_escape(m, context, "\"\n\\");
10382069f457SEric Paris 		if (has_comma)
10392069f457SEric Paris 			seq_putc(m, '\"');
10402069f457SEric Paris 	}
10411da177e4SLinus Torvalds 	kfree(context);
10421da177e4SLinus Torvalds 	return rc;
10431da177e4SLinus Torvalds }
10442069f457SEric Paris 
10452069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10462069f457SEric Paris {
1047e3489f89SAl Viro 	struct superblock_security_struct *sbsec = sb->s_security;
10482069f457SEric Paris 	int rc;
10492069f457SEric Paris 
1050e3489f89SAl Viro 	if (!(sbsec->flags & SE_SBINITIALIZED))
1051e3489f89SAl Viro 		return 0;
1052e3489f89SAl Viro 
105365cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
1054e3489f89SAl Viro 		return 0;
1055e3489f89SAl Viro 
1056e3489f89SAl Viro 	if (sbsec->flags & FSCONTEXT_MNT) {
1057e3489f89SAl Viro 		seq_putc(m, ',');
1058e3489f89SAl Viro 		seq_puts(m, FSCONTEXT_STR);
1059e3489f89SAl Viro 		rc = show_sid(m, sbsec->sid);
1060e3489f89SAl Viro 		if (rc)
10612069f457SEric Paris 			return rc;
1062383795c2SEric Paris 	}
1063e3489f89SAl Viro 	if (sbsec->flags & CONTEXT_MNT) {
1064e3489f89SAl Viro 		seq_putc(m, ',');
1065e3489f89SAl Viro 		seq_puts(m, CONTEXT_STR);
1066e3489f89SAl Viro 		rc = show_sid(m, sbsec->mntpoint_sid);
1067e3489f89SAl Viro 		if (rc)
10682069f457SEric Paris 			return rc;
10692069f457SEric Paris 	}
1070e3489f89SAl Viro 	if (sbsec->flags & DEFCONTEXT_MNT) {
1071e3489f89SAl Viro 		seq_putc(m, ',');
1072e3489f89SAl Viro 		seq_puts(m, DEFCONTEXT_STR);
1073e3489f89SAl Viro 		rc = show_sid(m, sbsec->def_sid);
1074e3489f89SAl Viro 		if (rc)
1075e3489f89SAl Viro 			return rc;
1076e3489f89SAl Viro 	}
1077e3489f89SAl Viro 	if (sbsec->flags & ROOTCONTEXT_MNT) {
1078e3489f89SAl Viro 		struct dentry *root = sbsec->sb->s_root;
1079e3489f89SAl Viro 		struct inode_security_struct *isec = backing_inode_security(root);
1080e3489f89SAl Viro 		seq_putc(m, ',');
1081e3489f89SAl Viro 		seq_puts(m, ROOTCONTEXT_STR);
1082e3489f89SAl Viro 		rc = show_sid(m, isec->sid);
1083e3489f89SAl Viro 		if (rc)
1084e3489f89SAl Viro 			return rc;
1085e3489f89SAl Viro 	}
1086e3489f89SAl Viro 	if (sbsec->flags & SBLABEL_MNT) {
1087e3489f89SAl Viro 		seq_putc(m, ',');
1088442155c1SDavid Howells 		seq_puts(m, SECLABEL_STR);
1089e3489f89SAl Viro 	}
1090e3489f89SAl Viro 	return 0;
1091e3489f89SAl Viro }
10922069f457SEric Paris 
10931da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
10961da177e4SLinus Torvalds 	case S_IFSOCK:
10971da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
10981da177e4SLinus Torvalds 	case S_IFLNK:
10991da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11001da177e4SLinus Torvalds 	case S_IFREG:
11011da177e4SLinus Torvalds 		return SECCLASS_FILE;
11021da177e4SLinus Torvalds 	case S_IFBLK:
11031da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11041da177e4SLinus Torvalds 	case S_IFDIR:
11051da177e4SLinus Torvalds 		return SECCLASS_DIR;
11061da177e4SLinus Torvalds 	case S_IFCHR:
11071da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11081da177e4SLinus Torvalds 	case S_IFIFO:
11091da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	return SECCLASS_FILE;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
111613402580SJames Morris static inline int default_protocol_stream(int protocol)
111713402580SJames Morris {
111813402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
111913402580SJames Morris }
112013402580SJames Morris 
112113402580SJames Morris static inline int default_protocol_dgram(int protocol)
112213402580SJames Morris {
112313402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
112413402580SJames Morris }
112513402580SJames Morris 
11261da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11271da177e4SLinus Torvalds {
1128aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1129da69a530SStephen Smalley 
11301da177e4SLinus Torvalds 	switch (family) {
11311da177e4SLinus Torvalds 	case PF_UNIX:
11321da177e4SLinus Torvalds 		switch (type) {
11331da177e4SLinus Torvalds 		case SOCK_STREAM:
11341da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11351da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11361da177e4SLinus Torvalds 		case SOCK_DGRAM:
11372a764b52SLuis Ressel 		case SOCK_RAW:
11381da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		break;
11411da177e4SLinus Torvalds 	case PF_INET:
11421da177e4SLinus Torvalds 	case PF_INET6:
11431da177e4SLinus Torvalds 		switch (type) {
11441da177e4SLinus Torvalds 		case SOCK_STREAM:
1145da69a530SStephen Smalley 		case SOCK_SEQPACKET:
114613402580SJames Morris 			if (default_protocol_stream(protocol))
11471da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1148da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1149da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
115013402580SJames Morris 			else
115113402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11521da177e4SLinus Torvalds 		case SOCK_DGRAM:
115313402580SJames Morris 			if (default_protocol_dgram(protocol))
11541da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1155ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1156ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1157da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
115813402580SJames Morris 			else
115913402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11602ee92d46SJames Morris 		case SOCK_DCCP:
11612ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
116213402580SJames Morris 		default:
11631da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11641da177e4SLinus Torvalds 		}
11651da177e4SLinus Torvalds 		break;
11661da177e4SLinus Torvalds 	case PF_NETLINK:
11671da177e4SLinus Torvalds 		switch (protocol) {
11681da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11691da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11707f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11711da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11721da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11731da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11741da177e4SLinus Torvalds 		case NETLINK_XFRM:
11751da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11761da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11771da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11786c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
11796c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
11801da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11811da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11826c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
11836c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
11846c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
11856c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
11866c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
11876c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
11881da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
11891da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
11900c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
11910c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
11926c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
11936c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
11946c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
11956c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
11966c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
11976c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
11986c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
11996c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
12001da177e4SLinus Torvalds 		default:
12011da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 	case PF_PACKET:
12041da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12051da177e4SLinus Torvalds 	case PF_KEY:
12061da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12073e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12083e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
1211da69a530SStephen Smalley 	if (extsockclass) {
1212da69a530SStephen Smalley 		switch (family) {
1213da69a530SStephen Smalley 		case PF_AX25:
1214da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1215da69a530SStephen Smalley 		case PF_IPX:
1216da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1217da69a530SStephen Smalley 		case PF_NETROM:
1218da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1219da69a530SStephen Smalley 		case PF_ATMPVC:
1220da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1221da69a530SStephen Smalley 		case PF_X25:
1222da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1223da69a530SStephen Smalley 		case PF_ROSE:
1224da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1225da69a530SStephen Smalley 		case PF_DECnet:
1226da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1227da69a530SStephen Smalley 		case PF_ATMSVC:
1228da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1229da69a530SStephen Smalley 		case PF_RDS:
1230da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1231da69a530SStephen Smalley 		case PF_IRDA:
1232da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1233da69a530SStephen Smalley 		case PF_PPPOX:
1234da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1235da69a530SStephen Smalley 		case PF_LLC:
1236da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1237da69a530SStephen Smalley 		case PF_CAN:
1238da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1239da69a530SStephen Smalley 		case PF_TIPC:
1240da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1241da69a530SStephen Smalley 		case PF_BLUETOOTH:
1242da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1243da69a530SStephen Smalley 		case PF_IUCV:
1244da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1245da69a530SStephen Smalley 		case PF_RXRPC:
1246da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1247da69a530SStephen Smalley 		case PF_ISDN:
1248da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1249da69a530SStephen Smalley 		case PF_PHONET:
1250da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1251da69a530SStephen Smalley 		case PF_IEEE802154:
1252da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1253da69a530SStephen Smalley 		case PF_CAIF:
1254da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1255da69a530SStephen Smalley 		case PF_ALG:
1256da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1257da69a530SStephen Smalley 		case PF_NFC:
1258da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1259da69a530SStephen Smalley 		case PF_VSOCK:
1260da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1261da69a530SStephen Smalley 		case PF_KCM:
1262da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1263da69a530SStephen Smalley 		case PF_QIPCRTR:
1264da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
12653051bf36SLinus Torvalds 		case PF_SMC:
12663051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
126768e8b849SBjörn Töpel 		case PF_XDP:
126868e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
126968e8b849SBjörn Töpel #if PF_MAX > 45
1270da69a530SStephen Smalley #error New address family defined, please update this function.
1271da69a530SStephen Smalley #endif
1272da69a530SStephen Smalley 		}
1273da69a530SStephen Smalley 	}
1274da69a530SStephen Smalley 
12751da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
1278134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
12791da177e4SLinus Torvalds 				 u16 tclass,
1280134509d5SStephen Smalley 				 u16 flags,
12811da177e4SLinus Torvalds 				 u32 *sid)
12821da177e4SLinus Torvalds {
12838e6c9693SLucian Adrian Grijincu 	int rc;
1284fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
12858e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
12881da177e4SLinus Torvalds 	if (!buffer)
12891da177e4SLinus Torvalds 		return -ENOMEM;
12901da177e4SLinus Torvalds 
12918e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
12928e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
12938e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
12948e6c9693SLucian Adrian Grijincu 	else {
1295134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
12968e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
12978e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
12988e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
12998e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
13008e6c9693SLucian Adrian Grijincu 				path[1] = '/';
13018e6c9693SLucian Adrian Grijincu 				path++;
13021da177e4SLinus Torvalds 			}
1303134509d5SStephen Smalley 		}
1304aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1305aa8e712cSStephen Smalley 					path, tclass, sid);
13067bb185edSStephen Smalley 		if (rc == -ENOENT) {
13077bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
13087bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
13097bb185edSStephen Smalley 			rc = 0;
13107bb185edSStephen Smalley 		}
13118e6c9693SLucian Adrian Grijincu 	}
13121da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
13131da177e4SLinus Torvalds 	return rc;
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
1316b754026bSOndrej Mosnacek static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1317b754026bSOndrej Mosnacek 				  u32 def_sid, u32 *sid)
1318b754026bSOndrej Mosnacek {
1319b754026bSOndrej Mosnacek #define INITCONTEXTLEN 255
1320b754026bSOndrej Mosnacek 	char *context;
1321b754026bSOndrej Mosnacek 	unsigned int len;
1322b754026bSOndrej Mosnacek 	int rc;
1323b754026bSOndrej Mosnacek 
1324b754026bSOndrej Mosnacek 	len = INITCONTEXTLEN;
1325b754026bSOndrej Mosnacek 	context = kmalloc(len + 1, GFP_NOFS);
1326b754026bSOndrej Mosnacek 	if (!context)
1327b754026bSOndrej Mosnacek 		return -ENOMEM;
1328b754026bSOndrej Mosnacek 
1329b754026bSOndrej Mosnacek 	context[len] = '\0';
1330b754026bSOndrej Mosnacek 	rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1331b754026bSOndrej Mosnacek 	if (rc == -ERANGE) {
1332b754026bSOndrej Mosnacek 		kfree(context);
1333b754026bSOndrej Mosnacek 
1334b754026bSOndrej Mosnacek 		/* Need a larger buffer.  Query for the right size. */
1335b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1336b754026bSOndrej Mosnacek 		if (rc < 0)
1337b754026bSOndrej Mosnacek 			return rc;
1338b754026bSOndrej Mosnacek 
1339b754026bSOndrej Mosnacek 		len = rc;
1340b754026bSOndrej Mosnacek 		context = kmalloc(len + 1, GFP_NOFS);
1341b754026bSOndrej Mosnacek 		if (!context)
1342b754026bSOndrej Mosnacek 			return -ENOMEM;
1343b754026bSOndrej Mosnacek 
1344b754026bSOndrej Mosnacek 		context[len] = '\0';
1345b754026bSOndrej Mosnacek 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1346b754026bSOndrej Mosnacek 				    context, len);
1347b754026bSOndrej Mosnacek 	}
1348b754026bSOndrej Mosnacek 	if (rc < 0) {
1349b754026bSOndrej Mosnacek 		kfree(context);
1350b754026bSOndrej Mosnacek 		if (rc != -ENODATA) {
1351b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  getxattr returned %d for dev=%s ino=%ld\n",
1352b754026bSOndrej Mosnacek 				__func__, -rc, inode->i_sb->s_id, inode->i_ino);
1353b754026bSOndrej Mosnacek 			return rc;
1354b754026bSOndrej Mosnacek 		}
1355b754026bSOndrej Mosnacek 		*sid = def_sid;
1356b754026bSOndrej Mosnacek 		return 0;
1357b754026bSOndrej Mosnacek 	}
1358b754026bSOndrej Mosnacek 
1359b754026bSOndrej Mosnacek 	rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1360b754026bSOndrej Mosnacek 					     def_sid, GFP_NOFS);
1361b754026bSOndrej Mosnacek 	if (rc) {
1362b754026bSOndrej Mosnacek 		char *dev = inode->i_sb->s_id;
1363b754026bSOndrej Mosnacek 		unsigned long ino = inode->i_ino;
1364b754026bSOndrej Mosnacek 
1365b754026bSOndrej Mosnacek 		if (rc == -EINVAL) {
1366b754026bSOndrej 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",
1367b754026bSOndrej Mosnacek 					      ino, dev, context);
1368b754026bSOndrej Mosnacek 		} else {
1369b754026bSOndrej Mosnacek 			pr_warn("SELinux: %s:  context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1370b754026bSOndrej Mosnacek 				__func__, context, -rc, dev, ino);
1371b754026bSOndrej Mosnacek 		}
1372b754026bSOndrej Mosnacek 	}
1373b754026bSOndrej Mosnacek 	kfree(context);
1374b754026bSOndrej Mosnacek 	return 0;
1375b754026bSOndrej Mosnacek }
1376b754026bSOndrej Mosnacek 
13771da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
13781da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
13791da177e4SLinus Torvalds {
13801da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
138180788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
13829287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
13839287aed2SAndreas Gruenbacher 	u16 sclass;
13841da177e4SLinus Torvalds 	struct dentry *dentry;
13851da177e4SLinus Torvalds 	int rc = 0;
13861da177e4SLinus Torvalds 
13876f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
138813457d07SAndreas Gruenbacher 		return 0;
13891da177e4SLinus Torvalds 
13909287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
13916f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
139223970741SEric Paris 		goto out_unlock;
13931da177e4SLinus Torvalds 
139413457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
139513457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
139613457d07SAndreas Gruenbacher 
13971da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
13980d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
13991da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
14001da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
14011da177e4SLinus Torvalds 		   server is ready to handle calls. */
14021da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
14031da177e4SLinus Torvalds 		if (list_empty(&isec->list))
14041da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
14051da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
140623970741SEric Paris 		goto out_unlock;
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds 
14099287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
14109287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
14119287aed2SAndreas Gruenbacher 	sid = isec->sid;
14129287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
14139287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
14149287aed2SAndreas Gruenbacher 
14151da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1416eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1417eb9ae686SDavid Quigley 		break;
14181da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
14195d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
14209287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
14211da177e4SLinus Torvalds 			break;
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
14241da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
14251da177e4SLinus Torvalds 		if (opt_dentry) {
14261da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
14271da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
14281da177e4SLinus Torvalds 		} else {
1429b127125dSAl Viro 			/*
1430b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1431b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1432b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1433b127125dSAl Viro 			 * two, depending upon that...
1434b127125dSAl Viro 			 */
14351da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1436b127125dSAl Viro 			if (!dentry)
1437b127125dSAl Viro 				dentry = d_find_any_alias(inode);
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 		if (!dentry) {
1440df7f54c0SEric Paris 			/*
1441df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1442df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1443df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1444df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1445df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1446df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1447df7f54c0SEric Paris 			 * be used again by userspace.
1448df7f54c0SEric Paris 			 */
14499287aed2SAndreas Gruenbacher 			goto out;
14501da177e4SLinus Torvalds 		}
14511da177e4SLinus Torvalds 
1452b754026bSOndrej Mosnacek 		rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1453b754026bSOndrej Mosnacek 					    &sid);
14541da177e4SLinus Torvalds 		dput(dentry);
1455b754026bSOndrej Mosnacek 		if (rc)
14569287aed2SAndreas Gruenbacher 			goto out;
14571da177e4SLinus Torvalds 		break;
14581da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
14599287aed2SAndreas Gruenbacher 		sid = task_sid;
14601da177e4SLinus Torvalds 		break;
14611da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
14621da177e4SLinus Torvalds 		/* Default to the fs SID. */
14639287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1466aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1467aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
14681da177e4SLinus Torvalds 		if (rc)
14699287aed2SAndreas Gruenbacher 			goto out;
14701da177e4SLinus Torvalds 		break;
1471c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
14729287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1473c312feb2SEric Paris 		break;
14741da177e4SLinus Torvalds 	default:
1475c312feb2SEric Paris 		/* Default to the fs superblock SID. */
14769287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
14771da177e4SLinus Torvalds 
1478134509d5SStephen Smalley 		if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1479f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1480f64410ecSPaul Moore 			 * procfs inodes */
1481b127125dSAl Viro 			if (opt_dentry) {
1482f64410ecSPaul Moore 				/* Called from d_instantiate or
1483f64410ecSPaul Moore 				 * d_splice_alias. */
1484f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1485b127125dSAl Viro 			} else {
1486f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1487b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1488b127125dSAl Viro 				 * a connected one, so try that first.
1489b127125dSAl Viro 				 */
1490f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1491b127125dSAl Viro 				if (!dentry)
1492b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1493b127125dSAl Viro 			}
1494f64410ecSPaul Moore 			/*
1495f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1496f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1497f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1498f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1499f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1500f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1501f64410ecSPaul Moore 			 * could be used again by userspace.
1502f64410ecSPaul Moore 			 */
1503f64410ecSPaul Moore 			if (!dentry)
15049287aed2SAndreas Gruenbacher 				goto out;
15059287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1506134509d5SStephen Smalley 						   sbsec->flags, &sid);
1507b754026bSOndrej Mosnacek 			if (rc) {
1508f64410ecSPaul Moore 				dput(dentry);
15099287aed2SAndreas Gruenbacher 				goto out;
15101da177e4SLinus Torvalds 			}
1511b754026bSOndrej Mosnacek 
1512b754026bSOndrej Mosnacek 			if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1513b754026bSOndrej Mosnacek 			    (inode->i_opflags & IOP_XATTR)) {
1514b754026bSOndrej Mosnacek 				rc = inode_doinit_use_xattr(inode, dentry,
1515b754026bSOndrej Mosnacek 							    sid, &sid);
1516b754026bSOndrej Mosnacek 				if (rc) {
1517b754026bSOndrej Mosnacek 					dput(dentry);
1518b754026bSOndrej Mosnacek 					goto out;
1519b754026bSOndrej Mosnacek 				}
1520b754026bSOndrej Mosnacek 			}
1521b754026bSOndrej Mosnacek 			dput(dentry);
1522b754026bSOndrej Mosnacek 		}
15231da177e4SLinus Torvalds 		break;
15241da177e4SLinus Torvalds 	}
15251da177e4SLinus Torvalds 
15269287aed2SAndreas Gruenbacher out:
15279287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15289287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
15299287aed2SAndreas Gruenbacher 		if (!sid || rc) {
15309287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
15319287aed2SAndreas Gruenbacher 			goto out_unlock;
15329287aed2SAndreas Gruenbacher 		}
15339287aed2SAndreas Gruenbacher 
15346f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
15359287aed2SAndreas Gruenbacher 		isec->sid = sid;
15369287aed2SAndreas Gruenbacher 	}
15371da177e4SLinus Torvalds 
153823970741SEric Paris out_unlock:
15399287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15401da177e4SLinus Torvalds 	return rc;
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
15441da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	u32 perm = 0;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	switch (sig) {
15491da177e4SLinus Torvalds 	case SIGCHLD:
15501da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
15511da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
15521da177e4SLinus Torvalds 		break;
15531da177e4SLinus Torvalds 	case SIGKILL:
15541da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
15551da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
15561da177e4SLinus Torvalds 		break;
15571da177e4SLinus Torvalds 	case SIGSTOP:
15581da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
15591da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
15601da177e4SLinus Torvalds 		break;
15611da177e4SLinus Torvalds 	default:
15621da177e4SLinus Torvalds 		/* All other signals. */
15631da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
15641da177e4SLinus Torvalds 		break;
15651da177e4SLinus Torvalds 	}
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	return perm;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
1570b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1571b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1572b68e418cSStephen Smalley #endif
1573b68e418cSStephen Smalley 
15741da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
15756a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
1576c1a85a00SMicah Morton 			       int cap, unsigned int opts, bool initns)
15771da177e4SLinus Torvalds {
15782bf49690SThomas Liu 	struct common_audit_data ad;
157906112163SEric Paris 	struct av_decision avd;
1580b68e418cSStephen Smalley 	u16 sclass;
15813699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1582b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
158306112163SEric Paris 	int rc;
15841da177e4SLinus Torvalds 
158550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
15861da177e4SLinus Torvalds 	ad.u.cap = cap;
15871da177e4SLinus Torvalds 
1588b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1589b68e418cSStephen Smalley 	case 0:
15908e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1591b68e418cSStephen Smalley 		break;
1592b68e418cSStephen Smalley 	case 1:
15938e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1594b68e418cSStephen Smalley 		break;
1595b68e418cSStephen Smalley 	default:
1596c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1597b68e418cSStephen Smalley 		BUG();
1598a35c6c83SEric Paris 		return -EINVAL;
1599b68e418cSStephen Smalley 	}
160006112163SEric Paris 
16016b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
16026b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
1603c1a85a00SMicah Morton 	if (!(opts & CAP_OPT_NOAUDIT)) {
16046b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
16056b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
16069ade0cf4SEric Paris 		if (rc2)
16079ade0cf4SEric Paris 			return rc2;
16089ade0cf4SEric Paris 	}
160906112163SEric Paris 	return rc;
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
16131da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
16141da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
161588e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
16161da177e4SLinus Torvalds 			  struct inode *inode,
16171da177e4SLinus Torvalds 			  u32 perms,
161819e49834SLinus Torvalds 			  struct common_audit_data *adp)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1621275bb41eSDavid Howells 	u32 sid;
16221da177e4SLinus Torvalds 
1623e0e81739SDavid Howells 	validate_creds(cred);
1624e0e81739SDavid Howells 
1625bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1626bbaca6c2SStephen Smalley 		return 0;
1627bbaca6c2SStephen Smalley 
162888e67f3bSDavid Howells 	sid = cred_sid(cred);
162980788c22SCasey Schaufler 	isec = selinux_inode(inode);
16301da177e4SLinus Torvalds 
16316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
16326b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16361da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16371da177e4SLinus Torvalds    pathname if needed. */
163888e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16391da177e4SLinus Torvalds 				  struct dentry *dentry,
16401da177e4SLinus Torvalds 				  u32 av)
16411da177e4SLinus Torvalds {
1642c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16432bf49690SThomas Liu 	struct common_audit_data ad;
164488e67f3bSDavid Howells 
164550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16462875fa00SEric Paris 	ad.u.dentry = dentry;
16475d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
164819e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16492875fa00SEric Paris }
16502875fa00SEric Paris 
16512875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16522875fa00SEric Paris    the path to help the auditing code to more easily generate the
16532875fa00SEric Paris    pathname if needed. */
16542875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
16553f7036a0SAl Viro 				const struct path *path,
16562875fa00SEric Paris 				u32 av)
16572875fa00SEric Paris {
1658c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
16592875fa00SEric Paris 	struct common_audit_data ad;
16602875fa00SEric Paris 
166150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
16622875fa00SEric Paris 	ad.u.path = *path;
16635d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
166419e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
166713f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
166813f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
166913f8e981SDavid Howells 				     struct file *file,
167013f8e981SDavid Howells 				     u32 av)
167113f8e981SDavid Howells {
167213f8e981SDavid Howells 	struct common_audit_data ad;
167313f8e981SDavid Howells 
167443af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
167543af5de7SVivek Goyal 	ad.u.file = file;
167619e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
167713f8e981SDavid Howells }
167813f8e981SDavid Howells 
1679f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1680f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1681f66e448cSChenbo Feng #endif
1682f66e448cSChenbo Feng 
16831da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
16841da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
16851da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
16861da177e4SLinus Torvalds    check a particular permission to the file.
16871da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
16881da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
16891da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
16901da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
169188e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
16921da177e4SLinus Torvalds 			 struct file *file,
16931da177e4SLinus Torvalds 			 u32 av)
16941da177e4SLinus Torvalds {
1695bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
1696496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16972bf49690SThomas Liu 	struct common_audit_data ad;
169888e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
16991da177e4SLinus Torvalds 	int rc;
17001da177e4SLinus Torvalds 
170143af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
170243af5de7SVivek Goyal 	ad.u.file = file;
17031da177e4SLinus Torvalds 
1704275bb41eSDavid Howells 	if (sid != fsec->sid) {
17056b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
17066b6bc620SStephen Smalley 				  sid, fsec->sid,
17071da177e4SLinus Torvalds 				  SECCLASS_FD,
17081da177e4SLinus Torvalds 				  FD__USE,
17091da177e4SLinus Torvalds 				  &ad);
17101da177e4SLinus Torvalds 		if (rc)
171188e67f3bSDavid Howells 			goto out;
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 
1714f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1715f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1716f66e448cSChenbo Feng 	if (rc)
1717f66e448cSChenbo Feng 		return rc;
1718f66e448cSChenbo Feng #endif
1719f66e448cSChenbo Feng 
17201da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
172188e67f3bSDavid Howells 	rc = 0;
17221da177e4SLinus Torvalds 	if (av)
172319e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
17241da177e4SLinus Torvalds 
172588e67f3bSDavid Howells out:
172688e67f3bSDavid Howells 	return rc;
17271da177e4SLinus Torvalds }
17281da177e4SLinus Torvalds 
1729c3c188b2SDavid Howells /*
1730c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1731c3c188b2SDavid Howells  */
1732c957f6dfSVivek Goyal static int
1733c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1734c957f6dfSVivek Goyal 				 struct inode *dir,
1735c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1736c3c188b2SDavid Howells 				 u32 *_new_isid)
1737c3c188b2SDavid Howells {
1738c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1739c3c188b2SDavid Howells 
1740c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1741c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1742c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1743c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1744c3c188b2SDavid Howells 		   tsec->create_sid) {
1745c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1746c3c188b2SDavid Howells 	} else {
174720cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1748aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1749aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1750c3c188b2SDavid Howells 					       name, _new_isid);
1751c3c188b2SDavid Howells 	}
1752c3c188b2SDavid Howells 
1753c3c188b2SDavid Howells 	return 0;
1754c3c188b2SDavid Howells }
1755c3c188b2SDavid Howells 
17561da177e4SLinus Torvalds /* Check whether a task can create a file. */
17571da177e4SLinus Torvalds static int may_create(struct inode *dir,
17581da177e4SLinus Torvalds 		      struct dentry *dentry,
17591da177e4SLinus Torvalds 		      u16 tclass)
17601da177e4SLinus Torvalds {
17610c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
17621da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
17631da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1764275bb41eSDavid Howells 	u32 sid, newsid;
17652bf49690SThomas Liu 	struct common_audit_data ad;
17661da177e4SLinus Torvalds 	int rc;
17671da177e4SLinus Torvalds 
176883da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
17691da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
17701da177e4SLinus Torvalds 
1771275bb41eSDavid Howells 	sid = tsec->sid;
1772275bb41eSDavid Howells 
177350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1774a269434dSEric Paris 	ad.u.dentry = dentry;
17751da177e4SLinus Torvalds 
17766b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
17776b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
17781da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
17791da177e4SLinus Torvalds 			  &ad);
17801da177e4SLinus Torvalds 	if (rc)
17811da177e4SLinus Torvalds 		return rc;
17821da177e4SLinus Torvalds 
1783210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1784210a2928SYang Guo 					   &newsid);
17851da177e4SLinus Torvalds 	if (rc)
17861da177e4SLinus Torvalds 		return rc;
17871da177e4SLinus Torvalds 
17886b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
17896b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
17901da177e4SLinus Torvalds 	if (rc)
17911da177e4SLinus Torvalds 		return rc;
17921da177e4SLinus Torvalds 
17936b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
17946b6bc620SStephen Smalley 			    newsid, sbsec->sid,
17951da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
17961da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds #define MAY_LINK	0
18001da177e4SLinus Torvalds #define MAY_UNLINK	1
18011da177e4SLinus Torvalds #define MAY_RMDIR	2
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
18041da177e4SLinus Torvalds static int may_link(struct inode *dir,
18051da177e4SLinus Torvalds 		    struct dentry *dentry,
18061da177e4SLinus Torvalds 		    int kind)
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds {
18091da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
18102bf49690SThomas Liu 	struct common_audit_data ad;
1811275bb41eSDavid Howells 	u32 sid = current_sid();
18121da177e4SLinus Torvalds 	u32 av;
18131da177e4SLinus Torvalds 	int rc;
18141da177e4SLinus Torvalds 
181583da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
181683da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
18171da177e4SLinus Torvalds 
181850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1819a269434dSEric Paris 	ad.u.dentry = dentry;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	av = DIR__SEARCH;
18221da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
18236b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18246b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
18251da177e4SLinus Torvalds 	if (rc)
18261da177e4SLinus Torvalds 		return rc;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	switch (kind) {
18291da177e4SLinus Torvalds 	case MAY_LINK:
18301da177e4SLinus Torvalds 		av = FILE__LINK;
18311da177e4SLinus Torvalds 		break;
18321da177e4SLinus Torvalds 	case MAY_UNLINK:
18331da177e4SLinus Torvalds 		av = FILE__UNLINK;
18341da177e4SLinus Torvalds 		break;
18351da177e4SLinus Torvalds 	case MAY_RMDIR:
18361da177e4SLinus Torvalds 		av = DIR__RMDIR;
18371da177e4SLinus Torvalds 		break;
18381da177e4SLinus Torvalds 	default:
1839c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
1840744ba35eSEric Paris 			__func__, kind);
18411da177e4SLinus Torvalds 		return 0;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
18446b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18456b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
18461da177e4SLinus Torvalds 	return rc;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
18501da177e4SLinus Torvalds 			     struct dentry *old_dentry,
18511da177e4SLinus Torvalds 			     struct inode *new_dir,
18521da177e4SLinus Torvalds 			     struct dentry *new_dentry)
18531da177e4SLinus Torvalds {
18541da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
18552bf49690SThomas Liu 	struct common_audit_data ad;
1856275bb41eSDavid Howells 	u32 sid = current_sid();
18571da177e4SLinus Torvalds 	u32 av;
18581da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
18591da177e4SLinus Torvalds 	int rc;
18601da177e4SLinus Torvalds 
186183da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
186283da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
1863e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
186483da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
18651da177e4SLinus Torvalds 
186650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18671da177e4SLinus Torvalds 
1868a269434dSEric Paris 	ad.u.dentry = old_dentry;
18696b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18706b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
18711da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
18721da177e4SLinus Torvalds 	if (rc)
18731da177e4SLinus Torvalds 		return rc;
18746b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18756b6bc620SStephen Smalley 			  sid, old_isec->sid,
18761da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
18771da177e4SLinus Torvalds 	if (rc)
18781da177e4SLinus Torvalds 		return rc;
18791da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
18806b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
18816b6bc620SStephen Smalley 				  sid, old_isec->sid,
18821da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
18831da177e4SLinus Torvalds 		if (rc)
18841da177e4SLinus Torvalds 			return rc;
18851da177e4SLinus Torvalds 	}
18861da177e4SLinus Torvalds 
1887a269434dSEric Paris 	ad.u.dentry = new_dentry;
18881da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
18892c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
18901da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
18916b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18926b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
18931da177e4SLinus Torvalds 	if (rc)
18941da177e4SLinus Torvalds 		return rc;
18952c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
189683da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
1897e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
18986b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
18996b6bc620SStephen Smalley 				  sid, new_isec->sid,
19001da177e4SLinus Torvalds 				  new_isec->sclass,
19011da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
19021da177e4SLinus Torvalds 		if (rc)
19031da177e4SLinus Torvalds 			return rc;
19041da177e4SLinus Torvalds 	}
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	return 0;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
191088e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
19111da177e4SLinus Torvalds 			       struct super_block *sb,
19121da177e4SLinus Torvalds 			       u32 perms,
19132bf49690SThomas Liu 			       struct common_audit_data *ad)
19141da177e4SLinus Torvalds {
19151da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
191688e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	sbsec = sb->s_security;
19196b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19206b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
19211da177e4SLinus Torvalds }
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
19241da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
19251da177e4SLinus Torvalds {
19261da177e4SLinus Torvalds 	u32 av = 0;
19271da177e4SLinus Torvalds 
1928dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
19291da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19301da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
19311da177e4SLinus Torvalds 		if (mask & MAY_READ)
19321da177e4SLinus Torvalds 			av |= FILE__READ;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
19351da177e4SLinus Torvalds 			av |= FILE__APPEND;
19361da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
19371da177e4SLinus Torvalds 			av |= FILE__WRITE;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	} else {
19401da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19411da177e4SLinus Torvalds 			av |= DIR__SEARCH;
19421da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
19431da177e4SLinus Torvalds 			av |= DIR__WRITE;
19441da177e4SLinus Torvalds 		if (mask & MAY_READ)
19451da177e4SLinus Torvalds 			av |= DIR__READ;
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	return av;
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
19521da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
19531da177e4SLinus Torvalds {
19541da177e4SLinus Torvalds 	u32 av = 0;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
19571da177e4SLinus Torvalds 		av |= FILE__READ;
19581da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
19591da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
19601da177e4SLinus Torvalds 			av |= FILE__APPEND;
19611da177e4SLinus Torvalds 		else
19621da177e4SLinus Torvalds 			av |= FILE__WRITE;
19631da177e4SLinus Torvalds 	}
19640794c66dSStephen Smalley 	if (!av) {
19650794c66dSStephen Smalley 		/*
19660794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
19670794c66dSStephen Smalley 		 */
19680794c66dSStephen Smalley 		av = FILE__IOCTL;
19690794c66dSStephen Smalley 	}
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	return av;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
19748b6a5a37SEric Paris /*
19758b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
19768b6a5a37SEric Paris  * open permission.
19778b6a5a37SEric Paris  */
19788b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
19798b6a5a37SEric Paris {
19808b6a5a37SEric Paris 	u32 av = file_to_av(file);
1981ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
19828b6a5a37SEric Paris 
1983aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
1984aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
19858b6a5a37SEric Paris 		av |= FILE__OPEN;
198649b7b8deSEric Paris 
19878b6a5a37SEric Paris 	return av;
19888b6a5a37SEric Paris }
19898b6a5a37SEric Paris 
19901da177e4SLinus Torvalds /* Hook functions begin here. */
19911da177e4SLinus Torvalds 
199279af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
199379af7307SStephen Smalley {
199479af7307SStephen Smalley 	u32 mysid = current_sid();
199579af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
199679af7307SStephen Smalley 
19976b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19986b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
199979af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
200079af7307SStephen Smalley }
200179af7307SStephen Smalley 
200279af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
200379af7307SStephen Smalley 				      struct task_struct *to)
200479af7307SStephen Smalley {
200579af7307SStephen Smalley 	u32 mysid = current_sid();
200679af7307SStephen Smalley 	u32 fromsid = task_sid(from);
200779af7307SStephen Smalley 	u32 tosid = task_sid(to);
200879af7307SStephen Smalley 	int rc;
200979af7307SStephen Smalley 
201079af7307SStephen Smalley 	if (mysid != fromsid) {
20116b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20126b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
201379af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
201479af7307SStephen Smalley 		if (rc)
201579af7307SStephen Smalley 			return rc;
201679af7307SStephen Smalley 	}
201779af7307SStephen Smalley 
20186b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20196b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
202079af7307SStephen Smalley 			    NULL);
202179af7307SStephen Smalley }
202279af7307SStephen Smalley 
202379af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
202479af7307SStephen Smalley 					  struct task_struct *to)
202579af7307SStephen Smalley {
202679af7307SStephen Smalley 	u32 fromsid = task_sid(from);
202779af7307SStephen Smalley 	u32 tosid = task_sid(to);
202879af7307SStephen Smalley 
20296b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20306b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
203179af7307SStephen Smalley 			    NULL);
203279af7307SStephen Smalley }
203379af7307SStephen Smalley 
203479af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
203579af7307SStephen Smalley 					struct task_struct *to,
203679af7307SStephen Smalley 					struct file *file)
203779af7307SStephen Smalley {
203879af7307SStephen Smalley 	u32 sid = task_sid(to);
2039bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
204083da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
204120cdef8dSPaul Moore 	struct inode_security_struct *isec;
204279af7307SStephen Smalley 	struct common_audit_data ad;
204379af7307SStephen Smalley 	int rc;
204479af7307SStephen Smalley 
204579af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
204679af7307SStephen Smalley 	ad.u.path = file->f_path;
204779af7307SStephen Smalley 
204879af7307SStephen Smalley 	if (sid != fsec->sid) {
20496b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20506b6bc620SStephen Smalley 				  sid, fsec->sid,
205179af7307SStephen Smalley 				  SECCLASS_FD,
205279af7307SStephen Smalley 				  FD__USE,
205379af7307SStephen Smalley 				  &ad);
205479af7307SStephen Smalley 		if (rc)
205579af7307SStephen Smalley 			return rc;
205679af7307SStephen Smalley 	}
205779af7307SStephen Smalley 
2058f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2059f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2060f66e448cSChenbo Feng 	if (rc)
2061f66e448cSChenbo Feng 		return rc;
2062f66e448cSChenbo Feng #endif
2063f66e448cSChenbo Feng 
206483da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
206579af7307SStephen Smalley 		return 0;
206679af7307SStephen Smalley 
206720cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
20686b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20696b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
207079af7307SStephen Smalley 			    &ad);
207179af7307SStephen Smalley }
207279af7307SStephen Smalley 
20739e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2074006ebb40SStephen Smalley 				     unsigned int mode)
20751da177e4SLinus Torvalds {
2076275bb41eSDavid Howells 	u32 sid = current_sid();
2077275bb41eSDavid Howells 	u32 csid = task_sid(child);
2078006ebb40SStephen Smalley 
2079be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
20806b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
20816b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2082be0554c9SStephen Smalley 
20836b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20846b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
20855cd9c58fSDavid Howells }
20865cd9c58fSDavid Howells 
20875cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
20885cd9c58fSDavid Howells {
20896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20906b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2091be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
20951da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
20961da177e4SLinus Torvalds {
20976b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20986b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2099be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
2102d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2103d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
210415a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
210515a2460eSDavid Howells 			  const kernel_cap_t *permitted)
21061da177e4SLinus Torvalds {
21076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21086b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2109be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds 
21125626d3e8SJames Morris /*
21135626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
21145626d3e8SJames Morris  * which was removed).
21155626d3e8SJames Morris  *
21165626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
21175626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
21185626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
21195626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
21205626d3e8SJames Morris  */
21215626d3e8SJames Morris 
21226a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2123c1a85a00SMicah Morton 			   int cap, unsigned int opts)
21241da177e4SLinus Torvalds {
2125c1a85a00SMicah Morton 	return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
21291da177e4SLinus Torvalds {
213088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
21311da177e4SLinus Torvalds 	int rc = 0;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	if (!sb)
21341da177e4SLinus Torvalds 		return 0;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	switch (cmds) {
21371da177e4SLinus Torvalds 	case Q_SYNC:
21381da177e4SLinus Torvalds 	case Q_QUOTAON:
21391da177e4SLinus Torvalds 	case Q_QUOTAOFF:
21401da177e4SLinus Torvalds 	case Q_SETINFO:
21411da177e4SLinus Torvalds 	case Q_SETQUOTA:
214288e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
21431da177e4SLinus Torvalds 		break;
21441da177e4SLinus Torvalds 	case Q_GETFMT:
21451da177e4SLinus Torvalds 	case Q_GETINFO:
21461da177e4SLinus Torvalds 	case Q_GETQUOTA:
214788e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
21481da177e4SLinus Torvalds 		break;
21491da177e4SLinus Torvalds 	default:
21501da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
21511da177e4SLinus Torvalds 		break;
21521da177e4SLinus Torvalds 	}
21531da177e4SLinus Torvalds 	return rc;
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
21571da177e4SLinus Torvalds {
215888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
215988e67f3bSDavid Howells 
21602875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
216312b3052cSEric Paris static int selinux_syslog(int type)
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	switch (type) {
2166d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2167d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
21686b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21696b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2170be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2171d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2172d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2173d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2174d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
21756b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21766b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2177be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2178be0554c9SStephen Smalley 				    NULL);
21791da177e4SLinus Torvalds 	}
2180be0554c9SStephen Smalley 	/* All other syslog types */
21816b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21826b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2183be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds /*
21871da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
21881da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
21891da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
21901da177e4SLinus Torvalds  *
21911da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
21921da177e4SLinus Torvalds  * processes that allocate mappings.
21931da177e4SLinus Torvalds  */
219434b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
21951da177e4SLinus Torvalds {
21961da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
21971da177e4SLinus Torvalds 
2198b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2199c1a85a00SMicah Morton 				 CAP_OPT_NOAUDIT, true);
22001da177e4SLinus Torvalds 	if (rc == 0)
22011da177e4SLinus Torvalds 		cap_sys_admin = 1;
22021da177e4SLinus Torvalds 
2203b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds /* binprm security operations */
22071da177e4SLinus Torvalds 
2208be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
22090c6181cbSPaul Moore {
22100c6181cbSPaul Moore 	u32 sid = 0;
22110c6181cbSPaul Moore 	struct task_struct *tracer;
22120c6181cbSPaul Moore 
22130c6181cbSPaul Moore 	rcu_read_lock();
2214be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
22150c6181cbSPaul Moore 	if (tracer)
22160c6181cbSPaul Moore 		sid = task_sid(tracer);
22170c6181cbSPaul Moore 	rcu_read_unlock();
22180c6181cbSPaul Moore 
22190c6181cbSPaul Moore 	return sid;
22200c6181cbSPaul Moore }
22210c6181cbSPaul Moore 
22227b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
22237b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
22247b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
22257b0d0b40SStephen Smalley {
22267b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2227380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
22287b0d0b40SStephen Smalley 	int rc;
2229af63f419SStephen Smalley 	u32 av;
22307b0d0b40SStephen Smalley 
22317b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
22327b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
22337b0d0b40SStephen Smalley 
22347b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
22357b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
22367b0d0b40SStephen Smalley 
22377b0d0b40SStephen Smalley 	/*
2238af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2239af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2240af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2241af63f419SStephen Smalley 	 * the old and new contexts.
2242af63f419SStephen Smalley 	 */
2243aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2244af63f419SStephen Smalley 		av = 0;
2245af63f419SStephen Smalley 		if (nnp)
2246af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2247af63f419SStephen Smalley 		if (nosuid)
2248af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
22496b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22506b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2251af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2252af63f419SStephen Smalley 		if (!rc)
2253af63f419SStephen Smalley 			return 0;
2254af63f419SStephen Smalley 	}
2255af63f419SStephen Smalley 
2256af63f419SStephen Smalley 	/*
2257af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2258af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2259af63f419SStephen Smalley 	 * of the permissions of the current SID.
22607b0d0b40SStephen Smalley 	 */
2261aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2262aa8e712cSStephen Smalley 					 new_tsec->sid);
2263af63f419SStephen Smalley 	if (!rc)
2264af63f419SStephen Smalley 		return 0;
2265af63f419SStephen Smalley 
22667b0d0b40SStephen Smalley 	/*
22677b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
22687b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
22697b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
22707b0d0b40SStephen Smalley 	 */
22717b0d0b40SStephen Smalley 	if (nnp)
22727b0d0b40SStephen Smalley 		return -EPERM;
22737b0d0b40SStephen Smalley 	return -EACCES;
22747b0d0b40SStephen Smalley }
22757b0d0b40SStephen Smalley 
2276a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
22771da177e4SLinus Torvalds {
2278a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2279a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
22801da177e4SLinus Torvalds 	struct inode_security_struct *isec;
22812bf49690SThomas Liu 	struct common_audit_data ad;
2282496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
22831da177e4SLinus Torvalds 	int rc;
22841da177e4SLinus Torvalds 
2285a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2286a6f76f23SDavid Howells 	 * the script interpreter */
2287ddb4a144SKees Cook 	if (bprm->called_set_creds)
22881da177e4SLinus Torvalds 		return 0;
22891da177e4SLinus Torvalds 
22900c6cfa62SCasey Schaufler 	old_tsec = selinux_cred(current_cred());
22910c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
229283da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	/* Default to the current task SID. */
2295a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2296a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
22971da177e4SLinus Torvalds 
229828eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2299a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2300a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2301a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
23021da177e4SLinus Torvalds 
2303a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2304a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
23051da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2306a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2307259e5e6cSAndy Lutomirski 
23087b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
23097b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23107b0d0b40SStephen Smalley 		if (rc)
23117b0d0b40SStephen Smalley 			return rc;
23121da177e4SLinus Torvalds 	} else {
23131da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2314aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2315aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2316652bb9b0SEric Paris 					     &new_tsec->sid);
23171da177e4SLinus Torvalds 		if (rc)
23181da177e4SLinus Torvalds 			return rc;
23197b0d0b40SStephen Smalley 
23207b0d0b40SStephen Smalley 		/*
23217b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
23227b0d0b40SStephen Smalley 		 * transition.
23237b0d0b40SStephen Smalley 		 */
23247b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23257b0d0b40SStephen Smalley 		if (rc)
23267b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds 
232943af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
233043af5de7SVivek Goyal 	ad.u.file = bprm->file;
23311da177e4SLinus Torvalds 
2332a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
23336b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23346b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
23351da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
23361da177e4SLinus Torvalds 		if (rc)
23371da177e4SLinus Torvalds 			return rc;
23381da177e4SLinus Torvalds 	} else {
23391da177e4SLinus Torvalds 		/* Check permissions for the transition. */
23406b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23416b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
23421da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
23431da177e4SLinus Torvalds 		if (rc)
23441da177e4SLinus Torvalds 			return rc;
23451da177e4SLinus Torvalds 
23466b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23476b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
23481da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
23491da177e4SLinus Torvalds 		if (rc)
23501da177e4SLinus Torvalds 			return rc;
23511da177e4SLinus Torvalds 
2352a6f76f23SDavid Howells 		/* Check for shared state */
2353a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
23546b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
23556b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2356a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2357a6f76f23SDavid Howells 					  NULL);
2358a6f76f23SDavid Howells 			if (rc)
2359a6f76f23SDavid Howells 				return -EPERM;
23601da177e4SLinus Torvalds 		}
23611da177e4SLinus Torvalds 
2362a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2363a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
23649227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2365be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2366a6f76f23SDavid Howells 			if (ptsid != 0) {
23676b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
23686b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2369a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2370a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2371a6f76f23SDavid Howells 				if (rc)
2372a6f76f23SDavid Howells 					return -EPERM;
2373a6f76f23SDavid Howells 			}
2374a6f76f23SDavid Howells 		}
2375a6f76f23SDavid Howells 
2376a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2377a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2378a6f76f23SDavid Howells 
23791da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
23801da177e4SLinus Torvalds 		   the noatsecure permission is granted between
23811da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
23826b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23836b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
238462874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
238562874c3aSKees Cook 				  NULL);
238662874c3aSKees Cook 		bprm->secureexec |= !!rc;
23871da177e4SLinus Torvalds 	}
23881da177e4SLinus Torvalds 
238962874c3aSKees Cook 	return 0;
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
2392c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2393c3c073f8SAl Viro {
2394c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2395c3c073f8SAl Viro }
2396c3c073f8SAl Viro 
23971da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2398745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2399745ca247SDavid Howells 					    struct files_struct *files)
24001da177e4SLinus Torvalds {
24011da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2402b20c8122SStephen Smalley 	struct tty_struct *tty;
240324ec839cSPeter Zijlstra 	int drop_tty = 0;
2404c3c073f8SAl Viro 	unsigned n;
24051da177e4SLinus Torvalds 
240624ec839cSPeter Zijlstra 	tty = get_current_tty();
24071da177e4SLinus Torvalds 	if (tty) {
24084a510969SPeter Hurley 		spin_lock(&tty->files_lock);
240937dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2410d996b62aSNick Piggin 			struct tty_file_private *file_priv;
241137dd0bd0SEric Paris 
24121da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
241313f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
241413f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
241513f8e981SDavid Howells 			   open file may belong to another process and we are
241613f8e981SDavid Howells 			   only interested in the inode-based check here. */
2417d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2418d996b62aSNick Piggin 						struct tty_file_private, list);
2419d996b62aSNick Piggin 			file = file_priv->file;
242013f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
242124ec839cSPeter Zijlstra 				drop_tty = 1;
24221da177e4SLinus Torvalds 		}
24234a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2424452a00d2SAlan Cox 		tty_kref_put(tty);
24251da177e4SLinus Torvalds 	}
242698a27ba4SEric W. Biederman 	/* Reset controlling tty. */
242798a27ba4SEric W. Biederman 	if (drop_tty)
242898a27ba4SEric W. Biederman 		no_tty();
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2431c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2432c3c073f8SAl Viro 	if (!n) /* none found? */
2433c3c073f8SAl Viro 		return;
24341da177e4SLinus Torvalds 
2435c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
243645525b26SAl Viro 	if (IS_ERR(devnull))
243745525b26SAl Viro 		devnull = NULL;
2438c3c073f8SAl Viro 	/* replace all the matching ones with this */
2439c3c073f8SAl Viro 	do {
244045525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2441c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
244245525b26SAl Viro 	if (devnull)
2443c3c073f8SAl Viro 		fput(devnull);
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds /*
2447a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
24481da177e4SLinus Torvalds  */
2449a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
24501da177e4SLinus Torvalds {
2451a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24521da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
24531da177e4SLinus Torvalds 	int rc, i;
24541da177e4SLinus Torvalds 
24550c6cfa62SCasey Schaufler 	new_tsec = selinux_cred(bprm->cred);
2456a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
24571da177e4SLinus Torvalds 		return;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2460a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
24611da177e4SLinus Torvalds 
2462a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2463a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2464a6f76f23SDavid Howells 
2465a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2466a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2467a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2468a6f76f23SDavid Howells 	 *
2469a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2470a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2471a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2472a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2473a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2474a6f76f23SDavid Howells 	 */
24756b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
24766b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2477a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2478a6f76f23SDavid Howells 	if (rc) {
2479eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2480eb2d55a3SOleg Nesterov 		task_lock(current);
2481a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2482a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2483a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2484a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2485a6f76f23SDavid Howells 		}
2486eb2d55a3SOleg Nesterov 		task_unlock(current);
2487baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2488eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2489a6f76f23SDavid Howells 	}
2490a6f76f23SDavid Howells }
2491a6f76f23SDavid Howells 
2492a6f76f23SDavid Howells /*
2493a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2494a6f76f23SDavid Howells  * due to exec
2495a6f76f23SDavid Howells  */
2496a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2497a6f76f23SDavid Howells {
24980c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
2499a6f76f23SDavid Howells 	u32 osid, sid;
2500ddbc7d06SArnd Bergmann 	int rc;
2501a6f76f23SDavid Howells 
2502a6f76f23SDavid Howells 	osid = tsec->osid;
2503a6f76f23SDavid Howells 	sid = tsec->sid;
2504a6f76f23SDavid Howells 
2505a6f76f23SDavid Howells 	if (sid == osid)
2506a6f76f23SDavid Howells 		return;
2507a6f76f23SDavid Howells 
2508a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2509a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2510a6f76f23SDavid Howells 	 * flush and unblock signals.
2511a6f76f23SDavid Howells 	 *
2512a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2513a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2514a6f76f23SDavid Howells 	 */
25156b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25166b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
25171da177e4SLinus Torvalds 	if (rc) {
2518ddbc7d06SArnd Bergmann 		clear_itimer();
2519ddbc7d06SArnd Bergmann 
25201da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
25219e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
25229e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
25239e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
25241da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
25251da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
25269e7c8f8cSOleg Nesterov 			recalc_sigpending();
25273bcac026SDavid Howells 		}
25281da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
25291da177e4SLinus Torvalds 	}
25301da177e4SLinus Torvalds 
2531a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2532a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2533ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
25340b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2535ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
25361da177e4SLinus Torvalds }
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds /* superblock security operations */
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
25411da177e4SLinus Torvalds {
2542*cb89e246SPaul Moore 	struct superblock_security_struct *sbsec;
2543*cb89e246SPaul Moore 
2544*cb89e246SPaul Moore 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2545*cb89e246SPaul Moore 	if (!sbsec)
2546*cb89e246SPaul Moore 		return -ENOMEM;
2547*cb89e246SPaul Moore 
2548*cb89e246SPaul Moore 	mutex_init(&sbsec->lock);
2549*cb89e246SPaul Moore 	INIT_LIST_HEAD(&sbsec->isec_head);
2550*cb89e246SPaul Moore 	spin_lock_init(&sbsec->isec_lock);
2551*cb89e246SPaul Moore 	sbsec->sb = sb;
2552*cb89e246SPaul Moore 	sbsec->sid = SECINITSID_UNLABELED;
2553*cb89e246SPaul Moore 	sbsec->def_sid = SECINITSID_FILE;
2554*cb89e246SPaul Moore 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2555*cb89e246SPaul Moore 	sb->s_security = sbsec;
2556*cb89e246SPaul Moore 
2557*cb89e246SPaul Moore 	return 0;
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	superblock_free_security(sb);
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
256599dbbb59SAl Viro static inline int opt_len(const char *s)
25661da177e4SLinus Torvalds {
256799dbbb59SAl Viro 	bool open_quote = false;
256899dbbb59SAl Viro 	int len;
256999dbbb59SAl Viro 	char c;
25701da177e4SLinus Torvalds 
257199dbbb59SAl Viro 	for (len = 0; (c = s[len]) != '\0'; len++) {
257299dbbb59SAl Viro 		if (c == '"')
25733528a953SCory Olmo 			open_quote = !open_quote;
257499dbbb59SAl Viro 		if (c == ',' && !open_quote)
257599dbbb59SAl Viro 			break;
25761da177e4SLinus Torvalds 	}
257799dbbb59SAl Viro 	return len;
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
2580204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
25815b400239SAl Viro {
258299dbbb59SAl Viro 	char *from = options;
258399dbbb59SAl Viro 	char *to = options;
258499dbbb59SAl Viro 	bool first = true;
2585fec63753SGen Zhang 	int rc;
25865b400239SAl Viro 
258799dbbb59SAl Viro 	while (1) {
258899dbbb59SAl Viro 		int len = opt_len(from);
2589fec63753SGen Zhang 		int token;
259099dbbb59SAl Viro 		char *arg = NULL;
259199dbbb59SAl Viro 
259299dbbb59SAl Viro 		token = match_opt_prefix(from, len, &arg);
259399dbbb59SAl Viro 
259499dbbb59SAl Viro 		if (token != Opt_error) {
259599dbbb59SAl Viro 			char *p, *q;
259699dbbb59SAl Viro 
259799dbbb59SAl Viro 			/* strip quotes */
259899dbbb59SAl Viro 			if (arg) {
259999dbbb59SAl Viro 				for (p = q = arg; p < from + len; p++) {
260099dbbb59SAl Viro 					char c = *p;
260199dbbb59SAl Viro 					if (c != '"')
260299dbbb59SAl Viro 						*q++ = c;
260399dbbb59SAl Viro 				}
260499dbbb59SAl Viro 				arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2605fec63753SGen Zhang 				if (!arg) {
2606fec63753SGen Zhang 					rc = -ENOMEM;
2607fec63753SGen Zhang 					goto free_opt;
2608fec63753SGen Zhang 				}
260999dbbb59SAl Viro 			}
261099dbbb59SAl Viro 			rc = selinux_add_opt(token, arg, mnt_opts);
261199dbbb59SAl Viro 			if (unlikely(rc)) {
261299dbbb59SAl Viro 				kfree(arg);
2613fec63753SGen Zhang 				goto free_opt;
26141da177e4SLinus Torvalds 			}
261599dbbb59SAl Viro 		} else {
261699dbbb59SAl Viro 			if (!first) {	// copy with preceding comma
261799dbbb59SAl Viro 				from--;
261899dbbb59SAl Viro 				len++;
261999dbbb59SAl Viro 			}
262099dbbb59SAl Viro 			if (to != from)
262199dbbb59SAl Viro 				memmove(to, from, len);
262299dbbb59SAl Viro 			to += len;
262399dbbb59SAl Viro 			first = false;
262499dbbb59SAl Viro 		}
262599dbbb59SAl Viro 		if (!from[len])
262699dbbb59SAl Viro 			break;
262799dbbb59SAl Viro 		from += len + 1;
262899dbbb59SAl Viro 	}
262999dbbb59SAl Viro 	*to = '\0';
263099dbbb59SAl Viro 	return 0;
2631fec63753SGen Zhang 
2632fec63753SGen Zhang free_opt:
2633fec63753SGen Zhang 	if (*mnt_opts) {
2634fec63753SGen Zhang 		selinux_free_mnt_opts(*mnt_opts);
2635fec63753SGen Zhang 		*mnt_opts = NULL;
2636fec63753SGen Zhang 	}
2637fec63753SGen Zhang 	return rc;
26385b400239SAl Viro }
26391da177e4SLinus Torvalds 
2640204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2641026eb167SEric Paris {
2642bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
2643026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2644bd323655SAl Viro 	u32 sid;
2645bd323655SAl Viro 	int rc;
2646026eb167SEric Paris 
2647026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2648026eb167SEric Paris 		return 0;
2649026eb167SEric Paris 
2650204cc0ccSAl Viro 	if (!opts)
2651026eb167SEric Paris 		return 0;
2652026eb167SEric Paris 
2653bd323655SAl Viro 	if (opts->fscontext) {
2654bd323655SAl Viro 		rc = parse_sid(sb, opts->fscontext, &sid);
2655026eb167SEric Paris 		if (rc)
2656c039bc3cSAl Viro 			return rc;
2657026eb167SEric Paris 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2658026eb167SEric Paris 			goto out_bad_option;
2659bd323655SAl Viro 	}
2660bd323655SAl Viro 	if (opts->context) {
2661bd323655SAl Viro 		rc = parse_sid(sb, opts->context, &sid);
2662bd323655SAl Viro 		if (rc)
2663bd323655SAl Viro 			return rc;
2664026eb167SEric Paris 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2665026eb167SEric Paris 			goto out_bad_option;
2666bd323655SAl Viro 	}
2667bd323655SAl Viro 	if (opts->rootcontext) {
2668026eb167SEric Paris 		struct inode_security_struct *root_isec;
266983da53c5SAndreas Gruenbacher 		root_isec = backing_inode_security(sb->s_root);
2670bd323655SAl Viro 		rc = parse_sid(sb, opts->rootcontext, &sid);
2671bd323655SAl Viro 		if (rc)
2672bd323655SAl Viro 			return rc;
2673026eb167SEric Paris 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2674026eb167SEric Paris 			goto out_bad_option;
2675026eb167SEric Paris 	}
2676bd323655SAl Viro 	if (opts->defcontext) {
2677bd323655SAl Viro 		rc = parse_sid(sb, opts->defcontext, &sid);
2678bd323655SAl Viro 		if (rc)
2679bd323655SAl Viro 			return rc;
2680026eb167SEric Paris 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2681026eb167SEric Paris 			goto out_bad_option;
2682026eb167SEric Paris 	}
2683c039bc3cSAl Viro 	return 0;
2684026eb167SEric Paris 
2685026eb167SEric Paris out_bad_option:
2686c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
268729b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
268829b1deb2SLinus Torvalds 	       sb->s_type->name);
2689c039bc3cSAl Viro 	return -EINVAL;
2690026eb167SEric Paris }
2691026eb167SEric Paris 
2692a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb)
26931da177e4SLinus Torvalds {
269488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26952bf49690SThomas Liu 	struct common_audit_data ad;
269674192246SJames Morris 
269750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2698a269434dSEric Paris 	ad.u.dentry = sb->s_root;
269988e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
27001da177e4SLinus Torvalds }
27011da177e4SLinus Torvalds 
2702726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
27031da177e4SLinus Torvalds {
270488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27052bf49690SThomas Liu 	struct common_audit_data ad;
27061da177e4SLinus Torvalds 
270750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2708a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
270988e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
2712808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
27138a04c43bSAl Viro 			 const struct path *path,
2714808d4e3cSAl Viro 			 const char *type,
27151da177e4SLinus Torvalds 			 unsigned long flags,
27161da177e4SLinus Torvalds 			 void *data)
27171da177e4SLinus Torvalds {
271888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2721d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
27221da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
27231da177e4SLinus Torvalds 	else
27242875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27281da177e4SLinus Torvalds {
272988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27301da177e4SLinus Torvalds 
273188e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27321da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27331da177e4SLinus Torvalds }
27341da177e4SLinus Torvalds 
27350b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc,
27360b52075eSAl Viro 				  struct fs_context *src_fc)
27370b52075eSAl Viro {
27380b52075eSAl Viro 	const struct selinux_mnt_opts *src = src_fc->security;
27390b52075eSAl Viro 	struct selinux_mnt_opts *opts;
27400b52075eSAl Viro 
27410b52075eSAl Viro 	if (!src)
27420b52075eSAl Viro 		return 0;
27430b52075eSAl Viro 
27440b52075eSAl Viro 	fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
27450b52075eSAl Viro 	if (!fc->security)
27460b52075eSAl Viro 		return -ENOMEM;
27470b52075eSAl Viro 
27480b52075eSAl Viro 	opts = fc->security;
27490b52075eSAl Viro 
27500b52075eSAl Viro 	if (src->fscontext) {
27510b52075eSAl Viro 		opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
27520b52075eSAl Viro 		if (!opts->fscontext)
27530b52075eSAl Viro 			return -ENOMEM;
27540b52075eSAl Viro 	}
27550b52075eSAl Viro 	if (src->context) {
27560b52075eSAl Viro 		opts->context = kstrdup(src->context, GFP_KERNEL);
27570b52075eSAl Viro 		if (!opts->context)
27580b52075eSAl Viro 			return -ENOMEM;
27590b52075eSAl Viro 	}
27600b52075eSAl Viro 	if (src->rootcontext) {
27610b52075eSAl Viro 		opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
27620b52075eSAl Viro 		if (!opts->rootcontext)
27630b52075eSAl Viro 			return -ENOMEM;
27640b52075eSAl Viro 	}
27650b52075eSAl Viro 	if (src->defcontext) {
27660b52075eSAl Viro 		opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
27670b52075eSAl Viro 		if (!opts->defcontext)
27680b52075eSAl Viro 			return -ENOMEM;
27690b52075eSAl Viro 	}
27700b52075eSAl Viro 	return 0;
27710b52075eSAl Viro }
27720b52075eSAl Viro 
2773442155c1SDavid Howells static const struct fs_parameter_spec selinux_param_specs[] = {
2774442155c1SDavid Howells 	fsparam_string(CONTEXT_STR,	Opt_context),
2775442155c1SDavid Howells 	fsparam_string(DEFCONTEXT_STR,	Opt_defcontext),
2776442155c1SDavid Howells 	fsparam_string(FSCONTEXT_STR,	Opt_fscontext),
2777442155c1SDavid Howells 	fsparam_string(ROOTCONTEXT_STR,	Opt_rootcontext),
2778442155c1SDavid Howells 	fsparam_flag  (SECLABEL_STR,	Opt_seclabel),
2779442155c1SDavid Howells 	{}
2780442155c1SDavid Howells };
2781442155c1SDavid Howells 
2782442155c1SDavid Howells static const struct fs_parameter_description selinux_fs_parameters = {
2783442155c1SDavid Howells 	.name		= "SELinux",
2784442155c1SDavid Howells 	.specs		= selinux_param_specs,
2785442155c1SDavid Howells };
2786442155c1SDavid Howells 
2787442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc,
2788442155c1SDavid Howells 					  struct fs_parameter *param)
2789442155c1SDavid Howells {
2790442155c1SDavid Howells 	struct fs_parse_result result;
2791442155c1SDavid Howells 	int opt, rc;
2792442155c1SDavid Howells 
2793442155c1SDavid Howells 	opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2794442155c1SDavid Howells 	if (opt < 0)
2795442155c1SDavid Howells 		return opt;
2796442155c1SDavid Howells 
2797442155c1SDavid Howells 	rc = selinux_add_opt(opt, param->string, &fc->security);
2798442155c1SDavid Howells 	if (!rc) {
2799442155c1SDavid Howells 		param->string = NULL;
2800442155c1SDavid Howells 		rc = 1;
2801442155c1SDavid Howells 	}
2802442155c1SDavid Howells 	return rc;
2803442155c1SDavid Howells }
2804442155c1SDavid Howells 
28051da177e4SLinus Torvalds /* inode security operations */
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
28081da177e4SLinus Torvalds {
2809*cb89e246SPaul Moore 	struct inode_security_struct *isec = selinux_inode(inode);
2810*cb89e246SPaul Moore 	u32 sid = current_sid();
2811*cb89e246SPaul Moore 
2812*cb89e246SPaul Moore 	spin_lock_init(&isec->lock);
2813*cb89e246SPaul Moore 	INIT_LIST_HEAD(&isec->list);
2814*cb89e246SPaul Moore 	isec->inode = inode;
2815*cb89e246SPaul Moore 	isec->sid = SECINITSID_UNLABELED;
2816*cb89e246SPaul Moore 	isec->sclass = SECCLASS_FILE;
2817*cb89e246SPaul Moore 	isec->task_sid = sid;
2818*cb89e246SPaul Moore 	isec->initialized = LABEL_INVALID;
2819*cb89e246SPaul Moore 
2820*cb89e246SPaul Moore 	return 0;
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
28241da177e4SLinus Torvalds {
28251da177e4SLinus Torvalds 	inode_free_security(inode);
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
2828d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
28294f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2830d47be3dfSDavid Quigley 					u32 *ctxlen)
2831d47be3dfSDavid Quigley {
2832d47be3dfSDavid Quigley 	u32 newsid;
2833d47be3dfSDavid Quigley 	int rc;
2834d47be3dfSDavid Quigley 
28350c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2836c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2837d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2838d47be3dfSDavid Quigley 					   &newsid);
2839c3c188b2SDavid Howells 	if (rc)
2840d47be3dfSDavid Quigley 		return rc;
2841d47be3dfSDavid Quigley 
2842aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2843aa8e712cSStephen Smalley 				       ctxlen);
2844d47be3dfSDavid Quigley }
2845d47be3dfSDavid Quigley 
2846a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2847a518b0a5SVivek Goyal 					  struct qstr *name,
2848a518b0a5SVivek Goyal 					  const struct cred *old,
2849a518b0a5SVivek Goyal 					  struct cred *new)
2850a518b0a5SVivek Goyal {
2851a518b0a5SVivek Goyal 	u32 newsid;
2852a518b0a5SVivek Goyal 	int rc;
2853a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2854a518b0a5SVivek Goyal 
28550c6cfa62SCasey Schaufler 	rc = selinux_determine_inode_label(selinux_cred(old),
2856a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
2857a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
2858a518b0a5SVivek Goyal 					   &newsid);
2859a518b0a5SVivek Goyal 	if (rc)
2860a518b0a5SVivek Goyal 		return rc;
2861a518b0a5SVivek Goyal 
28620c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
2863a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
2864a518b0a5SVivek Goyal 	return 0;
2865a518b0a5SVivek Goyal }
2866a518b0a5SVivek Goyal 
28675e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
28689548906bSTetsuo Handa 				       const struct qstr *qstr,
28699548906bSTetsuo Handa 				       const char **name,
28702a7dba39SEric Paris 				       void **value, size_t *len)
28715e41ff9eSStephen Smalley {
28720c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
28735e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2874c0d4f464SCorentin LABBE 	u32 newsid, clen;
28755e41ff9eSStephen Smalley 	int rc;
28769548906bSTetsuo Handa 	char *context;
28775e41ff9eSStephen Smalley 
28785e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
28795e41ff9eSStephen Smalley 
28805e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2881275bb41eSDavid Howells 
2882210a2928SYang Guo 	rc = selinux_determine_inode_label(tsec, dir, qstr,
28835e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
2884c3c188b2SDavid Howells 		&newsid);
2885c3c188b2SDavid Howells 	if (rc)
28865e41ff9eSStephen Smalley 		return rc;
28875e41ff9eSStephen Smalley 
2888296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
28890d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
289080788c22SCasey Schaufler 		struct inode_security_struct *isec = selinux_inode(inode);
2891296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2892296fddf7SEric Paris 		isec->sid = newsid;
28936f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
2894296fddf7SEric Paris 	}
28955e41ff9eSStephen Smalley 
289665cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state) ||
289765cddd50SOndrej Mosnacek 	    !(sbsec->flags & SBLABEL_MNT))
289825a74f3bSStephen Smalley 		return -EOPNOTSUPP;
289925a74f3bSStephen Smalley 
29009548906bSTetsuo Handa 	if (name)
29019548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
29025e41ff9eSStephen Smalley 
2903570bc1c2SStephen Smalley 	if (value && len) {
2904aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
2905aa8e712cSStephen Smalley 						   &context, &clen);
29069548906bSTetsuo Handa 		if (rc)
29075e41ff9eSStephen Smalley 			return rc;
29085e41ff9eSStephen Smalley 		*value = context;
2909570bc1c2SStephen Smalley 		*len = clen;
2910570bc1c2SStephen Smalley 	}
29115e41ff9eSStephen Smalley 
29125e41ff9eSStephen Smalley 	return 0;
29135e41ff9eSStephen Smalley }
29145e41ff9eSStephen Smalley 
29154acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
29161da177e4SLinus Torvalds {
29171da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
29181da177e4SLinus Torvalds }
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29211da177e4SLinus Torvalds {
29221da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
29261da177e4SLinus Torvalds {
29271da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
29311da177e4SLinus Torvalds {
29321da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
29331da177e4SLinus Torvalds }
29341da177e4SLinus Torvalds 
293518bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
29361da177e4SLinus Torvalds {
29371da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
29381da177e4SLinus Torvalds }
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
29411da177e4SLinus Torvalds {
29421da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
29431da177e4SLinus Torvalds }
29441da177e4SLinus Torvalds 
29451a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
29461da177e4SLinus Torvalds {
29471da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
29501da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
29511da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
29541da177e4SLinus Torvalds }
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
29571da177e4SLinus Torvalds {
295888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
295988e67f3bSDavid Howells 
29602875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
2963bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2964bda0be7aSNeilBrown 				     bool rcu)
29651da177e4SLinus Torvalds {
296688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2967bda0be7aSNeilBrown 	struct common_audit_data ad;
2968bda0be7aSNeilBrown 	struct inode_security_struct *isec;
2969bda0be7aSNeilBrown 	u32 sid;
29701da177e4SLinus Torvalds 
2971bda0be7aSNeilBrown 	validate_creds(cred);
2972bda0be7aSNeilBrown 
2973bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
2974bda0be7aSNeilBrown 	ad.u.dentry = dentry;
2975bda0be7aSNeilBrown 	sid = cred_sid(cred);
29765d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
29775d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
29785d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
2979bda0be7aSNeilBrown 
29801a37079cSStephen Smalley 	return avc_has_perm_flags(&selinux_state,
29811a37079cSStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
29821a37079cSStephen Smalley 				  rcu ? MAY_NOT_BLOCK : 0);
29831da177e4SLinus Torvalds }
29841da177e4SLinus Torvalds 
2985d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2986d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
29870188d5c0SStephen Smalley 					   int result)
2988d4cf970dSEric Paris {
2989d4cf970dSEric Paris 	struct common_audit_data ad;
299080788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
2991d4cf970dSEric Paris 	int rc;
2992d4cf970dSEric Paris 
299350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2994d4cf970dSEric Paris 	ad.u.inode = inode;
2995d4cf970dSEric Paris 
29966b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
29976b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
29980188d5c0SStephen Smalley 			    audited, denied, result, &ad);
2999d4cf970dSEric Paris 	if (rc)
3000d4cf970dSEric Paris 		return rc;
3001d4cf970dSEric Paris 	return 0;
3002d4cf970dSEric Paris }
3003d4cf970dSEric Paris 
3004e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
30051da177e4SLinus Torvalds {
300688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3007b782e0a6SEric Paris 	u32 perms;
3008b782e0a6SEric Paris 	bool from_access;
30095298d0b9SStephen Smalley 	bool no_block = mask & MAY_NOT_BLOCK;
30102e334057SEric Paris 	struct inode_security_struct *isec;
30112e334057SEric Paris 	u32 sid;
30122e334057SEric Paris 	struct av_decision avd;
30132e334057SEric Paris 	int rc, rc2;
30142e334057SEric Paris 	u32 audited, denied;
30151da177e4SLinus Torvalds 
3016b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3017d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3018d09ca739SEric Paris 
30191da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3020b782e0a6SEric Paris 	if (!mask)
30211da177e4SLinus Torvalds 		return 0;
30221da177e4SLinus Torvalds 
30232e334057SEric Paris 	validate_creds(cred);
3024b782e0a6SEric Paris 
30252e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
30262e334057SEric Paris 		return 0;
3027b782e0a6SEric Paris 
3028b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3029b782e0a6SEric Paris 
30302e334057SEric Paris 	sid = cred_sid(cred);
30315298d0b9SStephen Smalley 	isec = inode_security_rcu(inode, no_block);
30325d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
30335d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
30342e334057SEric Paris 
30356b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
30363a28cff3SStephen Smalley 				  sid, isec->sid, isec->sclass, perms,
30375298d0b9SStephen Smalley 				  no_block ? AVC_NONBLOCKING : 0,
30383a28cff3SStephen Smalley 				  &avd);
30392e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
30402e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
30412e334057SEric Paris 				     &denied);
30422e334057SEric Paris 	if (likely(!audited))
30432e334057SEric Paris 		return rc;
30442e334057SEric Paris 
30450188d5c0SStephen Smalley 	/* fall back to ref-walk if we have to generate audit */
30465298d0b9SStephen Smalley 	if (no_block)
30470188d5c0SStephen Smalley 		return -ECHILD;
30480188d5c0SStephen Smalley 
30490188d5c0SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
30502e334057SEric Paris 	if (rc2)
30512e334057SEric Paris 		return rc2;
30522e334057SEric Paris 	return rc;
30531da177e4SLinus Torvalds }
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
30561da177e4SLinus Torvalds {
305788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3058ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3059bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
306095dbf739SEric Paris 	__u32 av = FILE__WRITE;
30611da177e4SLinus Torvalds 
3062bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3063bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3064bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3065bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3066bc6a6008SAmerigo Wang 		if (!ia_valid)
30671da177e4SLinus Torvalds 			return 0;
3068bc6a6008SAmerigo Wang 	}
30691da177e4SLinus Torvalds 
3070bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3071bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
30722875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
30731da177e4SLinus Torvalds 
3074aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3075ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3076ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3077ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
307895dbf739SEric Paris 		av |= FILE__OPEN;
307995dbf739SEric Paris 
308095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
30811da177e4SLinus Torvalds }
30821da177e4SLinus Torvalds 
30833f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
30841da177e4SLinus Torvalds {
30853f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
30861da177e4SLinus Torvalds }
30871da177e4SLinus Torvalds 
3088db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3089db59000aSStephen Smalley {
3090db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3091c1a85a00SMicah Morton 	unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3092db59000aSStephen Smalley 
3093c1a85a00SMicah Morton 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3094db59000aSStephen Smalley 		return false;
3095c1a85a00SMicah Morton 	if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3096db59000aSStephen Smalley 		return false;
3097db59000aSStephen Smalley 	return true;
3098db59000aSStephen Smalley }
3099db59000aSStephen Smalley 
31008f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
31018f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
31021da177e4SLinus Torvalds {
3103c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
310420cdef8dSPaul Moore 	struct inode_security_struct *isec;
31051da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
31062bf49690SThomas Liu 	struct common_audit_data ad;
3107275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
31081da177e4SLinus Torvalds 	int rc = 0;
31091da177e4SLinus Torvalds 
31106b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
31116b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
31126b240306SEric W. Biederman 		if (rc)
31136b240306SEric W. Biederman 			return rc;
31146b240306SEric W. Biederman 
31156b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
31166b240306SEric W. Biederman 		   ordinary setattr permission. */
31176b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
31186b240306SEric W. Biederman 	}
31191da177e4SLinus Torvalds 
312065cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state))
31213e3e24b4SJonathan Lebon 		return (inode_owner_or_capable(inode) ? 0 : -EPERM);
31223e3e24b4SJonathan Lebon 
31231da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
312412f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
31251da177e4SLinus Torvalds 		return -EOPNOTSUPP;
31261da177e4SLinus Torvalds 
31272e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
31281da177e4SLinus Torvalds 		return -EPERM;
31291da177e4SLinus Torvalds 
313050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3131a269434dSEric Paris 	ad.u.dentry = dentry;
31321da177e4SLinus Torvalds 
313320cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
31346b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
31356b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
31361da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
31371da177e4SLinus Torvalds 	if (rc)
31381da177e4SLinus Torvalds 		return rc;
31391da177e4SLinus Torvalds 
3140aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3141aa8e712cSStephen Smalley 				     GFP_KERNEL);
314212b29f34SStephen Smalley 	if (rc == -EINVAL) {
3143db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3144d6ea83ecSEric Paris 			struct audit_buffer *ab;
3145d6ea83ecSEric Paris 			size_t audit_size;
3146d6ea83ecSEric Paris 
3147d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3148d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3149e3fea3f7SAl Viro 			if (value) {
3150add24372SColin Ian King 				const char *str = value;
3151add24372SColin Ian King 
3152d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3153d6ea83ecSEric Paris 					audit_size = size - 1;
3154d6ea83ecSEric Paris 				else
3155d6ea83ecSEric Paris 					audit_size = size;
3156e3fea3f7SAl Viro 			} else {
3157e3fea3f7SAl Viro 				audit_size = 0;
3158e3fea3f7SAl Viro 			}
3159cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3160cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3161d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3162d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3163d6ea83ecSEric Paris 			audit_log_end(ab);
3164d6ea83ecSEric Paris 
316512b29f34SStephen Smalley 			return rc;
3166d6ea83ecSEric Paris 		}
3167aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3168aa8e712cSStephen Smalley 						   size, &newsid);
316912b29f34SStephen Smalley 	}
31701da177e4SLinus Torvalds 	if (rc)
31711da177e4SLinus Torvalds 		return rc;
31721da177e4SLinus Torvalds 
31736b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
31746b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
31751da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
31761da177e4SLinus Torvalds 	if (rc)
31771da177e4SLinus Torvalds 		return rc;
31781da177e4SLinus Torvalds 
3179aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3180aa8e712cSStephen Smalley 					  sid, isec->sclass);
31811da177e4SLinus Torvalds 	if (rc)
31821da177e4SLinus Torvalds 		return rc;
31831da177e4SLinus Torvalds 
31846b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
31856b6bc620SStephen Smalley 			    newsid,
31861da177e4SLinus Torvalds 			    sbsec->sid,
31871da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
31881da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
31891da177e4SLinus Torvalds 			    &ad);
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
31928f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
31938f0cfa52SDavid Howells 					const void *value, size_t size,
31948f0cfa52SDavid Howells 					int flags)
31951da177e4SLinus Torvalds {
3196c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
319720cdef8dSPaul Moore 	struct inode_security_struct *isec;
31981da177e4SLinus Torvalds 	u32 newsid;
31991da177e4SLinus Torvalds 	int rc;
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32021da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
32031da177e4SLinus Torvalds 		return;
32041da177e4SLinus Torvalds 	}
32051da177e4SLinus Torvalds 
320665cddd50SOndrej Mosnacek 	if (!selinux_initialized(&selinux_state)) {
32073e3e24b4SJonathan Lebon 		/* If we haven't even been initialized, then we can't validate
32083e3e24b4SJonathan Lebon 		 * against a policy, so leave the label as invalid. It may
32093e3e24b4SJonathan Lebon 		 * resolve to a valid label on the next revalidation try if
32103e3e24b4SJonathan Lebon 		 * we've since initialized.
32113e3e24b4SJonathan Lebon 		 */
32123e3e24b4SJonathan Lebon 		return;
32133e3e24b4SJonathan Lebon 	}
32143e3e24b4SJonathan Lebon 
3215aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3216aa8e712cSStephen Smalley 					   &newsid);
32171da177e4SLinus Torvalds 	if (rc) {
3218c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
321912b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
322012b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
32211da177e4SLinus Torvalds 		return;
32221da177e4SLinus Torvalds 	}
32231da177e4SLinus Torvalds 
322420cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
32259287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3226aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
32271da177e4SLinus Torvalds 	isec->sid = newsid;
32286f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
32299287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3230aa9c2669SDavid Quigley 
32311da177e4SLinus Torvalds 	return;
32321da177e4SLinus Torvalds }
32331da177e4SLinus Torvalds 
32348f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
32351da177e4SLinus Torvalds {
323688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
323788e67f3bSDavid Howells 
32382875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
32421da177e4SLinus Torvalds {
324388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
324488e67f3bSDavid Howells 
32452875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
32461da177e4SLinus Torvalds }
32471da177e4SLinus Torvalds 
32488f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
32491da177e4SLinus Torvalds {
32506b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32516b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
32526b240306SEric W. Biederman 		if (rc)
32536b240306SEric W. Biederman 			return rc;
32546b240306SEric W. Biederman 
32556b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
32566b240306SEric W. Biederman 		   ordinary setattr permission. */
32576b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32586b240306SEric W. Biederman 	}
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
32611da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
32621da177e4SLinus Torvalds 	return -EACCES;
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
3265ac5656d8SAaron Goidel static int selinux_path_notify(const struct path *path, u64 mask,
3266ac5656d8SAaron Goidel 						unsigned int obj_type)
3267ac5656d8SAaron Goidel {
3268ac5656d8SAaron Goidel 	int ret;
3269ac5656d8SAaron Goidel 	u32 perm;
3270ac5656d8SAaron Goidel 
3271ac5656d8SAaron Goidel 	struct common_audit_data ad;
3272ac5656d8SAaron Goidel 
3273ac5656d8SAaron Goidel 	ad.type = LSM_AUDIT_DATA_PATH;
3274ac5656d8SAaron Goidel 	ad.u.path = *path;
3275ac5656d8SAaron Goidel 
3276ac5656d8SAaron Goidel 	/*
3277ac5656d8SAaron Goidel 	 * Set permission needed based on the type of mark being set.
3278ac5656d8SAaron Goidel 	 * Performs an additional check for sb watches.
3279ac5656d8SAaron Goidel 	 */
3280ac5656d8SAaron Goidel 	switch (obj_type) {
3281ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3282ac5656d8SAaron Goidel 		perm = FILE__WATCH_MOUNT;
3283ac5656d8SAaron Goidel 		break;
3284ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_SB:
3285ac5656d8SAaron Goidel 		perm = FILE__WATCH_SB;
3286ac5656d8SAaron Goidel 		ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3287ac5656d8SAaron Goidel 						FILESYSTEM__WATCH, &ad);
3288ac5656d8SAaron Goidel 		if (ret)
3289ac5656d8SAaron Goidel 			return ret;
3290ac5656d8SAaron Goidel 		break;
3291ac5656d8SAaron Goidel 	case FSNOTIFY_OBJ_TYPE_INODE:
3292ac5656d8SAaron Goidel 		perm = FILE__WATCH;
3293ac5656d8SAaron Goidel 		break;
3294ac5656d8SAaron Goidel 	default:
3295ac5656d8SAaron Goidel 		return -EINVAL;
3296ac5656d8SAaron Goidel 	}
3297ac5656d8SAaron Goidel 
3298ac5656d8SAaron Goidel 	/* blocking watches require the file:watch_with_perm permission */
3299ac5656d8SAaron Goidel 	if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3300ac5656d8SAaron Goidel 		perm |= FILE__WATCH_WITH_PERM;
3301ac5656d8SAaron Goidel 
3302ac5656d8SAaron Goidel 	/* watches on read-like events need the file:watch_reads permission */
3303ac5656d8SAaron Goidel 	if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3304ac5656d8SAaron Goidel 		perm |= FILE__WATCH_READS;
3305ac5656d8SAaron Goidel 
3306ac5656d8SAaron Goidel 	return path_has_perm(current_cred(), path, perm);
3307ac5656d8SAaron Goidel }
3308ac5656d8SAaron Goidel 
3309d381d8a9SJames Morris /*
3310abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3311d381d8a9SJames Morris  *
3312d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3313d381d8a9SJames Morris  */
3314ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
33151da177e4SLinus Torvalds {
331642492594SDavid P. Quigley 	u32 size;
331742492594SDavid P. Quigley 	int error;
331842492594SDavid P. Quigley 	char *context = NULL;
331920cdef8dSPaul Moore 	struct inode_security_struct *isec;
33201da177e4SLinus Torvalds 
33218c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
33228c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
33231da177e4SLinus Torvalds 
3324abc69bb6SStephen Smalley 	/*
3325abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3326abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3327abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3328abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3329abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3330abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3331abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3332abc69bb6SStephen Smalley 	 */
333320cdef8dSPaul Moore 	isec = inode_security(inode);
3334db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3335aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3336aa8e712cSStephen Smalley 						      isec->sid, &context,
3337abc69bb6SStephen Smalley 						      &size);
3338abc69bb6SStephen Smalley 	else
3339aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3340aa8e712cSStephen Smalley 						&context, &size);
334142492594SDavid P. Quigley 	if (error)
334242492594SDavid P. Quigley 		return error;
334342492594SDavid P. Quigley 	error = size;
334442492594SDavid P. Quigley 	if (alloc) {
334542492594SDavid P. Quigley 		*buffer = context;
334642492594SDavid P. Quigley 		goto out_nofree;
334742492594SDavid P. Quigley 	}
334842492594SDavid P. Quigley 	kfree(context);
334942492594SDavid P. Quigley out_nofree:
335042492594SDavid P. Quigley 	return error;
33511da177e4SLinus Torvalds }
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
33541da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
33551da177e4SLinus Torvalds {
33562c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
335753e0c2aaSOndrej Mosnacek 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
33581da177e4SLinus Torvalds 	u32 newsid;
33591da177e4SLinus Torvalds 	int rc;
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
33621da177e4SLinus Torvalds 		return -EOPNOTSUPP;
33631da177e4SLinus Torvalds 
336453e0c2aaSOndrej Mosnacek 	if (!(sbsec->flags & SBLABEL_MNT))
336553e0c2aaSOndrej Mosnacek 		return -EOPNOTSUPP;
336653e0c2aaSOndrej Mosnacek 
33671da177e4SLinus Torvalds 	if (!value || !size)
33681da177e4SLinus Torvalds 		return -EACCES;
33691da177e4SLinus Torvalds 
3370aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3371aa8e712cSStephen Smalley 				     GFP_KERNEL);
33721da177e4SLinus Torvalds 	if (rc)
33731da177e4SLinus Torvalds 		return rc;
33741da177e4SLinus Torvalds 
33759287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3376aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
33771da177e4SLinus Torvalds 	isec->sid = newsid;
33786f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
33799287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
33801da177e4SLinus Torvalds 	return 0;
33811da177e4SLinus Torvalds }
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
33841da177e4SLinus Torvalds {
33851da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
33861da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
33871da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
33881da177e4SLinus Torvalds 	return len;
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
3391d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3392713a04aeSAhmed S. Darwish {
3393e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3394713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3395713a04aeSAhmed S. Darwish }
3396713a04aeSAhmed S. Darwish 
339756909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
339856909eb3SVivek Goyal {
339956909eb3SVivek Goyal 	u32 sid;
340056909eb3SVivek Goyal 	struct task_security_struct *tsec;
340156909eb3SVivek Goyal 	struct cred *new_creds = *new;
340256909eb3SVivek Goyal 
340356909eb3SVivek Goyal 	if (new_creds == NULL) {
340456909eb3SVivek Goyal 		new_creds = prepare_creds();
340556909eb3SVivek Goyal 		if (!new_creds)
340656909eb3SVivek Goyal 			return -ENOMEM;
340756909eb3SVivek Goyal 	}
340856909eb3SVivek Goyal 
34090c6cfa62SCasey Schaufler 	tsec = selinux_cred(new_creds);
341056909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
341156909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
341256909eb3SVivek Goyal 	tsec->create_sid = sid;
341356909eb3SVivek Goyal 	*new = new_creds;
341456909eb3SVivek Goyal 	return 0;
341556909eb3SVivek Goyal }
341656909eb3SVivek Goyal 
341719472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
341819472b69SVivek Goyal {
341919472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
342019472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
342119472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
342219472b69SVivek Goyal 	 */
342319472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
342419472b69SVivek Goyal 		return 1; /* Discard */
342519472b69SVivek Goyal 	/*
342619472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
342719472b69SVivek Goyal 	 * by selinux.
342819472b69SVivek Goyal 	 */
342919472b69SVivek Goyal 	return -EOPNOTSUPP;
343019472b69SVivek Goyal }
343119472b69SVivek Goyal 
3432ec882da5SOndrej Mosnacek /* kernfs node operations */
3433ec882da5SOndrej Mosnacek 
3434c72c4cdeSYueHaibing static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3435ec882da5SOndrej Mosnacek 					struct kernfs_node *kn)
3436ec882da5SOndrej Mosnacek {
3437169ce0c0SStephen Smalley 	const struct task_security_struct *tsec = selinux_cred(current_cred());
3438ec882da5SOndrej Mosnacek 	u32 parent_sid, newsid, clen;
3439ec882da5SOndrej Mosnacek 	int rc;
3440ec882da5SOndrej Mosnacek 	char *context;
3441ec882da5SOndrej Mosnacek 
34421537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3443ec882da5SOndrej Mosnacek 	if (rc == -ENODATA)
3444ec882da5SOndrej Mosnacek 		return 0;
3445ec882da5SOndrej Mosnacek 	else if (rc < 0)
3446ec882da5SOndrej Mosnacek 		return rc;
3447ec882da5SOndrej Mosnacek 
3448ec882da5SOndrej Mosnacek 	clen = (u32)rc;
3449ec882da5SOndrej Mosnacek 	context = kmalloc(clen, GFP_KERNEL);
3450ec882da5SOndrej Mosnacek 	if (!context)
3451ec882da5SOndrej Mosnacek 		return -ENOMEM;
3452ec882da5SOndrej Mosnacek 
34531537ad15SOndrej Mosnacek 	rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3454ec882da5SOndrej Mosnacek 	if (rc < 0) {
3455ec882da5SOndrej Mosnacek 		kfree(context);
3456ec882da5SOndrej Mosnacek 		return rc;
3457ec882da5SOndrej Mosnacek 	}
3458ec882da5SOndrej Mosnacek 
3459ec882da5SOndrej Mosnacek 	rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3460ec882da5SOndrej Mosnacek 				     GFP_KERNEL);
3461ec882da5SOndrej Mosnacek 	kfree(context);
3462ec882da5SOndrej Mosnacek 	if (rc)
3463ec882da5SOndrej Mosnacek 		return rc;
3464ec882da5SOndrej Mosnacek 
3465ec882da5SOndrej Mosnacek 	if (tsec->create_sid) {
3466ec882da5SOndrej Mosnacek 		newsid = tsec->create_sid;
3467ec882da5SOndrej Mosnacek 	} else {
3468ec882da5SOndrej Mosnacek 		u16 secclass = inode_mode_to_security_class(kn->mode);
3469ec882da5SOndrej Mosnacek 		struct qstr q;
3470ec882da5SOndrej Mosnacek 
3471ec882da5SOndrej Mosnacek 		q.name = kn->name;
3472ec882da5SOndrej Mosnacek 		q.hash_len = hashlen_string(kn_dir, kn->name);
3473ec882da5SOndrej Mosnacek 
3474ec882da5SOndrej Mosnacek 		rc = security_transition_sid(&selinux_state, tsec->sid,
3475ec882da5SOndrej Mosnacek 					     parent_sid, secclass, &q,
3476ec882da5SOndrej Mosnacek 					     &newsid);
3477ec882da5SOndrej Mosnacek 		if (rc)
3478ec882da5SOndrej Mosnacek 			return rc;
3479ec882da5SOndrej Mosnacek 	}
3480ec882da5SOndrej Mosnacek 
3481ec882da5SOndrej Mosnacek 	rc = security_sid_to_context_force(&selinux_state, newsid,
3482ec882da5SOndrej Mosnacek 					   &context, &clen);
3483ec882da5SOndrej Mosnacek 	if (rc)
3484ec882da5SOndrej Mosnacek 		return rc;
3485ec882da5SOndrej Mosnacek 
34861537ad15SOndrej Mosnacek 	rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3487ec882da5SOndrej Mosnacek 			      XATTR_CREATE);
3488ec882da5SOndrej Mosnacek 	kfree(context);
3489ec882da5SOndrej Mosnacek 	return rc;
3490ec882da5SOndrej Mosnacek }
3491ec882da5SOndrej Mosnacek 
3492ec882da5SOndrej Mosnacek 
34931da177e4SLinus Torvalds /* file security operations */
34941da177e4SLinus Torvalds 
3495788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
34961da177e4SLinus Torvalds {
349788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3498496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
34991da177e4SLinus Torvalds 
35001da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
35011da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
35021da177e4SLinus Torvalds 		mask |= MAY_APPEND;
35031da177e4SLinus Torvalds 
3504389fb800SPaul Moore 	return file_has_perm(cred, file,
35051da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
3508788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3509788e7dd4SYuichi Nakamura {
3510496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3511bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3512b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
351320dda18bSStephen Smalley 	u32 sid = current_sid();
351420dda18bSStephen Smalley 
3515389fb800SPaul Moore 	if (!mask)
3516788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3517788e7dd4SYuichi Nakamura 		return 0;
3518788e7dd4SYuichi Nakamura 
3519b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
352020dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
35216b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
352283d49856SEric Paris 		/* No change since file_open check. */
352320dda18bSStephen Smalley 		return 0;
352420dda18bSStephen Smalley 
3525788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3526788e7dd4SYuichi Nakamura }
3527788e7dd4SYuichi Nakamura 
35281da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
35291da177e4SLinus Torvalds {
3530*cb89e246SPaul Moore 	struct file_security_struct *fsec = selinux_file(file);
3531*cb89e246SPaul Moore 	u32 sid = current_sid();
3532*cb89e246SPaul Moore 
3533*cb89e246SPaul Moore 	fsec->sid = sid;
3534*cb89e246SPaul Moore 	fsec->fown_sid = sid;
3535*cb89e246SPaul Moore 
3536*cb89e246SPaul Moore 	return 0;
35371da177e4SLinus Torvalds }
35381da177e4SLinus Torvalds 
3539fa1aa143SJeff Vander Stoep /*
3540fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3541fa1aa143SJeff Vander Stoep  * operation to an inode.
3542fa1aa143SJeff Vander Stoep  */
35431d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3544fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3545fa1aa143SJeff Vander Stoep {
3546fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3547bb6c6b02SCasey Schaufler 	struct file_security_struct *fsec = selinux_file(file);
3548fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
354920cdef8dSPaul Moore 	struct inode_security_struct *isec;
3550fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3551fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3552fa1aa143SJeff Vander Stoep 	int rc;
3553fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3554fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3555fa1aa143SJeff Vander Stoep 
3556fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3557fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3558fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3559fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3560fa1aa143SJeff Vander Stoep 
3561fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
35626b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
35636b6bc620SStephen Smalley 				  ssid, fsec->sid,
3564fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3565fa1aa143SJeff Vander Stoep 				FD__USE,
3566fa1aa143SJeff Vander Stoep 				&ad);
3567fa1aa143SJeff Vander Stoep 		if (rc)
3568fa1aa143SJeff Vander Stoep 			goto out;
3569fa1aa143SJeff Vander Stoep 	}
3570fa1aa143SJeff Vander Stoep 
3571fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3572fa1aa143SJeff Vander Stoep 		return 0;
3573fa1aa143SJeff Vander Stoep 
357420cdef8dSPaul Moore 	isec = inode_security(inode);
35756b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
35766b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3577fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3578fa1aa143SJeff Vander Stoep out:
3579fa1aa143SJeff Vander Stoep 	return rc;
3580fa1aa143SJeff Vander Stoep }
3581fa1aa143SJeff Vander Stoep 
35821da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
35831da177e4SLinus Torvalds 			      unsigned long arg)
35841da177e4SLinus Torvalds {
358588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
35860b24dcb7SEric Paris 	int error = 0;
35871da177e4SLinus Torvalds 
35880b24dcb7SEric Paris 	switch (cmd) {
35890b24dcb7SEric Paris 	case FIONREAD:
35900b24dcb7SEric Paris 	/* fall through */
35910b24dcb7SEric Paris 	case FIBMAP:
35920b24dcb7SEric Paris 	/* fall through */
35930b24dcb7SEric Paris 	case FIGETBSZ:
35940b24dcb7SEric Paris 	/* fall through */
35952f99c369SAl Viro 	case FS_IOC_GETFLAGS:
35960b24dcb7SEric Paris 	/* fall through */
35972f99c369SAl Viro 	case FS_IOC_GETVERSION:
35980b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
35990b24dcb7SEric Paris 		break;
36001da177e4SLinus Torvalds 
36012f99c369SAl Viro 	case FS_IOC_SETFLAGS:
36020b24dcb7SEric Paris 	/* fall through */
36032f99c369SAl Viro 	case FS_IOC_SETVERSION:
36040b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
36050b24dcb7SEric Paris 		break;
36060b24dcb7SEric Paris 
36070b24dcb7SEric Paris 	/* sys_ioctl() checks */
36080b24dcb7SEric Paris 	case FIONBIO:
36090b24dcb7SEric Paris 	/* fall through */
36100b24dcb7SEric Paris 	case FIOASYNC:
36110b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
36120b24dcb7SEric Paris 		break;
36130b24dcb7SEric Paris 
36140b24dcb7SEric Paris 	case KDSKBENT:
36150b24dcb7SEric Paris 	case KDSKBSENT:
36166a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3617c1a85a00SMicah Morton 					    CAP_OPT_NONE, true);
36180b24dcb7SEric Paris 		break;
36190b24dcb7SEric Paris 
36200b24dcb7SEric Paris 	/* default case assumes that the command will go
36210b24dcb7SEric Paris 	 * to the file's ioctl() function.
36220b24dcb7SEric Paris 	 */
36230b24dcb7SEric Paris 	default:
3624fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
36250b24dcb7SEric Paris 	}
36260b24dcb7SEric Paris 	return error;
36271da177e4SLinus Torvalds }
36281da177e4SLinus Torvalds 
3629b78b7d59SStephen Smalley static int default_noexec __ro_after_init;
3630fcaaade1SStephen Smalley 
36311da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
36321da177e4SLinus Torvalds {
363388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3634be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3635d84f4f99SDavid Howells 	int rc = 0;
363688e67f3bSDavid Howells 
3637fcaaade1SStephen Smalley 	if (default_noexec &&
3638892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3639892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
36401da177e4SLinus Torvalds 		/*
36411da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
36421da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
36431da177e4SLinus Torvalds 		 * This has an additional check.
36441da177e4SLinus Torvalds 		 */
36456b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36466b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3647be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
36481da177e4SLinus Torvalds 		if (rc)
3649d84f4f99SDavid Howells 			goto error;
36501da177e4SLinus Torvalds 	}
36511da177e4SLinus Torvalds 
36521da177e4SLinus Torvalds 	if (file) {
36531da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
36541da177e4SLinus Torvalds 		u32 av = FILE__READ;
36551da177e4SLinus Torvalds 
36561da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
36571da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
36581da177e4SLinus Torvalds 			av |= FILE__WRITE;
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
36611da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
36621da177e4SLinus Torvalds 
366388e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
36641da177e4SLinus Torvalds 	}
3665d84f4f99SDavid Howells 
3666d84f4f99SDavid Howells error:
3667d84f4f99SDavid Howells 	return rc;
36681da177e4SLinus Torvalds }
36691da177e4SLinus Torvalds 
3670e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
36711da177e4SLinus Torvalds {
3672b1d9e6b0SCasey Schaufler 	int rc = 0;
367398883bfdSPaul Moore 
367498883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
367598883bfdSPaul Moore 		u32 sid = current_sid();
36766b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36776b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
367898883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
367998883bfdSPaul Moore 	}
368098883bfdSPaul Moore 
368198883bfdSPaul Moore 	return rc;
3682e5467859SAl Viro }
36831da177e4SLinus Torvalds 
3684e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3685e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3686e5467859SAl Viro {
36873ba4bf5fSStephen Smalley 	struct common_audit_data ad;
36883ba4bf5fSStephen Smalley 	int rc;
36893ba4bf5fSStephen Smalley 
36903ba4bf5fSStephen Smalley 	if (file) {
36913ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
36923ba4bf5fSStephen Smalley 		ad.u.file = file;
36933ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
36943ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
36953ba4bf5fSStephen Smalley 		if (rc)
36963ba4bf5fSStephen Smalley 			return rc;
36973ba4bf5fSStephen Smalley 	}
36983ba4bf5fSStephen Smalley 
3699aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37001da177e4SLinus Torvalds 		prot = reqprot;
37011da177e4SLinus Torvalds 
37021da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
37031da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
37041da177e4SLinus Torvalds }
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
37071da177e4SLinus Torvalds 				 unsigned long reqprot,
37081da177e4SLinus Torvalds 				 unsigned long prot)
37091da177e4SLinus Torvalds {
371088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3711be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
37121da177e4SLinus Torvalds 
3713aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37141da177e4SLinus Torvalds 		prot = reqprot;
37151da177e4SLinus Torvalds 
3716fcaaade1SStephen Smalley 	if (default_noexec &&
3717fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3718d541bbeeSJames Morris 		int rc = 0;
3719db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3720db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
37216b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37226b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3723be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3724db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3725c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3726c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3727d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
37286b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37296b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3730be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3731db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3732db4c9641SStephen Smalley 			/*
3733db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3734db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3735db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3736db4c9641SStephen Smalley 			 * modified content.  This typically should only
3737db4c9641SStephen Smalley 			 * occur for text relocations.
3738db4c9641SStephen Smalley 			 */
3739d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3740db4c9641SStephen Smalley 		}
37416b992197SLorenzo Hernandez García-Hierro 		if (rc)
37426b992197SLorenzo Hernandez García-Hierro 			return rc;
37436b992197SLorenzo Hernandez García-Hierro 	}
37441da177e4SLinus Torvalds 
37451da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
37461da177e4SLinus Torvalds }
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
37491da177e4SLinus Torvalds {
375088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
375188e67f3bSDavid Howells 
375288e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
37531da177e4SLinus Torvalds }
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
37561da177e4SLinus Torvalds 			      unsigned long arg)
37571da177e4SLinus Torvalds {
375888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
37591da177e4SLinus Torvalds 	int err = 0;
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds 	switch (cmd) {
37621da177e4SLinus Torvalds 	case F_SETFL:
37631da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
376488e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
37651da177e4SLinus Torvalds 			break;
37661da177e4SLinus Torvalds 		}
37671da177e4SLinus Torvalds 		/* fall through */
37681da177e4SLinus Torvalds 	case F_SETOWN:
37691da177e4SLinus Torvalds 	case F_SETSIG:
37701da177e4SLinus Torvalds 	case F_GETFL:
37711da177e4SLinus Torvalds 	case F_GETOWN:
37721da177e4SLinus Torvalds 	case F_GETSIG:
37731d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
37741da177e4SLinus Torvalds 		/* Just check FD__USE permission */
377588e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
37761da177e4SLinus Torvalds 		break;
37771da177e4SLinus Torvalds 	case F_GETLK:
37781da177e4SLinus Torvalds 	case F_SETLK:
37791da177e4SLinus Torvalds 	case F_SETLKW:
37800d3f7a2dSJeff Layton 	case F_OFD_GETLK:
37810d3f7a2dSJeff Layton 	case F_OFD_SETLK:
37820d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
37831da177e4SLinus Torvalds #if BITS_PER_LONG == 32
37841da177e4SLinus Torvalds 	case F_GETLK64:
37851da177e4SLinus Torvalds 	case F_SETLK64:
37861da177e4SLinus Torvalds 	case F_SETLKW64:
37871da177e4SLinus Torvalds #endif
378888e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
37891da177e4SLinus Torvalds 		break;
37901da177e4SLinus Torvalds 	}
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	return err;
37931da177e4SLinus Torvalds }
37941da177e4SLinus Torvalds 
3795e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
37961da177e4SLinus Torvalds {
37971da177e4SLinus Torvalds 	struct file_security_struct *fsec;
37981da177e4SLinus Torvalds 
3799bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
3800275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
38011da177e4SLinus Torvalds }
38021da177e4SLinus Torvalds 
38031da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
38041da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
38051da177e4SLinus Torvalds {
38061da177e4SLinus Torvalds 	struct file *file;
380765c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
38081da177e4SLinus Torvalds 	u32 perm;
38091da177e4SLinus Torvalds 	struct file_security_struct *fsec;
38101da177e4SLinus Torvalds 
38111da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3812b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
38131da177e4SLinus Torvalds 
3814bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds 	if (!signum)
38171da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
38181da177e4SLinus Torvalds 	else
38191da177e4SLinus Torvalds 		perm = signal_to_av(signum);
38201da177e4SLinus Torvalds 
38216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38226b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
38231da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
38241da177e4SLinus Torvalds }
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
38271da177e4SLinus Torvalds {
382888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
382988e67f3bSDavid Howells 
383088e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
38311da177e4SLinus Torvalds }
38321da177e4SLinus Torvalds 
383394817692SAl Viro static int selinux_file_open(struct file *file)
3834788e7dd4SYuichi Nakamura {
3835788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3836788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3837d84f4f99SDavid Howells 
3838bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
383983da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3840788e7dd4SYuichi Nakamura 	/*
3841788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3842788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3843788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3844788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3845788e7dd4SYuichi Nakamura 	 * struct as its SID.
3846788e7dd4SYuichi Nakamura 	 */
3847788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
38486b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3849788e7dd4SYuichi Nakamura 	/*
3850788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3851788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3852788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3853788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3854788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3855788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3856788e7dd4SYuichi Nakamura 	 */
385794817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3858788e7dd4SYuichi Nakamura }
3859788e7dd4SYuichi Nakamura 
38601da177e4SLinus Torvalds /* task security operations */
38611da177e4SLinus Torvalds 
3862a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3863a79be238STetsuo Handa 			      unsigned long clone_flags)
38641da177e4SLinus Torvalds {
3865be0554c9SStephen Smalley 	u32 sid = current_sid();
3866be0554c9SStephen Smalley 
38676b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38686b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
38691da177e4SLinus Torvalds }
38701da177e4SLinus Torvalds 
3871f1752eecSDavid Howells /*
3872d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3873d84f4f99SDavid Howells  */
3874d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3875d84f4f99SDavid Howells 				gfp_t gfp)
3876d84f4f99SDavid Howells {
3877bbd3662aSCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
3878bbd3662aSCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
3879d84f4f99SDavid Howells 
3880bbd3662aSCasey Schaufler 	*tsec = *old_tsec;
3881d84f4f99SDavid Howells 	return 0;
3882d84f4f99SDavid Howells }
3883d84f4f99SDavid Howells 
3884d84f4f99SDavid Howells /*
3885ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3886ee18d64cSDavid Howells  */
3887ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3888ee18d64cSDavid Howells {
38890c6cfa62SCasey Schaufler 	const struct task_security_struct *old_tsec = selinux_cred(old);
38900c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
3891ee18d64cSDavid Howells 
3892ee18d64cSDavid Howells 	*tsec = *old_tsec;
3893ee18d64cSDavid Howells }
3894ee18d64cSDavid Howells 
38953ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
38963ec30113SMatthew Garrett {
38973ec30113SMatthew Garrett 	*secid = cred_sid(c);
38983ec30113SMatthew Garrett }
38993ec30113SMatthew Garrett 
3900ee18d64cSDavid Howells /*
39013a3b7ce9SDavid Howells  * set the security data for a kernel service
39023a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
39033a3b7ce9SDavid Howells  */
39043a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
39053a3b7ce9SDavid Howells {
39060c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
39073a3b7ce9SDavid Howells 	u32 sid = current_sid();
39083a3b7ce9SDavid Howells 	int ret;
39093a3b7ce9SDavid Howells 
39106b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
39116b6bc620SStephen Smalley 			   sid, secid,
39123a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
39133a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
39143a3b7ce9SDavid Howells 			   NULL);
39153a3b7ce9SDavid Howells 	if (ret == 0) {
39163a3b7ce9SDavid Howells 		tsec->sid = secid;
39173a3b7ce9SDavid Howells 		tsec->create_sid = 0;
39183a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
39193a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
39203a3b7ce9SDavid Howells 	}
39213a3b7ce9SDavid Howells 	return ret;
39223a3b7ce9SDavid Howells }
39233a3b7ce9SDavid Howells 
39243a3b7ce9SDavid Howells /*
39253a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
39263a3b7ce9SDavid Howells  * objective context of the specified inode
39273a3b7ce9SDavid Howells  */
39283a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
39293a3b7ce9SDavid Howells {
393083da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
39310c6cfa62SCasey Schaufler 	struct task_security_struct *tsec = selinux_cred(new);
39323a3b7ce9SDavid Howells 	u32 sid = current_sid();
39333a3b7ce9SDavid Howells 	int ret;
39343a3b7ce9SDavid Howells 
39356b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
39366b6bc620SStephen Smalley 			   sid, isec->sid,
39373a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
39383a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
39393a3b7ce9SDavid Howells 			   NULL);
39403a3b7ce9SDavid Howells 
39413a3b7ce9SDavid Howells 	if (ret == 0)
39423a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3943ef57471aSDavid Howells 	return ret;
39443a3b7ce9SDavid Howells }
39453a3b7ce9SDavid Howells 
3946dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
394725354c4fSEric Paris {
3948dd8dbf2eSEric Paris 	struct common_audit_data ad;
3949dd8dbf2eSEric Paris 
395050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3951dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3952dd8dbf2eSEric Paris 
39536b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39546b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3955dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
395625354c4fSEric Paris }
395725354c4fSEric Paris 
395861d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
395961d612eaSJeff Vander Stoep {
396061d612eaSJeff Vander Stoep 	struct common_audit_data ad;
396161d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
396261d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
396361d612eaSJeff Vander Stoep 	u32 sid = current_sid();
396461d612eaSJeff Vander Stoep 	int rc;
396561d612eaSJeff Vander Stoep 
396661d612eaSJeff Vander Stoep 	/* init_module */
396761d612eaSJeff Vander Stoep 	if (file == NULL)
39686b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
39696b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
397061d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
397161d612eaSJeff Vander Stoep 
397261d612eaSJeff Vander Stoep 	/* finit_module */
397320cdef8dSPaul Moore 
397443af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
397543af5de7SVivek Goyal 	ad.u.file = file;
397661d612eaSJeff Vander Stoep 
3977bb6c6b02SCasey Schaufler 	fsec = selinux_file(file);
397861d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
39796b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
39806b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
398161d612eaSJeff Vander Stoep 		if (rc)
398261d612eaSJeff Vander Stoep 			return rc;
398361d612eaSJeff Vander Stoep 	}
398461d612eaSJeff Vander Stoep 
398520cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
39866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39876b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
398861d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
398961d612eaSJeff Vander Stoep }
399061d612eaSJeff Vander Stoep 
399161d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
399261d612eaSJeff Vander Stoep 				    enum kernel_read_file_id id)
399361d612eaSJeff Vander Stoep {
399461d612eaSJeff Vander Stoep 	int rc = 0;
399561d612eaSJeff Vander Stoep 
399661d612eaSJeff Vander Stoep 	switch (id) {
399761d612eaSJeff Vander Stoep 	case READING_MODULE:
399861d612eaSJeff Vander Stoep 		rc = selinux_kernel_module_from_file(file);
399961d612eaSJeff Vander Stoep 		break;
400061d612eaSJeff Vander Stoep 	default:
400161d612eaSJeff Vander Stoep 		break;
400261d612eaSJeff Vander Stoep 	}
400361d612eaSJeff Vander Stoep 
400461d612eaSJeff Vander Stoep 	return rc;
400561d612eaSJeff Vander Stoep }
400661d612eaSJeff Vander Stoep 
4007c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id)
4008c77b8cdfSMimi Zohar {
4009c77b8cdfSMimi Zohar 	int rc = 0;
4010c77b8cdfSMimi Zohar 
4011c77b8cdfSMimi Zohar 	switch (id) {
4012c77b8cdfSMimi Zohar 	case LOADING_MODULE:
4013c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
4014c77b8cdfSMimi Zohar 	default:
4015c77b8cdfSMimi Zohar 		break;
4016c77b8cdfSMimi Zohar 	}
4017c77b8cdfSMimi Zohar 
4018c77b8cdfSMimi Zohar 	return rc;
4019c77b8cdfSMimi Zohar }
4020c77b8cdfSMimi Zohar 
40211da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
40221da177e4SLinus Torvalds {
40236b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40246b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4025be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
40261da177e4SLinus Torvalds }
40271da177e4SLinus Torvalds 
40281da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
40291da177e4SLinus Torvalds {
40306b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40316b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4032be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
40331da177e4SLinus Torvalds }
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
40361da177e4SLinus Torvalds {
40376b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40386b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4039be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
40401da177e4SLinus Torvalds }
40411da177e4SLinus Torvalds 
4042f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4043f9008e4cSDavid Quigley {
4044275bb41eSDavid Howells 	*secid = task_sid(p);
4045f9008e4cSDavid Quigley }
4046f9008e4cSDavid Quigley 
40471da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
40481da177e4SLinus Torvalds {
40496b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40506b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4051be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
40521da177e4SLinus Torvalds }
40531da177e4SLinus Torvalds 
405403e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
405503e68060SJames Morris {
40566b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40576b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4058be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
405903e68060SJames Morris }
406003e68060SJames Morris 
4061a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
4062a1836a42SDavid Quigley {
40636b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40646b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4065be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
4066a1836a42SDavid Quigley }
4067a1836a42SDavid Quigley 
40684298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4069791ec491SStephen Smalley 				unsigned int flags)
4070791ec491SStephen Smalley {
4071791ec491SStephen Smalley 	u32 av = 0;
4072791ec491SStephen Smalley 
407384e6885eSStephen Smalley 	if (!flags)
407484e6885eSStephen Smalley 		return 0;
4075791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
4076791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
4077791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
4078791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
40796b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40806b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
4081791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
4082791ec491SStephen Smalley }
4083791ec491SStephen Smalley 
40848fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
40858fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
40861da177e4SLinus Torvalds {
40878fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
40881da177e4SLinus Torvalds 
40891da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
40901da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
40911da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
4092d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
40931da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
40946b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
40956b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
4096be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
40971da177e4SLinus Torvalds 
40981da177e4SLinus Torvalds 	return 0;
40991da177e4SLinus Torvalds }
41001da177e4SLinus Torvalds 
4101b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
41021da177e4SLinus Torvalds {
41036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41046b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4105be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41061da177e4SLinus Torvalds }
41071da177e4SLinus Torvalds 
41081da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
41091da177e4SLinus Torvalds {
41106b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41116b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4112be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
41131da177e4SLinus Torvalds }
41141da177e4SLinus Torvalds 
411535601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
411635601547SDavid Quigley {
41176b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41186b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4119be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
412035601547SDavid Quigley }
412135601547SDavid Quigley 
4122ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
41236b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
41241da177e4SLinus Torvalds {
41256b4f3d01SStephen Smalley 	u32 secid;
41261da177e4SLinus Torvalds 	u32 perm;
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds 	if (!sig)
41291da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
41301da177e4SLinus Torvalds 	else
41311da177e4SLinus Torvalds 		perm = signal_to_av(sig);
41326b4f3d01SStephen Smalley 	if (!cred)
4133be0554c9SStephen Smalley 		secid = current_sid();
41346b4f3d01SStephen Smalley 	else
41356b4f3d01SStephen Smalley 		secid = cred_sid(cred);
41366b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41376b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
41381da177e4SLinus Torvalds }
41391da177e4SLinus Torvalds 
41401da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
41411da177e4SLinus Torvalds 				  struct inode *inode)
41421da177e4SLinus Torvalds {
414380788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
4144275bb41eSDavid Howells 	u32 sid = task_sid(p);
41451da177e4SLinus Torvalds 
41469287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4147db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4148275bb41eSDavid Howells 	isec->sid = sid;
41496f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
41509287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
41511da177e4SLinus Torvalds }
41521da177e4SLinus Torvalds 
41531da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
415467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
41552bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
41561da177e4SLinus Torvalds {
41571da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
41581da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
41591da177e4SLinus Torvalds 
4160bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
41611da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
41621da177e4SLinus Torvalds 	if (ih == NULL)
41631da177e4SLinus Torvalds 		goto out;
41641da177e4SLinus Torvalds 
41651da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
41661da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
41671da177e4SLinus Torvalds 		goto out;
41681da177e4SLinus Torvalds 
416948c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
417048c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
41711da177e4SLinus Torvalds 	ret = 0;
41721da177e4SLinus Torvalds 
417367f83cbfSVenkat Yekkirala 	if (proto)
417467f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
417567f83cbfSVenkat Yekkirala 
41761da177e4SLinus Torvalds 	switch (ih->protocol) {
41771da177e4SLinus Torvalds 	case IPPROTO_TCP: {
41781da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
41811da177e4SLinus Torvalds 			break;
41821da177e4SLinus Torvalds 
41831da177e4SLinus Torvalds 		offset += ihlen;
41841da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
41851da177e4SLinus Torvalds 		if (th == NULL)
41861da177e4SLinus Torvalds 			break;
41871da177e4SLinus Torvalds 
418848c62af6SEric Paris 		ad->u.net->sport = th->source;
418948c62af6SEric Paris 		ad->u.net->dport = th->dest;
41901da177e4SLinus Torvalds 		break;
41911da177e4SLinus Torvalds 	}
41921da177e4SLinus Torvalds 
41931da177e4SLinus Torvalds 	case IPPROTO_UDP: {
41941da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
41951da177e4SLinus Torvalds 
41961da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
41971da177e4SLinus Torvalds 			break;
41981da177e4SLinus Torvalds 
41991da177e4SLinus Torvalds 		offset += ihlen;
42001da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
42011da177e4SLinus Torvalds 		if (uh == NULL)
42021da177e4SLinus Torvalds 			break;
42031da177e4SLinus Torvalds 
420448c62af6SEric Paris 		ad->u.net->sport = uh->source;
420548c62af6SEric Paris 		ad->u.net->dport = uh->dest;
42061da177e4SLinus Torvalds 		break;
42071da177e4SLinus Torvalds 	}
42081da177e4SLinus Torvalds 
42092ee92d46SJames Morris 	case IPPROTO_DCCP: {
42102ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
42112ee92d46SJames Morris 
42122ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
42132ee92d46SJames Morris 			break;
42142ee92d46SJames Morris 
42152ee92d46SJames Morris 		offset += ihlen;
42162ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
42172ee92d46SJames Morris 		if (dh == NULL)
42182ee92d46SJames Morris 			break;
42192ee92d46SJames Morris 
422048c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
422148c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
42222ee92d46SJames Morris 		break;
42232ee92d46SJames Morris 	}
42242ee92d46SJames Morris 
4225d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4226d452930fSRichard Haines 	case IPPROTO_SCTP: {
4227d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4228d452930fSRichard Haines 
4229d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4230d452930fSRichard Haines 			break;
4231d452930fSRichard Haines 
4232d452930fSRichard Haines 		offset += ihlen;
4233d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4234d452930fSRichard Haines 		if (sh == NULL)
4235d452930fSRichard Haines 			break;
4236d452930fSRichard Haines 
4237d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4238d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4239d452930fSRichard Haines 		break;
4240d452930fSRichard Haines 	}
4241d452930fSRichard Haines #endif
42421da177e4SLinus Torvalds 	default:
42431da177e4SLinus Torvalds 		break;
42441da177e4SLinus Torvalds 	}
42451da177e4SLinus Torvalds out:
42461da177e4SLinus Torvalds 	return ret;
42471da177e4SLinus Torvalds }
42481da177e4SLinus Torvalds 
42491a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
42501da177e4SLinus Torvalds 
42511da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
425267f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
42532bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
42541da177e4SLinus Torvalds {
42551da177e4SLinus Torvalds 	u8 nexthdr;
42561da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
42571da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
425875f2811cSJesse Gross 	__be16 frag_off;
42591da177e4SLinus Torvalds 
4260bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
42611da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
42621da177e4SLinus Torvalds 	if (ip6 == NULL)
42631da177e4SLinus Torvalds 		goto out;
42641da177e4SLinus Torvalds 
426548c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
426648c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
42671da177e4SLinus Torvalds 	ret = 0;
42681da177e4SLinus Torvalds 
42691da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
42701da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
427175f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
42721da177e4SLinus Torvalds 	if (offset < 0)
42731da177e4SLinus Torvalds 		goto out;
42741da177e4SLinus Torvalds 
427567f83cbfSVenkat Yekkirala 	if (proto)
427667f83cbfSVenkat Yekkirala 		*proto = nexthdr;
427767f83cbfSVenkat Yekkirala 
42781da177e4SLinus Torvalds 	switch (nexthdr) {
42791da177e4SLinus Torvalds 	case IPPROTO_TCP: {
42801da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
42811da177e4SLinus Torvalds 
42821da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
42831da177e4SLinus Torvalds 		if (th == NULL)
42841da177e4SLinus Torvalds 			break;
42851da177e4SLinus Torvalds 
428648c62af6SEric Paris 		ad->u.net->sport = th->source;
428748c62af6SEric Paris 		ad->u.net->dport = th->dest;
42881da177e4SLinus Torvalds 		break;
42891da177e4SLinus Torvalds 	}
42901da177e4SLinus Torvalds 
42911da177e4SLinus Torvalds 	case IPPROTO_UDP: {
42921da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
42951da177e4SLinus Torvalds 		if (uh == NULL)
42961da177e4SLinus Torvalds 			break;
42971da177e4SLinus Torvalds 
429848c62af6SEric Paris 		ad->u.net->sport = uh->source;
429948c62af6SEric Paris 		ad->u.net->dport = uh->dest;
43001da177e4SLinus Torvalds 		break;
43011da177e4SLinus Torvalds 	}
43021da177e4SLinus Torvalds 
43032ee92d46SJames Morris 	case IPPROTO_DCCP: {
43042ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
43052ee92d46SJames Morris 
43062ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
43072ee92d46SJames Morris 		if (dh == NULL)
43082ee92d46SJames Morris 			break;
43092ee92d46SJames Morris 
431048c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
431148c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
43122ee92d46SJames Morris 		break;
43132ee92d46SJames Morris 	}
43142ee92d46SJames Morris 
4315d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4316d452930fSRichard Haines 	case IPPROTO_SCTP: {
4317d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4318d452930fSRichard Haines 
4319d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4320d452930fSRichard Haines 		if (sh == NULL)
4321d452930fSRichard Haines 			break;
4322d452930fSRichard Haines 
4323d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4324d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4325d452930fSRichard Haines 		break;
4326d452930fSRichard Haines 	}
4327d452930fSRichard Haines #endif
43281da177e4SLinus Torvalds 	/* includes fragments */
43291da177e4SLinus Torvalds 	default:
43301da177e4SLinus Torvalds 		break;
43311da177e4SLinus Torvalds 	}
43321da177e4SLinus Torvalds out:
43331da177e4SLinus Torvalds 	return ret;
43341da177e4SLinus Torvalds }
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds #endif /* IPV6 */
43371da177e4SLinus Torvalds 
43382bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4339cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
43401da177e4SLinus Torvalds {
4341cf9481e2SDavid Howells 	char *addrp;
4342cf9481e2SDavid Howells 	int ret;
43431da177e4SLinus Torvalds 
434448c62af6SEric Paris 	switch (ad->u.net->family) {
43451da177e4SLinus Torvalds 	case PF_INET:
434667f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4347cf9481e2SDavid Howells 		if (ret)
4348cf9481e2SDavid Howells 			goto parse_error;
434948c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
435048c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4351cf9481e2SDavid Howells 		goto okay;
43521da177e4SLinus Torvalds 
43531a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
43541da177e4SLinus Torvalds 	case PF_INET6:
435567f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4356cf9481e2SDavid Howells 		if (ret)
4357cf9481e2SDavid Howells 			goto parse_error;
435848c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
435948c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4360cf9481e2SDavid Howells 		goto okay;
43611da177e4SLinus Torvalds #endif	/* IPV6 */
43621da177e4SLinus Torvalds 	default:
4363cf9481e2SDavid Howells 		addrp = NULL;
4364cf9481e2SDavid Howells 		goto okay;
43651da177e4SLinus Torvalds 	}
43661da177e4SLinus Torvalds 
4367cf9481e2SDavid Howells parse_error:
4368c103a91eSpeter enderborg 	pr_warn(
436971f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
437071f1cb05SPaul Moore 	       " unable to parse packet\n");
43711da177e4SLinus Torvalds 	return ret;
4372cf9481e2SDavid Howells 
4373cf9481e2SDavid Howells okay:
4374cf9481e2SDavid Howells 	if (_addrp)
4375cf9481e2SDavid Howells 		*_addrp = addrp;
4376cf9481e2SDavid Howells 	return 0;
43771da177e4SLinus Torvalds }
43781da177e4SLinus Torvalds 
43794f6a993fSPaul Moore /**
4380220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
43814f6a993fSPaul Moore  * @skb: the packet
438275e22910SPaul Moore  * @family: protocol family
4383220deb96SPaul Moore  * @sid: the packet's peer label SID
43844f6a993fSPaul Moore  *
43854f6a993fSPaul Moore  * Description:
4386220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4387220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4388220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4389220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4390220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4391220deb96SPaul Moore  * peer labels.
43924f6a993fSPaul Moore  *
43934f6a993fSPaul Moore  */
4394220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
43954f6a993fSPaul Moore {
439671f1cb05SPaul Moore 	int err;
43974f6a993fSPaul Moore 	u32 xfrm_sid;
43984f6a993fSPaul Moore 	u32 nlbl_sid;
4399220deb96SPaul Moore 	u32 nlbl_type;
44004f6a993fSPaul Moore 
4401817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4402bed4d7efSPaul Moore 	if (unlikely(err))
4403bed4d7efSPaul Moore 		return -EACCES;
4404bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4405bed4d7efSPaul Moore 	if (unlikely(err))
4406bed4d7efSPaul Moore 		return -EACCES;
4407220deb96SPaul Moore 
4408aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4409aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
441071f1cb05SPaul Moore 	if (unlikely(err)) {
4411c103a91eSpeter enderborg 		pr_warn(
441271f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
441371f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4414220deb96SPaul Moore 		return -EACCES;
441571f1cb05SPaul Moore 	}
4416220deb96SPaul Moore 
4417220deb96SPaul Moore 	return 0;
44184f6a993fSPaul Moore }
44194f6a993fSPaul Moore 
4420446b8024SPaul Moore /**
4421446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4422446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4423446b8024SPaul Moore  * @skb_sid: the packet's SID
4424446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4425446b8024SPaul Moore  *
4426446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4427446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4428446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
4429446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4430446b8024SPaul Moore  *
4431446b8024SPaul Moore  */
4432446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4433446b8024SPaul Moore {
4434446b8024SPaul Moore 	int err = 0;
4435446b8024SPaul Moore 
4436446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4437aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4438aa8e712cSStephen Smalley 					    conn_sid);
4439446b8024SPaul Moore 	else
4440446b8024SPaul Moore 		*conn_sid = sk_sid;
4441446b8024SPaul Moore 
4442446b8024SPaul Moore 	return err;
4443446b8024SPaul Moore }
4444446b8024SPaul Moore 
44451da177e4SLinus Torvalds /* socket security operations */
4446d4f2d978SPaul Moore 
44472ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
44482ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4449d4f2d978SPaul Moore {
44502ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
44512ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
44522ad18bdfSHarry Ciao 		return 0;
44532ad18bdfSHarry Ciao 	}
44542ad18bdfSHarry Ciao 
4455aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4456aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4457d4f2d978SPaul Moore }
4458d4f2d978SPaul Moore 
4459be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
44601da177e4SLinus Torvalds {
4461253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
44622bf49690SThomas Liu 	struct common_audit_data ad;
446348c62af6SEric Paris 	struct lsm_network_audit net = {0,};
44641da177e4SLinus Torvalds 
4465253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4466253bfae6SPaul Moore 		return 0;
44671da177e4SLinus Torvalds 
446850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
446948c62af6SEric Paris 	ad.u.net = &net;
447048c62af6SEric Paris 	ad.u.net->sk = sk;
44711da177e4SLinus Torvalds 
44726b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
44736b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4474be0554c9SStephen Smalley 			    &ad);
44751da177e4SLinus Torvalds }
44761da177e4SLinus Torvalds 
44771da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
44781da177e4SLinus Torvalds 				 int protocol, int kern)
44791da177e4SLinus Torvalds {
44800c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
4481d4f2d978SPaul Moore 	u32 newsid;
4482275bb41eSDavid Howells 	u16 secclass;
44832ad18bdfSHarry Ciao 	int rc;
44841da177e4SLinus Torvalds 
44851da177e4SLinus Torvalds 	if (kern)
4486d4f2d978SPaul Moore 		return 0;
44871da177e4SLinus Torvalds 
4488275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
44892ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
44902ad18bdfSHarry Ciao 	if (rc)
44912ad18bdfSHarry Ciao 		return rc;
44922ad18bdfSHarry Ciao 
44936b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
44946b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
44951da177e4SLinus Torvalds }
44961da177e4SLinus Torvalds 
44977420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
44981da177e4SLinus Torvalds 				      int type, int protocol, int kern)
44991da177e4SLinus Torvalds {
45000c6cfa62SCasey Schaufler 	const struct task_security_struct *tsec = selinux_cred(current_cred());
45015d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4502892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
45039287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
45049287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4505275bb41eSDavid Howells 	int err = 0;
4506275bb41eSDavid Howells 
45079287aed2SAndreas Gruenbacher 	if (!kern) {
45089287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
45092ad18bdfSHarry Ciao 		if (err)
45102ad18bdfSHarry Ciao 			return err;
45112ad18bdfSHarry Ciao 	}
4512275bb41eSDavid Howells 
45139287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
45149287aed2SAndreas Gruenbacher 	isec->sid = sid;
45156f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
45161da177e4SLinus Torvalds 
4517892c141eSVenkat Yekkirala 	if (sock->sk) {
4518892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
45199287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
45209287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4521d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4522d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4523d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4524d452930fSRichard Haines 
4525389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4526892c141eSVenkat Yekkirala 	}
4527892c141eSVenkat Yekkirala 
45287420ed23SVenkat Yekkirala 	return err;
45291da177e4SLinus Torvalds }
45301da177e4SLinus Torvalds 
45310b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
45320b811db2SDavid Herrmann 				     struct socket *sockb)
45330b811db2SDavid Herrmann {
45340b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
45350b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
45360b811db2SDavid Herrmann 
45370b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
45380b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
45390b811db2SDavid Herrmann 
45400b811db2SDavid Herrmann 	return 0;
45410b811db2SDavid Herrmann }
45420b811db2SDavid Herrmann 
45431da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
45441da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
45451da177e4SLinus Torvalds    permission check between the socket and the port number. */
45461da177e4SLinus Torvalds 
45471da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
45481da177e4SLinus Torvalds {
4549253bfae6SPaul Moore 	struct sock *sk = sock->sk;
45500f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
45511da177e4SLinus Torvalds 	u16 family;
45521da177e4SLinus Torvalds 	int err;
45531da177e4SLinus Torvalds 
4554be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
45551da177e4SLinus Torvalds 	if (err)
45561da177e4SLinus Torvalds 		goto out;
45571da177e4SLinus Torvalds 
4558d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4559253bfae6SPaul Moore 	family = sk->sk_family;
45601da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
45611da177e4SLinus Torvalds 		char *addrp;
45622bf49690SThomas Liu 		struct common_audit_data ad;
456348c62af6SEric Paris 		struct lsm_network_audit net = {0,};
45641da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
45651da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
4566c750e692STetsuo Handa 		u16 family_sa;
45671da177e4SLinus Torvalds 		unsigned short snum;
4568e399f982SJames Morris 		u32 sid, node_perm;
45691da177e4SLinus Torvalds 
4570d452930fSRichard Haines 		/*
4571d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4572d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4573d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4574d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4575d452930fSRichard Haines 		 */
4576c750e692STetsuo Handa 		if (addrlen < offsetofend(struct sockaddr, sa_family))
4577c750e692STetsuo Handa 			return -EINVAL;
4578c750e692STetsuo Handa 		family_sa = address->sa_family;
45790f8db8ccSAlexey Kodanev 		switch (family_sa) {
45800f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
458168741a8aSRichard Haines 		case AF_INET:
458268741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
458368741a8aSRichard Haines 				return -EINVAL;
45841da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
45850f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
45860f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
45870f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
45880f8db8ccSAlexey Kodanev 				 */
45890f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
45900f8db8ccSAlexey Kodanev 					goto err_af;
45910f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
45920f8db8ccSAlexey Kodanev 			}
45931da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
45941da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
459568741a8aSRichard Haines 			break;
459668741a8aSRichard Haines 		case AF_INET6:
459768741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
459868741a8aSRichard Haines 				return -EINVAL;
45991da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
46001da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
46011da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
460268741a8aSRichard Haines 			break;
460368741a8aSRichard Haines 		default:
46040f8db8ccSAlexey Kodanev 			goto err_af;
46051da177e4SLinus Torvalds 		}
46061da177e4SLinus Torvalds 
460788b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
460888b7d370SAlexey Kodanev 		ad.u.net = &net;
460988b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
461088b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
461188b7d370SAlexey Kodanev 
4612227b60f5SStephen Hemminger 		if (snum) {
4613227b60f5SStephen Hemminger 			int low, high;
4614227b60f5SStephen Hemminger 
46150bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4616227b60f5SStephen Hemminger 
461782f31ebfSMaciej Żenczykowski 			if (inet_port_requires_bind_service(sock_net(sk), snum) ||
461882f31ebfSMaciej Żenczykowski 			    snum < low || snum > high) {
46193e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
46203e112172SPaul Moore 						      snum, &sid);
46211da177e4SLinus Torvalds 				if (err)
46221da177e4SLinus Torvalds 					goto out;
46236b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
46246b6bc620SStephen Smalley 						   sksec->sid, sid,
4625253bfae6SPaul Moore 						   sksec->sclass,
46261da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
46271da177e4SLinus Torvalds 				if (err)
46281da177e4SLinus Torvalds 					goto out;
46291da177e4SLinus Torvalds 			}
4630227b60f5SStephen Hemminger 		}
46311da177e4SLinus Torvalds 
4632253bfae6SPaul Moore 		switch (sksec->sclass) {
463313402580SJames Morris 		case SECCLASS_TCP_SOCKET:
46341da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
46351da177e4SLinus Torvalds 			break;
46361da177e4SLinus Torvalds 
463713402580SJames Morris 		case SECCLASS_UDP_SOCKET:
46381da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
46391da177e4SLinus Torvalds 			break;
46401da177e4SLinus Torvalds 
46412ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
46422ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
46432ee92d46SJames Morris 			break;
46442ee92d46SJames Morris 
4645d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4646d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4647d452930fSRichard Haines 			break;
4648d452930fSRichard Haines 
46491da177e4SLinus Torvalds 		default:
46501da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
46511da177e4SLinus Torvalds 			break;
46521da177e4SLinus Torvalds 		}
46531da177e4SLinus Torvalds 
465488b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
46551da177e4SLinus Torvalds 		if (err)
46561da177e4SLinus Torvalds 			goto out;
46571da177e4SLinus Torvalds 
46580f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
465948c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
46601da177e4SLinus Torvalds 		else
466148c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
46621da177e4SLinus Torvalds 
46636b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
46646b6bc620SStephen Smalley 				   sksec->sid, sid,
4665253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
46661da177e4SLinus Torvalds 		if (err)
46671da177e4SLinus Torvalds 			goto out;
46681da177e4SLinus Torvalds 	}
46691da177e4SLinus Torvalds out:
46701da177e4SLinus Torvalds 	return err;
46710f8db8ccSAlexey Kodanev err_af:
46720f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
46730f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
46740f8db8ccSAlexey Kodanev 		return -EINVAL;
46750f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
46761da177e4SLinus Torvalds }
46771da177e4SLinus Torvalds 
4678d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4679d61330c6SKees Cook  * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4680d452930fSRichard Haines  */
4681d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4682d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
46831da177e4SLinus Torvalds {
4684014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4685253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
46861da177e4SLinus Torvalds 	int err;
46871da177e4SLinus Torvalds 
4688be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
46891da177e4SLinus Torvalds 	if (err)
46901da177e4SLinus Torvalds 		return err;
469105174c95SPaolo Abeni 	if (addrlen < offsetofend(struct sockaddr, sa_family))
469205174c95SPaolo Abeni 		return -EINVAL;
469305174c95SPaolo Abeni 
469405174c95SPaolo Abeni 	/* connect(AF_UNSPEC) has special handling, as it is a documented
469505174c95SPaolo Abeni 	 * way to disconnect the socket
469605174c95SPaolo Abeni 	 */
469705174c95SPaolo Abeni 	if (address->sa_family == AF_UNSPEC)
469805174c95SPaolo Abeni 		return 0;
46991da177e4SLinus Torvalds 
47001da177e4SLinus Torvalds 	/*
4701d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4702d452930fSRichard Haines 	 * for the port.
47031da177e4SLinus Torvalds 	 */
4704253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4705d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4706d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
47072bf49690SThomas Liu 		struct common_audit_data ad;
470848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
47091da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
47101da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
47111da177e4SLinus Torvalds 		unsigned short snum;
47122ee92d46SJames Morris 		u32 sid, perm;
47131da177e4SLinus Torvalds 
4714d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4715d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4716d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4717d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4718d452930fSRichard Haines 		 */
471968741a8aSRichard Haines 		switch (address->sa_family) {
472068741a8aSRichard Haines 		case AF_INET:
47211da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4722911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
47231da177e4SLinus Torvalds 				return -EINVAL;
47241da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
472568741a8aSRichard Haines 			break;
472668741a8aSRichard Haines 		case AF_INET6:
47271da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4728911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
47291da177e4SLinus Torvalds 				return -EINVAL;
47301da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
473168741a8aSRichard Haines 			break;
473268741a8aSRichard Haines 		default:
473368741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
473468741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
473568741a8aSRichard Haines 			 */
473668741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
473768741a8aSRichard Haines 				return -EINVAL;
473868741a8aSRichard Haines 			else
473968741a8aSRichard Haines 				return -EAFNOSUPPORT;
47401da177e4SLinus Torvalds 		}
47411da177e4SLinus Torvalds 
47423e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
47431da177e4SLinus Torvalds 		if (err)
4744d452930fSRichard Haines 			return err;
47451da177e4SLinus Torvalds 
4746d452930fSRichard Haines 		switch (sksec->sclass) {
4747d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4748d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4749d452930fSRichard Haines 			break;
4750d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4751d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4752d452930fSRichard Haines 			break;
4753d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4754d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4755d452930fSRichard Haines 			break;
4756d452930fSRichard Haines 		}
47572ee92d46SJames Morris 
475850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
475948c62af6SEric Paris 		ad.u.net = &net;
476048c62af6SEric Paris 		ad.u.net->dport = htons(snum);
476188b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
47626b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
47636b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
47641da177e4SLinus Torvalds 		if (err)
4765d452930fSRichard Haines 			return err;
47661da177e4SLinus Torvalds 	}
47671da177e4SLinus Torvalds 
4768d452930fSRichard Haines 	return 0;
4769d452930fSRichard Haines }
4770014ab19aSPaul Moore 
4771d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4772d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4773d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4774d452930fSRichard Haines {
4775d452930fSRichard Haines 	int err;
4776d452930fSRichard Haines 	struct sock *sk = sock->sk;
4777d452930fSRichard Haines 
4778d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4779d452930fSRichard Haines 	if (err)
47801da177e4SLinus Torvalds 		return err;
4781d452930fSRichard Haines 
4782d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
47831da177e4SLinus Torvalds }
47841da177e4SLinus Torvalds 
47851da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
47861da177e4SLinus Torvalds {
4787be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
47881da177e4SLinus Torvalds }
47891da177e4SLinus Torvalds 
47901da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
47911da177e4SLinus Torvalds {
47921da177e4SLinus Torvalds 	int err;
47931da177e4SLinus Torvalds 	struct inode_security_struct *isec;
47941da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
47959287aed2SAndreas Gruenbacher 	u16 sclass;
47969287aed2SAndreas Gruenbacher 	u32 sid;
47971da177e4SLinus Torvalds 
4798be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
47991da177e4SLinus Torvalds 	if (err)
48001da177e4SLinus Torvalds 		return err;
48011da177e4SLinus Torvalds 
48025d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
48039287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
48049287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
48059287aed2SAndreas Gruenbacher 	sid = isec->sid;
48069287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
48079287aed2SAndreas Gruenbacher 
48089287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
48099287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
48109287aed2SAndreas Gruenbacher 	newisec->sid = sid;
48116f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
48121da177e4SLinus Torvalds 
48131da177e4SLinus Torvalds 	return 0;
48141da177e4SLinus Torvalds }
48151da177e4SLinus Torvalds 
48161da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
48171da177e4SLinus Torvalds 				  int size)
48181da177e4SLinus Torvalds {
4819be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
48201da177e4SLinus Torvalds }
48211da177e4SLinus Torvalds 
48221da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
48231da177e4SLinus Torvalds 				  int size, int flags)
48241da177e4SLinus Torvalds {
4825be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
48261da177e4SLinus Torvalds }
48271da177e4SLinus Torvalds 
48281da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
48291da177e4SLinus Torvalds {
4830be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48311da177e4SLinus Torvalds }
48321da177e4SLinus Torvalds 
48331da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
48341da177e4SLinus Torvalds {
4835be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48361da177e4SLinus Torvalds }
48371da177e4SLinus Torvalds 
48381da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
48391da177e4SLinus Torvalds {
4840f8687afeSPaul Moore 	int err;
4841f8687afeSPaul Moore 
4842be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4843f8687afeSPaul Moore 	if (err)
4844f8687afeSPaul Moore 		return err;
4845f8687afeSPaul Moore 
4846f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
48471da177e4SLinus Torvalds }
48481da177e4SLinus Torvalds 
48491da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
48501da177e4SLinus Torvalds 				     int optname)
48511da177e4SLinus Torvalds {
4852be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
48531da177e4SLinus Torvalds }
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
48561da177e4SLinus Torvalds {
4857be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
48581da177e4SLinus Torvalds }
48591da177e4SLinus Torvalds 
48603610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
48613610cda5SDavid S. Miller 					      struct sock *other,
48621da177e4SLinus Torvalds 					      struct sock *newsk)
48631da177e4SLinus Torvalds {
48643610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
48653610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
48664d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
48672bf49690SThomas Liu 	struct common_audit_data ad;
486848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
48691da177e4SLinus Torvalds 	int err;
48701da177e4SLinus Torvalds 
487150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
487248c62af6SEric Paris 	ad.u.net = &net;
487348c62af6SEric Paris 	ad.u.net->sk = other;
48741da177e4SLinus Torvalds 
48756b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
48766b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
48774d1e2451SPaul Moore 			   sksec_other->sclass,
48781da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
48791da177e4SLinus Torvalds 	if (err)
48801da177e4SLinus Torvalds 		return err;
48811da177e4SLinus Torvalds 
48821da177e4SLinus Torvalds 	/* server child socket */
48834d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4884aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4885aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
48864d1e2451SPaul Moore 	if (err)
48874237c75cSVenkat Yekkirala 		return err;
48884d1e2451SPaul Moore 
48894d1e2451SPaul Moore 	/* connecting socket */
48904d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
48914d1e2451SPaul Moore 
48924d1e2451SPaul Moore 	return 0;
48931da177e4SLinus Torvalds }
48941da177e4SLinus Torvalds 
48951da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
48961da177e4SLinus Torvalds 					struct socket *other)
48971da177e4SLinus Torvalds {
4898253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4899253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
49002bf49690SThomas Liu 	struct common_audit_data ad;
490148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49021da177e4SLinus Torvalds 
490350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
490448c62af6SEric Paris 	ad.u.net = &net;
490548c62af6SEric Paris 	ad.u.net->sk = other->sk;
49061da177e4SLinus Torvalds 
49076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49086b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4909253bfae6SPaul Moore 			    &ad);
49101da177e4SLinus Torvalds }
49111da177e4SLinus Torvalds 
4912cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4913cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
49142bf49690SThomas Liu 				    struct common_audit_data *ad)
4915effad8dfSPaul Moore {
4916effad8dfSPaul Moore 	int err;
4917effad8dfSPaul Moore 	u32 if_sid;
4918effad8dfSPaul Moore 	u32 node_sid;
4919effad8dfSPaul Moore 
4920cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4921effad8dfSPaul Moore 	if (err)
4922effad8dfSPaul Moore 		return err;
49236b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
49246b6bc620SStephen Smalley 			   peer_sid, if_sid,
4925effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4926effad8dfSPaul Moore 	if (err)
4927effad8dfSPaul Moore 		return err;
4928effad8dfSPaul Moore 
4929effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4930effad8dfSPaul Moore 	if (err)
4931effad8dfSPaul Moore 		return err;
49326b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49336b6bc620SStephen Smalley 			    peer_sid, node_sid,
4934effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4935effad8dfSPaul Moore }
4936effad8dfSPaul Moore 
4937220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4938d8395c87SPaul Moore 				       u16 family)
4939220deb96SPaul Moore {
4940277d342fSPaul Moore 	int err = 0;
4941220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4942220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
49432bf49690SThomas Liu 	struct common_audit_data ad;
494448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4945d8395c87SPaul Moore 	char *addrp;
4946d8395c87SPaul Moore 
494750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
494848c62af6SEric Paris 	ad.u.net = &net;
494948c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
495048c62af6SEric Paris 	ad.u.net->family = family;
4951d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4952d8395c87SPaul Moore 	if (err)
4953d8395c87SPaul Moore 		return err;
4954220deb96SPaul Moore 
495558bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
49566b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
49576b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4958d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4959220deb96SPaul Moore 		if (err)
4960220deb96SPaul Moore 			return err;
496158bfbb51SPaul Moore 	}
4962220deb96SPaul Moore 
4963d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4964220deb96SPaul Moore 	if (err)
4965220deb96SPaul Moore 		return err;
4966d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4967220deb96SPaul Moore 
49684e5ab4cbSJames Morris 	return err;
49694e5ab4cbSJames Morris }
4970d28d1e08STrent Jaeger 
49714e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
49724e5ab4cbSJames Morris {
4973220deb96SPaul Moore 	int err;
49744237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4975220deb96SPaul Moore 	u16 family = sk->sk_family;
4976220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
49772bf49690SThomas Liu 	struct common_audit_data ad;
497848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4979220deb96SPaul Moore 	char *addrp;
4980d8395c87SPaul Moore 	u8 secmark_active;
4981d8395c87SPaul Moore 	u8 peerlbl_active;
49824e5ab4cbSJames Morris 
49834e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4984220deb96SPaul Moore 		return 0;
49854e5ab4cbSJames Morris 
49864e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
498787fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
49884e5ab4cbSJames Morris 		family = PF_INET;
49894e5ab4cbSJames Morris 
4990d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4991d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4992d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4993d8395c87SPaul Moore 	 * as fast and as clean as possible. */
4994aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
4995d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4996d8395c87SPaul Moore 
4997d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
49982be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4999d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
5000d8395c87SPaul Moore 		return 0;
5001d8395c87SPaul Moore 
500250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
500348c62af6SEric Paris 	ad.u.net = &net;
500448c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
500548c62af6SEric Paris 	ad.u.net->family = family;
5006224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
50074e5ab4cbSJames Morris 	if (err)
5008220deb96SPaul Moore 		return err;
50094e5ab4cbSJames Morris 
5010d8395c87SPaul Moore 	if (peerlbl_active) {
5011d621d35eSPaul Moore 		u32 peer_sid;
5012220deb96SPaul Moore 
5013220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5014220deb96SPaul Moore 		if (err)
5015220deb96SPaul Moore 			return err;
5016cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5017cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5018dfaebe98SPaul Moore 		if (err) {
5019a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
5020effad8dfSPaul Moore 			return err;
5021dfaebe98SPaul Moore 		}
50226b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50236b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
5024d621d35eSPaul Moore 				   PEER__RECV, &ad);
502546d01d63SChad Hanson 		if (err) {
5026a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
502746d01d63SChad Hanson 			return err;
502846d01d63SChad Hanson 		}
5029d621d35eSPaul Moore 	}
5030d621d35eSPaul Moore 
5031d8395c87SPaul Moore 	if (secmark_active) {
50326b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50336b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5034effad8dfSPaul Moore 				   PACKET__RECV, &ad);
5035effad8dfSPaul Moore 		if (err)
5036effad8dfSPaul Moore 			return err;
5037effad8dfSPaul Moore 	}
5038effad8dfSPaul Moore 
5039d621d35eSPaul Moore 	return err;
50401da177e4SLinus Torvalds }
50411da177e4SLinus Torvalds 
50422c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
50431da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
50441da177e4SLinus Torvalds {
50451da177e4SLinus Torvalds 	int err = 0;
50461da177e4SLinus Torvalds 	char *scontext;
50471da177e4SLinus Torvalds 	u32 scontext_len;
5048253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
50493de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
50501da177e4SLinus Torvalds 
5051253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5052d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
5053d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
5054dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
5055253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
5056253bfae6SPaul Moore 		return -ENOPROTOOPT;
50571da177e4SLinus Torvalds 
5058aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5059aa8e712cSStephen Smalley 				      &scontext_len);
50601da177e4SLinus Torvalds 	if (err)
5061253bfae6SPaul Moore 		return err;
50621da177e4SLinus Torvalds 
50631da177e4SLinus Torvalds 	if (scontext_len > len) {
50641da177e4SLinus Torvalds 		err = -ERANGE;
50651da177e4SLinus Torvalds 		goto out_len;
50661da177e4SLinus Torvalds 	}
50671da177e4SLinus Torvalds 
50681da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
50691da177e4SLinus Torvalds 		err = -EFAULT;
50701da177e4SLinus Torvalds 
50711da177e4SLinus Torvalds out_len:
50721da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
50731da177e4SLinus Torvalds 		err = -EFAULT;
50741da177e4SLinus Torvalds 	kfree(scontext);
50751da177e4SLinus Torvalds 	return err;
50761da177e4SLinus Torvalds }
50771da177e4SLinus Torvalds 
5078dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
50792c7946a7SCatherine Zhang {
5080dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
508175e22910SPaul Moore 	u16 family;
5082899134f2SPaul Moore 	struct inode_security_struct *isec;
5083877ce7c1SCatherine Zhang 
5084aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
5085aa862900SPaul Moore 		family = PF_INET;
5086aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
5087aa862900SPaul Moore 		family = PF_INET6;
5088aa862900SPaul Moore 	else if (sock)
508975e22910SPaul Moore 		family = sock->sk->sk_family;
509075e22910SPaul Moore 	else
509175e22910SPaul Moore 		goto out;
509275e22910SPaul Moore 
5093899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
5094899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
5095899134f2SPaul Moore 		peer_secid = isec->sid;
5096899134f2SPaul Moore 	} else if (skb)
5097220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
50982c7946a7SCatherine Zhang 
509975e22910SPaul Moore out:
5100dc49c1f9SCatherine Zhang 	*secid = peer_secid;
510175e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
510275e22910SPaul Moore 		return -EINVAL;
510375e22910SPaul Moore 	return 0;
51042c7946a7SCatherine Zhang }
51052c7946a7SCatherine Zhang 
51067d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
51071da177e4SLinus Torvalds {
510884914b7eSPaul Moore 	struct sk_security_struct *sksec;
510984914b7eSPaul Moore 
511084914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
511184914b7eSPaul Moore 	if (!sksec)
511284914b7eSPaul Moore 		return -ENOMEM;
511384914b7eSPaul Moore 
511484914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
511584914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
51165dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
511784914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
511884914b7eSPaul Moore 	sk->sk_security = sksec;
511984914b7eSPaul Moore 
512084914b7eSPaul Moore 	return 0;
51211da177e4SLinus Torvalds }
51221da177e4SLinus Torvalds 
51231da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
51241da177e4SLinus Torvalds {
512584914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
512684914b7eSPaul Moore 
512784914b7eSPaul Moore 	sk->sk_security = NULL;
512884914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
512984914b7eSPaul Moore 	kfree(sksec);
51301da177e4SLinus Torvalds }
51311da177e4SLinus Torvalds 
5132892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5133892c141eSVenkat Yekkirala {
5134dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5135dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5136892c141eSVenkat Yekkirala 
5137dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5138dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5139dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
514099f59ed0SPaul Moore 
5141dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5142892c141eSVenkat Yekkirala }
5143892c141eSVenkat Yekkirala 
5144beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5145d28d1e08STrent Jaeger {
5146d28d1e08STrent Jaeger 	if (!sk)
5147beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5148892c141eSVenkat Yekkirala 	else {
5149892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5150d28d1e08STrent Jaeger 
5151beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5152892c141eSVenkat Yekkirala 	}
5153d28d1e08STrent Jaeger }
5154d28d1e08STrent Jaeger 
51559a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
51564237c75cSVenkat Yekkirala {
51575d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
51585d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
51594237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
51604237c75cSVenkat Yekkirala 
51612873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
51622873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
51634237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5164220deb96SPaul Moore 	sksec->sclass = isec->sclass;
51654237c75cSVenkat Yekkirala }
51664237c75cSVenkat Yekkirala 
5167d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5168d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5169d452930fSRichard Haines  * already present).
5170d452930fSRichard Haines  */
5171d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5172d452930fSRichard Haines 				      struct sk_buff *skb)
5173d452930fSRichard Haines {
5174d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5175d452930fSRichard Haines 	struct common_audit_data ad;
5176d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5177d452930fSRichard Haines 	u8 peerlbl_active;
5178d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5179d452930fSRichard Haines 	u32 conn_sid;
5180d452930fSRichard Haines 	int err = 0;
5181d452930fSRichard Haines 
5182aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5183d452930fSRichard Haines 		return 0;
5184d452930fSRichard Haines 
5185d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5186d452930fSRichard Haines 
5187d452930fSRichard Haines 	if (peerlbl_active) {
5188d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5189d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5190d452930fSRichard Haines 		 */
5191d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5192d452930fSRichard Haines 					      &peer_sid);
5193d452930fSRichard Haines 		if (err)
5194d452930fSRichard Haines 			return err;
5195d452930fSRichard Haines 
5196d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5197d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5198d452930fSRichard Haines 	}
5199d452930fSRichard Haines 
5200d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5201d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5202d452930fSRichard Haines 
5203d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5204d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5205d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5206d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5207d452930fSRichard Haines 		 */
5208d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5209d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5210d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5211d452930fSRichard Haines 		 * consistency among the peer SIDs.
5212d452930fSRichard Haines 		 */
5213d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5214d452930fSRichard Haines 		ad.u.net = &net;
5215d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
52166b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
52176b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5218d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5219d452930fSRichard Haines 		if (err)
5220d452930fSRichard Haines 			return err;
5221d452930fSRichard Haines 	}
5222d452930fSRichard Haines 
5223d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5224d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5225d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5226d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5227d452930fSRichard Haines 	 * plug this into the new socket.
5228d452930fSRichard Haines 	 */
5229d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5230d452930fSRichard Haines 	if (err)
5231d452930fSRichard Haines 		return err;
5232d452930fSRichard Haines 
5233d452930fSRichard Haines 	ep->secid = conn_sid;
5234d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5235d452930fSRichard Haines 
5236d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5237d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5238d452930fSRichard Haines }
5239d452930fSRichard Haines 
5240d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5241d452930fSRichard Haines  * based on their @optname.
5242d452930fSRichard Haines  */
5243d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5244d452930fSRichard Haines 				     struct sockaddr *address,
5245d452930fSRichard Haines 				     int addrlen)
5246d452930fSRichard Haines {
5247d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5248d452930fSRichard Haines 	void *addr_buf;
5249d452930fSRichard Haines 	struct sockaddr *addr;
5250d452930fSRichard Haines 	struct socket *sock;
5251d452930fSRichard Haines 
5252aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5253d452930fSRichard Haines 		return 0;
5254d452930fSRichard Haines 
5255d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5256d452930fSRichard Haines 	sock = sk->sk_socket;
5257d452930fSRichard Haines 	addr_buf = address;
5258d452930fSRichard Haines 
5259d452930fSRichard Haines 	while (walk_size < addrlen) {
5260c138325fSOndrej Mosnacek 		if (walk_size + sizeof(sa_family_t) > addrlen)
5261c138325fSOndrej Mosnacek 			return -EINVAL;
5262c138325fSOndrej Mosnacek 
5263d452930fSRichard Haines 		addr = addr_buf;
5264d452930fSRichard Haines 		switch (addr->sa_family) {
52654152dc91SAlexey Kodanev 		case AF_UNSPEC:
5266d452930fSRichard Haines 		case AF_INET:
5267d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5268d452930fSRichard Haines 			break;
5269d452930fSRichard Haines 		case AF_INET6:
5270d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5271d452930fSRichard Haines 			break;
5272d452930fSRichard Haines 		default:
52734152dc91SAlexey Kodanev 			return -EINVAL;
5274d452930fSRichard Haines 		}
5275d452930fSRichard Haines 
5276292c997aSXin Long 		if (walk_size + len > addrlen)
5277292c997aSXin Long 			return -EINVAL;
5278292c997aSXin Long 
5279d452930fSRichard Haines 		err = -EINVAL;
5280d452930fSRichard Haines 		switch (optname) {
5281d452930fSRichard Haines 		/* Bind checks */
5282d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5283d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5284d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5285d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5286d452930fSRichard Haines 			break;
5287d452930fSRichard Haines 		/* Connect checks */
5288d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5289d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5290d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5291d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5292d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5293d452930fSRichard Haines 			if (err)
5294d452930fSRichard Haines 				return err;
5295d452930fSRichard Haines 
5296d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5297d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5298d452930fSRichard Haines 			 * therefore selinux_netlbl_socket_connect_locked() is
5299d452930fSRichard Haines 			 * is called here. The situations handled are:
5300d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5301d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5302d452930fSRichard Haines 			 * primary address is selected.
5303d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5304d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5305d452930fSRichard Haines 			 * selinux_socket_connect().
5306d452930fSRichard Haines 			 */
5307d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5308d452930fSRichard Haines 			break;
5309d452930fSRichard Haines 		}
5310d452930fSRichard Haines 
5311d452930fSRichard Haines 		if (err)
5312d452930fSRichard Haines 			return err;
5313d452930fSRichard Haines 
5314d452930fSRichard Haines 		addr_buf += len;
5315d452930fSRichard Haines 		walk_size += len;
5316d452930fSRichard Haines 	}
5317d452930fSRichard Haines 
5318d452930fSRichard Haines 	return 0;
5319d452930fSRichard Haines }
5320d452930fSRichard Haines 
5321d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5322d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5323d452930fSRichard Haines 				  struct sock *newsk)
5324d452930fSRichard Haines {
5325d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5326d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5327d452930fSRichard Haines 
5328d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5329d452930fSRichard Haines 	 * the non-sctp clone version.
5330d452930fSRichard Haines 	 */
5331aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5332d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5333d452930fSRichard Haines 
5334d452930fSRichard Haines 	newsksec->sid = ep->secid;
5335d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5336d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5337d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5338d452930fSRichard Haines }
5339d452930fSRichard Haines 
53409a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
53414237c75cSVenkat Yekkirala 				     struct request_sock *req)
53424237c75cSVenkat Yekkirala {
53434237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
53444237c75cSVenkat Yekkirala 	int err;
53450b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5346446b8024SPaul Moore 	u32 connsid;
53474237c75cSVenkat Yekkirala 	u32 peersid;
53484237c75cSVenkat Yekkirala 
5349aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5350220deb96SPaul Moore 	if (err)
5351220deb96SPaul Moore 		return err;
5352446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
53534237c75cSVenkat Yekkirala 	if (err)
53544237c75cSVenkat Yekkirala 		return err;
5355446b8024SPaul Moore 	req->secid = connsid;
53566b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5357389fb800SPaul Moore 
5358389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
53594237c75cSVenkat Yekkirala }
53604237c75cSVenkat Yekkirala 
53619a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
53629a673e56SAdrian Bunk 				   const struct request_sock *req)
53634237c75cSVenkat Yekkirala {
53644237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
53654237c75cSVenkat Yekkirala 
53664237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
53676b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
53684237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
53694237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
53704237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
53714237c75cSVenkat Yekkirala 	   time it will have been created and available. */
537299f59ed0SPaul Moore 
53739f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
53749f2ad665SPaul Moore 	 * thread with access to newsksec */
5375389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
53764237c75cSVenkat Yekkirala }
53774237c75cSVenkat Yekkirala 
5378014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
53796b877699SVenkat Yekkirala {
5380aa862900SPaul Moore 	u16 family = sk->sk_family;
53816b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
53826b877699SVenkat Yekkirala 
5383aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5384aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5385aa862900SPaul Moore 		family = PF_INET;
5386aa862900SPaul Moore 
5387aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
53886b877699SVenkat Yekkirala }
53896b877699SVenkat Yekkirala 
53902606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
53912606fd1fSEric Paris {
53922606fd1fSEric Paris 	const struct task_security_struct *__tsec;
53932606fd1fSEric Paris 	u32 tsid;
53942606fd1fSEric Paris 
53950c6cfa62SCasey Schaufler 	__tsec = selinux_cred(current_cred());
53962606fd1fSEric Paris 	tsid = __tsec->sid;
53972606fd1fSEric Paris 
53986b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
53996b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
54006b6bc620SStephen Smalley 			    NULL);
54012606fd1fSEric Paris }
54022606fd1fSEric Paris 
54032606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
54042606fd1fSEric Paris {
54052606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
54062606fd1fSEric Paris }
54072606fd1fSEric Paris 
54082606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
54092606fd1fSEric Paris {
54102606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
54112606fd1fSEric Paris }
54122606fd1fSEric Paris 
54139a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
54149a673e56SAdrian Bunk 				      struct flowi *fl)
54154237c75cSVenkat Yekkirala {
54161d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
54174237c75cSVenkat Yekkirala }
54184237c75cSVenkat Yekkirala 
54195dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
54205dbbaf2dSPaul Moore {
54215dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
54225dbbaf2dSPaul Moore 
54235dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
54245dbbaf2dSPaul Moore 	if (!tunsec)
54255dbbaf2dSPaul Moore 		return -ENOMEM;
54265dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
54275dbbaf2dSPaul Moore 
54285dbbaf2dSPaul Moore 	*security = tunsec;
54295dbbaf2dSPaul Moore 	return 0;
54305dbbaf2dSPaul Moore }
54315dbbaf2dSPaul Moore 
54325dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
54335dbbaf2dSPaul Moore {
54345dbbaf2dSPaul Moore 	kfree(security);
54355dbbaf2dSPaul Moore }
54365dbbaf2dSPaul Moore 
5437ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5438ed6d76e4SPaul Moore {
5439ed6d76e4SPaul Moore 	u32 sid = current_sid();
5440ed6d76e4SPaul Moore 
5441ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5442ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5443ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5444ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5445ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5446ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5447ed6d76e4SPaul Moore 
54486b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54496b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5450ed6d76e4SPaul Moore 			    NULL);
5451ed6d76e4SPaul Moore }
5452ed6d76e4SPaul Moore 
54535dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5454ed6d76e4SPaul Moore {
54555dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
54565dbbaf2dSPaul Moore 
54576b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54586b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
54595dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
54605dbbaf2dSPaul Moore }
54615dbbaf2dSPaul Moore 
54625dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
54635dbbaf2dSPaul Moore {
54645dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5465ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5466ed6d76e4SPaul Moore 
5467ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5468ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5469ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5470ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5471ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5472ed6d76e4SPaul Moore 	 * protocols were being used */
5473ed6d76e4SPaul Moore 
54745dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5475ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
54765dbbaf2dSPaul Moore 
54775dbbaf2dSPaul Moore 	return 0;
5478ed6d76e4SPaul Moore }
5479ed6d76e4SPaul Moore 
54805dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5481ed6d76e4SPaul Moore {
54825dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5483ed6d76e4SPaul Moore 	u32 sid = current_sid();
5484ed6d76e4SPaul Moore 	int err;
5485ed6d76e4SPaul Moore 
54866b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
54876b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5488ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5489ed6d76e4SPaul Moore 	if (err)
5490ed6d76e4SPaul Moore 		return err;
54916b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
54926b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5493ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5494ed6d76e4SPaul Moore 	if (err)
5495ed6d76e4SPaul Moore 		return err;
54965dbbaf2dSPaul Moore 	tunsec->sid = sid;
5497ed6d76e4SPaul Moore 
5498ed6d76e4SPaul Moore 	return 0;
5499ed6d76e4SPaul Moore }
5500ed6d76e4SPaul Moore 
55011da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
55021da177e4SLinus Torvalds 
5503cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5504cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5505effad8dfSPaul Moore 				       u16 family)
55061da177e4SLinus Torvalds {
5507dfaebe98SPaul Moore 	int err;
5508effad8dfSPaul Moore 	char *addrp;
5509effad8dfSPaul Moore 	u32 peer_sid;
55102bf49690SThomas Liu 	struct common_audit_data ad;
551148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5512effad8dfSPaul Moore 	u8 secmark_active;
5513948bf85cSPaul Moore 	u8 netlbl_active;
5514effad8dfSPaul Moore 	u8 peerlbl_active;
55154237c75cSVenkat Yekkirala 
5516aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5517effad8dfSPaul Moore 		return NF_ACCEPT;
55184237c75cSVenkat Yekkirala 
5519effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5520948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
55212be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5522effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5523effad8dfSPaul Moore 		return NF_ACCEPT;
55244237c75cSVenkat Yekkirala 
5525d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5526d8395c87SPaul Moore 		return NF_DROP;
5527d8395c87SPaul Moore 
552850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
552948c62af6SEric Paris 	ad.u.net = &net;
5530cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
553148c62af6SEric Paris 	ad.u.net->family = family;
5532effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5533effad8dfSPaul Moore 		return NF_DROP;
55341da177e4SLinus Torvalds 
5535dfaebe98SPaul Moore 	if (peerlbl_active) {
5536cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5537cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5538dfaebe98SPaul Moore 		if (err) {
5539a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5540effad8dfSPaul Moore 			return NF_DROP;
5541dfaebe98SPaul Moore 		}
5542dfaebe98SPaul Moore 	}
5543effad8dfSPaul Moore 
5544effad8dfSPaul Moore 	if (secmark_active)
55456b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
55466b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5547effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5548effad8dfSPaul Moore 			return NF_DROP;
5549effad8dfSPaul Moore 
5550948bf85cSPaul Moore 	if (netlbl_active)
5551948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5552948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5553948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5554948bf85cSPaul Moore 		 * protection */
5555948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5556948bf85cSPaul Moore 			return NF_DROP;
5557948bf85cSPaul Moore 
5558effad8dfSPaul Moore 	return NF_ACCEPT;
5559effad8dfSPaul Moore }
5560effad8dfSPaul Moore 
556106198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5562effad8dfSPaul Moore 					 struct sk_buff *skb,
5563238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5564effad8dfSPaul Moore {
5565238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5566effad8dfSPaul Moore }
5567effad8dfSPaul Moore 
55681a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
556906198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5570effad8dfSPaul Moore 					 struct sk_buff *skb,
5571238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5572effad8dfSPaul Moore {
5573238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5574effad8dfSPaul Moore }
5575effad8dfSPaul Moore #endif	/* IPV6 */
5576effad8dfSPaul Moore 
5577948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5578948bf85cSPaul Moore 				      u16 family)
5579948bf85cSPaul Moore {
558047180068SPaul Moore 	struct sock *sk;
5581948bf85cSPaul Moore 	u32 sid;
5582948bf85cSPaul Moore 
5583948bf85cSPaul Moore 	if (!netlbl_enabled())
5584948bf85cSPaul Moore 		return NF_ACCEPT;
5585948bf85cSPaul Moore 
5586948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5587948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5588948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
558947180068SPaul Moore 	sk = skb->sk;
559047180068SPaul Moore 	if (sk) {
559147180068SPaul Moore 		struct sk_security_struct *sksec;
559247180068SPaul Moore 
5593e446f9dfSEric Dumazet 		if (sk_listener(sk))
559447180068SPaul Moore 			/* if the socket is the listening state then this
559547180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
559647180068SPaul Moore 			 * be labeled based on the connection/request_sock and
559747180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
559847180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
559947180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
560047180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
560147180068SPaul Moore 			 * as any IP option based labeling should be copied
560247180068SPaul Moore 			 * from the initial connection request (in the IP
560347180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
560447180068SPaul Moore 			 * security label in the packet itself this is the
560547180068SPaul Moore 			 * best we can do. */
560647180068SPaul Moore 			return NF_ACCEPT;
560747180068SPaul Moore 
560847180068SPaul Moore 		/* standard practice, label using the parent socket */
560947180068SPaul Moore 		sksec = sk->sk_security;
5610948bf85cSPaul Moore 		sid = sksec->sid;
5611948bf85cSPaul Moore 	} else
5612948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5613948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5614948bf85cSPaul Moore 		return NF_DROP;
5615948bf85cSPaul Moore 
5616948bf85cSPaul Moore 	return NF_ACCEPT;
5617948bf85cSPaul Moore }
5618948bf85cSPaul Moore 
561906198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5620948bf85cSPaul Moore 					struct sk_buff *skb,
5621238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5622948bf85cSPaul Moore {
5623948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5624948bf85cSPaul Moore }
5625948bf85cSPaul Moore 
56261a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
56272917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
56282917f57bSHuw Davies 					struct sk_buff *skb,
56292917f57bSHuw Davies 					const struct nf_hook_state *state)
56302917f57bSHuw Davies {
56312917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
56322917f57bSHuw Davies }
56332917f57bSHuw Davies #endif	/* IPV6 */
56342917f57bSHuw Davies 
5635effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5636effad8dfSPaul Moore 						int ifindex,
5637d8395c87SPaul Moore 						u16 family)
56384e5ab4cbSJames Morris {
563954abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
56404237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
56412bf49690SThomas Liu 	struct common_audit_data ad;
564248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5643d8395c87SPaul Moore 	char *addrp;
5644d8395c87SPaul Moore 	u8 proto;
56454e5ab4cbSJames Morris 
5646effad8dfSPaul Moore 	if (sk == NULL)
5647effad8dfSPaul Moore 		return NF_ACCEPT;
56484237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
56494e5ab4cbSJames Morris 
565050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
565148c62af6SEric Paris 	ad.u.net = &net;
565248c62af6SEric Paris 	ad.u.net->netif = ifindex;
565348c62af6SEric Paris 	ad.u.net->family = family;
5654d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5655d8395c87SPaul Moore 		return NF_DROP;
5656d8395c87SPaul Moore 
565758bfbb51SPaul Moore 	if (selinux_secmark_enabled())
56586b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56596b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5660d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
56612fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
56621da177e4SLinus Torvalds 
5663d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
56642fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5665effad8dfSPaul Moore 
5666effad8dfSPaul Moore 	return NF_ACCEPT;
5667effad8dfSPaul Moore }
5668effad8dfSPaul Moore 
5669cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5670cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5671effad8dfSPaul Moore 					 u16 family)
5672effad8dfSPaul Moore {
5673effad8dfSPaul Moore 	u32 secmark_perm;
5674effad8dfSPaul Moore 	u32 peer_sid;
5675cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5676effad8dfSPaul Moore 	struct sock *sk;
56772bf49690SThomas Liu 	struct common_audit_data ad;
567848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5679effad8dfSPaul Moore 	char *addrp;
5680effad8dfSPaul Moore 	u8 secmark_active;
5681effad8dfSPaul Moore 	u8 peerlbl_active;
5682effad8dfSPaul Moore 
5683effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5684effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5685effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5686effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5687aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5688d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5689c0828e50SPaul Moore 
5690effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
56912be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5692effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5693effad8dfSPaul Moore 		return NF_ACCEPT;
5694effad8dfSPaul Moore 
569554abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5696c0828e50SPaul Moore 
5697effad8dfSPaul Moore #ifdef CONFIG_XFRM
5698effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5699effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5700effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5701effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5702effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5703c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5704c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5705c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5706c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5707c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5708c0828e50SPaul Moore 	 *       connection. */
5709c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5710e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5711effad8dfSPaul Moore 		return NF_ACCEPT;
5712effad8dfSPaul Moore #endif
5713effad8dfSPaul Moore 
5714d8395c87SPaul Moore 	if (sk == NULL) {
5715446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5716446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5717446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5718446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
57194a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5720d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5721d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
572204f6d70fSEric Paris 				return NF_DROP;
57234a7ab3dcSSteffen Klassert 		} else {
57244a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5725d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
57264a7ab3dcSSteffen Klassert 		}
5727e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5728446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5729446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5730446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5731446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5732446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5733446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5734446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5735446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5736446b8024SPaul Moore 		 * for similar problems. */
5737446b8024SPaul Moore 		u32 skb_sid;
5738e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5739e446f9dfSEric Dumazet 
5740e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5741446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5742446b8024SPaul Moore 			return NF_DROP;
5743c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5744c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5745c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5746c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5747c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5748c0828e50SPaul Moore 		 * pass the packet. */
5749c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5750c0828e50SPaul Moore 			switch (family) {
5751c0828e50SPaul Moore 			case PF_INET:
5752c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5753c0828e50SPaul Moore 					return NF_ACCEPT;
5754c0828e50SPaul Moore 				break;
5755c0828e50SPaul Moore 			case PF_INET6:
5756c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5757c0828e50SPaul Moore 					return NF_ACCEPT;
5758a7a91a19SPaul Moore 				break;
5759c0828e50SPaul Moore 			default:
5760c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5761c0828e50SPaul Moore 			}
5762c0828e50SPaul Moore 		}
5763446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5764446b8024SPaul Moore 			return NF_DROP;
5765446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5766d8395c87SPaul Moore 	} else {
5767446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5768446b8024SPaul Moore 		 * associated socket. */
5769effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5770effad8dfSPaul Moore 		peer_sid = sksec->sid;
5771effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5772effad8dfSPaul Moore 	}
5773effad8dfSPaul Moore 
577450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
577548c62af6SEric Paris 	ad.u.net = &net;
577648c62af6SEric Paris 	ad.u.net->netif = ifindex;
577748c62af6SEric Paris 	ad.u.net->family = family;
5778d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
577904f6d70fSEric Paris 		return NF_DROP;
5780d8395c87SPaul Moore 
5781effad8dfSPaul Moore 	if (secmark_active)
57826b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57836b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5784effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
57851f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5786effad8dfSPaul Moore 
5787effad8dfSPaul Moore 	if (peerlbl_active) {
5788effad8dfSPaul Moore 		u32 if_sid;
5789effad8dfSPaul Moore 		u32 node_sid;
5790effad8dfSPaul Moore 
5791cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
579204f6d70fSEric Paris 			return NF_DROP;
57936b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57946b6bc620SStephen Smalley 				 peer_sid, if_sid,
5795effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
57961f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5797effad8dfSPaul Moore 
5798effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
579904f6d70fSEric Paris 			return NF_DROP;
58006b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58016b6bc620SStephen Smalley 				 peer_sid, node_sid,
5802effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
58031f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5804effad8dfSPaul Moore 	}
5805effad8dfSPaul Moore 
5806effad8dfSPaul Moore 	return NF_ACCEPT;
5807effad8dfSPaul Moore }
5808effad8dfSPaul Moore 
580906198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5810a224be76SDavid S. Miller 					   struct sk_buff *skb,
5811238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
58121da177e4SLinus Torvalds {
5813238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
58141da177e4SLinus Torvalds }
58151da177e4SLinus Torvalds 
58161a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
581706198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5818a224be76SDavid S. Miller 					   struct sk_buff *skb,
5819238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
58201da177e4SLinus Torvalds {
5821238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
58221da177e4SLinus Torvalds }
58231da177e4SLinus Torvalds #endif	/* IPV6 */
58241da177e4SLinus Torvalds 
58251da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
58261da177e4SLinus Torvalds 
58271da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
58281da177e4SLinus Torvalds {
5829df4779b5SHuaisheng Ye 	int err = 0;
5830df4779b5SHuaisheng Ye 	u32 perm;
5831df4779b5SHuaisheng Ye 	struct nlmsghdr *nlh;
5832df4779b5SHuaisheng Ye 	struct sk_security_struct *sksec = sk->sk_security;
5833df4779b5SHuaisheng Ye 
5834df4779b5SHuaisheng Ye 	if (skb->len < NLMSG_HDRLEN) {
5835df4779b5SHuaisheng Ye 		err = -EINVAL;
5836df4779b5SHuaisheng Ye 		goto out;
5837df4779b5SHuaisheng Ye 	}
5838df4779b5SHuaisheng Ye 	nlh = nlmsg_hdr(skb);
5839df4779b5SHuaisheng Ye 
5840df4779b5SHuaisheng Ye 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
5841df4779b5SHuaisheng Ye 	if (err) {
5842df4779b5SHuaisheng Ye 		if (err == -EINVAL) {
5843df4779b5SHuaisheng Ye 			pr_warn_ratelimited("SELinux: unrecognized netlink"
5844df4779b5SHuaisheng Ye 			       " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5845df4779b5SHuaisheng Ye 			       " pid=%d comm=%s\n",
5846df4779b5SHuaisheng Ye 			       sk->sk_protocol, nlh->nlmsg_type,
5847df4779b5SHuaisheng Ye 			       secclass_map[sksec->sclass - 1].name,
5848df4779b5SHuaisheng Ye 			       task_pid_nr(current), current->comm);
5849df4779b5SHuaisheng Ye 			if (!enforcing_enabled(&selinux_state) ||
5850df4779b5SHuaisheng Ye 			    security_get_allow_unknown(&selinux_state))
5851df4779b5SHuaisheng Ye 				err = 0;
5852df4779b5SHuaisheng Ye 		}
5853df4779b5SHuaisheng Ye 
5854df4779b5SHuaisheng Ye 		/* Ignore */
5855df4779b5SHuaisheng Ye 		if (err == -ENOENT)
5856df4779b5SHuaisheng Ye 			err = 0;
5857df4779b5SHuaisheng Ye 		goto out;
5858df4779b5SHuaisheng Ye 	}
5859df4779b5SHuaisheng Ye 
5860df4779b5SHuaisheng Ye 	err = sock_has_perm(sk, perm);
5861df4779b5SHuaisheng Ye out:
5862df4779b5SHuaisheng Ye 	return err;
58631da177e4SLinus Torvalds }
58641da177e4SLinus Torvalds 
5865ecd5f82eSCasey Schaufler static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
58661da177e4SLinus Torvalds {
58671da177e4SLinus Torvalds 	isec->sclass = sclass;
5868be0554c9SStephen Smalley 	isec->sid = current_sid();
58691da177e4SLinus Torvalds }
58701da177e4SLinus Torvalds 
58711da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
58726af963f1SStephen Smalley 			u32 perms)
58731da177e4SLinus Torvalds {
58741da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58752bf49690SThomas Liu 	struct common_audit_data ad;
5876275bb41eSDavid Howells 	u32 sid = current_sid();
58771da177e4SLinus Torvalds 
58787c653828SCasey Schaufler 	isec = selinux_ipc(ipc_perms);
58791da177e4SLinus Torvalds 
588050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
58811da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
58821da177e4SLinus Torvalds 
58836b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
58846b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
58851da177e4SLinus Torvalds }
58861da177e4SLinus Torvalds 
58871da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
58881da177e4SLinus Torvalds {
5889b82f3f68SHuaisheng Ye 	struct msg_security_struct *msec;
5890b82f3f68SHuaisheng Ye 
5891b82f3f68SHuaisheng Ye 	msec = selinux_msg_msg(msg);
5892b82f3f68SHuaisheng Ye 	msec->sid = SECINITSID_UNLABELED;
5893b82f3f68SHuaisheng Ye 
5894b82f3f68SHuaisheng Ye 	return 0;
58951da177e4SLinus Torvalds }
58961da177e4SLinus Torvalds 
58971da177e4SLinus Torvalds /* message queue security operations */
5898d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
58991da177e4SLinus Torvalds {
59001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59012bf49690SThomas Liu 	struct common_audit_data ad;
5902275bb41eSDavid Howells 	u32 sid = current_sid();
59031da177e4SLinus Torvalds 	int rc;
59041da177e4SLinus Torvalds 
5905ecd5f82eSCasey Schaufler 	isec = selinux_ipc(msq);
5906ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_MSGQ);
59071da177e4SLinus Torvalds 
590850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5909d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59101da177e4SLinus Torvalds 
59116b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59126b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
59131da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
59141da177e4SLinus Torvalds 	return rc;
59151da177e4SLinus Torvalds }
59161da177e4SLinus Torvalds 
5917d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
59181da177e4SLinus Torvalds {
59191da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59202bf49690SThomas Liu 	struct common_audit_data ad;
5921275bb41eSDavid Howells 	u32 sid = current_sid();
59221da177e4SLinus Torvalds 
59237c653828SCasey Schaufler 	isec = selinux_ipc(msq);
59241da177e4SLinus Torvalds 
592550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5926d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59271da177e4SLinus Torvalds 
59286b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59296b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
59301da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
59311da177e4SLinus Torvalds }
59321da177e4SLinus Torvalds 
5933d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
59341da177e4SLinus Torvalds {
59351da177e4SLinus Torvalds 	int err;
59361da177e4SLinus Torvalds 	int perms;
59371da177e4SLinus Torvalds 
59381da177e4SLinus Torvalds 	switch (cmd) {
59391da177e4SLinus Torvalds 	case IPC_INFO:
59401da177e4SLinus Torvalds 	case MSG_INFO:
59411da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
59426b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
59436b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
5944be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
59451da177e4SLinus Torvalds 	case IPC_STAT:
59461da177e4SLinus Torvalds 	case MSG_STAT:
594723c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
59481da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
59491da177e4SLinus Torvalds 		break;
59501da177e4SLinus Torvalds 	case IPC_SET:
59511da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
59521da177e4SLinus Torvalds 		break;
59531da177e4SLinus Torvalds 	case IPC_RMID:
59541da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
59551da177e4SLinus Torvalds 		break;
59561da177e4SLinus Torvalds 	default:
59571da177e4SLinus Torvalds 		return 0;
59581da177e4SLinus Torvalds 	}
59591da177e4SLinus Torvalds 
5960d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
59611da177e4SLinus Torvalds 	return err;
59621da177e4SLinus Torvalds }
59631da177e4SLinus Torvalds 
5964d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
59651da177e4SLinus Torvalds {
59661da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59671da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59682bf49690SThomas Liu 	struct common_audit_data ad;
5969275bb41eSDavid Howells 	u32 sid = current_sid();
59701da177e4SLinus Torvalds 	int rc;
59711da177e4SLinus Torvalds 
59727c653828SCasey Schaufler 	isec = selinux_ipc(msq);
59737c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
59741da177e4SLinus Torvalds 
59751da177e4SLinus Torvalds 	/*
59761da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
59771da177e4SLinus Torvalds 	 */
59781da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
59791da177e4SLinus Torvalds 		/*
59801da177e4SLinus Torvalds 		 * Compute new sid based on current process and
59811da177e4SLinus Torvalds 		 * message queue this message will be stored in
59821da177e4SLinus Torvalds 		 */
5983aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
5984aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
59851da177e4SLinus Torvalds 		if (rc)
59861da177e4SLinus Torvalds 			return rc;
59871da177e4SLinus Torvalds 	}
59881da177e4SLinus Torvalds 
598950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5990d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59911da177e4SLinus Torvalds 
59921da177e4SLinus Torvalds 	/* Can this process write to the queue? */
59936b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59946b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
59951da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
59961da177e4SLinus Torvalds 	if (!rc)
59971da177e4SLinus Torvalds 		/* Can this process send the message */
59986b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59996b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
6000275bb41eSDavid Howells 				  MSG__SEND, &ad);
60011da177e4SLinus Torvalds 	if (!rc)
60021da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
60036b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
60046b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
6005275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
60061da177e4SLinus Torvalds 
60071da177e4SLinus Torvalds 	return rc;
60081da177e4SLinus Torvalds }
60091da177e4SLinus Torvalds 
6010d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
60111da177e4SLinus Torvalds 				    struct task_struct *target,
60121da177e4SLinus Torvalds 				    long type, int mode)
60131da177e4SLinus Torvalds {
60141da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60151da177e4SLinus Torvalds 	struct msg_security_struct *msec;
60162bf49690SThomas Liu 	struct common_audit_data ad;
6017275bb41eSDavid Howells 	u32 sid = task_sid(target);
60181da177e4SLinus Torvalds 	int rc;
60191da177e4SLinus Torvalds 
60207c653828SCasey Schaufler 	isec = selinux_ipc(msq);
60217c653828SCasey Schaufler 	msec = selinux_msg_msg(msg);
60221da177e4SLinus Torvalds 
602350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6024d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60251da177e4SLinus Torvalds 
60266b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60276b6bc620SStephen Smalley 			  sid, isec->sid,
60281da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
60291da177e4SLinus Torvalds 	if (!rc)
60306b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
60316b6bc620SStephen Smalley 				  sid, msec->sid,
60321da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
60331da177e4SLinus Torvalds 	return rc;
60341da177e4SLinus Torvalds }
60351da177e4SLinus Torvalds 
60361da177e4SLinus Torvalds /* Shared Memory security operations */
60377191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
60381da177e4SLinus Torvalds {
60391da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60402bf49690SThomas Liu 	struct common_audit_data ad;
6041275bb41eSDavid Howells 	u32 sid = current_sid();
60421da177e4SLinus Torvalds 	int rc;
60431da177e4SLinus Torvalds 
6044ecd5f82eSCasey Schaufler 	isec = selinux_ipc(shp);
6045ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SHM);
60461da177e4SLinus Torvalds 
604750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
60487191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
60491da177e4SLinus Torvalds 
60506b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60516b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
60521da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
60531da177e4SLinus Torvalds 	return rc;
60541da177e4SLinus Torvalds }
60551da177e4SLinus Torvalds 
60567191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
60571da177e4SLinus Torvalds {
60581da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60592bf49690SThomas Liu 	struct common_audit_data ad;
6060275bb41eSDavid Howells 	u32 sid = current_sid();
60611da177e4SLinus Torvalds 
60627c653828SCasey Schaufler 	isec = selinux_ipc(shp);
60631da177e4SLinus Torvalds 
606450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
60657191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
60661da177e4SLinus Torvalds 
60676b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60686b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
60691da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
60701da177e4SLinus Torvalds }
60711da177e4SLinus Torvalds 
60721da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
60737191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
60741da177e4SLinus Torvalds {
60751da177e4SLinus Torvalds 	int perms;
60761da177e4SLinus Torvalds 	int err;
60771da177e4SLinus Torvalds 
60781da177e4SLinus Torvalds 	switch (cmd) {
60791da177e4SLinus Torvalds 	case IPC_INFO:
60801da177e4SLinus Torvalds 	case SHM_INFO:
60811da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60826b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60836b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6084be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60851da177e4SLinus Torvalds 	case IPC_STAT:
60861da177e4SLinus Torvalds 	case SHM_STAT:
6087c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
60881da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
60891da177e4SLinus Torvalds 		break;
60901da177e4SLinus Torvalds 	case IPC_SET:
60911da177e4SLinus Torvalds 		perms = SHM__SETATTR;
60921da177e4SLinus Torvalds 		break;
60931da177e4SLinus Torvalds 	case SHM_LOCK:
60941da177e4SLinus Torvalds 	case SHM_UNLOCK:
60951da177e4SLinus Torvalds 		perms = SHM__LOCK;
60961da177e4SLinus Torvalds 		break;
60971da177e4SLinus Torvalds 	case IPC_RMID:
60981da177e4SLinus Torvalds 		perms = SHM__DESTROY;
60991da177e4SLinus Torvalds 		break;
61001da177e4SLinus Torvalds 	default:
61011da177e4SLinus Torvalds 		return 0;
61021da177e4SLinus Torvalds 	}
61031da177e4SLinus Torvalds 
61047191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
61051da177e4SLinus Torvalds 	return err;
61061da177e4SLinus Torvalds }
61071da177e4SLinus Torvalds 
61087191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
61091da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
61101da177e4SLinus Torvalds {
61111da177e4SLinus Torvalds 	u32 perms;
61121da177e4SLinus Torvalds 
61131da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
61141da177e4SLinus Torvalds 		perms = SHM__READ;
61151da177e4SLinus Torvalds 	else
61161da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
61171da177e4SLinus Torvalds 
61187191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
61191da177e4SLinus Torvalds }
61201da177e4SLinus Torvalds 
61211da177e4SLinus Torvalds /* Semaphore security operations */
6122aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
61231da177e4SLinus Torvalds {
61241da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61252bf49690SThomas Liu 	struct common_audit_data ad;
6126275bb41eSDavid Howells 	u32 sid = current_sid();
61271da177e4SLinus Torvalds 	int rc;
61281da177e4SLinus Torvalds 
6129ecd5f82eSCasey Schaufler 	isec = selinux_ipc(sma);
6130ecd5f82eSCasey Schaufler 	ipc_init_security(isec, SECCLASS_SEM);
61311da177e4SLinus Torvalds 
613250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6133aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
61341da177e4SLinus Torvalds 
61356b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61366b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
61371da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
61381da177e4SLinus Torvalds 	return rc;
61391da177e4SLinus Torvalds }
61401da177e4SLinus Torvalds 
6141aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
61421da177e4SLinus Torvalds {
61431da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61442bf49690SThomas Liu 	struct common_audit_data ad;
6145275bb41eSDavid Howells 	u32 sid = current_sid();
61461da177e4SLinus Torvalds 
61477c653828SCasey Schaufler 	isec = selinux_ipc(sma);
61481da177e4SLinus Torvalds 
614950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6150aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
61511da177e4SLinus Torvalds 
61526b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
61536b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
61541da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
61551da177e4SLinus Torvalds }
61561da177e4SLinus Torvalds 
61571da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6158aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
61591da177e4SLinus Torvalds {
61601da177e4SLinus Torvalds 	int err;
61611da177e4SLinus Torvalds 	u32 perms;
61621da177e4SLinus Torvalds 
61631da177e4SLinus Torvalds 	switch (cmd) {
61641da177e4SLinus Torvalds 	case IPC_INFO:
61651da177e4SLinus Torvalds 	case SEM_INFO:
61661da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
61676b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
61686b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6169be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
61701da177e4SLinus Torvalds 	case GETPID:
61711da177e4SLinus Torvalds 	case GETNCNT:
61721da177e4SLinus Torvalds 	case GETZCNT:
61731da177e4SLinus Torvalds 		perms = SEM__GETATTR;
61741da177e4SLinus Torvalds 		break;
61751da177e4SLinus Torvalds 	case GETVAL:
61761da177e4SLinus Torvalds 	case GETALL:
61771da177e4SLinus Torvalds 		perms = SEM__READ;
61781da177e4SLinus Torvalds 		break;
61791da177e4SLinus Torvalds 	case SETVAL:
61801da177e4SLinus Torvalds 	case SETALL:
61811da177e4SLinus Torvalds 		perms = SEM__WRITE;
61821da177e4SLinus Torvalds 		break;
61831da177e4SLinus Torvalds 	case IPC_RMID:
61841da177e4SLinus Torvalds 		perms = SEM__DESTROY;
61851da177e4SLinus Torvalds 		break;
61861da177e4SLinus Torvalds 	case IPC_SET:
61871da177e4SLinus Torvalds 		perms = SEM__SETATTR;
61881da177e4SLinus Torvalds 		break;
61891da177e4SLinus Torvalds 	case IPC_STAT:
61901da177e4SLinus Torvalds 	case SEM_STAT:
6191a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
61921da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
61931da177e4SLinus Torvalds 		break;
61941da177e4SLinus Torvalds 	default:
61951da177e4SLinus Torvalds 		return 0;
61961da177e4SLinus Torvalds 	}
61971da177e4SLinus Torvalds 
6198aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
61991da177e4SLinus Torvalds 	return err;
62001da177e4SLinus Torvalds }
62011da177e4SLinus Torvalds 
6202aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
62031da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
62041da177e4SLinus Torvalds {
62051da177e4SLinus Torvalds 	u32 perms;
62061da177e4SLinus Torvalds 
62071da177e4SLinus Torvalds 	if (alter)
62081da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
62091da177e4SLinus Torvalds 	else
62101da177e4SLinus Torvalds 		perms = SEM__READ;
62111da177e4SLinus Torvalds 
6212aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
62131da177e4SLinus Torvalds }
62141da177e4SLinus Torvalds 
62151da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
62161da177e4SLinus Torvalds {
62171da177e4SLinus Torvalds 	u32 av = 0;
62181da177e4SLinus Torvalds 
62191da177e4SLinus Torvalds 	av = 0;
62201da177e4SLinus Torvalds 	if (flag & S_IRUGO)
62211da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
62221da177e4SLinus Torvalds 	if (flag & S_IWUGO)
62231da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
62241da177e4SLinus Torvalds 
62251da177e4SLinus Torvalds 	if (av == 0)
62261da177e4SLinus Torvalds 		return 0;
62271da177e4SLinus Torvalds 
62286af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
62291da177e4SLinus Torvalds }
62301da177e4SLinus Torvalds 
6231713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6232713a04aeSAhmed S. Darwish {
62337c653828SCasey Schaufler 	struct ipc_security_struct *isec = selinux_ipc(ipcp);
6234713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6235713a04aeSAhmed S. Darwish }
6236713a04aeSAhmed S. Darwish 
62371da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
62381da177e4SLinus Torvalds {
62391da177e4SLinus Torvalds 	if (inode)
62401da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
62411da177e4SLinus Torvalds }
62421da177e4SLinus Torvalds 
62431da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
624404ff9708SAl Viro 			       char *name, char **value)
62451da177e4SLinus Torvalds {
6246275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
62478c8570fbSDustin Kirkland 	u32 sid;
62481da177e4SLinus Torvalds 	int error;
624904ff9708SAl Viro 	unsigned len;
62501da177e4SLinus Torvalds 
6251275bb41eSDavid Howells 	rcu_read_lock();
62520c6cfa62SCasey Schaufler 	__tsec = selinux_cred(__task_cred(p));
62531da177e4SLinus Torvalds 
6254be0554c9SStephen Smalley 	if (current != p) {
62556b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62566b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6257be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6258be0554c9SStephen Smalley 		if (error)
6259be0554c9SStephen Smalley 			goto bad;
6260be0554c9SStephen Smalley 	}
6261be0554c9SStephen Smalley 
62621da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6263275bb41eSDavid Howells 		sid = __tsec->sid;
62641da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6265275bb41eSDavid Howells 		sid = __tsec->osid;
62661da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6267275bb41eSDavid Howells 		sid = __tsec->exec_sid;
62681da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6269275bb41eSDavid Howells 		sid = __tsec->create_sid;
62704eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6271275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
627242c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6273275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6274be0554c9SStephen Smalley 	else {
6275be0554c9SStephen Smalley 		error = -EINVAL;
6276be0554c9SStephen Smalley 		goto bad;
6277be0554c9SStephen Smalley 	}
6278275bb41eSDavid Howells 	rcu_read_unlock();
62791da177e4SLinus Torvalds 
62801da177e4SLinus Torvalds 	if (!sid)
62811da177e4SLinus Torvalds 		return 0;
62821da177e4SLinus Torvalds 
6283aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
628404ff9708SAl Viro 	if (error)
628504ff9708SAl Viro 		return error;
628604ff9708SAl Viro 	return len;
6287275bb41eSDavid Howells 
6288be0554c9SStephen Smalley bad:
6289275bb41eSDavid Howells 	rcu_read_unlock();
6290be0554c9SStephen Smalley 	return error;
62911da177e4SLinus Torvalds }
62921da177e4SLinus Torvalds 
6293b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
62941da177e4SLinus Torvalds {
62951da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6296d84f4f99SDavid Howells 	struct cred *new;
6297be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
62981da177e4SLinus Torvalds 	int error;
62991da177e4SLinus Torvalds 	char *str = value;
63001da177e4SLinus Torvalds 
63011da177e4SLinus Torvalds 	/*
63021da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
63031da177e4SLinus Torvalds 	 */
63041da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
63056b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63066b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6307be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
63081da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
63096b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63106b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6311be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
63124eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
63136b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63146b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6315be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
631642c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
63176b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63186b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6319be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
63201da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
63216b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63226b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6323be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
63241da177e4SLinus Torvalds 	else
63251da177e4SLinus Torvalds 		error = -EINVAL;
63261da177e4SLinus Torvalds 	if (error)
63271da177e4SLinus Torvalds 		return error;
63281da177e4SLinus Torvalds 
63291da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6330a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
63311da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
63321da177e4SLinus Torvalds 			str[size-1] = 0;
63331da177e4SLinus Torvalds 			size--;
63341da177e4SLinus Torvalds 		}
6335aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6336aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
633712b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6338db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6339d6ea83ecSEric Paris 				struct audit_buffer *ab;
6340d6ea83ecSEric Paris 				size_t audit_size;
6341d6ea83ecSEric Paris 
6342d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6343d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6344d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6345d6ea83ecSEric Paris 					audit_size = size - 1;
6346d6ea83ecSEric Paris 				else
6347d6ea83ecSEric Paris 					audit_size = size;
6348cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6349cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6350cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6351d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6352d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6353d6ea83ecSEric Paris 				audit_log_end(ab);
6354d6ea83ecSEric Paris 
635512b29f34SStephen Smalley 				return error;
6356d6ea83ecSEric Paris 			}
6357aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6358aa8e712cSStephen Smalley 						      &selinux_state,
6359aa8e712cSStephen Smalley 						      value, size, &sid);
636012b29f34SStephen Smalley 		}
63611da177e4SLinus Torvalds 		if (error)
63621da177e4SLinus Torvalds 			return error;
63631da177e4SLinus Torvalds 	}
63641da177e4SLinus Torvalds 
6365d84f4f99SDavid Howells 	new = prepare_creds();
6366d84f4f99SDavid Howells 	if (!new)
6367d84f4f99SDavid Howells 		return -ENOMEM;
6368d84f4f99SDavid Howells 
63691da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
63701da177e4SLinus Torvalds 	   performed during the actual operation (execve,
63711da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6372d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
63731da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
63741da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
63750c6cfa62SCasey Schaufler 	tsec = selinux_cred(new);
6376d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
63771da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6378d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
63791da177e4SLinus Torvalds 		tsec->create_sid = sid;
6380d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
6381464c258aSOndrej Mosnacek 		if (sid) {
6382464c258aSOndrej Mosnacek 			error = avc_has_perm(&selinux_state, mysid, sid,
6383464c258aSOndrej Mosnacek 					     SECCLASS_KEY, KEY__CREATE, NULL);
63844eb582cfSMichael LeMay 			if (error)
6385d84f4f99SDavid Howells 				goto abort_change;
6386464c258aSOndrej Mosnacek 		}
63874eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6388d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
638942c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6390d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6391d84f4f99SDavid Howells 		error = -EINVAL;
63921da177e4SLinus Torvalds 		if (sid == 0)
6393d84f4f99SDavid Howells 			goto abort_change;
6394d9250deaSKaiGai Kohei 
6395d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6396d84f4f99SDavid Howells 		error = -EPERM;
63975bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6398aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6399aa8e712cSStephen Smalley 							    tsec->sid, sid);
6400d84f4f99SDavid Howells 			if (error)
6401d84f4f99SDavid Howells 				goto abort_change;
64021da177e4SLinus Torvalds 		}
64031da177e4SLinus Torvalds 
64041da177e4SLinus Torvalds 		/* Check permissions for the transition. */
64056b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64066b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
64071da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
64081da177e4SLinus Torvalds 		if (error)
6409d84f4f99SDavid Howells 			goto abort_change;
64101da177e4SLinus Torvalds 
64111da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
64121da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6413be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
64140c6181cbSPaul Moore 		if (ptsid != 0) {
64156b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
64166b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6417d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6418d84f4f99SDavid Howells 			if (error)
6419d84f4f99SDavid Howells 				goto abort_change;
6420d84f4f99SDavid Howells 		}
6421d84f4f99SDavid Howells 
6422d84f4f99SDavid Howells 		tsec->sid = sid;
6423d84f4f99SDavid Howells 	} else {
6424d84f4f99SDavid Howells 		error = -EINVAL;
6425d84f4f99SDavid Howells 		goto abort_change;
6426d84f4f99SDavid Howells 	}
6427d84f4f99SDavid Howells 
6428d84f4f99SDavid Howells 	commit_creds(new);
64291da177e4SLinus Torvalds 	return size;
6430d84f4f99SDavid Howells 
6431d84f4f99SDavid Howells abort_change:
6432d84f4f99SDavid Howells 	abort_creds(new);
6433d84f4f99SDavid Howells 	return error;
64341da177e4SLinus Torvalds }
64351da177e4SLinus Torvalds 
6436746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6437746df9b5SDavid Quigley {
6438746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6439746df9b5SDavid Quigley }
6440746df9b5SDavid Quigley 
6441dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6442dc49c1f9SCatherine Zhang {
6443aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6444aa8e712cSStephen Smalley 				       secdata, seclen);
6445dc49c1f9SCatherine Zhang }
6446dc49c1f9SCatherine Zhang 
64477bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
644863cb3449SDavid Howells {
6449aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6450aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
645163cb3449SDavid Howells }
645263cb3449SDavid Howells 
6453dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6454dc49c1f9SCatherine Zhang {
6455dc49c1f9SCatherine Zhang 	kfree(secdata);
6456dc49c1f9SCatherine Zhang }
6457dc49c1f9SCatherine Zhang 
64586f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
64596f3be9f5SAndreas Gruenbacher {
646080788c22SCasey Schaufler 	struct inode_security_struct *isec = selinux_inode(inode);
64616f3be9f5SAndreas Gruenbacher 
64629287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
64636f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
64649287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
64656f3be9f5SAndreas Gruenbacher }
64666f3be9f5SAndreas Gruenbacher 
64671ee65e37SDavid P. Quigley /*
64681ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
64691ee65e37SDavid P. Quigley  */
64701ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
64711ee65e37SDavid P. Quigley {
647253e0c2aaSOndrej Mosnacek 	int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
647353e0c2aaSOndrej Mosnacek 					   ctx, ctxlen, 0);
647453e0c2aaSOndrej Mosnacek 	/* Do not return error when suppressing label (SBLABEL_MNT not set). */
647553e0c2aaSOndrej Mosnacek 	return rc == -EOPNOTSUPP ? 0 : rc;
64761ee65e37SDavid P. Quigley }
64771ee65e37SDavid P. Quigley 
64781ee65e37SDavid P. Quigley /*
64791ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
64801ee65e37SDavid P. Quigley  */
64811ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
64821ee65e37SDavid P. Quigley {
64831ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
64841ee65e37SDavid P. Quigley }
64851ee65e37SDavid P. Quigley 
64861ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
64871ee65e37SDavid P. Quigley {
64881ee65e37SDavid P. Quigley 	int len = 0;
64891ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
64901ee65e37SDavid P. Quigley 						ctx, true);
64911ee65e37SDavid P. Quigley 	if (len < 0)
64921ee65e37SDavid P. Quigley 		return len;
64931ee65e37SDavid P. Quigley 	*ctxlen = len;
64941ee65e37SDavid P. Quigley 	return 0;
64951ee65e37SDavid P. Quigley }
6496d720024eSMichael LeMay #ifdef CONFIG_KEYS
6497d720024eSMichael LeMay 
6498d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
64997e047ef5SDavid Howells 			     unsigned long flags)
6500d720024eSMichael LeMay {
6501d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6502d720024eSMichael LeMay 	struct key_security_struct *ksec;
6503d720024eSMichael LeMay 
6504d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6505d720024eSMichael LeMay 	if (!ksec)
6506d720024eSMichael LeMay 		return -ENOMEM;
6507d720024eSMichael LeMay 
65080c6cfa62SCasey Schaufler 	tsec = selinux_cred(cred);
6509d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6510d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
65114eb582cfSMichael LeMay 	else
6512d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6513d720024eSMichael LeMay 
6514275bb41eSDavid Howells 	k->security = ksec;
6515d720024eSMichael LeMay 	return 0;
6516d720024eSMichael LeMay }
6517d720024eSMichael LeMay 
6518d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6519d720024eSMichael LeMay {
6520d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6521d720024eSMichael LeMay 
6522d720024eSMichael LeMay 	k->security = NULL;
6523d720024eSMichael LeMay 	kfree(ksec);
6524d720024eSMichael LeMay }
6525d720024eSMichael LeMay 
6526d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6527d84f4f99SDavid Howells 				  const struct cred *cred,
6528f5895943SDavid Howells 				  unsigned perm)
6529d720024eSMichael LeMay {
6530d720024eSMichael LeMay 	struct key *key;
6531d720024eSMichael LeMay 	struct key_security_struct *ksec;
6532275bb41eSDavid Howells 	u32 sid;
6533d720024eSMichael LeMay 
6534d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
6535d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
6536d720024eSMichael LeMay 	   appear to be created. */
6537d720024eSMichael LeMay 	if (perm == 0)
6538d720024eSMichael LeMay 		return 0;
6539d720024eSMichael LeMay 
6540d84f4f99SDavid Howells 	sid = cred_sid(cred);
6541275bb41eSDavid Howells 
6542275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6543275bb41eSDavid Howells 	ksec = key->security;
6544275bb41eSDavid Howells 
65456b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
6546028db3e2SLinus Torvalds 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6547d720024eSMichael LeMay }
6548d720024eSMichael LeMay 
654970a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
655070a5bb72SDavid Howells {
655170a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
655270a5bb72SDavid Howells 	char *context = NULL;
655370a5bb72SDavid Howells 	unsigned len;
655470a5bb72SDavid Howells 	int rc;
655570a5bb72SDavid Howells 
6556aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6557aa8e712cSStephen Smalley 				     &context, &len);
655870a5bb72SDavid Howells 	if (!rc)
655970a5bb72SDavid Howells 		rc = len;
656070a5bb72SDavid Howells 	*_buffer = context;
656170a5bb72SDavid Howells 	return rc;
656270a5bb72SDavid Howells }
65633a976fa6SDaniel Jurgens #endif
656470a5bb72SDavid Howells 
65653a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6566cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6567cfc4d882SDaniel Jurgens {
6568cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6569cfc4d882SDaniel Jurgens 	int err;
6570cfc4d882SDaniel Jurgens 	u32 sid = 0;
6571cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6572cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6573cfc4d882SDaniel Jurgens 
6574409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6575cfc4d882SDaniel Jurgens 	if (err)
6576cfc4d882SDaniel Jurgens 		return err;
6577cfc4d882SDaniel Jurgens 
6578cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6579cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6580cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6581cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
65826b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65836b6bc620SStephen Smalley 			    sec->sid, sid,
6584cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6585cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6586cfc4d882SDaniel Jurgens }
6587cfc4d882SDaniel Jurgens 
6588ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6589ab861dfcSDaniel Jurgens 					    u8 port_num)
6590ab861dfcSDaniel Jurgens {
6591ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6592ab861dfcSDaniel Jurgens 	int err;
6593ab861dfcSDaniel Jurgens 	u32 sid = 0;
6594ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6595ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6596ab861dfcSDaniel Jurgens 
6597aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6598aa8e712cSStephen Smalley 				      &sid);
6599ab861dfcSDaniel Jurgens 
6600ab861dfcSDaniel Jurgens 	if (err)
6601ab861dfcSDaniel Jurgens 		return err;
6602ab861dfcSDaniel Jurgens 
6603ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6604ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6605ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6606ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
66076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66086b6bc620SStephen Smalley 			    sec->sid, sid,
6609ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6610ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6611ab861dfcSDaniel Jurgens }
6612ab861dfcSDaniel Jurgens 
66133a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
66143a976fa6SDaniel Jurgens {
66153a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
66163a976fa6SDaniel Jurgens 
66173a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
66183a976fa6SDaniel Jurgens 	if (!sec)
66193a976fa6SDaniel Jurgens 		return -ENOMEM;
66203a976fa6SDaniel Jurgens 	sec->sid = current_sid();
66213a976fa6SDaniel Jurgens 
66223a976fa6SDaniel Jurgens 	*ib_sec = sec;
66233a976fa6SDaniel Jurgens 	return 0;
66243a976fa6SDaniel Jurgens }
66253a976fa6SDaniel Jurgens 
66263a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
66273a976fa6SDaniel Jurgens {
66283a976fa6SDaniel Jurgens 	kfree(ib_sec);
66293a976fa6SDaniel Jurgens }
6630d720024eSMichael LeMay #endif
6631d720024eSMichael LeMay 
6632ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6633ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6634ec27c356SChenbo Feng 				     unsigned int size)
6635ec27c356SChenbo Feng {
6636ec27c356SChenbo Feng 	u32 sid = current_sid();
6637ec27c356SChenbo Feng 	int ret;
6638ec27c356SChenbo Feng 
6639ec27c356SChenbo Feng 	switch (cmd) {
6640ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
66416b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66426b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6643ec27c356SChenbo Feng 				   NULL);
6644ec27c356SChenbo Feng 		break;
6645ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
66466b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66476b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6648ec27c356SChenbo Feng 				   NULL);
6649ec27c356SChenbo Feng 		break;
6650ec27c356SChenbo Feng 	default:
6651ec27c356SChenbo Feng 		ret = 0;
6652ec27c356SChenbo Feng 		break;
6653ec27c356SChenbo Feng 	}
6654ec27c356SChenbo Feng 
6655ec27c356SChenbo Feng 	return ret;
6656ec27c356SChenbo Feng }
6657ec27c356SChenbo Feng 
6658ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6659ec27c356SChenbo Feng {
6660ec27c356SChenbo Feng 	u32 av = 0;
6661ec27c356SChenbo Feng 
6662ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6663ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6664ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6665ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6666ec27c356SChenbo Feng 	return av;
6667ec27c356SChenbo Feng }
6668ec27c356SChenbo Feng 
6669f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6670f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6671f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6672f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6673f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6674f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6675f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6676f66e448cSChenbo Feng  */
6677f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6678f66e448cSChenbo Feng {
6679f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6680f66e448cSChenbo Feng 	struct bpf_prog *prog;
6681f66e448cSChenbo Feng 	struct bpf_map *map;
6682f66e448cSChenbo Feng 	int ret;
6683f66e448cSChenbo Feng 
6684f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6685f66e448cSChenbo Feng 		map = file->private_data;
6686f66e448cSChenbo Feng 		bpfsec = map->security;
66876b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66886b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6689f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6690f66e448cSChenbo Feng 		if (ret)
6691f66e448cSChenbo Feng 			return ret;
6692f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6693f66e448cSChenbo Feng 		prog = file->private_data;
6694f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
66956b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66966b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6697f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6698f66e448cSChenbo Feng 		if (ret)
6699f66e448cSChenbo Feng 			return ret;
6700f66e448cSChenbo Feng 	}
6701f66e448cSChenbo Feng 	return 0;
6702f66e448cSChenbo Feng }
6703f66e448cSChenbo Feng 
6704ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6705ec27c356SChenbo Feng {
6706ec27c356SChenbo Feng 	u32 sid = current_sid();
6707ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6708ec27c356SChenbo Feng 
6709ec27c356SChenbo Feng 	bpfsec = map->security;
67106b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67116b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6712ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6713ec27c356SChenbo Feng }
6714ec27c356SChenbo Feng 
6715ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6716ec27c356SChenbo Feng {
6717ec27c356SChenbo Feng 	u32 sid = current_sid();
6718ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6719ec27c356SChenbo Feng 
6720ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
67216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67226b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6723ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6724ec27c356SChenbo Feng }
6725ec27c356SChenbo Feng 
6726ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6727ec27c356SChenbo Feng {
6728ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6729ec27c356SChenbo Feng 
6730ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6731ec27c356SChenbo Feng 	if (!bpfsec)
6732ec27c356SChenbo Feng 		return -ENOMEM;
6733ec27c356SChenbo Feng 
6734ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6735ec27c356SChenbo Feng 	map->security = bpfsec;
6736ec27c356SChenbo Feng 
6737ec27c356SChenbo Feng 	return 0;
6738ec27c356SChenbo Feng }
6739ec27c356SChenbo Feng 
6740ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6741ec27c356SChenbo Feng {
6742ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6743ec27c356SChenbo Feng 
6744ec27c356SChenbo Feng 	map->security = NULL;
6745ec27c356SChenbo Feng 	kfree(bpfsec);
6746ec27c356SChenbo Feng }
6747ec27c356SChenbo Feng 
6748ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6749ec27c356SChenbo Feng {
6750ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6751ec27c356SChenbo Feng 
6752ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6753ec27c356SChenbo Feng 	if (!bpfsec)
6754ec27c356SChenbo Feng 		return -ENOMEM;
6755ec27c356SChenbo Feng 
6756ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6757ec27c356SChenbo Feng 	aux->security = bpfsec;
6758ec27c356SChenbo Feng 
6759ec27c356SChenbo Feng 	return 0;
6760ec27c356SChenbo Feng }
6761ec27c356SChenbo Feng 
6762ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6763ec27c356SChenbo Feng {
6764ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6765ec27c356SChenbo Feng 
6766ec27c356SChenbo Feng 	aux->security = NULL;
6767ec27c356SChenbo Feng 	kfree(bpfsec);
6768ec27c356SChenbo Feng }
6769ec27c356SChenbo Feng #endif
6770ec27c356SChenbo Feng 
677159438b46SStephen Smalley static int selinux_lockdown(enum lockdown_reason what)
677259438b46SStephen Smalley {
677359438b46SStephen Smalley 	struct common_audit_data ad;
677459438b46SStephen Smalley 	u32 sid = current_sid();
677559438b46SStephen Smalley 	int invalid_reason = (what <= LOCKDOWN_NONE) ||
677659438b46SStephen Smalley 			     (what == LOCKDOWN_INTEGRITY_MAX) ||
677759438b46SStephen Smalley 			     (what >= LOCKDOWN_CONFIDENTIALITY_MAX);
677859438b46SStephen Smalley 
677959438b46SStephen Smalley 	if (WARN(invalid_reason, "Invalid lockdown reason")) {
678059438b46SStephen Smalley 		audit_log(audit_context(),
678159438b46SStephen Smalley 			  GFP_ATOMIC, AUDIT_SELINUX_ERR,
678259438b46SStephen Smalley 			  "lockdown_reason=invalid");
678359438b46SStephen Smalley 		return -EINVAL;
678459438b46SStephen Smalley 	}
678559438b46SStephen Smalley 
678659438b46SStephen Smalley 	ad.type = LSM_AUDIT_DATA_LOCKDOWN;
678759438b46SStephen Smalley 	ad.u.reason = what;
678859438b46SStephen Smalley 
678959438b46SStephen Smalley 	if (what <= LOCKDOWN_INTEGRITY_MAX)
679059438b46SStephen Smalley 		return avc_has_perm(&selinux_state,
679159438b46SStephen Smalley 				    sid, sid, SECCLASS_LOCKDOWN,
679259438b46SStephen Smalley 				    LOCKDOWN__INTEGRITY, &ad);
679359438b46SStephen Smalley 	else
679459438b46SStephen Smalley 		return avc_has_perm(&selinux_state,
679559438b46SStephen Smalley 				    sid, sid, SECCLASS_LOCKDOWN,
679659438b46SStephen Smalley 				    LOCKDOWN__CONFIDENTIALITY, &ad);
679759438b46SStephen Smalley }
679859438b46SStephen Smalley 
6799bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6800bbd3662aSCasey Schaufler 	.lbs_cred = sizeof(struct task_security_struct),
680133bf60caSCasey Schaufler 	.lbs_file = sizeof(struct file_security_struct),
6802afb1cbe3SCasey Schaufler 	.lbs_inode = sizeof(struct inode_security_struct),
6803ecd5f82eSCasey Schaufler 	.lbs_ipc = sizeof(struct ipc_security_struct),
6804ecd5f82eSCasey Schaufler 	.lbs_msg_msg = sizeof(struct msg_security_struct),
6805bbd3662aSCasey Schaufler };
6806bbd3662aSCasey Schaufler 
6807da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
6808da97e184SJoel Fernandes (Google) static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6809da97e184SJoel Fernandes (Google) {
6810da97e184SJoel Fernandes (Google) 	u32 requested, sid = current_sid();
6811da97e184SJoel Fernandes (Google) 
6812da97e184SJoel Fernandes (Google) 	if (type == PERF_SECURITY_OPEN)
6813da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__OPEN;
6814da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_CPU)
6815da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__CPU;
6816da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_KERNEL)
6817da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__KERNEL;
6818da97e184SJoel Fernandes (Google) 	else if (type == PERF_SECURITY_TRACEPOINT)
6819da97e184SJoel Fernandes (Google) 		requested = PERF_EVENT__TRACEPOINT;
6820da97e184SJoel Fernandes (Google) 	else
6821da97e184SJoel Fernandes (Google) 		return -EINVAL;
6822da97e184SJoel Fernandes (Google) 
6823da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6824da97e184SJoel Fernandes (Google) 			    requested, NULL);
6825da97e184SJoel Fernandes (Google) }
6826da97e184SJoel Fernandes (Google) 
6827da97e184SJoel Fernandes (Google) static int selinux_perf_event_alloc(struct perf_event *event)
6828da97e184SJoel Fernandes (Google) {
6829da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec;
6830da97e184SJoel Fernandes (Google) 
6831da97e184SJoel Fernandes (Google) 	perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6832da97e184SJoel Fernandes (Google) 	if (!perfsec)
6833da97e184SJoel Fernandes (Google) 		return -ENOMEM;
6834da97e184SJoel Fernandes (Google) 
6835da97e184SJoel Fernandes (Google) 	perfsec->sid = current_sid();
6836da97e184SJoel Fernandes (Google) 	event->security = perfsec;
6837da97e184SJoel Fernandes (Google) 
6838da97e184SJoel Fernandes (Google) 	return 0;
6839da97e184SJoel Fernandes (Google) }
6840da97e184SJoel Fernandes (Google) 
6841da97e184SJoel Fernandes (Google) static void selinux_perf_event_free(struct perf_event *event)
6842da97e184SJoel Fernandes (Google) {
6843da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6844da97e184SJoel Fernandes (Google) 
6845da97e184SJoel Fernandes (Google) 	event->security = NULL;
6846da97e184SJoel Fernandes (Google) 	kfree(perfsec);
6847da97e184SJoel Fernandes (Google) }
6848da97e184SJoel Fernandes (Google) 
6849da97e184SJoel Fernandes (Google) static int selinux_perf_event_read(struct perf_event *event)
6850da97e184SJoel Fernandes (Google) {
6851da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6852da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
6853da97e184SJoel Fernandes (Google) 
6854da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
6855da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
6856da97e184SJoel Fernandes (Google) }
6857da97e184SJoel Fernandes (Google) 
6858da97e184SJoel Fernandes (Google) static int selinux_perf_event_write(struct perf_event *event)
6859da97e184SJoel Fernandes (Google) {
6860da97e184SJoel Fernandes (Google) 	struct perf_event_security_struct *perfsec = event->security;
6861da97e184SJoel Fernandes (Google) 	u32 sid = current_sid();
6862da97e184SJoel Fernandes (Google) 
6863da97e184SJoel Fernandes (Google) 	return avc_has_perm(&selinux_state, sid, perfsec->sid,
6864da97e184SJoel Fernandes (Google) 			    SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
6865da97e184SJoel Fernandes (Google) }
6866da97e184SJoel Fernandes (Google) #endif
6867da97e184SJoel Fernandes (Google) 
6868cfff75d8SOndrej Mosnacek /*
6869cfff75d8SOndrej Mosnacek  * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
6870cfff75d8SOndrej Mosnacek  * 1. any hooks that don't belong to (2.) or (3.) below,
6871cfff75d8SOndrej Mosnacek  * 2. hooks that both access structures allocated by other hooks, and allocate
6872cfff75d8SOndrej Mosnacek  *    structures that can be later accessed by other hooks (mostly "cloning"
6873cfff75d8SOndrej Mosnacek  *    hooks),
6874cfff75d8SOndrej Mosnacek  * 3. hooks that only allocate structures that can be later accessed by other
6875cfff75d8SOndrej Mosnacek  *    hooks ("allocating" hooks).
6876cfff75d8SOndrej Mosnacek  *
6877cfff75d8SOndrej Mosnacek  * Please follow block comment delimiters in the list to keep this order.
6878cfff75d8SOndrej Mosnacek  *
6879cfff75d8SOndrej Mosnacek  * This ordering is needed for SELinux runtime disable to work at least somewhat
6880cfff75d8SOndrej Mosnacek  * safely. Breaking the ordering rules above might lead to NULL pointer derefs
6881cfff75d8SOndrej Mosnacek  * when disabling SELinux at runtime.
6882cfff75d8SOndrej Mosnacek  */
6883ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6884e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6885e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6886e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6887e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6888076c54c5SAhmed S. Darwish 
6889e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6890e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6891e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
6892e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
6893e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
6894e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
6895e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
6896e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
6897e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
689879af7307SStephen Smalley 
6899e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
69001da177e4SLinus Torvalds 
6901e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6902e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6903e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
69041da177e4SLinus Torvalds 
6905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6906204cc0ccSAl Viro 	LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6907e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6908e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
6912e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
6913e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6914e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
69151da177e4SLinus Torvalds 
6916e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6917a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6918e0007529SEric Paris 
6919e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6920e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6921e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
6922e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
6923e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6924e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6925e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6926e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6927e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6928e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6929e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6930e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6931e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6932e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6933e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6934e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6935e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6936e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6937e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6938e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6939e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6940e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6941e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6942e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
694356909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
694419472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
6945ac5656d8SAaron Goidel 	LSM_HOOK_INIT(path_notify, selinux_path_notify),
69461da177e4SLinus Torvalds 
6947ec882da5SOndrej Mosnacek 	LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
6948ec882da5SOndrej Mosnacek 
6949e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
6950e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6951e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6952e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6953e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6954e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6955e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
6956e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6957e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6958e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6959e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
69601da177e4SLinus Torvalds 
6961e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
69621da177e4SLinus Torvalds 
6963a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6964e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6965e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
69663ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6967e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6968e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6969e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6970c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
697161d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6972e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6973e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6974e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6975e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6976e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6977e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6978e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6979791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6980e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6981e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6982e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6983e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6984e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
6985e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6986788e7dd4SYuichi Nakamura 
6987e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6988e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
69891da177e4SLinus Torvalds 
6990e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6991e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6992e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6993e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
69941da177e4SLinus Torvalds 
6995e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6996e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6997e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
69981da177e4SLinus Torvalds 
6999e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7000e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7001e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
70021da177e4SLinus Torvalds 
7003e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
70041da177e4SLinus Torvalds 
7005e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7006e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
70071da177e4SLinus Torvalds 
7008e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7009e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7010e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
70116f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7012e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7013e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
70141da177e4SLinus Torvalds 
7015e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7016e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7017dc49c1f9SCatherine Zhang 
7018e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
7019e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
70200b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7021e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7022e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7023e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7024e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7025e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7026e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7027e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7028e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7029e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7030e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7031e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7032e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7033e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
7034e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
7035e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7036e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7037e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7038e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7039e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7040d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7041d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7042d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7043e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7044e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7045e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7046e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7047e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7048e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7049e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7050e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7051e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7052e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7053e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7054e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
70553a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
7056cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7057ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
7058ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
70593a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
70603a976fa6SDaniel Jurgens #endif
7061d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
7062e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7063e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7064e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7065e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7066e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7067e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7068e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
7069e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
70701da177e4SLinus Torvalds #endif
7071d720024eSMichael LeMay 
7072d720024eSMichael LeMay #ifdef CONFIG_KEYS
7073e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
7074e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
7075e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7076d720024eSMichael LeMay #endif
70779d57a7f9SAhmed S. Darwish 
70789d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
7079e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7080e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7081e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
70829d57a7f9SAhmed S. Darwish #endif
7083ec27c356SChenbo Feng 
7084ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
7085ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
7086ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7087ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7088ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7089ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7090ec27c356SChenbo Feng #endif
7091da97e184SJoel Fernandes (Google) 
7092da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
7093da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7094da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7095da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7096da97e184SJoel Fernandes (Google) 	LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7097da97e184SJoel Fernandes (Google) #endif
709859438b46SStephen Smalley 
709959438b46SStephen Smalley 	LSM_HOOK_INIT(locked_down, selinux_lockdown),
7100cfff75d8SOndrej Mosnacek 
7101cfff75d8SOndrej Mosnacek 	/*
7102cfff75d8SOndrej Mosnacek 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7103cfff75d8SOndrej Mosnacek 	 */
7104cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7105cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7106cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7107cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
7108cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7109cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7110cfff75d8SOndrej Mosnacek #endif
7111cfff75d8SOndrej Mosnacek 
7112cfff75d8SOndrej Mosnacek 	/*
7113cfff75d8SOndrej Mosnacek 	 * PUT "ALLOCATING" HOOKS HERE
7114cfff75d8SOndrej Mosnacek 	 */
7115cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7116cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(msg_queue_alloc_security,
7117cfff75d8SOndrej Mosnacek 		      selinux_msg_queue_alloc_security),
7118cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7119cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7120cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7121cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7122cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7123cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7124cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7125cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7126cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_INFINIBAND
7127cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7128cfff75d8SOndrej Mosnacek #endif
7129cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM
7130cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7131cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7132cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7133cfff75d8SOndrej Mosnacek 		      selinux_xfrm_state_alloc_acquire),
7134cfff75d8SOndrej Mosnacek #endif
7135cfff75d8SOndrej Mosnacek #ifdef CONFIG_KEYS
7136cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7137cfff75d8SOndrej Mosnacek #endif
7138cfff75d8SOndrej Mosnacek #ifdef CONFIG_AUDIT
7139cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7140cfff75d8SOndrej Mosnacek #endif
7141cfff75d8SOndrej Mosnacek #ifdef CONFIG_BPF_SYSCALL
7142cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7143cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7144cfff75d8SOndrej Mosnacek #endif
7145cfff75d8SOndrej Mosnacek #ifdef CONFIG_PERF_EVENTS
7146cfff75d8SOndrej Mosnacek 	LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7147cfff75d8SOndrej Mosnacek #endif
71481da177e4SLinus Torvalds };
71491da177e4SLinus Torvalds 
71501da177e4SLinus Torvalds static __init int selinux_init(void)
71511da177e4SLinus Torvalds {
7152c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
71531da177e4SLinus Torvalds 
7154aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
7155e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
7156aa8e712cSStephen Smalley 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
7157aa8e712cSStephen Smalley 	selinux_ss_init(&selinux_state.ss);
71586b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
7159aa8e712cSStephen Smalley 
71601da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
7161d84f4f99SDavid Howells 	cred_init_security();
71621da177e4SLinus Torvalds 
7163fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7164fcaaade1SStephen Smalley 
71651da177e4SLinus Torvalds 	avc_init();
71661da177e4SLinus Torvalds 
7167aa8e712cSStephen Smalley 	avtab_cache_init();
7168aa8e712cSStephen Smalley 
7169aa8e712cSStephen Smalley 	ebitmap_cache_init();
7170aa8e712cSStephen Smalley 
7171aa8e712cSStephen Smalley 	hashtab_cache_init();
7172aa8e712cSStephen Smalley 
7173d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
71741da177e4SLinus Torvalds 
7175615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7176615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7177615e51fdSPaul Moore 
71788f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
71798f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
71808f408ab6SDaniel Jurgens 
7181aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7182c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
7183828dfe1dSEric Paris 	else
7184c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
7185d720024eSMichael LeMay 
7186442155c1SDavid Howells 	fs_validate_description(&selinux_fs_parameters);
7187442155c1SDavid Howells 
71881da177e4SLinus Torvalds 	return 0;
71891da177e4SLinus Torvalds }
71901da177e4SLinus Torvalds 
7191e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7192e8c26255SAl Viro {
7193204cc0ccSAl Viro 	selinux_set_mnt_opts(sb, NULL, 0, NULL);
7194e8c26255SAl Viro }
7195e8c26255SAl Viro 
71961da177e4SLinus Torvalds void selinux_complete_init(void)
71971da177e4SLinus Torvalds {
7198c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
71991da177e4SLinus Torvalds 
72001da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7201c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
7202e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
72031da177e4SLinus Torvalds }
72041da177e4SLinus Torvalds 
72051da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
72061da177e4SLinus Torvalds    all processes and objects when they are created. */
72073d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
720807aed2f2SKees Cook 	.name = "selinux",
720914bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
72106c5a682eSStephen Smalley 	.enabled = &selinux_enabled_boot,
7211bbd3662aSCasey Schaufler 	.blobs = &selinux_blob_sizes,
72123d6e5f6dSKees Cook 	.init = selinux_init,
72133d6e5f6dSKees Cook };
72141da177e4SLinus Torvalds 
7215c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
72161da177e4SLinus Torvalds 
7217591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7218effad8dfSPaul Moore 	{
7219effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
72202597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
72216e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
72221da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7223effad8dfSPaul Moore 	},
7224effad8dfSPaul Moore 	{
7225effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
72262597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7227effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7228effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7229948bf85cSPaul Moore 	},
7230948bf85cSPaul Moore 	{
7231948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
72322597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7233948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7234948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
723525db6beaSJiri Pirko 	},
72361a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7237effad8dfSPaul Moore 	{
7238effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
72392597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
72406e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
72411da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7242effad8dfSPaul Moore 	},
7243effad8dfSPaul Moore 	{
7244effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
72452597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7246effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7247effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
724825db6beaSJiri Pirko 	},
72492917f57bSHuw Davies 	{
72502917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
72512917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
72522917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
72532917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
72542917f57bSHuw Davies 	},
72551da177e4SLinus Torvalds #endif	/* IPV6 */
725625db6beaSJiri Pirko };
72571da177e4SLinus Torvalds 
72588e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
72598e71bf75SFlorian Westphal {
72608e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
72618e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
72628e71bf75SFlorian Westphal }
72638e71bf75SFlorian Westphal 
72648e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
72658e71bf75SFlorian Westphal {
72668e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
72678e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
72688e71bf75SFlorian Westphal }
72698e71bf75SFlorian Westphal 
72708e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
72718e71bf75SFlorian Westphal 	.init = selinux_nf_register,
72728e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
72738e71bf75SFlorian Westphal };
72748e71bf75SFlorian Westphal 
72751da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
72761da177e4SLinus Torvalds {
727725db6beaSJiri Pirko 	int err;
72781da177e4SLinus Torvalds 
72796c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
728025db6beaSJiri Pirko 		return 0;
72811da177e4SLinus Torvalds 
7282c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
72831da177e4SLinus Torvalds 
72848e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
72851da177e4SLinus Torvalds 	if (err)
72868e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
72871da177e4SLinus Torvalds 
728825db6beaSJiri Pirko 	return 0;
72891da177e4SLinus Torvalds }
72901da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
72911da177e4SLinus Torvalds 
72921da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
72931da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
72941da177e4SLinus Torvalds {
7295c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
72961da177e4SLinus Torvalds 
72978e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
72981da177e4SLinus Torvalds }
72991da177e4SLinus Torvalds #endif
73001da177e4SLinus Torvalds 
7301c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
73021da177e4SLinus Torvalds 
73031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
73041da177e4SLinus Torvalds #define selinux_nf_ip_exit()
73051da177e4SLinus Torvalds #endif
73061da177e4SLinus Torvalds 
7307c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
73081da177e4SLinus Torvalds 
73091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7310aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
73111da177e4SLinus Torvalds {
731265cddd50SOndrej Mosnacek 	if (selinux_initialized(state)) {
73131da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
73141da177e4SLinus Torvalds 		return -EINVAL;
73151da177e4SLinus Torvalds 	}
73161da177e4SLinus Torvalds 
731765cddd50SOndrej Mosnacek 	if (selinux_disabled(state)) {
73181da177e4SLinus Torvalds 		/* Only do this once. */
73191da177e4SLinus Torvalds 		return -EINVAL;
73201da177e4SLinus Torvalds 	}
73211da177e4SLinus Torvalds 
732265cddd50SOndrej Mosnacek 	selinux_mark_disabled(state);
7323aa8e712cSStephen Smalley 
7324c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
73251da177e4SLinus Torvalds 
7326cfff75d8SOndrej Mosnacek 	/*
7327cfff75d8SOndrej Mosnacek 	 * Unregister netfilter hooks.
7328cfff75d8SOndrej Mosnacek 	 * Must be done before security_delete_hooks() to avoid breaking
7329cfff75d8SOndrej Mosnacek 	 * runtime disable.
7330cfff75d8SOndrej Mosnacek 	 */
7331cfff75d8SOndrej Mosnacek 	selinux_nf_ip_exit();
7332cfff75d8SOndrej Mosnacek 
7333b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
73341da177e4SLinus Torvalds 
7335af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7336af8ff049SEric Paris 	avc_disable();
7337af8ff049SEric Paris 
73381da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
73391da177e4SLinus Torvalds 	exit_sel_fs();
73401da177e4SLinus Torvalds 
73411da177e4SLinus Torvalds 	return 0;
73421da177e4SLinus Torvalds }
73431da177e4SLinus Torvalds #endif
7344