xref: /openbmc/linux/security/selinux/hooks.c (revision e919328810bca2ea3f7a224a34cbccd7baf826d5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  NSA Security-Enhanced Linux (SELinux) security module
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This file contains the SELinux hook function implementations.
51da177e4SLinus Torvalds  *
67efbb60bSStephen Smalley  *  Authors:  Stephen Smalley, <sds@tycho.nsa.gov>
71da177e4SLinus Torvalds  *	      Chris Vance, <cvance@nai.com>
81da177e4SLinus Torvalds  *	      Wayne Salamon, <wsalamon@nai.com>
91da177e4SLinus Torvalds  *	      James Morris <jmorris@redhat.com>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
122069f457SEric Paris  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
132069f457SEric Paris  *					   Eric Paris <eparis@redhat.com>
141da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
151da177e4SLinus Torvalds  *			    <dgoeddel@trustedcs.com>
16ed6d76e4SPaul Moore  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
1782c21bfaSPaul Moore  *	Paul Moore <paul@paul-moore.com>
18788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19788e7dd4SYuichi Nakamura  *		       Yuichi Nakamura <ynakam@hitachisoft.jp>
203a976fa6SDaniel Jurgens  *  Copyright (C) 2016 Mellanox Technologies
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
231da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
241da177e4SLinus Torvalds  *	as published by the Free Software Foundation.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/init.h>
280b24dcb7SEric Paris #include <linux/kd.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
300d094efeSRoland McGrath #include <linux/tracehook.h>
311da177e4SLinus Torvalds #include <linux/errno.h>
323f07c014SIngo Molnar #include <linux/sched/signal.h>
3329930025SIngo Molnar #include <linux/sched/task.h>
343c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
351da177e4SLinus Torvalds #include <linux/xattr.h>
361da177e4SLinus Torvalds #include <linux/capability.h>
371da177e4SLinus Torvalds #include <linux/unistd.h>
381da177e4SLinus Torvalds #include <linux/mm.h>
391da177e4SLinus Torvalds #include <linux/mman.h>
401da177e4SLinus Torvalds #include <linux/slab.h>
411da177e4SLinus Torvalds #include <linux/pagemap.h>
420b24dcb7SEric Paris #include <linux/proc_fs.h>
431da177e4SLinus Torvalds #include <linux/swap.h>
441da177e4SLinus Torvalds #include <linux/spinlock.h>
451da177e4SLinus Torvalds #include <linux/syscalls.h>
462a7dba39SEric Paris #include <linux/dcache.h>
471da177e4SLinus Torvalds #include <linux/file.h>
489f3acc31SAl Viro #include <linux/fdtable.h>
491da177e4SLinus Torvalds #include <linux/namei.h>
501da177e4SLinus Torvalds #include <linux/mount.h>
511da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
521da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
531da177e4SLinus Torvalds #include <linux/tty.h>
541da177e4SLinus Torvalds #include <net/icmp.h>
55227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
561da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5747180068SPaul Moore #include <net/inet_connection_sock.h>
58220deb96SPaul Moore #include <net/net_namespace.h>
59d621d35eSPaul Moore #include <net/netlabel.h>
60f5269710SEric Paris #include <linux/uaccess.h>
611da177e4SLinus Torvalds #include <asm/ioctls.h>
6260063497SArun Sharma #include <linux/atomic.h>
631da177e4SLinus Torvalds #include <linux/bitops.h>
641da177e4SLinus Torvalds #include <linux/interrupt.h>
651da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6677954983SHong zhi guo #include <net/netlink.h>
671da177e4SLinus Torvalds #include <linux/tcp.h>
681da177e4SLinus Torvalds #include <linux/udp.h>
692ee92d46SJames Morris #include <linux/dccp.h>
70d452930fSRichard Haines #include <linux/sctp.h>
71d452930fSRichard Haines #include <net/sctp/structs.h>
721da177e4SLinus Torvalds #include <linux/quota.h>
731da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
741da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
751da177e4SLinus Torvalds #include <linux/parser.h>
761da177e4SLinus Torvalds #include <linux/nfs_mount.h>
771da177e4SLinus Torvalds #include <net/ipv6.h>
781da177e4SLinus Torvalds #include <linux/hugetlb.h>
791da177e4SLinus Torvalds #include <linux/personality.h>
801da177e4SLinus Torvalds #include <linux/audit.h>
816931dfc9SEric Paris #include <linux/string.h>
82877ce7c1SCatherine Zhang #include <linux/selinux.h>
8323970741SEric Paris #include <linux/mutex.h>
84f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8500234592SKees Cook #include <linux/syslog.h>
863486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8744fc7ea0SPaul Gortmaker #include <linux/export.h>
8840401530SAl Viro #include <linux/msg.h>
8940401530SAl Viro #include <linux/shm.h>
90ec27c356SChenbo Feng #include <linux/bpf.h>
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #include "avc.h"
931da177e4SLinus Torvalds #include "objsec.h"
941da177e4SLinus Torvalds #include "netif.h"
95224dfbd8SPaul Moore #include "netnode.h"
963e112172SPaul Moore #include "netport.h"
97409dcf31SDaniel Jurgens #include "ibpkey.h"
98d28d1e08STrent Jaeger #include "xfrm.h"
99c60475bfSPaul Moore #include "netlabel.h"
1009d57a7f9SAhmed S. Darwish #include "audit.h"
1017b98a585SJames Morris #include "avc_ss.h"
1021da177e4SLinus Torvalds 
103aa8e712cSStephen Smalley struct selinux_state selinux_state;
104aa8e712cSStephen Smalley 
105d621d35eSPaul Moore /* SECMARK reference count */
10656a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
107d621d35eSPaul Moore 
1081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
109aa8e712cSStephen Smalley static int selinux_enforcing_boot;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1121da177e4SLinus Torvalds {
113f5269710SEric Paris 	unsigned long enforcing;
11429707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
115aa8e712cSStephen Smalley 		selinux_enforcing_boot = enforcing ? 1 : 0;
1161da177e4SLinus Torvalds 	return 1;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
119aa8e712cSStephen Smalley #else
120aa8e712cSStephen Smalley #define selinux_enforcing_boot 1
1211da177e4SLinus Torvalds #endif
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1241da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1271da177e4SLinus Torvalds {
128f5269710SEric Paris 	unsigned long enabled;
12929707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
130f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1311da177e4SLinus Torvalds 	return 1;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
13430d55280SStephen Smalley #else
13530d55280SStephen Smalley int selinux_enabled = 1;
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 
151e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
15263205654SSangwoo static struct kmem_cache *file_security_cache;
1537cae7e26SJames Morris 
154d621d35eSPaul Moore /**
155d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
156d621d35eSPaul Moore  *
157d621d35eSPaul Moore  * Description:
158d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
159d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
160d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1612be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1622be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
163d621d35eSPaul Moore  *
164d621d35eSPaul Moore  */
165d621d35eSPaul Moore static int selinux_secmark_enabled(void)
166d621d35eSPaul Moore {
167aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
168aa8e712cSStephen Smalley 		atomic_read(&selinux_secmark_refcount));
1692be4d74fSChris PeBenito }
1702be4d74fSChris PeBenito 
1712be4d74fSChris PeBenito /**
1722be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1732be4d74fSChris PeBenito  *
1742be4d74fSChris PeBenito  * Description:
1752be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1762be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1772be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1782be4d74fSChris PeBenito  * is always considered enabled.
1792be4d74fSChris PeBenito  *
1802be4d74fSChris PeBenito  */
1812be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1822be4d74fSChris PeBenito {
183aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
184aa8e712cSStephen Smalley 		netlbl_enabled() || selinux_xfrm_enabled());
185d621d35eSPaul Moore }
186d621d35eSPaul Moore 
187615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
188615e51fdSPaul Moore {
189615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
190615e51fdSPaul Moore 		sel_netif_flush();
191615e51fdSPaul Moore 		sel_netnode_flush();
192615e51fdSPaul Moore 		sel_netport_flush();
193615e51fdSPaul Moore 		synchronize_net();
194615e51fdSPaul Moore 	}
195615e51fdSPaul Moore 	return 0;
196615e51fdSPaul Moore }
197615e51fdSPaul Moore 
1988f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event)
1998f408ab6SDaniel Jurgens {
200409dcf31SDaniel Jurgens 	if (event == AVC_CALLBACK_RESET) {
201409dcf31SDaniel Jurgens 		sel_ib_pkey_flush();
2028f408ab6SDaniel Jurgens 		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
203409dcf31SDaniel Jurgens 	}
2048f408ab6SDaniel Jurgens 
2058f408ab6SDaniel Jurgens 	return 0;
2068f408ab6SDaniel Jurgens }
2078f408ab6SDaniel Jurgens 
208d84f4f99SDavid Howells /*
209d84f4f99SDavid Howells  * initialise the security for the init task
210d84f4f99SDavid Howells  */
211d84f4f99SDavid Howells static void cred_init_security(void)
2121da177e4SLinus Torvalds {
2133b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
2141da177e4SLinus Torvalds 	struct task_security_struct *tsec;
2151da177e4SLinus Torvalds 
21689d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
2171da177e4SLinus Torvalds 	if (!tsec)
218d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
2191da177e4SLinus Torvalds 
220d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
221f1752eecSDavid Howells 	cred->security = tsec;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224275bb41eSDavid Howells /*
22588e67f3bSDavid Howells  * get the security ID of a set of credentials
22688e67f3bSDavid Howells  */
22788e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
22888e67f3bSDavid Howells {
22988e67f3bSDavid Howells 	const struct task_security_struct *tsec;
23088e67f3bSDavid Howells 
23188e67f3bSDavid Howells 	tsec = cred->security;
23288e67f3bSDavid Howells 	return tsec->sid;
23388e67f3bSDavid Howells }
23488e67f3bSDavid Howells 
23588e67f3bSDavid Howells /*
2363b11a1deSDavid Howells  * get the objective security ID of a task
237275bb41eSDavid Howells  */
238275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
239275bb41eSDavid Howells {
240275bb41eSDavid Howells 	u32 sid;
241275bb41eSDavid Howells 
242275bb41eSDavid Howells 	rcu_read_lock();
24388e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
244275bb41eSDavid Howells 	rcu_read_unlock();
245275bb41eSDavid Howells 	return sid;
246275bb41eSDavid Howells }
247275bb41eSDavid Howells 
24888e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
24988e67f3bSDavid Howells 
2501da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct inode_security_struct *isec;
253275bb41eSDavid Howells 	u32 sid = current_sid();
2541da177e4SLinus Torvalds 
255a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2561da177e4SLinus Torvalds 	if (!isec)
2571da177e4SLinus Torvalds 		return -ENOMEM;
2581da177e4SLinus Torvalds 
2599287aed2SAndreas Gruenbacher 	spin_lock_init(&isec->lock);
2601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2611da177e4SLinus Torvalds 	isec->inode = inode;
2621da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2631da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
264275bb41eSDavid Howells 	isec->task_sid = sid;
26542059112SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
2661da177e4SLinus Torvalds 	inode->i_security = isec;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	return 0;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2715d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2725d226df4SAndreas Gruenbacher 
2735d226df4SAndreas Gruenbacher /*
2745d226df4SAndreas Gruenbacher  * Try reloading inode security labels that have been marked as invalid.  The
2755d226df4SAndreas Gruenbacher  * @may_sleep parameter indicates when sleeping and thus reloading labels is
27642059112SAndreas Gruenbacher  * allowed; when set to false, returns -ECHILD when the label is
277*e9193288SAl Viro  * invalid.  The @dentry parameter should be set to a dentry of the inode.
2785d226df4SAndreas Gruenbacher  */
2795d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode,
280*e9193288SAl Viro 				       struct dentry *dentry,
2815d226df4SAndreas Gruenbacher 				       bool may_sleep)
2825d226df4SAndreas Gruenbacher {
2835d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
2845d226df4SAndreas Gruenbacher 
2855d226df4SAndreas Gruenbacher 	might_sleep_if(may_sleep);
2865d226df4SAndreas Gruenbacher 
287aa8e712cSStephen Smalley 	if (selinux_state.initialized &&
288aa8e712cSStephen Smalley 	    isec->initialized != LABEL_INITIALIZED) {
2895d226df4SAndreas Gruenbacher 		if (!may_sleep)
2905d226df4SAndreas Gruenbacher 			return -ECHILD;
2915d226df4SAndreas Gruenbacher 
2925d226df4SAndreas Gruenbacher 		/*
2935d226df4SAndreas Gruenbacher 		 * Try reloading the inode security label.  This will fail if
2945d226df4SAndreas Gruenbacher 		 * @opt_dentry is NULL and no dentry for this inode can be
2955d226df4SAndreas Gruenbacher 		 * found; in that case, continue using the old label.
2965d226df4SAndreas Gruenbacher 		 */
297*e9193288SAl Viro 		inode_doinit_with_dentry(inode, dentry);
2985d226df4SAndreas Gruenbacher 	}
2995d226df4SAndreas Gruenbacher 	return 0;
3005d226df4SAndreas Gruenbacher }
3015d226df4SAndreas Gruenbacher 
3025d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
3035d226df4SAndreas Gruenbacher {
3045d226df4SAndreas Gruenbacher 	return inode->i_security;
3055d226df4SAndreas Gruenbacher }
3065d226df4SAndreas Gruenbacher 
3075d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
3085d226df4SAndreas Gruenbacher {
3095d226df4SAndreas Gruenbacher 	int error;
3105d226df4SAndreas Gruenbacher 
3115d226df4SAndreas Gruenbacher 	error = __inode_security_revalidate(inode, NULL, !rcu);
3125d226df4SAndreas Gruenbacher 	if (error)
3135d226df4SAndreas Gruenbacher 		return ERR_PTR(error);
3145d226df4SAndreas Gruenbacher 	return inode->i_security;
3155d226df4SAndreas Gruenbacher }
3165d226df4SAndreas Gruenbacher 
31783da53c5SAndreas Gruenbacher /*
31883da53c5SAndreas Gruenbacher  * Get the security label of an inode.
31983da53c5SAndreas Gruenbacher  */
32083da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode)
32183da53c5SAndreas Gruenbacher {
3225d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, NULL, true);
32383da53c5SAndreas Gruenbacher 	return inode->i_security;
32483da53c5SAndreas Gruenbacher }
32583da53c5SAndreas Gruenbacher 
3262c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
3272c97165bSPaul Moore {
3282c97165bSPaul Moore 	struct inode *inode = d_backing_inode(dentry);
3292c97165bSPaul Moore 
3302c97165bSPaul Moore 	return inode->i_security;
3312c97165bSPaul Moore }
3322c97165bSPaul Moore 
33383da53c5SAndreas Gruenbacher /*
33483da53c5SAndreas Gruenbacher  * Get the security label of a dentry's backing inode.
33583da53c5SAndreas Gruenbacher  */
33683da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
33783da53c5SAndreas Gruenbacher {
33883da53c5SAndreas Gruenbacher 	struct inode *inode = d_backing_inode(dentry);
33983da53c5SAndreas Gruenbacher 
3405d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
34183da53c5SAndreas Gruenbacher 	return inode->i_security;
34283da53c5SAndreas Gruenbacher }
34383da53c5SAndreas Gruenbacher 
3443dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head)
3453dc91d43SSteven Rostedt {
3463dc91d43SSteven Rostedt 	struct inode_security_struct *isec;
3473dc91d43SSteven Rostedt 
3483dc91d43SSteven Rostedt 	isec = container_of(head, struct inode_security_struct, rcu);
3493dc91d43SSteven Rostedt 	kmem_cache_free(sel_inode_cache, isec);
3503dc91d43SSteven Rostedt }
3513dc91d43SSteven Rostedt 
3521da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3551da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3561da177e4SLinus Torvalds 
3579629d04aSWaiman Long 	/*
3589629d04aSWaiman Long 	 * As not all inode security structures are in a list, we check for
3599629d04aSWaiman Long 	 * empty list outside of the lock to make sure that we won't waste
3609629d04aSWaiman Long 	 * time taking a lock doing nothing.
3619629d04aSWaiman Long 	 *
3629629d04aSWaiman Long 	 * The list_del_init() function can be safely called more than once.
3639629d04aSWaiman Long 	 * It should not be possible for this function to be called with
3649629d04aSWaiman Long 	 * concurrent list_add(), but for better safety against future changes
3659629d04aSWaiman Long 	 * in the code, we use list_empty_careful() here.
3669629d04aSWaiman Long 	 */
3679629d04aSWaiman Long 	if (!list_empty_careful(&isec->list)) {
3681da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
3691da177e4SLinus Torvalds 		list_del_init(&isec->list);
3701da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
3719629d04aSWaiman Long 	}
3721da177e4SLinus Torvalds 
3733dc91d43SSteven Rostedt 	/*
3743dc91d43SSteven Rostedt 	 * The inode may still be referenced in a path walk and
3753dc91d43SSteven Rostedt 	 * a call to selinux_inode_permission() can be made
3763dc91d43SSteven Rostedt 	 * after inode_free_security() is called. Ideally, the VFS
3773dc91d43SSteven Rostedt 	 * wouldn't do this, but fixing that is a much harder
3783dc91d43SSteven Rostedt 	 * job. For now, simply free the i_security via RCU, and
3793dc91d43SSteven Rostedt 	 * leave the current inode->i_security pointer intact.
3803dc91d43SSteven Rostedt 	 * The inode will be freed after the RCU grace period too.
3813dc91d43SSteven Rostedt 	 */
3823dc91d43SSteven Rostedt 	call_rcu(&isec->rcu, inode_free_rcu);
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	struct file_security_struct *fsec;
388275bb41eSDavid Howells 	u32 sid = current_sid();
3891da177e4SLinus Torvalds 
39063205654SSangwoo 	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
3911da177e4SLinus Torvalds 	if (!fsec)
3921da177e4SLinus Torvalds 		return -ENOMEM;
3931da177e4SLinus Torvalds 
394275bb41eSDavid Howells 	fsec->sid = sid;
395275bb41eSDavid Howells 	fsec->fown_sid = sid;
3961da177e4SLinus Torvalds 	file->f_security = fsec;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	return 0;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static void file_free_security(struct file *file)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
4041da177e4SLinus Torvalds 	file->f_security = NULL;
40563205654SSangwoo 	kmem_cache_free(file_security_cache, fsec);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
4111da177e4SLinus Torvalds 
41289d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
4131da177e4SLinus Torvalds 	if (!sbsec)
4141da177e4SLinus Torvalds 		return -ENOMEM;
4151da177e4SLinus Torvalds 
416bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
4171da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
4181da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
4191da177e4SLinus Torvalds 	sbsec->sb = sb;
4201da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
4211da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
422c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
4231da177e4SLinus Torvalds 	sb->s_security = sbsec;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	return 0;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4311da177e4SLinus Torvalds 	sb->s_security = NULL;
4321da177e4SLinus Torvalds 	kfree(sbsec);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds enum {
44131e87930SEric Paris 	Opt_error = -1,
4421da177e4SLinus Torvalds 	Opt_context = 1,
4431da177e4SLinus Torvalds 	Opt_fscontext = 2,
444c9180a57SEric Paris 	Opt_defcontext = 3,
445c9180a57SEric Paris 	Opt_rootcontext = 4,
44611689d47SDavid P. Quigley 	Opt_labelsupport = 5,
447d355987fSEric Paris 	Opt_nextmntopt = 6,
4481da177e4SLinus Torvalds };
4491da177e4SLinus Torvalds 
450d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
451d355987fSEric Paris 
452a447c093SSteven Whitehouse static const match_table_t tokens = {
453832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
454832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
455832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
456832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
45711689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
45831e87930SEric Paris 	{Opt_error, NULL},
4591da177e4SLinus Torvalds };
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4621da177e4SLinus Torvalds 
463c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
464c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
465275bb41eSDavid Howells 			const struct cred *cred)
466c312feb2SEric Paris {
467275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
468c312feb2SEric Paris 	int rc;
469c312feb2SEric Paris 
4706b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4716b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
472c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
473c312feb2SEric Paris 	if (rc)
474c312feb2SEric Paris 		return rc;
475c312feb2SEric Paris 
4766b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4776b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
478c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
479c312feb2SEric Paris 	return rc;
480c312feb2SEric Paris }
481c312feb2SEric Paris 
4820808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4830808925eSEric Paris 			struct superblock_security_struct *sbsec,
484275bb41eSDavid Howells 			const struct cred *cred)
4850808925eSEric Paris {
486275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
4870808925eSEric Paris 	int rc;
4886b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4896b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4900808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
4910808925eSEric Paris 	if (rc)
4920808925eSEric Paris 		return rc;
4930808925eSEric Paris 
4946b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4956b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
4960808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4970808925eSEric Paris 	return rc;
4980808925eSEric Paris }
4990808925eSEric Paris 
500b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
501b43e725dSEric Paris {
502b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
503b43e725dSEric Paris 
504d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
505b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
506d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
5079fc2b4b4SJ. Bruce Fields 		sbsec->behavior == SECURITY_FS_USE_NATIVE ||
508d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
509d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
510d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
511d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
512a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
5132651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
514aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
5152651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
5162651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
517b43e725dSEric Paris }
518b43e725dSEric Paris 
519c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5221da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
523c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5241da177e4SLinus Torvalds 	int rc = 0;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
5271da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
5281da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
5291da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
5301da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
5311da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
5325d6c3191SAndreas Gruenbacher 		if (!(root_inode->i_opflags & IOP_XATTR)) {
53329b1deb2SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
53429b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
5351da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5361da177e4SLinus Torvalds 			goto out;
5371da177e4SLinus Torvalds 		}
5385d6c3191SAndreas Gruenbacher 
5395d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
5401da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5411da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
5421da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
54329b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
54429b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5451da177e4SLinus Torvalds 			else
5461da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
54729b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
54829b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
5491da177e4SLinus Torvalds 			goto out;
5501da177e4SLinus Torvalds 		}
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
553eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5540b4d3452SScott Mayhew 
5550b4d3452SScott Mayhew 	/*
5560b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5570b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5580b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5590b4d3452SScott Mayhew 	 */
560b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
56112f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5620b4d3452SScott Mayhew 	else
5630b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
564ddd29ec6SDavid P. Quigley 
5651da177e4SLinus Torvalds 	/* Initialize the root inode. */
566c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5691da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5701da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5711da177e4SLinus Torvalds 	   populates itself. */
5721da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5731da177e4SLinus Torvalds next_inode:
5741da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
5751da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5761da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
5771da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5781da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
579923190d3SStephen Smalley 		list_del_init(&isec->list);
5801da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5811da177e4SLinus Torvalds 		inode = igrab(inode);
5821da177e4SLinus Torvalds 		if (inode) {
5831da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
5841da177e4SLinus Torvalds 				inode_doinit(inode);
5851da177e4SLinus Torvalds 			iput(inode);
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5881da177e4SLinus Torvalds 		goto next_inode;
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
5911da177e4SLinus Torvalds out:
592c9180a57SEric Paris 	return rc;
593c9180a57SEric Paris }
594c9180a57SEric Paris 
595c9180a57SEric Paris /*
596c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
597c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
598c9180a57SEric Paris  * mount options, or whatever.
599c9180a57SEric Paris  */
600c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
601e0007529SEric Paris 				struct security_mnt_opts *opts)
602c9180a57SEric Paris {
603c9180a57SEric Paris 	int rc = 0, i;
604c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
605c9180a57SEric Paris 	char *context = NULL;
606c9180a57SEric Paris 	u32 len;
607c9180a57SEric Paris 	char tmp;
608c9180a57SEric Paris 
609e0007529SEric Paris 	security_init_mnt_opts(opts);
610c9180a57SEric Paris 
6110d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
612c9180a57SEric Paris 		return -EINVAL;
613c9180a57SEric Paris 
614aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
615c9180a57SEric Paris 		return -EINVAL;
616c9180a57SEric Paris 
617af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
618af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
619af8e50ccSEric Paris 
6200d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
621c9180a57SEric Paris 	/* count the number of mount options for this sb */
622af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
623c9180a57SEric Paris 		if (tmp & 0x01)
624e0007529SEric Paris 			opts->num_mnt_opts++;
625c9180a57SEric Paris 		tmp >>= 1;
626c9180a57SEric Paris 	}
62711689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
6280b4bdb35SEric Paris 	if (sbsec->flags & SBLABEL_MNT)
62911689d47SDavid P. Quigley 		opts->num_mnt_opts++;
630c9180a57SEric Paris 
631e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
632e0007529SEric Paris 	if (!opts->mnt_opts) {
633c9180a57SEric Paris 		rc = -ENOMEM;
634c9180a57SEric Paris 		goto out_free;
635c9180a57SEric Paris 	}
636c9180a57SEric Paris 
637e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
638e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
639c9180a57SEric Paris 		rc = -ENOMEM;
640c9180a57SEric Paris 		goto out_free;
641c9180a57SEric Paris 	}
642c9180a57SEric Paris 
643c9180a57SEric Paris 	i = 0;
644c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
645aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, sbsec->sid,
646aa8e712cSStephen Smalley 					     &context, &len);
647c9180a57SEric Paris 		if (rc)
648c9180a57SEric Paris 			goto out_free;
649e0007529SEric Paris 		opts->mnt_opts[i] = context;
650e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
651c9180a57SEric Paris 	}
652c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
653aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state,
654aa8e712cSStephen Smalley 					     sbsec->mntpoint_sid,
655aa8e712cSStephen Smalley 					     &context, &len);
656c9180a57SEric Paris 		if (rc)
657c9180a57SEric Paris 			goto out_free;
658e0007529SEric Paris 		opts->mnt_opts[i] = context;
659e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
660c9180a57SEric Paris 	}
661c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
662aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, sbsec->def_sid,
663aa8e712cSStephen Smalley 					     &context, &len);
664c9180a57SEric Paris 		if (rc)
665c9180a57SEric Paris 			goto out_free;
666e0007529SEric Paris 		opts->mnt_opts[i] = context;
667e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
668c9180a57SEric Paris 	}
669c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
67083da53c5SAndreas Gruenbacher 		struct dentry *root = sbsec->sb->s_root;
67183da53c5SAndreas Gruenbacher 		struct inode_security_struct *isec = backing_inode_security(root);
672c9180a57SEric Paris 
673aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, isec->sid,
674aa8e712cSStephen Smalley 					     &context, &len);
675c9180a57SEric Paris 		if (rc)
676c9180a57SEric Paris 			goto out_free;
677e0007529SEric Paris 		opts->mnt_opts[i] = context;
678e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
679c9180a57SEric Paris 	}
68012f348b9SEric Paris 	if (sbsec->flags & SBLABEL_MNT) {
68111689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
68212f348b9SEric Paris 		opts->mnt_opts_flags[i++] = SBLABEL_MNT;
68311689d47SDavid P. Quigley 	}
684c9180a57SEric Paris 
685e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
686c9180a57SEric Paris 
687c9180a57SEric Paris 	return 0;
688c9180a57SEric Paris 
689c9180a57SEric Paris out_free:
690e0007529SEric Paris 	security_free_mnt_opts(opts);
691c9180a57SEric Paris 	return rc;
692c9180a57SEric Paris }
693c9180a57SEric Paris 
694c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
695c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
696c9180a57SEric Paris {
6970d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
6980d90a7ecSDavid P. Quigley 
699c9180a57SEric Paris 	/* check if the old mount command had the same options */
7000d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
701c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
702c9180a57SEric Paris 		    (old_sid != new_sid))
703c9180a57SEric Paris 			return 1;
704c9180a57SEric Paris 
705c9180a57SEric Paris 	/* check if we were passed the same options twice,
706c9180a57SEric Paris 	 * aka someone passed context=a,context=b
707c9180a57SEric Paris 	 */
7080d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
7090d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
710c9180a57SEric Paris 			return 1;
711c9180a57SEric Paris 	return 0;
712c9180a57SEric Paris }
713e0007529SEric Paris 
714c9180a57SEric Paris /*
715c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
716c9180a57SEric Paris  * labeling information.
717c9180a57SEric Paris  */
718e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
719649f6e77SDavid Quigley 				struct security_mnt_opts *opts,
720649f6e77SDavid Quigley 				unsigned long kern_flags,
721649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
722c9180a57SEric Paris {
723275bb41eSDavid Howells 	const struct cred *cred = current_cred();
724c9180a57SEric Paris 	int rc = 0, i;
725c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
72629b1deb2SLinus Torvalds 	const char *name = sb->s_type->name;
72783da53c5SAndreas Gruenbacher 	struct dentry *root = sbsec->sb->s_root;
7282c97165bSPaul Moore 	struct inode_security_struct *root_isec;
729c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
730c9180a57SEric Paris 	u32 defcontext_sid = 0;
731e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
732e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
733e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
734c9180a57SEric Paris 
735c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
736c9180a57SEric Paris 
737aa8e712cSStephen Smalley 	if (!selinux_state.initialized) {
738c9180a57SEric Paris 		if (!num_opts) {
739c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
740c9180a57SEric Paris 			   after the initial policy is loaded and the security
741c9180a57SEric Paris 			   server is ready to handle calls. */
742c9180a57SEric Paris 			goto out;
743c9180a57SEric Paris 		}
744c9180a57SEric Paris 		rc = -EINVAL;
745744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
746744ba35eSEric Paris 			"before the security server is initialized\n");
747c9180a57SEric Paris 		goto out;
748c9180a57SEric Paris 	}
749649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
750649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
751649f6e77SDavid Quigley 		 * place the results is not allowed */
752649f6e77SDavid Quigley 		rc = -EINVAL;
753649f6e77SDavid Quigley 		goto out;
754649f6e77SDavid Quigley 	}
755c9180a57SEric Paris 
756c9180a57SEric Paris 	/*
757e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
758e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
759e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
760e0007529SEric Paris 	 * we need to skip the double mount verification.
761e0007529SEric Paris 	 *
762e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
763e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
764e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
765e0007529SEric Paris 	 * will be used for both mounts)
766e0007529SEric Paris 	 */
7670d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
768e0007529SEric Paris 	    && (num_opts == 0))
769e0007529SEric Paris 		goto out;
770e0007529SEric Paris 
7712c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
7722c97165bSPaul Moore 
773e0007529SEric Paris 	/*
774c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
775c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
776c9180a57SEric Paris 	 * than once with different security options.
777c9180a57SEric Paris 	 */
778c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
779c9180a57SEric Paris 		u32 sid;
78011689d47SDavid P. Quigley 
78112f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
78211689d47SDavid P. Quigley 			continue;
783aa8e712cSStephen Smalley 		rc = security_context_str_to_sid(&selinux_state,
784aa8e712cSStephen Smalley 						 mount_options[i], &sid,
785aa8e712cSStephen Smalley 						 GFP_KERNEL);
786c9180a57SEric Paris 		if (rc) {
78744be2f65SRasmus Villemoes 			printk(KERN_WARNING "SELinux: security_context_str_to_sid"
78829b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
78929b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, name, rc);
790c9180a57SEric Paris 			goto out;
791c9180a57SEric Paris 		}
792c9180a57SEric Paris 		switch (flags[i]) {
793c9180a57SEric Paris 		case FSCONTEXT_MNT:
794c9180a57SEric Paris 			fscontext_sid = sid;
795c9180a57SEric Paris 
796c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
797c9180a57SEric Paris 					fscontext_sid))
798c9180a57SEric Paris 				goto out_double_mount;
799c9180a57SEric Paris 
800c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
801c9180a57SEric Paris 			break;
802c9180a57SEric Paris 		case CONTEXT_MNT:
803c9180a57SEric Paris 			context_sid = sid;
804c9180a57SEric Paris 
805c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
806c9180a57SEric Paris 					context_sid))
807c9180a57SEric Paris 				goto out_double_mount;
808c9180a57SEric Paris 
809c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
810c9180a57SEric Paris 			break;
811c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
812c9180a57SEric Paris 			rootcontext_sid = sid;
813c9180a57SEric Paris 
814c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
815c9180a57SEric Paris 					rootcontext_sid))
816c9180a57SEric Paris 				goto out_double_mount;
817c9180a57SEric Paris 
818c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
819c9180a57SEric Paris 
820c9180a57SEric Paris 			break;
821c9180a57SEric Paris 		case DEFCONTEXT_MNT:
822c9180a57SEric Paris 			defcontext_sid = sid;
823c9180a57SEric Paris 
824c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
825c9180a57SEric Paris 					defcontext_sid))
826c9180a57SEric Paris 				goto out_double_mount;
827c9180a57SEric Paris 
828c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
829c9180a57SEric Paris 
830c9180a57SEric Paris 			break;
831c9180a57SEric Paris 		default:
832c9180a57SEric Paris 			rc = -EINVAL;
833c9180a57SEric Paris 			goto out;
834c9180a57SEric Paris 		}
835c9180a57SEric Paris 	}
836c9180a57SEric Paris 
8370d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
838c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
8390d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
840c9180a57SEric Paris 			goto out_double_mount;
841c9180a57SEric Paris 		rc = 0;
842c9180a57SEric Paris 		goto out;
843c9180a57SEric Paris 	}
844c9180a57SEric Paris 
845089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
846134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
847134509d5SStephen Smalley 
8488e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
8496a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
8508e014720SStephen Smalley 	    !strcmp(sb->s_type->name, "sysfs") ||
851901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "pstore") ||
852901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
853901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
854134509d5SStephen Smalley 		sbsec->flags |= SE_SBGENFS;
855c9180a57SEric Paris 
856eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
857eb9ae686SDavid Quigley 		/*
858eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
859eb9ae686SDavid Quigley 		 * filesystem type.
860eb9ae686SDavid Quigley 		 */
861aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
862c9180a57SEric Paris 		if (rc) {
863eb9ae686SDavid Quigley 			printk(KERN_WARNING
864eb9ae686SDavid Quigley 				"%s: security_fs_use(%s) returned %d\n",
865089be43eSJames Morris 					__func__, sb->s_type->name, rc);
866c9180a57SEric Paris 			goto out;
867c9180a57SEric Paris 		}
868eb9ae686SDavid Quigley 	}
869aad82892SSeth Forshee 
870aad82892SSeth Forshee 	/*
87101593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
87201593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
87301593d32SStephen Smalley 	 * line and security labels must be ignored.
874aad82892SSeth Forshee 	 */
87501593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
87601593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
87701593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
87801593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
879aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
880aad82892SSeth Forshee 		    defcontext_sid) {
881aad82892SSeth Forshee 			rc = -EACCES;
882aad82892SSeth Forshee 			goto out;
883aad82892SSeth Forshee 		}
884aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
885aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
886aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
887aa8e712cSStephen Smalley 						     current_sid(),
888aa8e712cSStephen Smalley 						     current_sid(),
889aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
890aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
891aad82892SSeth Forshee 			if (rc)
892aad82892SSeth Forshee 				goto out;
893aad82892SSeth Forshee 		}
894aad82892SSeth Forshee 		goto out_set_opts;
895aad82892SSeth Forshee 	}
896aad82892SSeth Forshee 
897c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
898c9180a57SEric Paris 	if (fscontext_sid) {
899275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
900c9180a57SEric Paris 		if (rc)
901c9180a57SEric Paris 			goto out;
902c9180a57SEric Paris 
903c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
904c9180a57SEric Paris 	}
905c9180a57SEric Paris 
906c9180a57SEric Paris 	/*
907c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
908c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
909c9180a57SEric Paris 	 * the superblock context if not already set.
910c9180a57SEric Paris 	 */
911eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
912eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
913eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
914eb9ae686SDavid Quigley 	}
915eb9ae686SDavid Quigley 
916c9180a57SEric Paris 	if (context_sid) {
917c9180a57SEric Paris 		if (!fscontext_sid) {
918275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
919275bb41eSDavid Howells 							  cred);
920c9180a57SEric Paris 			if (rc)
921c9180a57SEric Paris 				goto out;
922c9180a57SEric Paris 			sbsec->sid = context_sid;
923c9180a57SEric Paris 		} else {
924275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
925275bb41eSDavid Howells 							     cred);
926c9180a57SEric Paris 			if (rc)
927c9180a57SEric Paris 				goto out;
928c9180a57SEric Paris 		}
929c9180a57SEric Paris 		if (!rootcontext_sid)
930c9180a57SEric Paris 			rootcontext_sid = context_sid;
931c9180a57SEric Paris 
932c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
933c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
934c9180a57SEric Paris 	}
935c9180a57SEric Paris 
936c9180a57SEric Paris 	if (rootcontext_sid) {
937275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
938275bb41eSDavid Howells 						     cred);
939c9180a57SEric Paris 		if (rc)
940c9180a57SEric Paris 			goto out;
941c9180a57SEric Paris 
942c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
9436f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
944c9180a57SEric Paris 	}
945c9180a57SEric Paris 
946c9180a57SEric Paris 	if (defcontext_sid) {
947eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
948eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
949c9180a57SEric Paris 			rc = -EINVAL;
950c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
951c9180a57SEric Paris 			       "invalid for this filesystem type\n");
952c9180a57SEric Paris 			goto out;
953c9180a57SEric Paris 		}
954c9180a57SEric Paris 
955c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
956c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
957275bb41eSDavid Howells 							     sbsec, cred);
958c9180a57SEric Paris 			if (rc)
959c9180a57SEric Paris 				goto out;
960c9180a57SEric Paris 		}
961c9180a57SEric Paris 
962c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
963c9180a57SEric Paris 	}
964c9180a57SEric Paris 
965aad82892SSeth Forshee out_set_opts:
966c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
967c9180a57SEric Paris out:
968bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
9691da177e4SLinus Torvalds 	return rc;
970c9180a57SEric Paris out_double_mount:
971c9180a57SEric Paris 	rc = -EINVAL;
972c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
97329b1deb2SLinus Torvalds 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
974c9180a57SEric Paris 	goto out;
975c9180a57SEric Paris }
976c9180a57SEric Paris 
977094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
978094f7b69SJeff Layton 				    const struct super_block *newsb)
979094f7b69SJeff Layton {
980094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
981094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
982094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
983094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
984094f7b69SJeff Layton 
985094f7b69SJeff Layton 	if (oldflags != newflags)
986094f7b69SJeff Layton 		goto mismatch;
987094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
988094f7b69SJeff Layton 		goto mismatch;
989094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
990094f7b69SJeff Layton 		goto mismatch;
991094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
992094f7b69SJeff Layton 		goto mismatch;
993094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
99483da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
99583da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
996094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
997094f7b69SJeff Layton 			goto mismatch;
998094f7b69SJeff Layton 	}
999094f7b69SJeff Layton 	return 0;
1000094f7b69SJeff Layton mismatch:
1001094f7b69SJeff Layton 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
1002094f7b69SJeff Layton 			    "different security settings for (dev %s, "
1003094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
1004094f7b69SJeff Layton 	return -EBUSY;
1005094f7b69SJeff Layton }
1006094f7b69SJeff Layton 
1007094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
10080b4d3452SScott Mayhew 					struct super_block *newsb,
10090b4d3452SScott Mayhew 					unsigned long kern_flags,
10100b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
1011c9180a57SEric Paris {
10120b4d3452SScott Mayhew 	int rc = 0;
1013c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
1014c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
1015c9180a57SEric Paris 
1016c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
1017c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
1018c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
1019c9180a57SEric Paris 
10200f5e6420SEric Paris 	/*
10210f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
1022e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
10230f5e6420SEric Paris 	 */
1024aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
1025094f7b69SJeff Layton 		return 0;
1026c9180a57SEric Paris 
10270b4d3452SScott Mayhew 	/*
10280b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
10290b4d3452SScott Mayhew 	 * place the results is not allowed.
10300b4d3452SScott Mayhew 	 */
10310b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
10320b4d3452SScott Mayhew 		return -EINVAL;
10330b4d3452SScott Mayhew 
1034c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
10350d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
1036c9180a57SEric Paris 
1037094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
10380d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
1039094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
10405a552617SEric Paris 
1041c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
1042c9180a57SEric Paris 
1043c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
1044c9180a57SEric Paris 
1045c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
1046c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
1047c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
1048c9180a57SEric Paris 
10490b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
10500b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
1051aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
10520b4d3452SScott Mayhew 		if (rc)
10530b4d3452SScott Mayhew 			goto out;
10540b4d3452SScott Mayhew 	}
10550b4d3452SScott Mayhew 
10560b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
10570b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
10580b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
10590b4d3452SScott Mayhew 	}
10600b4d3452SScott Mayhew 
1061c9180a57SEric Paris 	if (set_context) {
1062c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
1063c9180a57SEric Paris 
1064c9180a57SEric Paris 		if (!set_fscontext)
1065c9180a57SEric Paris 			newsbsec->sid = sid;
1066c9180a57SEric Paris 		if (!set_rootcontext) {
106783da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1068c9180a57SEric Paris 			newisec->sid = sid;
1069c9180a57SEric Paris 		}
1070c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
1071c9180a57SEric Paris 	}
1072c9180a57SEric Paris 	if (set_rootcontext) {
107383da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
107483da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1075c9180a57SEric Paris 
1076c9180a57SEric Paris 		newisec->sid = oldisec->sid;
1077c9180a57SEric Paris 	}
1078c9180a57SEric Paris 
1079c9180a57SEric Paris 	sb_finish_set_opts(newsb);
10800b4d3452SScott Mayhew out:
1081c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
10820b4d3452SScott Mayhew 	return rc;
1083c9180a57SEric Paris }
1084c9180a57SEric Paris 
10852e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
10862e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
1087c9180a57SEric Paris {
1088e0007529SEric Paris 	char *p;
1089c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
1090c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
1091e0007529SEric Paris 	int rc, num_mnt_opts = 0;
1092c9180a57SEric Paris 
1093e0007529SEric Paris 	opts->num_mnt_opts = 0;
1094c9180a57SEric Paris 
1095c9180a57SEric Paris 	/* Standard string-based options. */
1096c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
1097c9180a57SEric Paris 		int token;
1098c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
1099c9180a57SEric Paris 
1100c9180a57SEric Paris 		if (!*p)
1101c9180a57SEric Paris 			continue;
1102c9180a57SEric Paris 
1103c9180a57SEric Paris 		token = match_token(p, tokens, args);
1104c9180a57SEric Paris 
1105c9180a57SEric Paris 		switch (token) {
1106c9180a57SEric Paris 		case Opt_context:
1107c9180a57SEric Paris 			if (context || defcontext) {
1108c9180a57SEric Paris 				rc = -EINVAL;
1109c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1110c9180a57SEric Paris 				goto out_err;
1111c9180a57SEric Paris 			}
1112c9180a57SEric Paris 			context = match_strdup(&args[0]);
1113c9180a57SEric Paris 			if (!context) {
1114c9180a57SEric Paris 				rc = -ENOMEM;
1115c9180a57SEric Paris 				goto out_err;
1116c9180a57SEric Paris 			}
1117c9180a57SEric Paris 			break;
1118c9180a57SEric Paris 
1119c9180a57SEric Paris 		case Opt_fscontext:
1120c9180a57SEric Paris 			if (fscontext) {
1121c9180a57SEric Paris 				rc = -EINVAL;
1122c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1123c9180a57SEric Paris 				goto out_err;
1124c9180a57SEric Paris 			}
1125c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
1126c9180a57SEric Paris 			if (!fscontext) {
1127c9180a57SEric Paris 				rc = -ENOMEM;
1128c9180a57SEric Paris 				goto out_err;
1129c9180a57SEric Paris 			}
1130c9180a57SEric Paris 			break;
1131c9180a57SEric Paris 
1132c9180a57SEric Paris 		case Opt_rootcontext:
1133c9180a57SEric Paris 			if (rootcontext) {
1134c9180a57SEric Paris 				rc = -EINVAL;
1135c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1136c9180a57SEric Paris 				goto out_err;
1137c9180a57SEric Paris 			}
1138c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
1139c9180a57SEric Paris 			if (!rootcontext) {
1140c9180a57SEric Paris 				rc = -ENOMEM;
1141c9180a57SEric Paris 				goto out_err;
1142c9180a57SEric Paris 			}
1143c9180a57SEric Paris 			break;
1144c9180a57SEric Paris 
1145c9180a57SEric Paris 		case Opt_defcontext:
1146c9180a57SEric Paris 			if (context || defcontext) {
1147c9180a57SEric Paris 				rc = -EINVAL;
1148c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1149c9180a57SEric Paris 				goto out_err;
1150c9180a57SEric Paris 			}
1151c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
1152c9180a57SEric Paris 			if (!defcontext) {
1153c9180a57SEric Paris 				rc = -ENOMEM;
1154c9180a57SEric Paris 				goto out_err;
1155c9180a57SEric Paris 			}
1156c9180a57SEric Paris 			break;
115711689d47SDavid P. Quigley 		case Opt_labelsupport:
115811689d47SDavid P. Quigley 			break;
1159c9180a57SEric Paris 		default:
1160c9180a57SEric Paris 			rc = -EINVAL;
1161c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
1162c9180a57SEric Paris 			goto out_err;
1163c9180a57SEric Paris 
1164c9180a57SEric Paris 		}
1165c9180a57SEric Paris 	}
1166c9180a57SEric Paris 
1167e0007529SEric Paris 	rc = -ENOMEM;
11688931c3bdSTetsuo Handa 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
1169e0007529SEric Paris 	if (!opts->mnt_opts)
1170e0007529SEric Paris 		goto out_err;
1171e0007529SEric Paris 
11728931c3bdSTetsuo Handa 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
11738931c3bdSTetsuo Handa 				       GFP_KERNEL);
1174023f108dSPaul Moore 	if (!opts->mnt_opts_flags)
1175e0007529SEric Paris 		goto out_err;
1176c9180a57SEric Paris 
1177e0007529SEric Paris 	if (fscontext) {
1178e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
1179e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1180e0007529SEric Paris 	}
1181e0007529SEric Paris 	if (context) {
1182e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
1183e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1184e0007529SEric Paris 	}
1185e0007529SEric Paris 	if (rootcontext) {
1186e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
1187e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1188e0007529SEric Paris 	}
1189e0007529SEric Paris 	if (defcontext) {
1190e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
1191e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1192e0007529SEric Paris 	}
1193e0007529SEric Paris 
1194e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
1195e0007529SEric Paris 	return 0;
1196e0007529SEric Paris 
1197c9180a57SEric Paris out_err:
1198023f108dSPaul Moore 	security_free_mnt_opts(opts);
1199c9180a57SEric Paris 	kfree(context);
1200c9180a57SEric Paris 	kfree(defcontext);
1201c9180a57SEric Paris 	kfree(fscontext);
1202c9180a57SEric Paris 	kfree(rootcontext);
1203c9180a57SEric Paris 	return rc;
12041da177e4SLinus Torvalds }
1205e0007529SEric Paris /*
1206e0007529SEric Paris  * string mount options parsing and call set the sbsec
1207e0007529SEric Paris  */
1208e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
1209e0007529SEric Paris {
1210e0007529SEric Paris 	int rc = 0;
1211e0007529SEric Paris 	char *options = data;
1212e0007529SEric Paris 	struct security_mnt_opts opts;
1213e0007529SEric Paris 
1214e0007529SEric Paris 	security_init_mnt_opts(&opts);
1215e0007529SEric Paris 
1216e0007529SEric Paris 	if (!data)
1217e0007529SEric Paris 		goto out;
1218e0007529SEric Paris 
1219e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1220e0007529SEric Paris 
1221e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1222e0007529SEric Paris 	if (rc)
1223e0007529SEric Paris 		goto out_err;
1224e0007529SEric Paris 
1225e0007529SEric Paris out:
1226649f6e77SDavid Quigley 	rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1227e0007529SEric Paris 
1228e0007529SEric Paris out_err:
1229e0007529SEric Paris 	security_free_mnt_opts(&opts);
1230e0007529SEric Paris 	return rc;
1231e0007529SEric Paris }
12321da177e4SLinus Torvalds 
12333583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
12343583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
12352069f457SEric Paris {
12362069f457SEric Paris 	int i;
12372069f457SEric Paris 	char *prefix;
12382069f457SEric Paris 
12392069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
124011689d47SDavid P. Quigley 		char *has_comma;
124111689d47SDavid P. Quigley 
124211689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
124311689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
124411689d47SDavid P. Quigley 		else
124511689d47SDavid P. Quigley 			has_comma = NULL;
12462069f457SEric Paris 
12472069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
12482069f457SEric Paris 		case CONTEXT_MNT:
12492069f457SEric Paris 			prefix = CONTEXT_STR;
12502069f457SEric Paris 			break;
12512069f457SEric Paris 		case FSCONTEXT_MNT:
12522069f457SEric Paris 			prefix = FSCONTEXT_STR;
12532069f457SEric Paris 			break;
12542069f457SEric Paris 		case ROOTCONTEXT_MNT:
12552069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
12562069f457SEric Paris 			break;
12572069f457SEric Paris 		case DEFCONTEXT_MNT:
12582069f457SEric Paris 			prefix = DEFCONTEXT_STR;
12592069f457SEric Paris 			break;
126012f348b9SEric Paris 		case SBLABEL_MNT:
126111689d47SDavid P. Quigley 			seq_putc(m, ',');
126211689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
126311689d47SDavid P. Quigley 			continue;
12642069f457SEric Paris 		default:
12652069f457SEric Paris 			BUG();
1266a35c6c83SEric Paris 			return;
12672069f457SEric Paris 		};
12682069f457SEric Paris 		/* we need a comma before each option */
12692069f457SEric Paris 		seq_putc(m, ',');
12702069f457SEric Paris 		seq_puts(m, prefix);
12712069f457SEric Paris 		if (has_comma)
12722069f457SEric Paris 			seq_putc(m, '\"');
1273a068acf2SKees Cook 		seq_escape(m, opts->mnt_opts[i], "\"\n\\");
12742069f457SEric Paris 		if (has_comma)
12752069f457SEric Paris 			seq_putc(m, '\"');
12762069f457SEric Paris 	}
12772069f457SEric Paris }
12782069f457SEric Paris 
12792069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
12802069f457SEric Paris {
12812069f457SEric Paris 	struct security_mnt_opts opts;
12822069f457SEric Paris 	int rc;
12832069f457SEric Paris 
12842069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1285383795c2SEric Paris 	if (rc) {
1286383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1287383795c2SEric Paris 		if (rc == -EINVAL)
1288383795c2SEric Paris 			rc = 0;
12892069f457SEric Paris 		return rc;
1290383795c2SEric Paris 	}
12912069f457SEric Paris 
12922069f457SEric Paris 	selinux_write_opts(m, &opts);
12932069f457SEric Paris 
12942069f457SEric Paris 	security_free_mnt_opts(&opts);
12952069f457SEric Paris 
12962069f457SEric Paris 	return rc;
12972069f457SEric Paris }
12982069f457SEric Paris 
12991da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
13021da177e4SLinus Torvalds 	case S_IFSOCK:
13031da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
13041da177e4SLinus Torvalds 	case S_IFLNK:
13051da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
13061da177e4SLinus Torvalds 	case S_IFREG:
13071da177e4SLinus Torvalds 		return SECCLASS_FILE;
13081da177e4SLinus Torvalds 	case S_IFBLK:
13091da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
13101da177e4SLinus Torvalds 	case S_IFDIR:
13111da177e4SLinus Torvalds 		return SECCLASS_DIR;
13121da177e4SLinus Torvalds 	case S_IFCHR:
13131da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
13141da177e4SLinus Torvalds 	case S_IFIFO:
13151da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	return SECCLASS_FILE;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
132213402580SJames Morris static inline int default_protocol_stream(int protocol)
132313402580SJames Morris {
132413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
132513402580SJames Morris }
132613402580SJames Morris 
132713402580SJames Morris static inline int default_protocol_dgram(int protocol)
132813402580SJames Morris {
132913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
133013402580SJames Morris }
133113402580SJames Morris 
13321da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
13331da177e4SLinus Torvalds {
1334aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1335da69a530SStephen Smalley 
13361da177e4SLinus Torvalds 	switch (family) {
13371da177e4SLinus Torvalds 	case PF_UNIX:
13381da177e4SLinus Torvalds 		switch (type) {
13391da177e4SLinus Torvalds 		case SOCK_STREAM:
13401da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
13411da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
13421da177e4SLinus Torvalds 		case SOCK_DGRAM:
13432a764b52SLuis Ressel 		case SOCK_RAW:
13441da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
13451da177e4SLinus Torvalds 		}
13461da177e4SLinus Torvalds 		break;
13471da177e4SLinus Torvalds 	case PF_INET:
13481da177e4SLinus Torvalds 	case PF_INET6:
13491da177e4SLinus Torvalds 		switch (type) {
13501da177e4SLinus Torvalds 		case SOCK_STREAM:
1351da69a530SStephen Smalley 		case SOCK_SEQPACKET:
135213402580SJames Morris 			if (default_protocol_stream(protocol))
13531da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1354da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1355da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
135613402580SJames Morris 			else
135713402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
13581da177e4SLinus Torvalds 		case SOCK_DGRAM:
135913402580SJames Morris 			if (default_protocol_dgram(protocol))
13601da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1361ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1362ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1363da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
136413402580SJames Morris 			else
136513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
13662ee92d46SJames Morris 		case SOCK_DCCP:
13672ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
136813402580SJames Morris 		default:
13691da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 		break;
13721da177e4SLinus Torvalds 	case PF_NETLINK:
13731da177e4SLinus Torvalds 		switch (protocol) {
13741da177e4SLinus Torvalds 		case NETLINK_ROUTE:
13751da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
13767f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
13771da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
13781da177e4SLinus Torvalds 		case NETLINK_NFLOG:
13791da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
13801da177e4SLinus Torvalds 		case NETLINK_XFRM:
13811da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
13821da177e4SLinus Torvalds 		case NETLINK_SELINUX:
13831da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
13846c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
13856c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
13861da177e4SLinus Torvalds 		case NETLINK_AUDIT:
13871da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
13886c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
13896c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
13906c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
13916c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
13926c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
13936c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
13941da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
13951da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
13960c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
13970c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
13986c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
13996c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
14006c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
14016c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
14026c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
14036c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
14046c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
14056c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
14061da177e4SLinus Torvalds 		default:
14071da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
14081da177e4SLinus Torvalds 		}
14091da177e4SLinus Torvalds 	case PF_PACKET:
14101da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
14111da177e4SLinus Torvalds 	case PF_KEY:
14121da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
14133e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
14143e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
1417da69a530SStephen Smalley 	if (extsockclass) {
1418da69a530SStephen Smalley 		switch (family) {
1419da69a530SStephen Smalley 		case PF_AX25:
1420da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1421da69a530SStephen Smalley 		case PF_IPX:
1422da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1423da69a530SStephen Smalley 		case PF_NETROM:
1424da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1425da69a530SStephen Smalley 		case PF_ATMPVC:
1426da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1427da69a530SStephen Smalley 		case PF_X25:
1428da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1429da69a530SStephen Smalley 		case PF_ROSE:
1430da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1431da69a530SStephen Smalley 		case PF_DECnet:
1432da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1433da69a530SStephen Smalley 		case PF_ATMSVC:
1434da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1435da69a530SStephen Smalley 		case PF_RDS:
1436da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1437da69a530SStephen Smalley 		case PF_IRDA:
1438da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1439da69a530SStephen Smalley 		case PF_PPPOX:
1440da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1441da69a530SStephen Smalley 		case PF_LLC:
1442da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1443da69a530SStephen Smalley 		case PF_CAN:
1444da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1445da69a530SStephen Smalley 		case PF_TIPC:
1446da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1447da69a530SStephen Smalley 		case PF_BLUETOOTH:
1448da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1449da69a530SStephen Smalley 		case PF_IUCV:
1450da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1451da69a530SStephen Smalley 		case PF_RXRPC:
1452da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1453da69a530SStephen Smalley 		case PF_ISDN:
1454da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1455da69a530SStephen Smalley 		case PF_PHONET:
1456da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1457da69a530SStephen Smalley 		case PF_IEEE802154:
1458da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1459da69a530SStephen Smalley 		case PF_CAIF:
1460da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1461da69a530SStephen Smalley 		case PF_ALG:
1462da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1463da69a530SStephen Smalley 		case PF_NFC:
1464da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1465da69a530SStephen Smalley 		case PF_VSOCK:
1466da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1467da69a530SStephen Smalley 		case PF_KCM:
1468da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1469da69a530SStephen Smalley 		case PF_QIPCRTR:
1470da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
14713051bf36SLinus Torvalds 		case PF_SMC:
14723051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
14733051bf36SLinus Torvalds #if PF_MAX > 44
1474da69a530SStephen Smalley #error New address family defined, please update this function.
1475da69a530SStephen Smalley #endif
1476da69a530SStephen Smalley 		}
1477da69a530SStephen Smalley 	}
1478da69a530SStephen Smalley 
14791da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
1482134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
14831da177e4SLinus Torvalds 				 u16 tclass,
1484134509d5SStephen Smalley 				 u16 flags,
14851da177e4SLinus Torvalds 				 u32 *sid)
14861da177e4SLinus Torvalds {
14878e6c9693SLucian Adrian Grijincu 	int rc;
1488fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
14898e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
14921da177e4SLinus Torvalds 	if (!buffer)
14931da177e4SLinus Torvalds 		return -ENOMEM;
14941da177e4SLinus Torvalds 
14958e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
14968e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
14978e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
14988e6c9693SLucian Adrian Grijincu 	else {
1499134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
15008e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
15018e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
15028e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
15038e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
15048e6c9693SLucian Adrian Grijincu 				path[1] = '/';
15058e6c9693SLucian Adrian Grijincu 				path++;
15061da177e4SLinus Torvalds 			}
1507134509d5SStephen Smalley 		}
1508aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1509aa8e712cSStephen Smalley 					path, tclass, sid);
15108e6c9693SLucian Adrian Grijincu 	}
15111da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
15121da177e4SLinus Torvalds 	return rc;
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
15161da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
15171da177e4SLinus Torvalds {
15181da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
15191da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
15209287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
15219287aed2SAndreas Gruenbacher 	u16 sclass;
15221da177e4SLinus Torvalds 	struct dentry *dentry;
15231da177e4SLinus Torvalds #define INITCONTEXTLEN 255
15241da177e4SLinus Torvalds 	char *context = NULL;
15251da177e4SLinus Torvalds 	unsigned len = 0;
15261da177e4SLinus Torvalds 	int rc = 0;
15271da177e4SLinus Torvalds 
15286f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
152913457d07SAndreas Gruenbacher 		return 0;
15301da177e4SLinus Torvalds 
15319287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15326f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
153323970741SEric Paris 		goto out_unlock;
15341da177e4SLinus Torvalds 
153513457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
153613457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
153713457d07SAndreas Gruenbacher 
15381da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
15390d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
15401da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
15411da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
15421da177e4SLinus Torvalds 		   server is ready to handle calls. */
15431da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
15441da177e4SLinus Torvalds 		if (list_empty(&isec->list))
15451da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
15461da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
154723970741SEric Paris 		goto out_unlock;
15481da177e4SLinus Torvalds 	}
15491da177e4SLinus Torvalds 
15509287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
15519287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
15529287aed2SAndreas Gruenbacher 	sid = isec->sid;
15539287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
15549287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15559287aed2SAndreas Gruenbacher 
15561da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1557eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1558eb9ae686SDavid Quigley 		break;
15591da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
15605d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
15619287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
15621da177e4SLinus Torvalds 			break;
15631da177e4SLinus Torvalds 		}
15641da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
15651da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
15661da177e4SLinus Torvalds 		if (opt_dentry) {
15671da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
15681da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
15691da177e4SLinus Torvalds 		} else {
15701da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
15711da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
15721da177e4SLinus Torvalds 		}
15731da177e4SLinus Torvalds 		if (!dentry) {
1574df7f54c0SEric Paris 			/*
1575df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1576df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1577df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1578df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1579df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1580df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1581df7f54c0SEric Paris 			 * be used again by userspace.
1582df7f54c0SEric Paris 			 */
15839287aed2SAndreas Gruenbacher 			goto out;
15841da177e4SLinus Torvalds 		}
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
15874cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
15881da177e4SLinus Torvalds 		if (!context) {
15891da177e4SLinus Torvalds 			rc = -ENOMEM;
15901da177e4SLinus Torvalds 			dput(dentry);
15919287aed2SAndreas Gruenbacher 			goto out;
15921da177e4SLinus Torvalds 		}
15934cb912f1SEric Paris 		context[len] = '\0';
15945d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
15951da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1596314dabb8SJames Morris 			kfree(context);
1597314dabb8SJames Morris 
15981da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
15995d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
16001da177e4SLinus Torvalds 			if (rc < 0) {
16011da177e4SLinus Torvalds 				dput(dentry);
16029287aed2SAndreas Gruenbacher 				goto out;
16031da177e4SLinus Torvalds 			}
16041da177e4SLinus Torvalds 			len = rc;
16054cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
16061da177e4SLinus Torvalds 			if (!context) {
16071da177e4SLinus Torvalds 				rc = -ENOMEM;
16081da177e4SLinus Torvalds 				dput(dentry);
16099287aed2SAndreas Gruenbacher 				goto out;
16101da177e4SLinus Torvalds 			}
16114cb912f1SEric Paris 			context[len] = '\0';
16125d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
16131da177e4SLinus Torvalds 		}
16141da177e4SLinus Torvalds 		dput(dentry);
16151da177e4SLinus Torvalds 		if (rc < 0) {
16161da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1617744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1618dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
16191da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
16201da177e4SLinus Torvalds 				kfree(context);
16219287aed2SAndreas Gruenbacher 				goto out;
16221da177e4SLinus Torvalds 			}
16231da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
16241da177e4SLinus Torvalds 			sid = sbsec->def_sid;
16251da177e4SLinus Torvalds 			rc = 0;
16261da177e4SLinus Torvalds 		} else {
1627aa8e712cSStephen Smalley 			rc = security_context_to_sid_default(&selinux_state,
1628aa8e712cSStephen Smalley 							     context, rc, &sid,
1629869ab514SStephen Smalley 							     sbsec->def_sid,
1630869ab514SStephen Smalley 							     GFP_NOFS);
16311da177e4SLinus Torvalds 			if (rc) {
16324ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
16334ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
16344ba0a8adSEric Paris 
16354ba0a8adSEric Paris 				if (rc == -EINVAL) {
16364ba0a8adSEric Paris 					if (printk_ratelimit())
16374ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
16384ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
16394ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
16404ba0a8adSEric Paris 				} else {
1641744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
16421da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
16434ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
16444ba0a8adSEric Paris 				}
16451da177e4SLinus Torvalds 				kfree(context);
16461da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
16471da177e4SLinus Torvalds 				rc = 0;
16481da177e4SLinus Torvalds 				break;
16491da177e4SLinus Torvalds 			}
16501da177e4SLinus Torvalds 		}
16511da177e4SLinus Torvalds 		kfree(context);
16521da177e4SLinus Torvalds 		break;
16531da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
16549287aed2SAndreas Gruenbacher 		sid = task_sid;
16551da177e4SLinus Torvalds 		break;
16561da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
16571da177e4SLinus Torvalds 		/* Default to the fs SID. */
16589287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1661aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1662aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
16631da177e4SLinus Torvalds 		if (rc)
16649287aed2SAndreas Gruenbacher 			goto out;
16651da177e4SLinus Torvalds 		break;
1666c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
16679287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1668c312feb2SEric Paris 		break;
16691da177e4SLinus Torvalds 	default:
1670c312feb2SEric Paris 		/* Default to the fs superblock SID. */
16719287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
16721da177e4SLinus Torvalds 
1673134509d5SStephen Smalley 		if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1674f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1675f64410ecSPaul Moore 			 * procfs inodes */
1676f64410ecSPaul Moore 			if (opt_dentry)
1677f64410ecSPaul Moore 				/* Called from d_instantiate or
1678f64410ecSPaul Moore 				 * d_splice_alias. */
1679f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1680f64410ecSPaul Moore 			else
1681f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1682f64410ecSPaul Moore 				 * find a dentry. */
1683f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1684f64410ecSPaul Moore 			/*
1685f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1686f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1687f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1688f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1689f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1690f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1691f64410ecSPaul Moore 			 * could be used again by userspace.
1692f64410ecSPaul Moore 			 */
1693f64410ecSPaul Moore 			if (!dentry)
16949287aed2SAndreas Gruenbacher 				goto out;
16959287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1696134509d5SStephen Smalley 						   sbsec->flags, &sid);
1697f64410ecSPaul Moore 			dput(dentry);
16981da177e4SLinus Torvalds 			if (rc)
16999287aed2SAndreas Gruenbacher 				goto out;
17001da177e4SLinus Torvalds 		}
17011da177e4SLinus Torvalds 		break;
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
17049287aed2SAndreas Gruenbacher out:
17059287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
17069287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
17079287aed2SAndreas Gruenbacher 		if (!sid || rc) {
17089287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
17099287aed2SAndreas Gruenbacher 			goto out_unlock;
17109287aed2SAndreas Gruenbacher 		}
17119287aed2SAndreas Gruenbacher 
17126f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
17139287aed2SAndreas Gruenbacher 		isec->sid = sid;
17149287aed2SAndreas Gruenbacher 	}
17151da177e4SLinus Torvalds 
171623970741SEric Paris out_unlock:
17179287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
17181da177e4SLinus Torvalds 	return rc;
17191da177e4SLinus Torvalds }
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
17221da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	u32 perm = 0;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	switch (sig) {
17271da177e4SLinus Torvalds 	case SIGCHLD:
17281da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
17291da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
17301da177e4SLinus Torvalds 		break;
17311da177e4SLinus Torvalds 	case SIGKILL:
17321da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
17331da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
17341da177e4SLinus Torvalds 		break;
17351da177e4SLinus Torvalds 	case SIGSTOP:
17361da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
17371da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
17381da177e4SLinus Torvalds 		break;
17391da177e4SLinus Torvalds 	default:
17401da177e4SLinus Torvalds 		/* All other signals. */
17411da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
17421da177e4SLinus Torvalds 		break;
17431da177e4SLinus Torvalds 	}
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	return perm;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
1748b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1749b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1750b68e418cSStephen Smalley #endif
1751b68e418cSStephen Smalley 
17521da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
17536a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
17548e4ff6f2SStephen Smalley 			       int cap, int audit, bool initns)
17551da177e4SLinus Torvalds {
17562bf49690SThomas Liu 	struct common_audit_data ad;
175706112163SEric Paris 	struct av_decision avd;
1758b68e418cSStephen Smalley 	u16 sclass;
17593699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1760b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
176106112163SEric Paris 	int rc;
17621da177e4SLinus Torvalds 
176350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
17641da177e4SLinus Torvalds 	ad.u.cap = cap;
17651da177e4SLinus Torvalds 
1766b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1767b68e418cSStephen Smalley 	case 0:
17688e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1769b68e418cSStephen Smalley 		break;
1770b68e418cSStephen Smalley 	case 1:
17718e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1772b68e418cSStephen Smalley 		break;
1773b68e418cSStephen Smalley 	default:
1774b68e418cSStephen Smalley 		printk(KERN_ERR
1775b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1776b68e418cSStephen Smalley 		BUG();
1777a35c6c83SEric Paris 		return -EINVAL;
1778b68e418cSStephen Smalley 	}
177906112163SEric Paris 
17806b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
17816b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
17829ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
17836b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
17846b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
17859ade0cf4SEric Paris 		if (rc2)
17869ade0cf4SEric Paris 			return rc2;
17879ade0cf4SEric Paris 	}
178806112163SEric Paris 	return rc;
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
17921da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
17931da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
179488e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
17951da177e4SLinus Torvalds 			  struct inode *inode,
17961da177e4SLinus Torvalds 			  u32 perms,
179719e49834SLinus Torvalds 			  struct common_audit_data *adp)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1800275bb41eSDavid Howells 	u32 sid;
18011da177e4SLinus Torvalds 
1802e0e81739SDavid Howells 	validate_creds(cred);
1803e0e81739SDavid Howells 
1804bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1805bbaca6c2SStephen Smalley 		return 0;
1806bbaca6c2SStephen Smalley 
180788e67f3bSDavid Howells 	sid = cred_sid(cred);
18081da177e4SLinus Torvalds 	isec = inode->i_security;
18091da177e4SLinus Torvalds 
18106b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18116b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
18151da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
18161da177e4SLinus Torvalds    pathname if needed. */
181788e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
18181da177e4SLinus Torvalds 				  struct dentry *dentry,
18191da177e4SLinus Torvalds 				  u32 av)
18201da177e4SLinus Torvalds {
1821c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
18222bf49690SThomas Liu 	struct common_audit_data ad;
182388e67f3bSDavid Howells 
182450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18252875fa00SEric Paris 	ad.u.dentry = dentry;
18265d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
182719e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
18282875fa00SEric Paris }
18292875fa00SEric Paris 
18302875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
18312875fa00SEric Paris    the path to help the auditing code to more easily generate the
18322875fa00SEric Paris    pathname if needed. */
18332875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
18343f7036a0SAl Viro 				const struct path *path,
18352875fa00SEric Paris 				u32 av)
18362875fa00SEric Paris {
1837c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
18382875fa00SEric Paris 	struct common_audit_data ad;
18392875fa00SEric Paris 
184050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
18412875fa00SEric Paris 	ad.u.path = *path;
18425d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
184319e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
184613f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
184713f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
184813f8e981SDavid Howells 				     struct file *file,
184913f8e981SDavid Howells 				     u32 av)
185013f8e981SDavid Howells {
185113f8e981SDavid Howells 	struct common_audit_data ad;
185213f8e981SDavid Howells 
185343af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
185443af5de7SVivek Goyal 	ad.u.file = file;
185519e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
185613f8e981SDavid Howells }
185713f8e981SDavid Howells 
1858f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1859f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1860f66e448cSChenbo Feng #endif
1861f66e448cSChenbo Feng 
18621da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
18631da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
18641da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
18651da177e4SLinus Torvalds    check a particular permission to the file.
18661da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
18671da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
18681da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
18691da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
187088e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
18711da177e4SLinus Torvalds 			 struct file *file,
18721da177e4SLinus Torvalds 			 u32 av)
18731da177e4SLinus Torvalds {
18741da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1875496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
18762bf49690SThomas Liu 	struct common_audit_data ad;
187788e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
18781da177e4SLinus Torvalds 	int rc;
18791da177e4SLinus Torvalds 
188043af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
188143af5de7SVivek Goyal 	ad.u.file = file;
18821da177e4SLinus Torvalds 
1883275bb41eSDavid Howells 	if (sid != fsec->sid) {
18846b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
18856b6bc620SStephen Smalley 				  sid, fsec->sid,
18861da177e4SLinus Torvalds 				  SECCLASS_FD,
18871da177e4SLinus Torvalds 				  FD__USE,
18881da177e4SLinus Torvalds 				  &ad);
18891da177e4SLinus Torvalds 		if (rc)
189088e67f3bSDavid Howells 			goto out;
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 
1893f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1894f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1895f66e448cSChenbo Feng 	if (rc)
1896f66e448cSChenbo Feng 		return rc;
1897f66e448cSChenbo Feng #endif
1898f66e448cSChenbo Feng 
18991da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
190088e67f3bSDavid Howells 	rc = 0;
19011da177e4SLinus Torvalds 	if (av)
190219e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
19031da177e4SLinus Torvalds 
190488e67f3bSDavid Howells out:
190588e67f3bSDavid Howells 	return rc;
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
1908c3c188b2SDavid Howells /*
1909c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1910c3c188b2SDavid Howells  */
1911c957f6dfSVivek Goyal static int
1912c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1913c957f6dfSVivek Goyal 				 struct inode *dir,
1914c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1915c3c188b2SDavid Howells 				 u32 *_new_isid)
1916c3c188b2SDavid Howells {
1917c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1918c3c188b2SDavid Howells 
1919c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1920c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1921c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1922c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1923c3c188b2SDavid Howells 		   tsec->create_sid) {
1924c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1925c3c188b2SDavid Howells 	} else {
192620cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1927aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1928aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1929c3c188b2SDavid Howells 					       name, _new_isid);
1930c3c188b2SDavid Howells 	}
1931c3c188b2SDavid Howells 
1932c3c188b2SDavid Howells 	return 0;
1933c3c188b2SDavid Howells }
1934c3c188b2SDavid Howells 
19351da177e4SLinus Torvalds /* Check whether a task can create a file. */
19361da177e4SLinus Torvalds static int may_create(struct inode *dir,
19371da177e4SLinus Torvalds 		      struct dentry *dentry,
19381da177e4SLinus Torvalds 		      u16 tclass)
19391da177e4SLinus Torvalds {
19405fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
19411da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
19421da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1943275bb41eSDavid Howells 	u32 sid, newsid;
19442bf49690SThomas Liu 	struct common_audit_data ad;
19451da177e4SLinus Torvalds 	int rc;
19461da177e4SLinus Torvalds 
194783da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
19481da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
19491da177e4SLinus Torvalds 
1950275bb41eSDavid Howells 	sid = tsec->sid;
1951275bb41eSDavid Howells 
195250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1953a269434dSEric Paris 	ad.u.dentry = dentry;
19541da177e4SLinus Torvalds 
19556b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19566b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
19571da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
19581da177e4SLinus Torvalds 			  &ad);
19591da177e4SLinus Torvalds 	if (rc)
19601da177e4SLinus Torvalds 		return rc;
19611da177e4SLinus Torvalds 
1962c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(), dir,
1963c957f6dfSVivek Goyal 					   &dentry->d_name, tclass, &newsid);
19641da177e4SLinus Torvalds 	if (rc)
19651da177e4SLinus Torvalds 		return rc;
19661da177e4SLinus Torvalds 
19676b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19686b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
19691da177e4SLinus Torvalds 	if (rc)
19701da177e4SLinus Torvalds 		return rc;
19711da177e4SLinus Torvalds 
19726b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19736b6bc620SStephen Smalley 			    newsid, sbsec->sid,
19741da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
19751da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds #define MAY_LINK	0
19791da177e4SLinus Torvalds #define MAY_UNLINK	1
19801da177e4SLinus Torvalds #define MAY_RMDIR	2
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
19831da177e4SLinus Torvalds static int may_link(struct inode *dir,
19841da177e4SLinus Torvalds 		    struct dentry *dentry,
19851da177e4SLinus Torvalds 		    int kind)
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds {
19881da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
19892bf49690SThomas Liu 	struct common_audit_data ad;
1990275bb41eSDavid Howells 	u32 sid = current_sid();
19911da177e4SLinus Torvalds 	u32 av;
19921da177e4SLinus Torvalds 	int rc;
19931da177e4SLinus Torvalds 
199483da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
199583da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
19961da177e4SLinus Torvalds 
199750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1998a269434dSEric Paris 	ad.u.dentry = dentry;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	av = DIR__SEARCH;
20011da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
20026b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20036b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
20041da177e4SLinus Torvalds 	if (rc)
20051da177e4SLinus Torvalds 		return rc;
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	switch (kind) {
20081da177e4SLinus Torvalds 	case MAY_LINK:
20091da177e4SLinus Torvalds 		av = FILE__LINK;
20101da177e4SLinus Torvalds 		break;
20111da177e4SLinus Torvalds 	case MAY_UNLINK:
20121da177e4SLinus Torvalds 		av = FILE__UNLINK;
20131da177e4SLinus Torvalds 		break;
20141da177e4SLinus Torvalds 	case MAY_RMDIR:
20151da177e4SLinus Torvalds 		av = DIR__RMDIR;
20161da177e4SLinus Torvalds 		break;
20171da177e4SLinus Torvalds 	default:
2018744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
2019744ba35eSEric Paris 			__func__, kind);
20201da177e4SLinus Torvalds 		return 0;
20211da177e4SLinus Torvalds 	}
20221da177e4SLinus Torvalds 
20236b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20246b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
20251da177e4SLinus Torvalds 	return rc;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
20291da177e4SLinus Torvalds 			     struct dentry *old_dentry,
20301da177e4SLinus Torvalds 			     struct inode *new_dir,
20311da177e4SLinus Torvalds 			     struct dentry *new_dentry)
20321da177e4SLinus Torvalds {
20331da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
20342bf49690SThomas Liu 	struct common_audit_data ad;
2035275bb41eSDavid Howells 	u32 sid = current_sid();
20361da177e4SLinus Torvalds 	u32 av;
20371da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
20381da177e4SLinus Torvalds 	int rc;
20391da177e4SLinus Torvalds 
204083da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
204183da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
2042e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
204383da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
20441da177e4SLinus Torvalds 
204550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
20461da177e4SLinus Torvalds 
2047a269434dSEric Paris 	ad.u.dentry = old_dentry;
20486b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20496b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
20501da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
20511da177e4SLinus Torvalds 	if (rc)
20521da177e4SLinus Torvalds 		return rc;
20536b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20546b6bc620SStephen Smalley 			  sid, old_isec->sid,
20551da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
20561da177e4SLinus Torvalds 	if (rc)
20571da177e4SLinus Torvalds 		return rc;
20581da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
20596b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20606b6bc620SStephen Smalley 				  sid, old_isec->sid,
20611da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
20621da177e4SLinus Torvalds 		if (rc)
20631da177e4SLinus Torvalds 			return rc;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
2066a269434dSEric Paris 	ad.u.dentry = new_dentry;
20671da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
20682c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
20691da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
20706b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20716b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
20721da177e4SLinus Torvalds 	if (rc)
20731da177e4SLinus Torvalds 		return rc;
20742c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
207583da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
2076e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
20776b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20786b6bc620SStephen Smalley 				  sid, new_isec->sid,
20791da177e4SLinus Torvalds 				  new_isec->sclass,
20801da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
20811da177e4SLinus Torvalds 		if (rc)
20821da177e4SLinus Torvalds 			return rc;
20831da177e4SLinus Torvalds 	}
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	return 0;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
208988e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
20901da177e4SLinus Torvalds 			       struct super_block *sb,
20911da177e4SLinus Torvalds 			       u32 perms,
20922bf49690SThomas Liu 			       struct common_audit_data *ad)
20931da177e4SLinus Torvalds {
20941da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
209588e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	sbsec = sb->s_security;
20986b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20996b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
21031da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds 	u32 av = 0;
21061da177e4SLinus Torvalds 
2107dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
21081da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
21091da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
21101da177e4SLinus Torvalds 		if (mask & MAY_READ)
21111da177e4SLinus Torvalds 			av |= FILE__READ;
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
21141da177e4SLinus Torvalds 			av |= FILE__APPEND;
21151da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
21161da177e4SLinus Torvalds 			av |= FILE__WRITE;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	} else {
21191da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
21201da177e4SLinus Torvalds 			av |= DIR__SEARCH;
21211da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
21221da177e4SLinus Torvalds 			av |= DIR__WRITE;
21231da177e4SLinus Torvalds 		if (mask & MAY_READ)
21241da177e4SLinus Torvalds 			av |= DIR__READ;
21251da177e4SLinus Torvalds 	}
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	return av;
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
21311da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
21321da177e4SLinus Torvalds {
21331da177e4SLinus Torvalds 	u32 av = 0;
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
21361da177e4SLinus Torvalds 		av |= FILE__READ;
21371da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
21381da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
21391da177e4SLinus Torvalds 			av |= FILE__APPEND;
21401da177e4SLinus Torvalds 		else
21411da177e4SLinus Torvalds 			av |= FILE__WRITE;
21421da177e4SLinus Torvalds 	}
21430794c66dSStephen Smalley 	if (!av) {
21440794c66dSStephen Smalley 		/*
21450794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
21460794c66dSStephen Smalley 		 */
21470794c66dSStephen Smalley 		av = FILE__IOCTL;
21480794c66dSStephen Smalley 	}
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	return av;
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
21538b6a5a37SEric Paris /*
21548b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
21558b6a5a37SEric Paris  * open permission.
21568b6a5a37SEric Paris  */
21578b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
21588b6a5a37SEric Paris {
21598b6a5a37SEric Paris 	u32 av = file_to_av(file);
2160ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
21618b6a5a37SEric Paris 
2162aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
2163aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
21648b6a5a37SEric Paris 		av |= FILE__OPEN;
216549b7b8deSEric Paris 
21668b6a5a37SEric Paris 	return av;
21678b6a5a37SEric Paris }
21688b6a5a37SEric Paris 
21691da177e4SLinus Torvalds /* Hook functions begin here. */
21701da177e4SLinus Torvalds 
217179af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
217279af7307SStephen Smalley {
217379af7307SStephen Smalley 	u32 mysid = current_sid();
217479af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
217579af7307SStephen Smalley 
21766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21776b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
217879af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
217979af7307SStephen Smalley }
218079af7307SStephen Smalley 
218179af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
218279af7307SStephen Smalley 				      struct task_struct *to)
218379af7307SStephen Smalley {
218479af7307SStephen Smalley 	u32 mysid = current_sid();
218579af7307SStephen Smalley 	u32 fromsid = task_sid(from);
218679af7307SStephen Smalley 	u32 tosid = task_sid(to);
218779af7307SStephen Smalley 	int rc;
218879af7307SStephen Smalley 
218979af7307SStephen Smalley 	if (mysid != fromsid) {
21906b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
21916b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
219279af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
219379af7307SStephen Smalley 		if (rc)
219479af7307SStephen Smalley 			return rc;
219579af7307SStephen Smalley 	}
219679af7307SStephen Smalley 
21976b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21986b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
219979af7307SStephen Smalley 			    NULL);
220079af7307SStephen Smalley }
220179af7307SStephen Smalley 
220279af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
220379af7307SStephen Smalley 					  struct task_struct *to)
220479af7307SStephen Smalley {
220579af7307SStephen Smalley 	u32 fromsid = task_sid(from);
220679af7307SStephen Smalley 	u32 tosid = task_sid(to);
220779af7307SStephen Smalley 
22086b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22096b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
221079af7307SStephen Smalley 			    NULL);
221179af7307SStephen Smalley }
221279af7307SStephen Smalley 
221379af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
221479af7307SStephen Smalley 					struct task_struct *to,
221579af7307SStephen Smalley 					struct file *file)
221679af7307SStephen Smalley {
221779af7307SStephen Smalley 	u32 sid = task_sid(to);
221879af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
221983da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
222020cdef8dSPaul Moore 	struct inode_security_struct *isec;
222179af7307SStephen Smalley 	struct common_audit_data ad;
222279af7307SStephen Smalley 	int rc;
222379af7307SStephen Smalley 
222479af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
222579af7307SStephen Smalley 	ad.u.path = file->f_path;
222679af7307SStephen Smalley 
222779af7307SStephen Smalley 	if (sid != fsec->sid) {
22286b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22296b6bc620SStephen Smalley 				  sid, fsec->sid,
223079af7307SStephen Smalley 				  SECCLASS_FD,
223179af7307SStephen Smalley 				  FD__USE,
223279af7307SStephen Smalley 				  &ad);
223379af7307SStephen Smalley 		if (rc)
223479af7307SStephen Smalley 			return rc;
223579af7307SStephen Smalley 	}
223679af7307SStephen Smalley 
2237f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2238f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2239f66e448cSChenbo Feng 	if (rc)
2240f66e448cSChenbo Feng 		return rc;
2241f66e448cSChenbo Feng #endif
2242f66e448cSChenbo Feng 
224383da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
224479af7307SStephen Smalley 		return 0;
224579af7307SStephen Smalley 
224620cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
22476b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22486b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
224979af7307SStephen Smalley 			    &ad);
225079af7307SStephen Smalley }
225179af7307SStephen Smalley 
22529e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2253006ebb40SStephen Smalley 				     unsigned int mode)
22541da177e4SLinus Torvalds {
2255275bb41eSDavid Howells 	u32 sid = current_sid();
2256275bb41eSDavid Howells 	u32 csid = task_sid(child);
2257006ebb40SStephen Smalley 
2258be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
22596b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22606b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2261be0554c9SStephen Smalley 
22626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22636b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
22645cd9c58fSDavid Howells }
22655cd9c58fSDavid Howells 
22665cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
22675cd9c58fSDavid Howells {
22686b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22696b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2270be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
22741da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
22751da177e4SLinus Torvalds {
22766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22776b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2278be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
2281d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2282d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
228315a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
228415a2460eSDavid Howells 			  const kernel_cap_t *permitted)
22851da177e4SLinus Torvalds {
22866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22876b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2288be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
22915626d3e8SJames Morris /*
22925626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
22935626d3e8SJames Morris  * which was removed).
22945626d3e8SJames Morris  *
22955626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
22965626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
22975626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
22985626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
22995626d3e8SJames Morris  */
23005626d3e8SJames Morris 
23016a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
23026a9de491SEric Paris 			   int cap, int audit)
23031da177e4SLinus Torvalds {
23048e4ff6f2SStephen Smalley 	return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
23081da177e4SLinus Torvalds {
230988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
23101da177e4SLinus Torvalds 	int rc = 0;
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 	if (!sb)
23131da177e4SLinus Torvalds 		return 0;
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 	switch (cmds) {
23161da177e4SLinus Torvalds 	case Q_SYNC:
23171da177e4SLinus Torvalds 	case Q_QUOTAON:
23181da177e4SLinus Torvalds 	case Q_QUOTAOFF:
23191da177e4SLinus Torvalds 	case Q_SETINFO:
23201da177e4SLinus Torvalds 	case Q_SETQUOTA:
232188e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
23221da177e4SLinus Torvalds 		break;
23231da177e4SLinus Torvalds 	case Q_GETFMT:
23241da177e4SLinus Torvalds 	case Q_GETINFO:
23251da177e4SLinus Torvalds 	case Q_GETQUOTA:
232688e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
23271da177e4SLinus Torvalds 		break;
23281da177e4SLinus Torvalds 	default:
23291da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
23301da177e4SLinus Torvalds 		break;
23311da177e4SLinus Torvalds 	}
23321da177e4SLinus Torvalds 	return rc;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
23361da177e4SLinus Torvalds {
233788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
233888e67f3bSDavid Howells 
23392875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
234212b3052cSEric Paris static int selinux_syslog(int type)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	switch (type) {
2345d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2346d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
23476b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
23486b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2349be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2350d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2351d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2352d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2353d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
23546b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
23556b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2356be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2357be0554c9SStephen Smalley 				    NULL);
23581da177e4SLinus Torvalds 	}
2359be0554c9SStephen Smalley 	/* All other syslog types */
23606b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
23616b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2362be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds /*
23661da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
23671da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
23681da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
23691da177e4SLinus Torvalds  *
23701da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
23711da177e4SLinus Torvalds  * processes that allocate mappings.
23721da177e4SLinus Torvalds  */
237334b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
23741da177e4SLinus Torvalds {
23751da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
23761da177e4SLinus Torvalds 
2377b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
23788e4ff6f2SStephen Smalley 				 SECURITY_CAP_NOAUDIT, true);
23791da177e4SLinus Torvalds 	if (rc == 0)
23801da177e4SLinus Torvalds 		cap_sys_admin = 1;
23811da177e4SLinus Torvalds 
2382b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds /* binprm security operations */
23861da177e4SLinus Torvalds 
2387be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
23880c6181cbSPaul Moore {
23890c6181cbSPaul Moore 	u32 sid = 0;
23900c6181cbSPaul Moore 	struct task_struct *tracer;
23910c6181cbSPaul Moore 
23920c6181cbSPaul Moore 	rcu_read_lock();
2393be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
23940c6181cbSPaul Moore 	if (tracer)
23950c6181cbSPaul Moore 		sid = task_sid(tracer);
23960c6181cbSPaul Moore 	rcu_read_unlock();
23970c6181cbSPaul Moore 
23980c6181cbSPaul Moore 	return sid;
23990c6181cbSPaul Moore }
24000c6181cbSPaul Moore 
24017b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
24027b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
24037b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
24047b0d0b40SStephen Smalley {
24057b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2406380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
24077b0d0b40SStephen Smalley 	int rc;
2408af63f419SStephen Smalley 	u32 av;
24097b0d0b40SStephen Smalley 
24107b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
24117b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
24127b0d0b40SStephen Smalley 
24137b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
24147b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
24157b0d0b40SStephen Smalley 
24167b0d0b40SStephen Smalley 	/*
2417af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2418af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2419af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2420af63f419SStephen Smalley 	 * the old and new contexts.
2421af63f419SStephen Smalley 	 */
2422aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2423af63f419SStephen Smalley 		av = 0;
2424af63f419SStephen Smalley 		if (nnp)
2425af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2426af63f419SStephen Smalley 		if (nosuid)
2427af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
24286b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
24296b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2430af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2431af63f419SStephen Smalley 		if (!rc)
2432af63f419SStephen Smalley 			return 0;
2433af63f419SStephen Smalley 	}
2434af63f419SStephen Smalley 
2435af63f419SStephen Smalley 	/*
2436af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2437af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2438af63f419SStephen Smalley 	 * of the permissions of the current SID.
24397b0d0b40SStephen Smalley 	 */
2440aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2441aa8e712cSStephen Smalley 					 new_tsec->sid);
2442af63f419SStephen Smalley 	if (!rc)
2443af63f419SStephen Smalley 		return 0;
2444af63f419SStephen Smalley 
24457b0d0b40SStephen Smalley 	/*
24467b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
24477b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
24487b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
24497b0d0b40SStephen Smalley 	 */
24507b0d0b40SStephen Smalley 	if (nnp)
24517b0d0b40SStephen Smalley 		return -EPERM;
24527b0d0b40SStephen Smalley 	return -EACCES;
24537b0d0b40SStephen Smalley }
24547b0d0b40SStephen Smalley 
2455a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
24561da177e4SLinus Torvalds {
2457a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2458a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24591da177e4SLinus Torvalds 	struct inode_security_struct *isec;
24602bf49690SThomas Liu 	struct common_audit_data ad;
2461496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
24621da177e4SLinus Torvalds 	int rc;
24631da177e4SLinus Torvalds 
2464a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2465a6f76f23SDavid Howells 	 * the script interpreter */
2466ddb4a144SKees Cook 	if (bprm->called_set_creds)
24671da177e4SLinus Torvalds 		return 0;
24681da177e4SLinus Torvalds 
2469a6f76f23SDavid Howells 	old_tsec = current_security();
2470a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
247183da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	/* Default to the current task SID. */
2474a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2475a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
24761da177e4SLinus Torvalds 
247728eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2478a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2479a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2480a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
24811da177e4SLinus Torvalds 
2482a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2483a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
24841da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2485a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2486259e5e6cSAndy Lutomirski 
24877b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
24887b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
24897b0d0b40SStephen Smalley 		if (rc)
24907b0d0b40SStephen Smalley 			return rc;
24911da177e4SLinus Torvalds 	} else {
24921da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2493aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2494aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2495652bb9b0SEric Paris 					     &new_tsec->sid);
24961da177e4SLinus Torvalds 		if (rc)
24971da177e4SLinus Torvalds 			return rc;
24987b0d0b40SStephen Smalley 
24997b0d0b40SStephen Smalley 		/*
25007b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
25017b0d0b40SStephen Smalley 		 * transition.
25027b0d0b40SStephen Smalley 		 */
25037b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
25047b0d0b40SStephen Smalley 		if (rc)
25057b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
25061da177e4SLinus Torvalds 	}
25071da177e4SLinus Torvalds 
250843af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
250943af5de7SVivek Goyal 	ad.u.file = bprm->file;
25101da177e4SLinus Torvalds 
2511a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
25126b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25136b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
25141da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
25151da177e4SLinus Torvalds 		if (rc)
25161da177e4SLinus Torvalds 			return rc;
25171da177e4SLinus Torvalds 	} else {
25181da177e4SLinus Torvalds 		/* Check permissions for the transition. */
25196b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25206b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
25211da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
25221da177e4SLinus Torvalds 		if (rc)
25231da177e4SLinus Torvalds 			return rc;
25241da177e4SLinus Torvalds 
25256b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25266b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
25271da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
25281da177e4SLinus Torvalds 		if (rc)
25291da177e4SLinus Torvalds 			return rc;
25301da177e4SLinus Torvalds 
2531a6f76f23SDavid Howells 		/* Check for shared state */
2532a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
25336b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
25346b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2535a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2536a6f76f23SDavid Howells 					  NULL);
2537a6f76f23SDavid Howells 			if (rc)
2538a6f76f23SDavid Howells 				return -EPERM;
25391da177e4SLinus Torvalds 		}
25401da177e4SLinus Torvalds 
2541a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2542a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
25439227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2544be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2545a6f76f23SDavid Howells 			if (ptsid != 0) {
25466b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
25476b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2548a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2549a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2550a6f76f23SDavid Howells 				if (rc)
2551a6f76f23SDavid Howells 					return -EPERM;
2552a6f76f23SDavid Howells 			}
2553a6f76f23SDavid Howells 		}
2554a6f76f23SDavid Howells 
2555a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2556a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2557a6f76f23SDavid Howells 
25581da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
25591da177e4SLinus Torvalds 		   the noatsecure permission is granted between
25601da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
25616b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25626b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
256362874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
256462874c3aSKees Cook 				  NULL);
256562874c3aSKees Cook 		bprm->secureexec |= !!rc;
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 
256862874c3aSKees Cook 	return 0;
25691da177e4SLinus Torvalds }
25701da177e4SLinus Torvalds 
2571c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2572c3c073f8SAl Viro {
2573c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2574c3c073f8SAl Viro }
2575c3c073f8SAl Viro 
25761da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2577745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2578745ca247SDavid Howells 					    struct files_struct *files)
25791da177e4SLinus Torvalds {
25801da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2581b20c8122SStephen Smalley 	struct tty_struct *tty;
258224ec839cSPeter Zijlstra 	int drop_tty = 0;
2583c3c073f8SAl Viro 	unsigned n;
25841da177e4SLinus Torvalds 
258524ec839cSPeter Zijlstra 	tty = get_current_tty();
25861da177e4SLinus Torvalds 	if (tty) {
25874a510969SPeter Hurley 		spin_lock(&tty->files_lock);
258837dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2589d996b62aSNick Piggin 			struct tty_file_private *file_priv;
259037dd0bd0SEric Paris 
25911da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
259213f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
259313f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
259413f8e981SDavid Howells 			   open file may belong to another process and we are
259513f8e981SDavid Howells 			   only interested in the inode-based check here. */
2596d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2597d996b62aSNick Piggin 						struct tty_file_private, list);
2598d996b62aSNick Piggin 			file = file_priv->file;
259913f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
260024ec839cSPeter Zijlstra 				drop_tty = 1;
26011da177e4SLinus Torvalds 		}
26024a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2603452a00d2SAlan Cox 		tty_kref_put(tty);
26041da177e4SLinus Torvalds 	}
260598a27ba4SEric W. Biederman 	/* Reset controlling tty. */
260698a27ba4SEric W. Biederman 	if (drop_tty)
260798a27ba4SEric W. Biederman 		no_tty();
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2610c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2611c3c073f8SAl Viro 	if (!n) /* none found? */
2612c3c073f8SAl Viro 		return;
26131da177e4SLinus Torvalds 
2614c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
261545525b26SAl Viro 	if (IS_ERR(devnull))
261645525b26SAl Viro 		devnull = NULL;
2617c3c073f8SAl Viro 	/* replace all the matching ones with this */
2618c3c073f8SAl Viro 	do {
261945525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2620c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
262145525b26SAl Viro 	if (devnull)
2622c3c073f8SAl Viro 		fput(devnull);
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds /*
2626a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
26271da177e4SLinus Torvalds  */
2628a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
26291da177e4SLinus Torvalds {
2630a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
26311da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
26321da177e4SLinus Torvalds 	int rc, i;
26331da177e4SLinus Torvalds 
2634a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2635a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
26361da177e4SLinus Torvalds 		return;
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2639a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
26401da177e4SLinus Torvalds 
2641a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2642a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2643a6f76f23SDavid Howells 
2644a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2645a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2646a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2647a6f76f23SDavid Howells 	 *
2648a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2649a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2650a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2651a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2652a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2653a6f76f23SDavid Howells 	 */
26546b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
26556b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2656a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2657a6f76f23SDavid Howells 	if (rc) {
2658eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2659eb2d55a3SOleg Nesterov 		task_lock(current);
2660a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2661a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2662a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2663a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2664a6f76f23SDavid Howells 		}
2665eb2d55a3SOleg Nesterov 		task_unlock(current);
2666baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2667eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2668a6f76f23SDavid Howells 	}
2669a6f76f23SDavid Howells }
2670a6f76f23SDavid Howells 
2671a6f76f23SDavid Howells /*
2672a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2673a6f76f23SDavid Howells  * due to exec
2674a6f76f23SDavid Howells  */
2675a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2676a6f76f23SDavid Howells {
2677a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2678a6f76f23SDavid Howells 	struct itimerval itimer;
2679a6f76f23SDavid Howells 	u32 osid, sid;
2680a6f76f23SDavid Howells 	int rc, i;
2681a6f76f23SDavid Howells 
2682a6f76f23SDavid Howells 	osid = tsec->osid;
2683a6f76f23SDavid Howells 	sid = tsec->sid;
2684a6f76f23SDavid Howells 
2685a6f76f23SDavid Howells 	if (sid == osid)
2686a6f76f23SDavid Howells 		return;
2687a6f76f23SDavid Howells 
2688a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2689a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2690a6f76f23SDavid Howells 	 * flush and unblock signals.
2691a6f76f23SDavid Howells 	 *
2692a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2693a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2694a6f76f23SDavid Howells 	 */
26956b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
26966b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
26971da177e4SLinus Torvalds 	if (rc) {
2698baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
26991da177e4SLinus Torvalds 			memset(&itimer, 0, sizeof itimer);
27001da177e4SLinus Torvalds 			for (i = 0; i < 3; i++)
27011da177e4SLinus Torvalds 				do_setitimer(i, &itimer, NULL);
2702baa73d9eSNicolas Pitre 		}
27031da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
27049e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
27059e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
27069e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
27071da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
27081da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
27099e7c8f8cSOleg Nesterov 			recalc_sigpending();
27103bcac026SDavid Howells 		}
27111da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
27121da177e4SLinus Torvalds 	}
27131da177e4SLinus Torvalds 
2714a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2715a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2716ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
27170b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2718ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds /* superblock security operations */
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
27241da177e4SLinus Torvalds {
27251da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
27291da177e4SLinus Torvalds {
27301da177e4SLinus Torvalds 	superblock_free_security(sb);
27311da177e4SLinus Torvalds }
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
27341da177e4SLinus Torvalds {
27351da177e4SLinus Torvalds 	if (plen > olen)
27361da177e4SLinus Torvalds 		return 0;
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
27421da177e4SLinus Torvalds {
2743832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2744832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2745832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
274611689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
274711689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
27511da177e4SLinus Torvalds {
27521da177e4SLinus Torvalds 	if (!*first) {
27531da177e4SLinus Torvalds 		**to = ',';
27541da177e4SLinus Torvalds 		*to += 1;
27553528a953SCory Olmo 	} else
27561da177e4SLinus Torvalds 		*first = 0;
27571da177e4SLinus Torvalds 	memcpy(*to, from, len);
27581da177e4SLinus Torvalds 	*to += len;
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
27613528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
27623528a953SCory Olmo 				       int len)
27633528a953SCory Olmo {
27643528a953SCory Olmo 	int current_size = 0;
27653528a953SCory Olmo 
27663528a953SCory Olmo 	if (!*first) {
27673528a953SCory Olmo 		**to = '|';
27683528a953SCory Olmo 		*to += 1;
2769828dfe1dSEric Paris 	} else
27703528a953SCory Olmo 		*first = 0;
27713528a953SCory Olmo 
27723528a953SCory Olmo 	while (current_size < len) {
27733528a953SCory Olmo 		if (*from != '"') {
27743528a953SCory Olmo 			**to = *from;
27753528a953SCory Olmo 			*to += 1;
27763528a953SCory Olmo 		}
27773528a953SCory Olmo 		from += 1;
27783528a953SCory Olmo 		current_size += 1;
27793528a953SCory Olmo 	}
27803528a953SCory Olmo }
27813528a953SCory Olmo 
2782e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
27831da177e4SLinus Torvalds {
27841da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
27851da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
27861da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
27873528a953SCory Olmo 	int open_quote = 0;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	in_curr = orig;
27901da177e4SLinus Torvalds 	sec_curr = copy;
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
27931da177e4SLinus Torvalds 	if (!nosec) {
27941da177e4SLinus Torvalds 		rc = -ENOMEM;
27951da177e4SLinus Torvalds 		goto out;
27961da177e4SLinus Torvalds 	}
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds 	nosec_save = nosec;
27991da177e4SLinus Torvalds 	fnosec = fsec = 1;
28001da177e4SLinus Torvalds 	in_save = in_end = orig;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	do {
28033528a953SCory Olmo 		if (*in_end == '"')
28043528a953SCory Olmo 			open_quote = !open_quote;
28053528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
28063528a953SCory Olmo 				*in_end == '\0') {
28071da177e4SLinus Torvalds 			int len = in_end - in_curr;
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
28103528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
28111da177e4SLinus Torvalds 			else
28121da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 			in_curr = in_end + 1;
28151da177e4SLinus Torvalds 		}
28161da177e4SLinus Torvalds 	} while (*in_end++);
28171da177e4SLinus Torvalds 
28186931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2819da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
28201da177e4SLinus Torvalds out:
28211da177e4SLinus Torvalds 	return rc;
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
2824026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2825026eb167SEric Paris {
2826026eb167SEric Paris 	int rc, i, *flags;
2827026eb167SEric Paris 	struct security_mnt_opts opts;
2828026eb167SEric Paris 	char *secdata, **mount_options;
2829026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2830026eb167SEric Paris 
2831026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2832026eb167SEric Paris 		return 0;
2833026eb167SEric Paris 
2834026eb167SEric Paris 	if (!data)
2835026eb167SEric Paris 		return 0;
2836026eb167SEric Paris 
2837026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2838026eb167SEric Paris 		return 0;
2839026eb167SEric Paris 
2840026eb167SEric Paris 	security_init_mnt_opts(&opts);
2841026eb167SEric Paris 	secdata = alloc_secdata();
2842026eb167SEric Paris 	if (!secdata)
2843026eb167SEric Paris 		return -ENOMEM;
2844026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2845026eb167SEric Paris 	if (rc)
2846026eb167SEric Paris 		goto out_free_secdata;
2847026eb167SEric Paris 
2848026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2849026eb167SEric Paris 	if (rc)
2850026eb167SEric Paris 		goto out_free_secdata;
2851026eb167SEric Paris 
2852026eb167SEric Paris 	mount_options = opts.mnt_opts;
2853026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2854026eb167SEric Paris 
2855026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2856026eb167SEric Paris 		u32 sid;
2857026eb167SEric Paris 
285812f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
2859026eb167SEric Paris 			continue;
2860aa8e712cSStephen Smalley 		rc = security_context_str_to_sid(&selinux_state,
2861aa8e712cSStephen Smalley 						 mount_options[i], &sid,
2862aa8e712cSStephen Smalley 						 GFP_KERNEL);
2863026eb167SEric Paris 		if (rc) {
286444be2f65SRasmus Villemoes 			printk(KERN_WARNING "SELinux: security_context_str_to_sid"
286529b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
286629b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2867026eb167SEric Paris 			goto out_free_opts;
2868026eb167SEric Paris 		}
2869026eb167SEric Paris 		rc = -EINVAL;
2870026eb167SEric Paris 		switch (flags[i]) {
2871026eb167SEric Paris 		case FSCONTEXT_MNT:
2872026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2873026eb167SEric Paris 				goto out_bad_option;
2874026eb167SEric Paris 			break;
2875026eb167SEric Paris 		case CONTEXT_MNT:
2876026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2877026eb167SEric Paris 				goto out_bad_option;
2878026eb167SEric Paris 			break;
2879026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2880026eb167SEric Paris 			struct inode_security_struct *root_isec;
288183da53c5SAndreas Gruenbacher 			root_isec = backing_inode_security(sb->s_root);
2882026eb167SEric Paris 
2883026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2884026eb167SEric Paris 				goto out_bad_option;
2885026eb167SEric Paris 			break;
2886026eb167SEric Paris 		}
2887026eb167SEric Paris 		case DEFCONTEXT_MNT:
2888026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2889026eb167SEric Paris 				goto out_bad_option;
2890026eb167SEric Paris 			break;
2891026eb167SEric Paris 		default:
2892026eb167SEric Paris 			goto out_free_opts;
2893026eb167SEric Paris 		}
2894026eb167SEric Paris 	}
2895026eb167SEric Paris 
2896026eb167SEric Paris 	rc = 0;
2897026eb167SEric Paris out_free_opts:
2898026eb167SEric Paris 	security_free_mnt_opts(&opts);
2899026eb167SEric Paris out_free_secdata:
2900026eb167SEric Paris 	free_secdata(secdata);
2901026eb167SEric Paris 	return rc;
2902026eb167SEric Paris out_bad_option:
2903026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
290429b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
290529b1deb2SLinus Torvalds 	       sb->s_type->name);
2906026eb167SEric Paris 	goto out_free_opts;
2907026eb167SEric Paris }
2908026eb167SEric Paris 
290912204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
29101da177e4SLinus Torvalds {
291188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29122bf49690SThomas Liu 	struct common_audit_data ad;
29131da177e4SLinus Torvalds 	int rc;
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
29161da177e4SLinus Torvalds 	if (rc)
29171da177e4SLinus Torvalds 		return rc;
29181da177e4SLinus Torvalds 
291974192246SJames Morris 	/* Allow all mounts performed by the kernel */
292074192246SJames Morris 	if (flags & MS_KERNMOUNT)
292174192246SJames Morris 		return 0;
292274192246SJames Morris 
292350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2924a269434dSEric Paris 	ad.u.dentry = sb->s_root;
292588e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds 
2928726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
29291da177e4SLinus Torvalds {
293088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29312bf49690SThomas Liu 	struct common_audit_data ad;
29321da177e4SLinus Torvalds 
293350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2934a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
293588e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
29361da177e4SLinus Torvalds }
29371da177e4SLinus Torvalds 
2938808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
29398a04c43bSAl Viro 			 const struct path *path,
2940808d4e3cSAl Viro 			 const char *type,
29411da177e4SLinus Torvalds 			 unsigned long flags,
29421da177e4SLinus Torvalds 			 void *data)
29431da177e4SLinus Torvalds {
294488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2947d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
29481da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
29491da177e4SLinus Torvalds 	else
29502875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
29541da177e4SLinus Torvalds {
295588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29561da177e4SLinus Torvalds 
295788e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
29581da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
29591da177e4SLinus Torvalds }
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds /* inode security operations */
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
29641da177e4SLinus Torvalds {
29651da177e4SLinus Torvalds 	return inode_alloc_security(inode);
29661da177e4SLinus Torvalds }
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
29691da177e4SLinus Torvalds {
29701da177e4SLinus Torvalds 	inode_free_security(inode);
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
2973d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
29744f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2975d47be3dfSDavid Quigley 					u32 *ctxlen)
2976d47be3dfSDavid Quigley {
2977d47be3dfSDavid Quigley 	u32 newsid;
2978d47be3dfSDavid Quigley 	int rc;
2979d47be3dfSDavid Quigley 
2980c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2981c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2982d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2983d47be3dfSDavid Quigley 					   &newsid);
2984c3c188b2SDavid Howells 	if (rc)
2985d47be3dfSDavid Quigley 		return rc;
2986d47be3dfSDavid Quigley 
2987aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2988aa8e712cSStephen Smalley 				       ctxlen);
2989d47be3dfSDavid Quigley }
2990d47be3dfSDavid Quigley 
2991a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2992a518b0a5SVivek Goyal 					  struct qstr *name,
2993a518b0a5SVivek Goyal 					  const struct cred *old,
2994a518b0a5SVivek Goyal 					  struct cred *new)
2995a518b0a5SVivek Goyal {
2996a518b0a5SVivek Goyal 	u32 newsid;
2997a518b0a5SVivek Goyal 	int rc;
2998a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2999a518b0a5SVivek Goyal 
3000a518b0a5SVivek Goyal 	rc = selinux_determine_inode_label(old->security,
3001a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
3002a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
3003a518b0a5SVivek Goyal 					   &newsid);
3004a518b0a5SVivek Goyal 	if (rc)
3005a518b0a5SVivek Goyal 		return rc;
3006a518b0a5SVivek Goyal 
3007a518b0a5SVivek Goyal 	tsec = new->security;
3008a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
3009a518b0a5SVivek Goyal 	return 0;
3010a518b0a5SVivek Goyal }
3011a518b0a5SVivek Goyal 
30125e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
30139548906bSTetsuo Handa 				       const struct qstr *qstr,
30149548906bSTetsuo Handa 				       const char **name,
30152a7dba39SEric Paris 				       void **value, size_t *len)
30165e41ff9eSStephen Smalley {
30175fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
30185e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
3019c0d4f464SCorentin LABBE 	u32 newsid, clen;
30205e41ff9eSStephen Smalley 	int rc;
30219548906bSTetsuo Handa 	char *context;
30225e41ff9eSStephen Smalley 
30235e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
30245e41ff9eSStephen Smalley 
30255e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
3026275bb41eSDavid Howells 
3027c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
3028c3c188b2SDavid Howells 		dir, qstr,
30295e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
3030c3c188b2SDavid Howells 		&newsid);
3031c3c188b2SDavid Howells 	if (rc)
30325e41ff9eSStephen Smalley 		return rc;
30335e41ff9eSStephen Smalley 
3034296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
30350d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
3036296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
3037296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
3038296fddf7SEric Paris 		isec->sid = newsid;
30396f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
3040296fddf7SEric Paris 	}
30415e41ff9eSStephen Smalley 
3042aa8e712cSStephen Smalley 	if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
304325a74f3bSStephen Smalley 		return -EOPNOTSUPP;
304425a74f3bSStephen Smalley 
30459548906bSTetsuo Handa 	if (name)
30469548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
30475e41ff9eSStephen Smalley 
3048570bc1c2SStephen Smalley 	if (value && len) {
3049aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
3050aa8e712cSStephen Smalley 						   &context, &clen);
30519548906bSTetsuo Handa 		if (rc)
30525e41ff9eSStephen Smalley 			return rc;
30535e41ff9eSStephen Smalley 		*value = context;
3054570bc1c2SStephen Smalley 		*len = clen;
3055570bc1c2SStephen Smalley 	}
30565e41ff9eSStephen Smalley 
30575e41ff9eSStephen Smalley 	return 0;
30585e41ff9eSStephen Smalley }
30595e41ff9eSStephen Smalley 
30604acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
30611da177e4SLinus Torvalds {
30621da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
30631da177e4SLinus Torvalds }
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30661da177e4SLinus Torvalds {
30671da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
30681da177e4SLinus Torvalds }
30691da177e4SLinus Torvalds 
30701da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
30711da177e4SLinus Torvalds {
30721da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
30731da177e4SLinus Torvalds }
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
30761da177e4SLinus Torvalds {
30771da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
308018bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
30811da177e4SLinus Torvalds {
30821da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
30831da177e4SLinus Torvalds }
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
30881da177e4SLinus Torvalds }
30891da177e4SLinus Torvalds 
30901a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
30911da177e4SLinus Torvalds {
30921da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
30931da177e4SLinus Torvalds }
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
30961da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
30971da177e4SLinus Torvalds {
30981da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
30991da177e4SLinus Torvalds }
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
31021da177e4SLinus Torvalds {
310388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
310488e67f3bSDavid Howells 
31052875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
3108bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3109bda0be7aSNeilBrown 				     bool rcu)
31101da177e4SLinus Torvalds {
311188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3112bda0be7aSNeilBrown 	struct common_audit_data ad;
3113bda0be7aSNeilBrown 	struct inode_security_struct *isec;
3114bda0be7aSNeilBrown 	u32 sid;
31151da177e4SLinus Torvalds 
3116bda0be7aSNeilBrown 	validate_creds(cred);
3117bda0be7aSNeilBrown 
3118bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
3119bda0be7aSNeilBrown 	ad.u.dentry = dentry;
3120bda0be7aSNeilBrown 	sid = cred_sid(cred);
31215d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
31225d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31235d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
3124bda0be7aSNeilBrown 
31256b6bc620SStephen Smalley 	return avc_has_perm_flags(&selinux_state,
31266b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
3127bda0be7aSNeilBrown 				  rcu ? MAY_NOT_BLOCK : 0);
31281da177e4SLinus Torvalds }
31291da177e4SLinus Torvalds 
3130d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
3131d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
3132626b9740SStephen Smalley 					   int result,
3133d4cf970dSEric Paris 					   unsigned flags)
3134d4cf970dSEric Paris {
3135d4cf970dSEric Paris 	struct common_audit_data ad;
3136d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
3137d4cf970dSEric Paris 	int rc;
3138d4cf970dSEric Paris 
313950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
3140d4cf970dSEric Paris 	ad.u.inode = inode;
3141d4cf970dSEric Paris 
31426b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
31436b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
3144626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
3145d4cf970dSEric Paris 	if (rc)
3146d4cf970dSEric Paris 		return rc;
3147d4cf970dSEric Paris 	return 0;
3148d4cf970dSEric Paris }
3149d4cf970dSEric Paris 
3150e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
31511da177e4SLinus Torvalds {
315288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3153b782e0a6SEric Paris 	u32 perms;
3154b782e0a6SEric Paris 	bool from_access;
3155cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
31562e334057SEric Paris 	struct inode_security_struct *isec;
31572e334057SEric Paris 	u32 sid;
31582e334057SEric Paris 	struct av_decision avd;
31592e334057SEric Paris 	int rc, rc2;
31602e334057SEric Paris 	u32 audited, denied;
31611da177e4SLinus Torvalds 
3162b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3163d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3164d09ca739SEric Paris 
31651da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3166b782e0a6SEric Paris 	if (!mask)
31671da177e4SLinus Torvalds 		return 0;
31681da177e4SLinus Torvalds 
31692e334057SEric Paris 	validate_creds(cred);
3170b782e0a6SEric Paris 
31712e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
31722e334057SEric Paris 		return 0;
3173b782e0a6SEric Paris 
3174b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3175b782e0a6SEric Paris 
31762e334057SEric Paris 	sid = cred_sid(cred);
31775d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
31785d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31795d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
31802e334057SEric Paris 
31816b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
31826b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, perms, 0, &avd);
31832e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
31842e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
31852e334057SEric Paris 				     &denied);
31862e334057SEric Paris 	if (likely(!audited))
31872e334057SEric Paris 		return rc;
31882e334057SEric Paris 
3189626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
31902e334057SEric Paris 	if (rc2)
31912e334057SEric Paris 		return rc2;
31922e334057SEric Paris 	return rc;
31931da177e4SLinus Torvalds }
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
31961da177e4SLinus Torvalds {
319788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3198ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3199bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
320095dbf739SEric Paris 	__u32 av = FILE__WRITE;
32011da177e4SLinus Torvalds 
3202bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3203bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3204bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3205bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3206bc6a6008SAmerigo Wang 		if (!ia_valid)
32071da177e4SLinus Torvalds 			return 0;
3208bc6a6008SAmerigo Wang 	}
32091da177e4SLinus Torvalds 
3210bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3211bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
32122875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
32131da177e4SLinus Torvalds 
3214aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3215ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3216ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3217ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
321895dbf739SEric Paris 		av |= FILE__OPEN;
321995dbf739SEric Paris 
322095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds 
32233f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
32241da177e4SLinus Torvalds {
32253f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
3228db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3229db59000aSStephen Smalley {
3230db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3231db59000aSStephen Smalley 	int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3232db59000aSStephen Smalley 
3233db59000aSStephen Smalley 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3234db59000aSStephen Smalley 		return false;
3235db59000aSStephen Smalley 	if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3236db59000aSStephen Smalley 		return false;
3237db59000aSStephen Smalley 	return true;
3238db59000aSStephen Smalley }
3239db59000aSStephen Smalley 
32408f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
32418f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
32421da177e4SLinus Torvalds {
3243c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
324420cdef8dSPaul Moore 	struct inode_security_struct *isec;
32451da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
32462bf49690SThomas Liu 	struct common_audit_data ad;
3247275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
32481da177e4SLinus Torvalds 	int rc = 0;
32491da177e4SLinus Torvalds 
32506b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32516b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
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 	sbsec = inode->i_sb->s_security;
326112f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
32621da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32631da177e4SLinus Torvalds 
32642e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
32651da177e4SLinus Torvalds 		return -EPERM;
32661da177e4SLinus Torvalds 
326750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3268a269434dSEric Paris 	ad.u.dentry = dentry;
32691da177e4SLinus Torvalds 
327020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
32716b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32726b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
32731da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
32741da177e4SLinus Torvalds 	if (rc)
32751da177e4SLinus Torvalds 		return rc;
32761da177e4SLinus Torvalds 
3277aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3278aa8e712cSStephen Smalley 				     GFP_KERNEL);
327912b29f34SStephen Smalley 	if (rc == -EINVAL) {
3280db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3281d6ea83ecSEric Paris 			struct audit_buffer *ab;
3282d6ea83ecSEric Paris 			size_t audit_size;
3283d6ea83ecSEric Paris 
3284d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3285d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3286e3fea3f7SAl Viro 			if (value) {
3287add24372SColin Ian King 				const char *str = value;
3288add24372SColin Ian King 
3289d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3290d6ea83ecSEric Paris 					audit_size = size - 1;
3291d6ea83ecSEric Paris 				else
3292d6ea83ecSEric Paris 					audit_size = size;
3293e3fea3f7SAl Viro 			} else {
3294e3fea3f7SAl Viro 				audit_size = 0;
3295e3fea3f7SAl Viro 			}
3296d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
3297d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3298d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3299d6ea83ecSEric Paris 			audit_log_end(ab);
3300d6ea83ecSEric Paris 
330112b29f34SStephen Smalley 			return rc;
3302d6ea83ecSEric Paris 		}
3303aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3304aa8e712cSStephen Smalley 						   size, &newsid);
330512b29f34SStephen Smalley 	}
33061da177e4SLinus Torvalds 	if (rc)
33071da177e4SLinus Torvalds 		return rc;
33081da177e4SLinus Torvalds 
33096b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
33106b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
33111da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
33121da177e4SLinus Torvalds 	if (rc)
33131da177e4SLinus Torvalds 		return rc;
33141da177e4SLinus Torvalds 
3315aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3316aa8e712cSStephen Smalley 					  sid, isec->sclass);
33171da177e4SLinus Torvalds 	if (rc)
33181da177e4SLinus Torvalds 		return rc;
33191da177e4SLinus Torvalds 
33206b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
33216b6bc620SStephen Smalley 			    newsid,
33221da177e4SLinus Torvalds 			    sbsec->sid,
33231da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
33241da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
33251da177e4SLinus Torvalds 			    &ad);
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
33288f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
33298f0cfa52SDavid Howells 					const void *value, size_t size,
33308f0cfa52SDavid Howells 					int flags)
33311da177e4SLinus Torvalds {
3332c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
333320cdef8dSPaul Moore 	struct inode_security_struct *isec;
33341da177e4SLinus Torvalds 	u32 newsid;
33351da177e4SLinus Torvalds 	int rc;
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33381da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
33391da177e4SLinus Torvalds 		return;
33401da177e4SLinus Torvalds 	}
33411da177e4SLinus Torvalds 
3342aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3343aa8e712cSStephen Smalley 					   &newsid);
33441da177e4SLinus Torvalds 	if (rc) {
334512b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
334612b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
334712b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
33481da177e4SLinus Torvalds 		return;
33491da177e4SLinus Torvalds 	}
33501da177e4SLinus Torvalds 
335120cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
33529287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3353aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
33541da177e4SLinus Torvalds 	isec->sid = newsid;
33556f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
33569287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3357aa9c2669SDavid Quigley 
33581da177e4SLinus Torvalds 	return;
33591da177e4SLinus Torvalds }
33601da177e4SLinus Torvalds 
33618f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
33621da177e4SLinus Torvalds {
336388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
336488e67f3bSDavid Howells 
33652875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33661da177e4SLinus Torvalds }
33671da177e4SLinus Torvalds 
33681da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
33691da177e4SLinus Torvalds {
337088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
337188e67f3bSDavid Howells 
33722875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33731da177e4SLinus Torvalds }
33741da177e4SLinus Torvalds 
33758f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
33761da177e4SLinus Torvalds {
33776b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33786b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
33796b240306SEric W. Biederman 		if (rc)
33806b240306SEric W. Biederman 			return rc;
33816b240306SEric W. Biederman 
33826b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
33836b240306SEric W. Biederman 		   ordinary setattr permission. */
33846b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
33856b240306SEric W. Biederman 	}
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
33881da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
33891da177e4SLinus Torvalds 	return -EACCES;
33901da177e4SLinus Torvalds }
33911da177e4SLinus Torvalds 
3392d381d8a9SJames Morris /*
3393abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3394d381d8a9SJames Morris  *
3395d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3396d381d8a9SJames Morris  */
3397ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
33981da177e4SLinus Torvalds {
339942492594SDavid P. Quigley 	u32 size;
340042492594SDavid P. Quigley 	int error;
340142492594SDavid P. Quigley 	char *context = NULL;
340220cdef8dSPaul Moore 	struct inode_security_struct *isec;
34031da177e4SLinus Torvalds 
34048c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34058c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
34061da177e4SLinus Torvalds 
3407abc69bb6SStephen Smalley 	/*
3408abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3409abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3410abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3411abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3412abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3413abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3414abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3415abc69bb6SStephen Smalley 	 */
341620cdef8dSPaul Moore 	isec = inode_security(inode);
3417db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3418aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3419aa8e712cSStephen Smalley 						      isec->sid, &context,
3420abc69bb6SStephen Smalley 						      &size);
3421abc69bb6SStephen Smalley 	else
3422aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3423aa8e712cSStephen Smalley 						&context, &size);
342442492594SDavid P. Quigley 	if (error)
342542492594SDavid P. Quigley 		return error;
342642492594SDavid P. Quigley 	error = size;
342742492594SDavid P. Quigley 	if (alloc) {
342842492594SDavid P. Quigley 		*buffer = context;
342942492594SDavid P. Quigley 		goto out_nofree;
343042492594SDavid P. Quigley 	}
343142492594SDavid P. Quigley 	kfree(context);
343242492594SDavid P. Quigley out_nofree:
343342492594SDavid P. Quigley 	return error;
34341da177e4SLinus Torvalds }
34351da177e4SLinus Torvalds 
34361da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
34371da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
34381da177e4SLinus Torvalds {
34392c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
34401da177e4SLinus Torvalds 	u32 newsid;
34411da177e4SLinus Torvalds 	int rc;
34421da177e4SLinus Torvalds 
34431da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34441da177e4SLinus Torvalds 		return -EOPNOTSUPP;
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds 	if (!value || !size)
34471da177e4SLinus Torvalds 		return -EACCES;
34481da177e4SLinus Torvalds 
3449aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3450aa8e712cSStephen Smalley 				     GFP_KERNEL);
34511da177e4SLinus Torvalds 	if (rc)
34521da177e4SLinus Torvalds 		return rc;
34531da177e4SLinus Torvalds 
34549287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3455aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
34561da177e4SLinus Torvalds 	isec->sid = newsid;
34576f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
34589287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
34591da177e4SLinus Torvalds 	return 0;
34601da177e4SLinus Torvalds }
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
34631da177e4SLinus Torvalds {
34641da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
34651da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
34661da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
34671da177e4SLinus Torvalds 	return len;
34681da177e4SLinus Torvalds }
34691da177e4SLinus Torvalds 
3470d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3471713a04aeSAhmed S. Darwish {
3472e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3473713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3474713a04aeSAhmed S. Darwish }
3475713a04aeSAhmed S. Darwish 
347656909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
347756909eb3SVivek Goyal {
347856909eb3SVivek Goyal 	u32 sid;
347956909eb3SVivek Goyal 	struct task_security_struct *tsec;
348056909eb3SVivek Goyal 	struct cred *new_creds = *new;
348156909eb3SVivek Goyal 
348256909eb3SVivek Goyal 	if (new_creds == NULL) {
348356909eb3SVivek Goyal 		new_creds = prepare_creds();
348456909eb3SVivek Goyal 		if (!new_creds)
348556909eb3SVivek Goyal 			return -ENOMEM;
348656909eb3SVivek Goyal 	}
348756909eb3SVivek Goyal 
348856909eb3SVivek Goyal 	tsec = new_creds->security;
348956909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
349056909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
349156909eb3SVivek Goyal 	tsec->create_sid = sid;
349256909eb3SVivek Goyal 	*new = new_creds;
349356909eb3SVivek Goyal 	return 0;
349456909eb3SVivek Goyal }
349556909eb3SVivek Goyal 
349619472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
349719472b69SVivek Goyal {
349819472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
349919472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
350019472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
350119472b69SVivek Goyal 	 */
350219472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
350319472b69SVivek Goyal 		return 1; /* Discard */
350419472b69SVivek Goyal 	/*
350519472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
350619472b69SVivek Goyal 	 * by selinux.
350719472b69SVivek Goyal 	 */
350819472b69SVivek Goyal 	return -EOPNOTSUPP;
350919472b69SVivek Goyal }
351019472b69SVivek Goyal 
35111da177e4SLinus Torvalds /* file security operations */
35121da177e4SLinus Torvalds 
3513788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
35141da177e4SLinus Torvalds {
351588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3516496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
35191da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
35201da177e4SLinus Torvalds 		mask |= MAY_APPEND;
35211da177e4SLinus Torvalds 
3522389fb800SPaul Moore 	return file_has_perm(cred, file,
35231da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
35241da177e4SLinus Torvalds }
35251da177e4SLinus Torvalds 
3526788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3527788e7dd4SYuichi Nakamura {
3528496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
352920dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
3530b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
353120dda18bSStephen Smalley 	u32 sid = current_sid();
353220dda18bSStephen Smalley 
3533389fb800SPaul Moore 	if (!mask)
3534788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3535788e7dd4SYuichi Nakamura 		return 0;
3536788e7dd4SYuichi Nakamura 
3537b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
353820dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
35396b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
354083d49856SEric Paris 		/* No change since file_open check. */
354120dda18bSStephen Smalley 		return 0;
354220dda18bSStephen Smalley 
3543788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3544788e7dd4SYuichi Nakamura }
3545788e7dd4SYuichi Nakamura 
35461da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
35471da177e4SLinus Torvalds {
35481da177e4SLinus Torvalds 	return file_alloc_security(file);
35491da177e4SLinus Torvalds }
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
35521da177e4SLinus Torvalds {
35531da177e4SLinus Torvalds 	file_free_security(file);
35541da177e4SLinus Torvalds }
35551da177e4SLinus Torvalds 
3556fa1aa143SJeff Vander Stoep /*
3557fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3558fa1aa143SJeff Vander Stoep  * operation to an inode.
3559fa1aa143SJeff Vander Stoep  */
35601d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3561fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3562fa1aa143SJeff Vander Stoep {
3563fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3564fa1aa143SJeff Vander Stoep 	struct file_security_struct *fsec = file->f_security;
3565fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
356620cdef8dSPaul Moore 	struct inode_security_struct *isec;
3567fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3568fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3569fa1aa143SJeff Vander Stoep 	int rc;
3570fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3571fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3572fa1aa143SJeff Vander Stoep 
3573fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3574fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3575fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3576fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3577fa1aa143SJeff Vander Stoep 
3578fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
35796b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
35806b6bc620SStephen Smalley 				  ssid, fsec->sid,
3581fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3582fa1aa143SJeff Vander Stoep 				FD__USE,
3583fa1aa143SJeff Vander Stoep 				&ad);
3584fa1aa143SJeff Vander Stoep 		if (rc)
3585fa1aa143SJeff Vander Stoep 			goto out;
3586fa1aa143SJeff Vander Stoep 	}
3587fa1aa143SJeff Vander Stoep 
3588fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3589fa1aa143SJeff Vander Stoep 		return 0;
3590fa1aa143SJeff Vander Stoep 
359120cdef8dSPaul Moore 	isec = inode_security(inode);
35926b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
35936b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3594fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3595fa1aa143SJeff Vander Stoep out:
3596fa1aa143SJeff Vander Stoep 	return rc;
3597fa1aa143SJeff Vander Stoep }
3598fa1aa143SJeff Vander Stoep 
35991da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
36001da177e4SLinus Torvalds 			      unsigned long arg)
36011da177e4SLinus Torvalds {
360288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
36030b24dcb7SEric Paris 	int error = 0;
36041da177e4SLinus Torvalds 
36050b24dcb7SEric Paris 	switch (cmd) {
36060b24dcb7SEric Paris 	case FIONREAD:
36070b24dcb7SEric Paris 	/* fall through */
36080b24dcb7SEric Paris 	case FIBMAP:
36090b24dcb7SEric Paris 	/* fall through */
36100b24dcb7SEric Paris 	case FIGETBSZ:
36110b24dcb7SEric Paris 	/* fall through */
36122f99c369SAl Viro 	case FS_IOC_GETFLAGS:
36130b24dcb7SEric Paris 	/* fall through */
36142f99c369SAl Viro 	case FS_IOC_GETVERSION:
36150b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
36160b24dcb7SEric Paris 		break;
36171da177e4SLinus Torvalds 
36182f99c369SAl Viro 	case FS_IOC_SETFLAGS:
36190b24dcb7SEric Paris 	/* fall through */
36202f99c369SAl Viro 	case FS_IOC_SETVERSION:
36210b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
36220b24dcb7SEric Paris 		break;
36230b24dcb7SEric Paris 
36240b24dcb7SEric Paris 	/* sys_ioctl() checks */
36250b24dcb7SEric Paris 	case FIONBIO:
36260b24dcb7SEric Paris 	/* fall through */
36270b24dcb7SEric Paris 	case FIOASYNC:
36280b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
36290b24dcb7SEric Paris 		break;
36300b24dcb7SEric Paris 
36310b24dcb7SEric Paris 	case KDSKBENT:
36320b24dcb7SEric Paris 	case KDSKBSENT:
36336a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
36348e4ff6f2SStephen Smalley 					    SECURITY_CAP_AUDIT, true);
36350b24dcb7SEric Paris 		break;
36360b24dcb7SEric Paris 
36370b24dcb7SEric Paris 	/* default case assumes that the command will go
36380b24dcb7SEric Paris 	 * to the file's ioctl() function.
36390b24dcb7SEric Paris 	 */
36400b24dcb7SEric Paris 	default:
3641fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
36420b24dcb7SEric Paris 	}
36430b24dcb7SEric Paris 	return error;
36441da177e4SLinus Torvalds }
36451da177e4SLinus Torvalds 
3646fcaaade1SStephen Smalley static int default_noexec;
3647fcaaade1SStephen Smalley 
36481da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
36491da177e4SLinus Torvalds {
365088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3651be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3652d84f4f99SDavid Howells 	int rc = 0;
365388e67f3bSDavid Howells 
3654fcaaade1SStephen Smalley 	if (default_noexec &&
3655892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3656892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
36571da177e4SLinus Torvalds 		/*
36581da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
36591da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
36601da177e4SLinus Torvalds 		 * This has an additional check.
36611da177e4SLinus Torvalds 		 */
36626b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36636b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3664be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
36651da177e4SLinus Torvalds 		if (rc)
3666d84f4f99SDavid Howells 			goto error;
36671da177e4SLinus Torvalds 	}
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	if (file) {
36701da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
36711da177e4SLinus Torvalds 		u32 av = FILE__READ;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
36741da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
36751da177e4SLinus Torvalds 			av |= FILE__WRITE;
36761da177e4SLinus Torvalds 
36771da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
36781da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
36791da177e4SLinus Torvalds 
368088e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
36811da177e4SLinus Torvalds 	}
3682d84f4f99SDavid Howells 
3683d84f4f99SDavid Howells error:
3684d84f4f99SDavid Howells 	return rc;
36851da177e4SLinus Torvalds }
36861da177e4SLinus Torvalds 
3687e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
36881da177e4SLinus Torvalds {
3689b1d9e6b0SCasey Schaufler 	int rc = 0;
369098883bfdSPaul Moore 
369198883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
369298883bfdSPaul Moore 		u32 sid = current_sid();
36936b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36946b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
369598883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
369698883bfdSPaul Moore 	}
369798883bfdSPaul Moore 
369898883bfdSPaul Moore 	return rc;
3699e5467859SAl Viro }
37001da177e4SLinus Torvalds 
3701e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3702e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3703e5467859SAl Viro {
37043ba4bf5fSStephen Smalley 	struct common_audit_data ad;
37053ba4bf5fSStephen Smalley 	int rc;
37063ba4bf5fSStephen Smalley 
37073ba4bf5fSStephen Smalley 	if (file) {
37083ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
37093ba4bf5fSStephen Smalley 		ad.u.file = file;
37103ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
37113ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
37123ba4bf5fSStephen Smalley 		if (rc)
37133ba4bf5fSStephen Smalley 			return rc;
37143ba4bf5fSStephen Smalley 	}
37153ba4bf5fSStephen Smalley 
3716aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37171da177e4SLinus Torvalds 		prot = reqprot;
37181da177e4SLinus Torvalds 
37191da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
37201da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
37211da177e4SLinus Torvalds }
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
37241da177e4SLinus Torvalds 				 unsigned long reqprot,
37251da177e4SLinus Torvalds 				 unsigned long prot)
37261da177e4SLinus Torvalds {
372788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3728be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
37291da177e4SLinus Torvalds 
3730aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37311da177e4SLinus Torvalds 		prot = reqprot;
37321da177e4SLinus Torvalds 
3733fcaaade1SStephen Smalley 	if (default_noexec &&
3734fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3735d541bbeeSJames Morris 		int rc = 0;
3736db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3737db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
37386b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37396b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3740be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3741db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3742c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3743c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3744d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
37456b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37466b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3747be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3748db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3749db4c9641SStephen Smalley 			/*
3750db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3751db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3752db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3753db4c9641SStephen Smalley 			 * modified content.  This typically should only
3754db4c9641SStephen Smalley 			 * occur for text relocations.
3755db4c9641SStephen Smalley 			 */
3756d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3757db4c9641SStephen Smalley 		}
37586b992197SLorenzo Hernandez García-Hierro 		if (rc)
37596b992197SLorenzo Hernandez García-Hierro 			return rc;
37606b992197SLorenzo Hernandez García-Hierro 	}
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
37631da177e4SLinus Torvalds }
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
37661da177e4SLinus Torvalds {
376788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
376888e67f3bSDavid Howells 
376988e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
37721da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
37731da177e4SLinus Torvalds 			      unsigned long arg)
37741da177e4SLinus Torvalds {
377588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
37761da177e4SLinus Torvalds 	int err = 0;
37771da177e4SLinus Torvalds 
37781da177e4SLinus Torvalds 	switch (cmd) {
37791da177e4SLinus Torvalds 	case F_SETFL:
37801da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
378188e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
37821da177e4SLinus Torvalds 			break;
37831da177e4SLinus Torvalds 		}
37841da177e4SLinus Torvalds 		/* fall through */
37851da177e4SLinus Torvalds 	case F_SETOWN:
37861da177e4SLinus Torvalds 	case F_SETSIG:
37871da177e4SLinus Torvalds 	case F_GETFL:
37881da177e4SLinus Torvalds 	case F_GETOWN:
37891da177e4SLinus Torvalds 	case F_GETSIG:
37901d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
37911da177e4SLinus Torvalds 		/* Just check FD__USE permission */
379288e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
37931da177e4SLinus Torvalds 		break;
37941da177e4SLinus Torvalds 	case F_GETLK:
37951da177e4SLinus Torvalds 	case F_SETLK:
37961da177e4SLinus Torvalds 	case F_SETLKW:
37970d3f7a2dSJeff Layton 	case F_OFD_GETLK:
37980d3f7a2dSJeff Layton 	case F_OFD_SETLK:
37990d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
38001da177e4SLinus Torvalds #if BITS_PER_LONG == 32
38011da177e4SLinus Torvalds 	case F_GETLK64:
38021da177e4SLinus Torvalds 	case F_SETLK64:
38031da177e4SLinus Torvalds 	case F_SETLKW64:
38041da177e4SLinus Torvalds #endif
380588e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
38061da177e4SLinus Torvalds 		break;
38071da177e4SLinus Torvalds 	}
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 	return err;
38101da177e4SLinus Torvalds }
38111da177e4SLinus Torvalds 
3812e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
38131da177e4SLinus Torvalds {
38141da177e4SLinus Torvalds 	struct file_security_struct *fsec;
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds 	fsec = file->f_security;
3817275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
38181da177e4SLinus Torvalds }
38191da177e4SLinus Torvalds 
38201da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
38211da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
38221da177e4SLinus Torvalds {
38231da177e4SLinus Torvalds 	struct file *file;
382465c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
38251da177e4SLinus Torvalds 	u32 perm;
38261da177e4SLinus Torvalds 	struct file_security_struct *fsec;
38271da177e4SLinus Torvalds 
38281da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3829b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 	fsec = file->f_security;
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	if (!signum)
38341da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
38351da177e4SLinus Torvalds 	else
38361da177e4SLinus Torvalds 		perm = signal_to_av(signum);
38371da177e4SLinus Torvalds 
38386b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38396b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
38401da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
38411da177e4SLinus Torvalds }
38421da177e4SLinus Torvalds 
38431da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
38441da177e4SLinus Torvalds {
384588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
384688e67f3bSDavid Howells 
384788e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
38481da177e4SLinus Torvalds }
38491da177e4SLinus Torvalds 
385083d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3851788e7dd4SYuichi Nakamura {
3852788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3853788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3854d84f4f99SDavid Howells 
3855788e7dd4SYuichi Nakamura 	fsec = file->f_security;
385683da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3857788e7dd4SYuichi Nakamura 	/*
3858788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3859788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3860788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3861788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3862788e7dd4SYuichi Nakamura 	 * struct as its SID.
3863788e7dd4SYuichi Nakamura 	 */
3864788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
38656b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3866788e7dd4SYuichi Nakamura 	/*
3867788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3868788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3869788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3870788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3871788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3872788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3873788e7dd4SYuichi Nakamura 	 */
387413f8e981SDavid Howells 	return file_path_has_perm(cred, file, open_file_to_av(file));
3875788e7dd4SYuichi Nakamura }
3876788e7dd4SYuichi Nakamura 
38771da177e4SLinus Torvalds /* task security operations */
38781da177e4SLinus Torvalds 
3879a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3880a79be238STetsuo Handa 			      unsigned long clone_flags)
38811da177e4SLinus Torvalds {
3882be0554c9SStephen Smalley 	u32 sid = current_sid();
3883be0554c9SStephen Smalley 
38846b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38856b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
38861da177e4SLinus Torvalds }
38871da177e4SLinus Torvalds 
3888f1752eecSDavid Howells /*
3889ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3890ee18d64cSDavid Howells  */
3891ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3892ee18d64cSDavid Howells {
3893ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3894ee18d64cSDavid Howells 
3895ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3896ee18d64cSDavid Howells 	if (!tsec)
3897ee18d64cSDavid Howells 		return -ENOMEM;
3898ee18d64cSDavid Howells 
3899ee18d64cSDavid Howells 	cred->security = tsec;
3900ee18d64cSDavid Howells 	return 0;
3901ee18d64cSDavid Howells }
3902ee18d64cSDavid Howells 
3903ee18d64cSDavid Howells /*
3904f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3905f1752eecSDavid Howells  */
3906f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
39071da177e4SLinus Torvalds {
3908f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3909e0e81739SDavid Howells 
39102edeaa34STetsuo Handa 	/*
39112edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
39122edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
39132edeaa34STetsuo Handa 	 */
39142edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3915e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3916f1752eecSDavid Howells 	kfree(tsec);
39171da177e4SLinus Torvalds }
39181da177e4SLinus Torvalds 
3919d84f4f99SDavid Howells /*
3920d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3921d84f4f99SDavid Howells  */
3922d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3923d84f4f99SDavid Howells 				gfp_t gfp)
3924d84f4f99SDavid Howells {
3925d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3926d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3927d84f4f99SDavid Howells 
3928d84f4f99SDavid Howells 	old_tsec = old->security;
3929d84f4f99SDavid Howells 
3930d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3931d84f4f99SDavid Howells 	if (!tsec)
3932d84f4f99SDavid Howells 		return -ENOMEM;
3933d84f4f99SDavid Howells 
3934d84f4f99SDavid Howells 	new->security = tsec;
3935d84f4f99SDavid Howells 	return 0;
3936d84f4f99SDavid Howells }
3937d84f4f99SDavid Howells 
3938d84f4f99SDavid Howells /*
3939ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3940ee18d64cSDavid Howells  */
3941ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3942ee18d64cSDavid Howells {
3943ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3944ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3945ee18d64cSDavid Howells 
3946ee18d64cSDavid Howells 	*tsec = *old_tsec;
3947ee18d64cSDavid Howells }
3948ee18d64cSDavid Howells 
39493ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
39503ec30113SMatthew Garrett {
39513ec30113SMatthew Garrett 	*secid = cred_sid(c);
39523ec30113SMatthew Garrett }
39533ec30113SMatthew Garrett 
3954ee18d64cSDavid Howells /*
39553a3b7ce9SDavid Howells  * set the security data for a kernel service
39563a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
39573a3b7ce9SDavid Howells  */
39583a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
39593a3b7ce9SDavid Howells {
39603a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
39613a3b7ce9SDavid Howells 	u32 sid = current_sid();
39623a3b7ce9SDavid Howells 	int ret;
39633a3b7ce9SDavid Howells 
39646b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
39656b6bc620SStephen Smalley 			   sid, secid,
39663a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
39673a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
39683a3b7ce9SDavid Howells 			   NULL);
39693a3b7ce9SDavid Howells 	if (ret == 0) {
39703a3b7ce9SDavid Howells 		tsec->sid = secid;
39713a3b7ce9SDavid Howells 		tsec->create_sid = 0;
39723a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
39733a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
39743a3b7ce9SDavid Howells 	}
39753a3b7ce9SDavid Howells 	return ret;
39763a3b7ce9SDavid Howells }
39773a3b7ce9SDavid Howells 
39783a3b7ce9SDavid Howells /*
39793a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
39803a3b7ce9SDavid Howells  * objective context of the specified inode
39813a3b7ce9SDavid Howells  */
39823a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
39833a3b7ce9SDavid Howells {
398483da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
39853a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
39863a3b7ce9SDavid Howells 	u32 sid = current_sid();
39873a3b7ce9SDavid Howells 	int ret;
39883a3b7ce9SDavid Howells 
39896b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
39906b6bc620SStephen Smalley 			   sid, isec->sid,
39913a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
39923a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
39933a3b7ce9SDavid Howells 			   NULL);
39943a3b7ce9SDavid Howells 
39953a3b7ce9SDavid Howells 	if (ret == 0)
39963a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3997ef57471aSDavid Howells 	return ret;
39983a3b7ce9SDavid Howells }
39993a3b7ce9SDavid Howells 
4000dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
400125354c4fSEric Paris {
4002dd8dbf2eSEric Paris 	struct common_audit_data ad;
4003dd8dbf2eSEric Paris 
400450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
4005dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
4006dd8dbf2eSEric Paris 
40076b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40086b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4009dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
401025354c4fSEric Paris }
401125354c4fSEric Paris 
401261d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
401361d612eaSJeff Vander Stoep {
401461d612eaSJeff Vander Stoep 	struct common_audit_data ad;
401561d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
401661d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
401761d612eaSJeff Vander Stoep 	u32 sid = current_sid();
401861d612eaSJeff Vander Stoep 	int rc;
401961d612eaSJeff Vander Stoep 
402061d612eaSJeff Vander Stoep 	/* init_module */
402161d612eaSJeff Vander Stoep 	if (file == NULL)
40226b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
40236b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
402461d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
402561d612eaSJeff Vander Stoep 
402661d612eaSJeff Vander Stoep 	/* finit_module */
402720cdef8dSPaul Moore 
402843af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
402943af5de7SVivek Goyal 	ad.u.file = file;
403061d612eaSJeff Vander Stoep 
403161d612eaSJeff Vander Stoep 	fsec = file->f_security;
403261d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
40336b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
40346b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
403561d612eaSJeff Vander Stoep 		if (rc)
403661d612eaSJeff Vander Stoep 			return rc;
403761d612eaSJeff Vander Stoep 	}
403861d612eaSJeff Vander Stoep 
403920cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
40406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40416b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
404261d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
404361d612eaSJeff Vander Stoep }
404461d612eaSJeff Vander Stoep 
404561d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
404661d612eaSJeff Vander Stoep 				    enum kernel_read_file_id id)
404761d612eaSJeff Vander Stoep {
404861d612eaSJeff Vander Stoep 	int rc = 0;
404961d612eaSJeff Vander Stoep 
405061d612eaSJeff Vander Stoep 	switch (id) {
405161d612eaSJeff Vander Stoep 	case READING_MODULE:
405261d612eaSJeff Vander Stoep 		rc = selinux_kernel_module_from_file(file);
405361d612eaSJeff Vander Stoep 		break;
405461d612eaSJeff Vander Stoep 	default:
405561d612eaSJeff Vander Stoep 		break;
405661d612eaSJeff Vander Stoep 	}
405761d612eaSJeff Vander Stoep 
405861d612eaSJeff Vander Stoep 	return rc;
405961d612eaSJeff Vander Stoep }
406061d612eaSJeff Vander Stoep 
40611da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
40621da177e4SLinus Torvalds {
40636b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40646b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4065be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
40661da177e4SLinus Torvalds }
40671da177e4SLinus Torvalds 
40681da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
40691da177e4SLinus Torvalds {
40706b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40716b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4072be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
40731da177e4SLinus Torvalds }
40741da177e4SLinus Torvalds 
40751da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
40761da177e4SLinus Torvalds {
40776b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40786b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4079be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
40801da177e4SLinus Torvalds }
40811da177e4SLinus Torvalds 
4082f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4083f9008e4cSDavid Quigley {
4084275bb41eSDavid Howells 	*secid = task_sid(p);
4085f9008e4cSDavid Quigley }
4086f9008e4cSDavid Quigley 
40871da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
40881da177e4SLinus Torvalds {
40896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40906b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4091be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
40921da177e4SLinus Torvalds }
40931da177e4SLinus Torvalds 
409403e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
409503e68060SJames Morris {
40966b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40976b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4098be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
409903e68060SJames Morris }
410003e68060SJames Morris 
4101a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
4102a1836a42SDavid Quigley {
41036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41046b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4105be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
4106a1836a42SDavid Quigley }
4107a1836a42SDavid Quigley 
41084298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4109791ec491SStephen Smalley 				unsigned int flags)
4110791ec491SStephen Smalley {
4111791ec491SStephen Smalley 	u32 av = 0;
4112791ec491SStephen Smalley 
411384e6885eSStephen Smalley 	if (!flags)
411484e6885eSStephen Smalley 		return 0;
4115791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
4116791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
4117791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
4118791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
41196b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41206b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
4121791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
4122791ec491SStephen Smalley }
4123791ec491SStephen Smalley 
41248fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
41258fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
41261da177e4SLinus Torvalds {
41278fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
41281da177e4SLinus Torvalds 
41291da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
41301da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
41311da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
4132d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
41331da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
41346b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
41356b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
4136be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds 	return 0;
41391da177e4SLinus Torvalds }
41401da177e4SLinus Torvalds 
4141b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
41421da177e4SLinus Torvalds {
41436b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41446b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4145be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41461da177e4SLinus Torvalds }
41471da177e4SLinus Torvalds 
41481da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
41491da177e4SLinus Torvalds {
41506b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41516b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4152be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
41531da177e4SLinus Torvalds }
41541da177e4SLinus Torvalds 
415535601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
415635601547SDavid Quigley {
41576b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41586b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4159be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
416035601547SDavid Quigley }
416135601547SDavid Quigley 
4162f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
41636b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
41641da177e4SLinus Torvalds {
41656b4f3d01SStephen Smalley 	u32 secid;
41661da177e4SLinus Torvalds 	u32 perm;
41671da177e4SLinus Torvalds 
41681da177e4SLinus Torvalds 	if (!sig)
41691da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
41701da177e4SLinus Torvalds 	else
41711da177e4SLinus Torvalds 		perm = signal_to_av(sig);
41726b4f3d01SStephen Smalley 	if (!cred)
4173be0554c9SStephen Smalley 		secid = current_sid();
41746b4f3d01SStephen Smalley 	else
41756b4f3d01SStephen Smalley 		secid = cred_sid(cred);
41766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41776b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
41781da177e4SLinus Torvalds }
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
41811da177e4SLinus Torvalds 				  struct inode *inode)
41821da177e4SLinus Torvalds {
41831da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
4184275bb41eSDavid Howells 	u32 sid = task_sid(p);
41851da177e4SLinus Torvalds 
41869287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4187db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4188275bb41eSDavid Howells 	isec->sid = sid;
41896f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
41909287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
41911da177e4SLinus Torvalds }
41921da177e4SLinus Torvalds 
41931da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
419467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
41952bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
41961da177e4SLinus Torvalds {
41971da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
41981da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
41991da177e4SLinus Torvalds 
4200bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
42011da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
42021da177e4SLinus Torvalds 	if (ih == NULL)
42031da177e4SLinus Torvalds 		goto out;
42041da177e4SLinus Torvalds 
42051da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
42061da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
42071da177e4SLinus Torvalds 		goto out;
42081da177e4SLinus Torvalds 
420948c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
421048c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
42111da177e4SLinus Torvalds 	ret = 0;
42121da177e4SLinus Torvalds 
421367f83cbfSVenkat Yekkirala 	if (proto)
421467f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
421567f83cbfSVenkat Yekkirala 
42161da177e4SLinus Torvalds 	switch (ih->protocol) {
42171da177e4SLinus Torvalds 	case IPPROTO_TCP: {
42181da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
42211da177e4SLinus Torvalds 			break;
42221da177e4SLinus Torvalds 
42231da177e4SLinus Torvalds 		offset += ihlen;
42241da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
42251da177e4SLinus Torvalds 		if (th == NULL)
42261da177e4SLinus Torvalds 			break;
42271da177e4SLinus Torvalds 
422848c62af6SEric Paris 		ad->u.net->sport = th->source;
422948c62af6SEric Paris 		ad->u.net->dport = th->dest;
42301da177e4SLinus Torvalds 		break;
42311da177e4SLinus Torvalds 	}
42321da177e4SLinus Torvalds 
42331da177e4SLinus Torvalds 	case IPPROTO_UDP: {
42341da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
42371da177e4SLinus Torvalds 			break;
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 		offset += ihlen;
42401da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
42411da177e4SLinus Torvalds 		if (uh == NULL)
42421da177e4SLinus Torvalds 			break;
42431da177e4SLinus Torvalds 
424448c62af6SEric Paris 		ad->u.net->sport = uh->source;
424548c62af6SEric Paris 		ad->u.net->dport = uh->dest;
42461da177e4SLinus Torvalds 		break;
42471da177e4SLinus Torvalds 	}
42481da177e4SLinus Torvalds 
42492ee92d46SJames Morris 	case IPPROTO_DCCP: {
42502ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
42512ee92d46SJames Morris 
42522ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
42532ee92d46SJames Morris 			break;
42542ee92d46SJames Morris 
42552ee92d46SJames Morris 		offset += ihlen;
42562ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
42572ee92d46SJames Morris 		if (dh == NULL)
42582ee92d46SJames Morris 			break;
42592ee92d46SJames Morris 
426048c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
426148c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
42622ee92d46SJames Morris 		break;
42632ee92d46SJames Morris 	}
42642ee92d46SJames Morris 
4265d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4266d452930fSRichard Haines 	case IPPROTO_SCTP: {
4267d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4268d452930fSRichard Haines 
4269d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4270d452930fSRichard Haines 			break;
4271d452930fSRichard Haines 
4272d452930fSRichard Haines 		offset += ihlen;
4273d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4274d452930fSRichard Haines 		if (sh == NULL)
4275d452930fSRichard Haines 			break;
4276d452930fSRichard Haines 
4277d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4278d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4279d452930fSRichard Haines 		break;
4280d452930fSRichard Haines 	}
4281d452930fSRichard Haines #endif
42821da177e4SLinus Torvalds 	default:
42831da177e4SLinus Torvalds 		break;
42841da177e4SLinus Torvalds 	}
42851da177e4SLinus Torvalds out:
42861da177e4SLinus Torvalds 	return ret;
42871da177e4SLinus Torvalds }
42881da177e4SLinus Torvalds 
42891a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
42901da177e4SLinus Torvalds 
42911da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
429267f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
42932bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
42941da177e4SLinus Torvalds {
42951da177e4SLinus Torvalds 	u8 nexthdr;
42961da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
42971da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
429875f2811cSJesse Gross 	__be16 frag_off;
42991da177e4SLinus Torvalds 
4300bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
43011da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
43021da177e4SLinus Torvalds 	if (ip6 == NULL)
43031da177e4SLinus Torvalds 		goto out;
43041da177e4SLinus Torvalds 
430548c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
430648c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
43071da177e4SLinus Torvalds 	ret = 0;
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
43101da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
431175f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
43121da177e4SLinus Torvalds 	if (offset < 0)
43131da177e4SLinus Torvalds 		goto out;
43141da177e4SLinus Torvalds 
431567f83cbfSVenkat Yekkirala 	if (proto)
431667f83cbfSVenkat Yekkirala 		*proto = nexthdr;
431767f83cbfSVenkat Yekkirala 
43181da177e4SLinus Torvalds 	switch (nexthdr) {
43191da177e4SLinus Torvalds 	case IPPROTO_TCP: {
43201da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
43211da177e4SLinus Torvalds 
43221da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
43231da177e4SLinus Torvalds 		if (th == NULL)
43241da177e4SLinus Torvalds 			break;
43251da177e4SLinus Torvalds 
432648c62af6SEric Paris 		ad->u.net->sport = th->source;
432748c62af6SEric Paris 		ad->u.net->dport = th->dest;
43281da177e4SLinus Torvalds 		break;
43291da177e4SLinus Torvalds 	}
43301da177e4SLinus Torvalds 
43311da177e4SLinus Torvalds 	case IPPROTO_UDP: {
43321da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
43331da177e4SLinus Torvalds 
43341da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
43351da177e4SLinus Torvalds 		if (uh == NULL)
43361da177e4SLinus Torvalds 			break;
43371da177e4SLinus Torvalds 
433848c62af6SEric Paris 		ad->u.net->sport = uh->source;
433948c62af6SEric Paris 		ad->u.net->dport = uh->dest;
43401da177e4SLinus Torvalds 		break;
43411da177e4SLinus Torvalds 	}
43421da177e4SLinus Torvalds 
43432ee92d46SJames Morris 	case IPPROTO_DCCP: {
43442ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
43452ee92d46SJames Morris 
43462ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
43472ee92d46SJames Morris 		if (dh == NULL)
43482ee92d46SJames Morris 			break;
43492ee92d46SJames Morris 
435048c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
435148c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
43522ee92d46SJames Morris 		break;
43532ee92d46SJames Morris 	}
43542ee92d46SJames Morris 
4355d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4356d452930fSRichard Haines 	case IPPROTO_SCTP: {
4357d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4358d452930fSRichard Haines 
4359d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4360d452930fSRichard Haines 		if (sh == NULL)
4361d452930fSRichard Haines 			break;
4362d452930fSRichard Haines 
4363d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4364d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4365d452930fSRichard Haines 		break;
4366d452930fSRichard Haines 	}
4367d452930fSRichard Haines #endif
43681da177e4SLinus Torvalds 	/* includes fragments */
43691da177e4SLinus Torvalds 	default:
43701da177e4SLinus Torvalds 		break;
43711da177e4SLinus Torvalds 	}
43721da177e4SLinus Torvalds out:
43731da177e4SLinus Torvalds 	return ret;
43741da177e4SLinus Torvalds }
43751da177e4SLinus Torvalds 
43761da177e4SLinus Torvalds #endif /* IPV6 */
43771da177e4SLinus Torvalds 
43782bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4379cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
43801da177e4SLinus Torvalds {
4381cf9481e2SDavid Howells 	char *addrp;
4382cf9481e2SDavid Howells 	int ret;
43831da177e4SLinus Torvalds 
438448c62af6SEric Paris 	switch (ad->u.net->family) {
43851da177e4SLinus Torvalds 	case PF_INET:
438667f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4387cf9481e2SDavid Howells 		if (ret)
4388cf9481e2SDavid Howells 			goto parse_error;
438948c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
439048c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4391cf9481e2SDavid Howells 		goto okay;
43921da177e4SLinus Torvalds 
43931a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
43941da177e4SLinus Torvalds 	case PF_INET6:
439567f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4396cf9481e2SDavid Howells 		if (ret)
4397cf9481e2SDavid Howells 			goto parse_error;
439848c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
439948c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4400cf9481e2SDavid Howells 		goto okay;
44011da177e4SLinus Torvalds #endif	/* IPV6 */
44021da177e4SLinus Torvalds 	default:
4403cf9481e2SDavid Howells 		addrp = NULL;
4404cf9481e2SDavid Howells 		goto okay;
44051da177e4SLinus Torvalds 	}
44061da177e4SLinus Torvalds 
4407cf9481e2SDavid Howells parse_error:
440871f1cb05SPaul Moore 	printk(KERN_WARNING
440971f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
441071f1cb05SPaul Moore 	       " unable to parse packet\n");
44111da177e4SLinus Torvalds 	return ret;
4412cf9481e2SDavid Howells 
4413cf9481e2SDavid Howells okay:
4414cf9481e2SDavid Howells 	if (_addrp)
4415cf9481e2SDavid Howells 		*_addrp = addrp;
4416cf9481e2SDavid Howells 	return 0;
44171da177e4SLinus Torvalds }
44181da177e4SLinus Torvalds 
44194f6a993fSPaul Moore /**
4420220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
44214f6a993fSPaul Moore  * @skb: the packet
442275e22910SPaul Moore  * @family: protocol family
4423220deb96SPaul Moore  * @sid: the packet's peer label SID
44244f6a993fSPaul Moore  *
44254f6a993fSPaul Moore  * Description:
4426220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4427220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4428220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4429220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4430220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4431220deb96SPaul Moore  * peer labels.
44324f6a993fSPaul Moore  *
44334f6a993fSPaul Moore  */
4434220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
44354f6a993fSPaul Moore {
443671f1cb05SPaul Moore 	int err;
44374f6a993fSPaul Moore 	u32 xfrm_sid;
44384f6a993fSPaul Moore 	u32 nlbl_sid;
4439220deb96SPaul Moore 	u32 nlbl_type;
44404f6a993fSPaul Moore 
4441817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4442bed4d7efSPaul Moore 	if (unlikely(err))
4443bed4d7efSPaul Moore 		return -EACCES;
4444bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4445bed4d7efSPaul Moore 	if (unlikely(err))
4446bed4d7efSPaul Moore 		return -EACCES;
4447220deb96SPaul Moore 
4448aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4449aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
445071f1cb05SPaul Moore 	if (unlikely(err)) {
445171f1cb05SPaul Moore 		printk(KERN_WARNING
445271f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
445371f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4454220deb96SPaul Moore 		return -EACCES;
445571f1cb05SPaul Moore 	}
4456220deb96SPaul Moore 
4457220deb96SPaul Moore 	return 0;
44584f6a993fSPaul Moore }
44594f6a993fSPaul Moore 
4460446b8024SPaul Moore /**
4461446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4462446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4463446b8024SPaul Moore  * @skb_sid: the packet's SID
4464446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4465446b8024SPaul Moore  *
4466446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4467446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4468446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
4469446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4470446b8024SPaul Moore  *
4471446b8024SPaul Moore  */
4472446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4473446b8024SPaul Moore {
4474446b8024SPaul Moore 	int err = 0;
4475446b8024SPaul Moore 
4476446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4477aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4478aa8e712cSStephen Smalley 					    conn_sid);
4479446b8024SPaul Moore 	else
4480446b8024SPaul Moore 		*conn_sid = sk_sid;
4481446b8024SPaul Moore 
4482446b8024SPaul Moore 	return err;
4483446b8024SPaul Moore }
4484446b8024SPaul Moore 
44851da177e4SLinus Torvalds /* socket security operations */
4486d4f2d978SPaul Moore 
44872ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
44882ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4489d4f2d978SPaul Moore {
44902ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
44912ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
44922ad18bdfSHarry Ciao 		return 0;
44932ad18bdfSHarry Ciao 	}
44942ad18bdfSHarry Ciao 
4495aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4496aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4497d4f2d978SPaul Moore }
4498d4f2d978SPaul Moore 
4499be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
45001da177e4SLinus Torvalds {
4501253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45022bf49690SThomas Liu 	struct common_audit_data ad;
450348c62af6SEric Paris 	struct lsm_network_audit net = {0,};
45041da177e4SLinus Torvalds 
4505253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4506253bfae6SPaul Moore 		return 0;
45071da177e4SLinus Torvalds 
450850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
450948c62af6SEric Paris 	ad.u.net = &net;
451048c62af6SEric Paris 	ad.u.net->sk = sk;
45111da177e4SLinus Torvalds 
45126b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
45136b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4514be0554c9SStephen Smalley 			    &ad);
45151da177e4SLinus Torvalds }
45161da177e4SLinus Torvalds 
45171da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
45181da177e4SLinus Torvalds 				 int protocol, int kern)
45191da177e4SLinus Torvalds {
45205fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4521d4f2d978SPaul Moore 	u32 newsid;
4522275bb41eSDavid Howells 	u16 secclass;
45232ad18bdfSHarry Ciao 	int rc;
45241da177e4SLinus Torvalds 
45251da177e4SLinus Torvalds 	if (kern)
4526d4f2d978SPaul Moore 		return 0;
45271da177e4SLinus Torvalds 
4528275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
45292ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
45302ad18bdfSHarry Ciao 	if (rc)
45312ad18bdfSHarry Ciao 		return rc;
45322ad18bdfSHarry Ciao 
45336b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
45346b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
45351da177e4SLinus Torvalds }
45361da177e4SLinus Torvalds 
45377420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
45381da177e4SLinus Torvalds 				      int type, int protocol, int kern)
45391da177e4SLinus Torvalds {
45405fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
45415d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4542892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
45439287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
45449287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4545275bb41eSDavid Howells 	int err = 0;
4546275bb41eSDavid Howells 
45479287aed2SAndreas Gruenbacher 	if (!kern) {
45489287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
45492ad18bdfSHarry Ciao 		if (err)
45502ad18bdfSHarry Ciao 			return err;
45512ad18bdfSHarry Ciao 	}
4552275bb41eSDavid Howells 
45539287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
45549287aed2SAndreas Gruenbacher 	isec->sid = sid;
45556f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
45561da177e4SLinus Torvalds 
4557892c141eSVenkat Yekkirala 	if (sock->sk) {
4558892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
45599287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
45609287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4561d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4562d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4563d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4564d452930fSRichard Haines 
4565389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4566892c141eSVenkat Yekkirala 	}
4567892c141eSVenkat Yekkirala 
45687420ed23SVenkat Yekkirala 	return err;
45691da177e4SLinus Torvalds }
45701da177e4SLinus Torvalds 
45711da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
45721da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
45731da177e4SLinus Torvalds    permission check between the socket and the port number. */
45741da177e4SLinus Torvalds 
45751da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
45761da177e4SLinus Torvalds {
4577253bfae6SPaul Moore 	struct sock *sk = sock->sk;
45781da177e4SLinus Torvalds 	u16 family;
45791da177e4SLinus Torvalds 	int err;
45801da177e4SLinus Torvalds 
4581be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
45821da177e4SLinus Torvalds 	if (err)
45831da177e4SLinus Torvalds 		goto out;
45841da177e4SLinus Torvalds 
4585d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4586253bfae6SPaul Moore 	family = sk->sk_family;
45871da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
45881da177e4SLinus Torvalds 		char *addrp;
4589253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
45902bf49690SThomas Liu 		struct common_audit_data ad;
459148c62af6SEric Paris 		struct lsm_network_audit net = {0,};
45921da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
45931da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
45941da177e4SLinus Torvalds 		unsigned short snum;
4595e399f982SJames Morris 		u32 sid, node_perm;
45961da177e4SLinus Torvalds 
4597d452930fSRichard Haines 		/*
4598d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4599d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4600d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4601d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4602d452930fSRichard Haines 		 */
460368741a8aSRichard Haines 		switch (address->sa_family) {
460468741a8aSRichard Haines 		case AF_INET:
460568741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
460668741a8aSRichard Haines 				return -EINVAL;
46071da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
46081da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
46091da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
461068741a8aSRichard Haines 			break;
461168741a8aSRichard Haines 		case AF_INET6:
461268741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
461368741a8aSRichard Haines 				return -EINVAL;
46141da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
46151da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
46161da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
461768741a8aSRichard Haines 			break;
461868741a8aSRichard Haines 		default:
461968741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
462068741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
462168741a8aSRichard Haines 			 */
462268741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
462368741a8aSRichard Haines 				return -EINVAL;
462468741a8aSRichard Haines 			else
462568741a8aSRichard Haines 				return -EAFNOSUPPORT;
46261da177e4SLinus Torvalds 		}
46271da177e4SLinus Torvalds 
4628227b60f5SStephen Hemminger 		if (snum) {
4629227b60f5SStephen Hemminger 			int low, high;
4630227b60f5SStephen Hemminger 
46310bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4632227b60f5SStephen Hemminger 
46334548b683SKrister Johansen 			if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
46344548b683SKrister Johansen 			    snum > high) {
46353e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
46363e112172SPaul Moore 						      snum, &sid);
46371da177e4SLinus Torvalds 				if (err)
46381da177e4SLinus Torvalds 					goto out;
463950c205f5SEric Paris 				ad.type = LSM_AUDIT_DATA_NET;
464048c62af6SEric Paris 				ad.u.net = &net;
464148c62af6SEric Paris 				ad.u.net->sport = htons(snum);
464248c62af6SEric Paris 				ad.u.net->family = family;
46436b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
46446b6bc620SStephen Smalley 						   sksec->sid, sid,
4645253bfae6SPaul Moore 						   sksec->sclass,
46461da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
46471da177e4SLinus Torvalds 				if (err)
46481da177e4SLinus Torvalds 					goto out;
46491da177e4SLinus Torvalds 			}
4650227b60f5SStephen Hemminger 		}
46511da177e4SLinus Torvalds 
4652253bfae6SPaul Moore 		switch (sksec->sclass) {
465313402580SJames Morris 		case SECCLASS_TCP_SOCKET:
46541da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
46551da177e4SLinus Torvalds 			break;
46561da177e4SLinus Torvalds 
465713402580SJames Morris 		case SECCLASS_UDP_SOCKET:
46581da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
46591da177e4SLinus Torvalds 			break;
46601da177e4SLinus Torvalds 
46612ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
46622ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
46632ee92d46SJames Morris 			break;
46642ee92d46SJames Morris 
4665d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4666d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4667d452930fSRichard Haines 			break;
4668d452930fSRichard Haines 
46691da177e4SLinus Torvalds 		default:
46701da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
46711da177e4SLinus Torvalds 			break;
46721da177e4SLinus Torvalds 		}
46731da177e4SLinus Torvalds 
4674224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
46751da177e4SLinus Torvalds 		if (err)
46761da177e4SLinus Torvalds 			goto out;
46771da177e4SLinus Torvalds 
467850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
467948c62af6SEric Paris 		ad.u.net = &net;
468048c62af6SEric Paris 		ad.u.net->sport = htons(snum);
468148c62af6SEric Paris 		ad.u.net->family = family;
46821da177e4SLinus Torvalds 
4683d452930fSRichard Haines 		if (address->sa_family == AF_INET)
468448c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
46851da177e4SLinus Torvalds 		else
468648c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
46871da177e4SLinus Torvalds 
46886b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
46896b6bc620SStephen Smalley 				   sksec->sid, sid,
4690253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
46911da177e4SLinus Torvalds 		if (err)
46921da177e4SLinus Torvalds 			goto out;
46931da177e4SLinus Torvalds 	}
46941da177e4SLinus Torvalds out:
46951da177e4SLinus Torvalds 	return err;
46961da177e4SLinus Torvalds }
46971da177e4SLinus Torvalds 
4698d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4699d452930fSRichard Haines  * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.txt
4700d452930fSRichard Haines  */
4701d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4702d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
47031da177e4SLinus Torvalds {
4704014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4705253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
47061da177e4SLinus Torvalds 	int err;
47071da177e4SLinus Torvalds 
4708be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
47091da177e4SLinus Torvalds 	if (err)
47101da177e4SLinus Torvalds 		return err;
47111da177e4SLinus Torvalds 
47121da177e4SLinus Torvalds 	/*
4713d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4714d452930fSRichard Haines 	 * for the port.
47151da177e4SLinus Torvalds 	 */
4716253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4717d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4718d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
47192bf49690SThomas Liu 		struct common_audit_data ad;
472048c62af6SEric Paris 		struct lsm_network_audit net = {0,};
47211da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
47221da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
47231da177e4SLinus Torvalds 		unsigned short snum;
47242ee92d46SJames Morris 		u32 sid, perm;
47251da177e4SLinus Torvalds 
4726d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4727d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4728d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4729d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4730d452930fSRichard Haines 		 */
473168741a8aSRichard Haines 		switch (address->sa_family) {
473268741a8aSRichard Haines 		case AF_INET:
47331da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4734911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
47351da177e4SLinus Torvalds 				return -EINVAL;
47361da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
473768741a8aSRichard Haines 			break;
473868741a8aSRichard Haines 		case AF_INET6:
47391da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4740911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
47411da177e4SLinus Torvalds 				return -EINVAL;
47421da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
474368741a8aSRichard Haines 			break;
474468741a8aSRichard Haines 		default:
474568741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
474668741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
474768741a8aSRichard Haines 			 */
474868741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
474968741a8aSRichard Haines 				return -EINVAL;
475068741a8aSRichard Haines 			else
475168741a8aSRichard Haines 				return -EAFNOSUPPORT;
47521da177e4SLinus Torvalds 		}
47531da177e4SLinus Torvalds 
47543e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
47551da177e4SLinus Torvalds 		if (err)
4756d452930fSRichard Haines 			return err;
47571da177e4SLinus Torvalds 
4758d452930fSRichard Haines 		switch (sksec->sclass) {
4759d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4760d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4761d452930fSRichard Haines 			break;
4762d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4763d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4764d452930fSRichard Haines 			break;
4765d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4766d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4767d452930fSRichard Haines 			break;
4768d452930fSRichard Haines 		}
47692ee92d46SJames Morris 
477050c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
477148c62af6SEric Paris 		ad.u.net = &net;
477248c62af6SEric Paris 		ad.u.net->dport = htons(snum);
477348c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
47746b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
47756b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
47761da177e4SLinus Torvalds 		if (err)
4777d452930fSRichard Haines 			return err;
47781da177e4SLinus Torvalds 	}
47791da177e4SLinus Torvalds 
4780d452930fSRichard Haines 	return 0;
4781d452930fSRichard Haines }
4782014ab19aSPaul Moore 
4783d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4784d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4785d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4786d452930fSRichard Haines {
4787d452930fSRichard Haines 	int err;
4788d452930fSRichard Haines 	struct sock *sk = sock->sk;
4789d452930fSRichard Haines 
4790d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4791d452930fSRichard Haines 	if (err)
47921da177e4SLinus Torvalds 		return err;
4793d452930fSRichard Haines 
4794d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
47951da177e4SLinus Torvalds }
47961da177e4SLinus Torvalds 
47971da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
47981da177e4SLinus Torvalds {
4799be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
48001da177e4SLinus Torvalds }
48011da177e4SLinus Torvalds 
48021da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
48031da177e4SLinus Torvalds {
48041da177e4SLinus Torvalds 	int err;
48051da177e4SLinus Torvalds 	struct inode_security_struct *isec;
48061da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
48079287aed2SAndreas Gruenbacher 	u16 sclass;
48089287aed2SAndreas Gruenbacher 	u32 sid;
48091da177e4SLinus Torvalds 
4810be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
48111da177e4SLinus Torvalds 	if (err)
48121da177e4SLinus Torvalds 		return err;
48131da177e4SLinus Torvalds 
48145d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
48159287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
48169287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
48179287aed2SAndreas Gruenbacher 	sid = isec->sid;
48189287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
48199287aed2SAndreas Gruenbacher 
48209287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
48219287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
48229287aed2SAndreas Gruenbacher 	newisec->sid = sid;
48236f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
48241da177e4SLinus Torvalds 
48251da177e4SLinus Torvalds 	return 0;
48261da177e4SLinus Torvalds }
48271da177e4SLinus Torvalds 
48281da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
48291da177e4SLinus Torvalds 				  int size)
48301da177e4SLinus Torvalds {
4831be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
48321da177e4SLinus Torvalds }
48331da177e4SLinus Torvalds 
48341da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
48351da177e4SLinus Torvalds 				  int size, int flags)
48361da177e4SLinus Torvalds {
4837be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
48381da177e4SLinus Torvalds }
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
48411da177e4SLinus Torvalds {
4842be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48431da177e4SLinus Torvalds }
48441da177e4SLinus Torvalds 
48451da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
48461da177e4SLinus Torvalds {
4847be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48481da177e4SLinus Torvalds }
48491da177e4SLinus Torvalds 
48501da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
48511da177e4SLinus Torvalds {
4852f8687afeSPaul Moore 	int err;
4853f8687afeSPaul Moore 
4854be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4855f8687afeSPaul Moore 	if (err)
4856f8687afeSPaul Moore 		return err;
4857f8687afeSPaul Moore 
4858f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
48591da177e4SLinus Torvalds }
48601da177e4SLinus Torvalds 
48611da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
48621da177e4SLinus Torvalds 				     int optname)
48631da177e4SLinus Torvalds {
4864be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
48651da177e4SLinus Torvalds }
48661da177e4SLinus Torvalds 
48671da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
48681da177e4SLinus Torvalds {
4869be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
48701da177e4SLinus Torvalds }
48711da177e4SLinus Torvalds 
48723610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
48733610cda5SDavid S. Miller 					      struct sock *other,
48741da177e4SLinus Torvalds 					      struct sock *newsk)
48751da177e4SLinus Torvalds {
48763610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
48773610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
48784d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
48792bf49690SThomas Liu 	struct common_audit_data ad;
488048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
48811da177e4SLinus Torvalds 	int err;
48821da177e4SLinus Torvalds 
488350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
488448c62af6SEric Paris 	ad.u.net = &net;
488548c62af6SEric Paris 	ad.u.net->sk = other;
48861da177e4SLinus Torvalds 
48876b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
48886b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
48894d1e2451SPaul Moore 			   sksec_other->sclass,
48901da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
48911da177e4SLinus Torvalds 	if (err)
48921da177e4SLinus Torvalds 		return err;
48931da177e4SLinus Torvalds 
48941da177e4SLinus Torvalds 	/* server child socket */
48954d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4896aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4897aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
48984d1e2451SPaul Moore 	if (err)
48994237c75cSVenkat Yekkirala 		return err;
49004d1e2451SPaul Moore 
49014d1e2451SPaul Moore 	/* connecting socket */
49024d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
49034d1e2451SPaul Moore 
49044d1e2451SPaul Moore 	return 0;
49051da177e4SLinus Torvalds }
49061da177e4SLinus Torvalds 
49071da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
49081da177e4SLinus Torvalds 					struct socket *other)
49091da177e4SLinus Torvalds {
4910253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4911253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
49122bf49690SThomas Liu 	struct common_audit_data ad;
491348c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49141da177e4SLinus Torvalds 
491550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
491648c62af6SEric Paris 	ad.u.net = &net;
491748c62af6SEric Paris 	ad.u.net->sk = other->sk;
49181da177e4SLinus Torvalds 
49196b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49206b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4921253bfae6SPaul Moore 			    &ad);
49221da177e4SLinus Torvalds }
49231da177e4SLinus Torvalds 
4924cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4925cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
49262bf49690SThomas Liu 				    struct common_audit_data *ad)
4927effad8dfSPaul Moore {
4928effad8dfSPaul Moore 	int err;
4929effad8dfSPaul Moore 	u32 if_sid;
4930effad8dfSPaul Moore 	u32 node_sid;
4931effad8dfSPaul Moore 
4932cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4933effad8dfSPaul Moore 	if (err)
4934effad8dfSPaul Moore 		return err;
49356b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
49366b6bc620SStephen Smalley 			   peer_sid, if_sid,
4937effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4938effad8dfSPaul Moore 	if (err)
4939effad8dfSPaul Moore 		return err;
4940effad8dfSPaul Moore 
4941effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4942effad8dfSPaul Moore 	if (err)
4943effad8dfSPaul Moore 		return err;
49446b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49456b6bc620SStephen Smalley 			    peer_sid, node_sid,
4946effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4947effad8dfSPaul Moore }
4948effad8dfSPaul Moore 
4949220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4950d8395c87SPaul Moore 				       u16 family)
4951220deb96SPaul Moore {
4952277d342fSPaul Moore 	int err = 0;
4953220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4954220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
49552bf49690SThomas Liu 	struct common_audit_data ad;
495648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4957d8395c87SPaul Moore 	char *addrp;
4958d8395c87SPaul Moore 
495950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
496048c62af6SEric Paris 	ad.u.net = &net;
496148c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
496248c62af6SEric Paris 	ad.u.net->family = family;
4963d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4964d8395c87SPaul Moore 	if (err)
4965d8395c87SPaul Moore 		return err;
4966220deb96SPaul Moore 
496758bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
49686b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
49696b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4970d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4971220deb96SPaul Moore 		if (err)
4972220deb96SPaul Moore 			return err;
497358bfbb51SPaul Moore 	}
4974220deb96SPaul Moore 
4975d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4976220deb96SPaul Moore 	if (err)
4977220deb96SPaul Moore 		return err;
4978d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4979220deb96SPaul Moore 
49804e5ab4cbSJames Morris 	return err;
49814e5ab4cbSJames Morris }
4982d28d1e08STrent Jaeger 
49834e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
49844e5ab4cbSJames Morris {
4985220deb96SPaul Moore 	int err;
49864237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4987220deb96SPaul Moore 	u16 family = sk->sk_family;
4988220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
49892bf49690SThomas Liu 	struct common_audit_data ad;
499048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4991220deb96SPaul Moore 	char *addrp;
4992d8395c87SPaul Moore 	u8 secmark_active;
4993d8395c87SPaul Moore 	u8 peerlbl_active;
49944e5ab4cbSJames Morris 
49954e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4996220deb96SPaul Moore 		return 0;
49974e5ab4cbSJames Morris 
49984e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
499987fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
50004e5ab4cbSJames Morris 		family = PF_INET;
50014e5ab4cbSJames Morris 
5002d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5003d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
5004d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5005d8395c87SPaul Moore 	 * as fast and as clean as possible. */
5006aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5007d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
5008d8395c87SPaul Moore 
5009d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
50102be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5011d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
5012d8395c87SPaul Moore 		return 0;
5013d8395c87SPaul Moore 
501450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
501548c62af6SEric Paris 	ad.u.net = &net;
501648c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
501748c62af6SEric Paris 	ad.u.net->family = family;
5018224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
50194e5ab4cbSJames Morris 	if (err)
5020220deb96SPaul Moore 		return err;
50214e5ab4cbSJames Morris 
5022d8395c87SPaul Moore 	if (peerlbl_active) {
5023d621d35eSPaul Moore 		u32 peer_sid;
5024220deb96SPaul Moore 
5025220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5026220deb96SPaul Moore 		if (err)
5027220deb96SPaul Moore 			return err;
5028cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5029cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5030dfaebe98SPaul Moore 		if (err) {
5031a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
5032effad8dfSPaul Moore 			return err;
5033dfaebe98SPaul Moore 		}
50346b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50356b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
5036d621d35eSPaul Moore 				   PEER__RECV, &ad);
503746d01d63SChad Hanson 		if (err) {
5038a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
503946d01d63SChad Hanson 			return err;
504046d01d63SChad Hanson 		}
5041d621d35eSPaul Moore 	}
5042d621d35eSPaul Moore 
5043d8395c87SPaul Moore 	if (secmark_active) {
50446b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50456b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5046effad8dfSPaul Moore 				   PACKET__RECV, &ad);
5047effad8dfSPaul Moore 		if (err)
5048effad8dfSPaul Moore 			return err;
5049effad8dfSPaul Moore 	}
5050effad8dfSPaul Moore 
5051d621d35eSPaul Moore 	return err;
50521da177e4SLinus Torvalds }
50531da177e4SLinus Torvalds 
50542c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
50551da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
50561da177e4SLinus Torvalds {
50571da177e4SLinus Torvalds 	int err = 0;
50581da177e4SLinus Torvalds 	char *scontext;
50591da177e4SLinus Torvalds 	u32 scontext_len;
5060253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
50613de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
50621da177e4SLinus Torvalds 
5063253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5064d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
5065d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
5066dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
5067253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
5068253bfae6SPaul Moore 		return -ENOPROTOOPT;
50691da177e4SLinus Torvalds 
5070aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5071aa8e712cSStephen Smalley 				      &scontext_len);
50721da177e4SLinus Torvalds 	if (err)
5073253bfae6SPaul Moore 		return err;
50741da177e4SLinus Torvalds 
50751da177e4SLinus Torvalds 	if (scontext_len > len) {
50761da177e4SLinus Torvalds 		err = -ERANGE;
50771da177e4SLinus Torvalds 		goto out_len;
50781da177e4SLinus Torvalds 	}
50791da177e4SLinus Torvalds 
50801da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
50811da177e4SLinus Torvalds 		err = -EFAULT;
50821da177e4SLinus Torvalds 
50831da177e4SLinus Torvalds out_len:
50841da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
50851da177e4SLinus Torvalds 		err = -EFAULT;
50861da177e4SLinus Torvalds 	kfree(scontext);
50871da177e4SLinus Torvalds 	return err;
50881da177e4SLinus Torvalds }
50891da177e4SLinus Torvalds 
5090dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
50912c7946a7SCatherine Zhang {
5092dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
509375e22910SPaul Moore 	u16 family;
5094899134f2SPaul Moore 	struct inode_security_struct *isec;
5095877ce7c1SCatherine Zhang 
5096aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
5097aa862900SPaul Moore 		family = PF_INET;
5098aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
5099aa862900SPaul Moore 		family = PF_INET6;
5100aa862900SPaul Moore 	else if (sock)
510175e22910SPaul Moore 		family = sock->sk->sk_family;
510275e22910SPaul Moore 	else
510375e22910SPaul Moore 		goto out;
510475e22910SPaul Moore 
5105899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
5106899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
5107899134f2SPaul Moore 		peer_secid = isec->sid;
5108899134f2SPaul Moore 	} else if (skb)
5109220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
51102c7946a7SCatherine Zhang 
511175e22910SPaul Moore out:
5112dc49c1f9SCatherine Zhang 	*secid = peer_secid;
511375e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
511475e22910SPaul Moore 		return -EINVAL;
511575e22910SPaul Moore 	return 0;
51162c7946a7SCatherine Zhang }
51172c7946a7SCatherine Zhang 
51187d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
51191da177e4SLinus Torvalds {
512084914b7eSPaul Moore 	struct sk_security_struct *sksec;
512184914b7eSPaul Moore 
512284914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
512384914b7eSPaul Moore 	if (!sksec)
512484914b7eSPaul Moore 		return -ENOMEM;
512584914b7eSPaul Moore 
512684914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
512784914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
51285dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
512984914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
513084914b7eSPaul Moore 	sk->sk_security = sksec;
513184914b7eSPaul Moore 
513284914b7eSPaul Moore 	return 0;
51331da177e4SLinus Torvalds }
51341da177e4SLinus Torvalds 
51351da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
51361da177e4SLinus Torvalds {
513784914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
513884914b7eSPaul Moore 
513984914b7eSPaul Moore 	sk->sk_security = NULL;
514084914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
514184914b7eSPaul Moore 	kfree(sksec);
51421da177e4SLinus Torvalds }
51431da177e4SLinus Torvalds 
5144892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5145892c141eSVenkat Yekkirala {
5146dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5147dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5148892c141eSVenkat Yekkirala 
5149dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5150dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5151dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
515299f59ed0SPaul Moore 
5153dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5154892c141eSVenkat Yekkirala }
5155892c141eSVenkat Yekkirala 
5156beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5157d28d1e08STrent Jaeger {
5158d28d1e08STrent Jaeger 	if (!sk)
5159beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5160892c141eSVenkat Yekkirala 	else {
5161892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5162d28d1e08STrent Jaeger 
5163beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5164892c141eSVenkat Yekkirala 	}
5165d28d1e08STrent Jaeger }
5166d28d1e08STrent Jaeger 
51679a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
51684237c75cSVenkat Yekkirala {
51695d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
51705d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
51714237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
51724237c75cSVenkat Yekkirala 
51732873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
51742873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
51754237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5176220deb96SPaul Moore 	sksec->sclass = isec->sclass;
51774237c75cSVenkat Yekkirala }
51784237c75cSVenkat Yekkirala 
5179d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5180d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5181d452930fSRichard Haines  * already present).
5182d452930fSRichard Haines  */
5183d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5184d452930fSRichard Haines 				      struct sk_buff *skb)
5185d452930fSRichard Haines {
5186d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5187d452930fSRichard Haines 	struct common_audit_data ad;
5188d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5189d452930fSRichard Haines 	u8 peerlbl_active;
5190d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5191d452930fSRichard Haines 	u32 conn_sid;
5192d452930fSRichard Haines 	int err = 0;
5193d452930fSRichard Haines 
5194aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5195d452930fSRichard Haines 		return 0;
5196d452930fSRichard Haines 
5197d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5198d452930fSRichard Haines 
5199d452930fSRichard Haines 	if (peerlbl_active) {
5200d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5201d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5202d452930fSRichard Haines 		 */
5203d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5204d452930fSRichard Haines 					      &peer_sid);
5205d452930fSRichard Haines 		if (err)
5206d452930fSRichard Haines 			return err;
5207d452930fSRichard Haines 
5208d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5209d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5210d452930fSRichard Haines 	}
5211d452930fSRichard Haines 
5212d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5213d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5214d452930fSRichard Haines 
5215d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5216d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5217d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5218d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5219d452930fSRichard Haines 		 */
5220d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5221d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5222d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5223d452930fSRichard Haines 		 * consistency among the peer SIDs.
5224d452930fSRichard Haines 		 */
5225d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5226d452930fSRichard Haines 		ad.u.net = &net;
5227d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
52286b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
52296b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5230d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5231d452930fSRichard Haines 		if (err)
5232d452930fSRichard Haines 			return err;
5233d452930fSRichard Haines 	}
5234d452930fSRichard Haines 
5235d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5236d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5237d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5238d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5239d452930fSRichard Haines 	 * plug this into the new socket.
5240d452930fSRichard Haines 	 */
5241d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5242d452930fSRichard Haines 	if (err)
5243d452930fSRichard Haines 		return err;
5244d452930fSRichard Haines 
5245d452930fSRichard Haines 	ep->secid = conn_sid;
5246d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5247d452930fSRichard Haines 
5248d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5249d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5250d452930fSRichard Haines }
5251d452930fSRichard Haines 
5252d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5253d452930fSRichard Haines  * based on their @optname.
5254d452930fSRichard Haines  */
5255d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5256d452930fSRichard Haines 				     struct sockaddr *address,
5257d452930fSRichard Haines 				     int addrlen)
5258d452930fSRichard Haines {
5259d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5260d452930fSRichard Haines 	void *addr_buf;
5261d452930fSRichard Haines 	struct sockaddr *addr;
5262d452930fSRichard Haines 	struct socket *sock;
5263d452930fSRichard Haines 
5264aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5265d452930fSRichard Haines 		return 0;
5266d452930fSRichard Haines 
5267d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5268d452930fSRichard Haines 	sock = sk->sk_socket;
5269d452930fSRichard Haines 	addr_buf = address;
5270d452930fSRichard Haines 
5271d452930fSRichard Haines 	while (walk_size < addrlen) {
5272d452930fSRichard Haines 		addr = addr_buf;
5273d452930fSRichard Haines 		switch (addr->sa_family) {
5274d452930fSRichard Haines 		case AF_INET:
5275d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5276d452930fSRichard Haines 			break;
5277d452930fSRichard Haines 		case AF_INET6:
5278d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5279d452930fSRichard Haines 			break;
5280d452930fSRichard Haines 		default:
5281d452930fSRichard Haines 			return -EAFNOSUPPORT;
5282d452930fSRichard Haines 		}
5283d452930fSRichard Haines 
5284d452930fSRichard Haines 		err = -EINVAL;
5285d452930fSRichard Haines 		switch (optname) {
5286d452930fSRichard Haines 		/* Bind checks */
5287d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5288d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5289d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5290d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5291d452930fSRichard Haines 			break;
5292d452930fSRichard Haines 		/* Connect checks */
5293d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5294d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5295d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5296d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5297d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5298d452930fSRichard Haines 			if (err)
5299d452930fSRichard Haines 				return err;
5300d452930fSRichard Haines 
5301d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5302d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5303d452930fSRichard Haines 			 * therefore selinux_netlbl_socket_connect_locked() is
5304d452930fSRichard Haines 			 * is called here. The situations handled are:
5305d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5306d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5307d452930fSRichard Haines 			 * primary address is selected.
5308d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5309d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5310d452930fSRichard Haines 			 * selinux_socket_connect().
5311d452930fSRichard Haines 			 */
5312d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5313d452930fSRichard Haines 			break;
5314d452930fSRichard Haines 		}
5315d452930fSRichard Haines 
5316d452930fSRichard Haines 		if (err)
5317d452930fSRichard Haines 			return err;
5318d452930fSRichard Haines 
5319d452930fSRichard Haines 		addr_buf += len;
5320d452930fSRichard Haines 		walk_size += len;
5321d452930fSRichard Haines 	}
5322d452930fSRichard Haines 
5323d452930fSRichard Haines 	return 0;
5324d452930fSRichard Haines }
5325d452930fSRichard Haines 
5326d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5327d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5328d452930fSRichard Haines 				  struct sock *newsk)
5329d452930fSRichard Haines {
5330d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5331d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5332d452930fSRichard Haines 
5333d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5334d452930fSRichard Haines 	 * the non-sctp clone version.
5335d452930fSRichard Haines 	 */
5336aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5337d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5338d452930fSRichard Haines 
5339d452930fSRichard Haines 	newsksec->sid = ep->secid;
5340d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5341d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5342d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5343d452930fSRichard Haines }
5344d452930fSRichard Haines 
53459a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
53464237c75cSVenkat Yekkirala 				     struct request_sock *req)
53474237c75cSVenkat Yekkirala {
53484237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
53494237c75cSVenkat Yekkirala 	int err;
53500b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5351446b8024SPaul Moore 	u32 connsid;
53524237c75cSVenkat Yekkirala 	u32 peersid;
53534237c75cSVenkat Yekkirala 
5354aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5355220deb96SPaul Moore 	if (err)
5356220deb96SPaul Moore 		return err;
5357446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
53584237c75cSVenkat Yekkirala 	if (err)
53594237c75cSVenkat Yekkirala 		return err;
5360446b8024SPaul Moore 	req->secid = connsid;
53616b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5362389fb800SPaul Moore 
5363389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
53644237c75cSVenkat Yekkirala }
53654237c75cSVenkat Yekkirala 
53669a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
53679a673e56SAdrian Bunk 				   const struct request_sock *req)
53684237c75cSVenkat Yekkirala {
53694237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
53704237c75cSVenkat Yekkirala 
53714237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
53726b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
53734237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
53744237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
53754237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
53764237c75cSVenkat Yekkirala 	   time it will have been created and available. */
537799f59ed0SPaul Moore 
53789f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
53799f2ad665SPaul Moore 	 * thread with access to newsksec */
5380389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
53814237c75cSVenkat Yekkirala }
53824237c75cSVenkat Yekkirala 
5383014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
53846b877699SVenkat Yekkirala {
5385aa862900SPaul Moore 	u16 family = sk->sk_family;
53866b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
53876b877699SVenkat Yekkirala 
5388aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5389aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5390aa862900SPaul Moore 		family = PF_INET;
5391aa862900SPaul Moore 
5392aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
53936b877699SVenkat Yekkirala }
53946b877699SVenkat Yekkirala 
53952606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
53962606fd1fSEric Paris {
53972606fd1fSEric Paris 	const struct task_security_struct *__tsec;
53982606fd1fSEric Paris 	u32 tsid;
53992606fd1fSEric Paris 
54002606fd1fSEric Paris 	__tsec = current_security();
54012606fd1fSEric Paris 	tsid = __tsec->sid;
54022606fd1fSEric Paris 
54036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54046b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
54056b6bc620SStephen Smalley 			    NULL);
54062606fd1fSEric Paris }
54072606fd1fSEric Paris 
54082606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
54092606fd1fSEric Paris {
54102606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
54112606fd1fSEric Paris }
54122606fd1fSEric Paris 
54132606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
54142606fd1fSEric Paris {
54152606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
54162606fd1fSEric Paris }
54172606fd1fSEric Paris 
54189a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
54199a673e56SAdrian Bunk 				      struct flowi *fl)
54204237c75cSVenkat Yekkirala {
54211d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
54224237c75cSVenkat Yekkirala }
54234237c75cSVenkat Yekkirala 
54245dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
54255dbbaf2dSPaul Moore {
54265dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
54275dbbaf2dSPaul Moore 
54285dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
54295dbbaf2dSPaul Moore 	if (!tunsec)
54305dbbaf2dSPaul Moore 		return -ENOMEM;
54315dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
54325dbbaf2dSPaul Moore 
54335dbbaf2dSPaul Moore 	*security = tunsec;
54345dbbaf2dSPaul Moore 	return 0;
54355dbbaf2dSPaul Moore }
54365dbbaf2dSPaul Moore 
54375dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
54385dbbaf2dSPaul Moore {
54395dbbaf2dSPaul Moore 	kfree(security);
54405dbbaf2dSPaul Moore }
54415dbbaf2dSPaul Moore 
5442ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5443ed6d76e4SPaul Moore {
5444ed6d76e4SPaul Moore 	u32 sid = current_sid();
5445ed6d76e4SPaul Moore 
5446ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5447ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5448ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5449ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5450ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5451ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5452ed6d76e4SPaul Moore 
54536b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54546b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5455ed6d76e4SPaul Moore 			    NULL);
5456ed6d76e4SPaul Moore }
5457ed6d76e4SPaul Moore 
54585dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5459ed6d76e4SPaul Moore {
54605dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
54615dbbaf2dSPaul Moore 
54626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54636b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
54645dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
54655dbbaf2dSPaul Moore }
54665dbbaf2dSPaul Moore 
54675dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
54685dbbaf2dSPaul Moore {
54695dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5470ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5471ed6d76e4SPaul Moore 
5472ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5473ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5474ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5475ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5476ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5477ed6d76e4SPaul Moore 	 * protocols were being used */
5478ed6d76e4SPaul Moore 
54795dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5480ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
54815dbbaf2dSPaul Moore 
54825dbbaf2dSPaul Moore 	return 0;
5483ed6d76e4SPaul Moore }
5484ed6d76e4SPaul Moore 
54855dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5486ed6d76e4SPaul Moore {
54875dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5488ed6d76e4SPaul Moore 	u32 sid = current_sid();
5489ed6d76e4SPaul Moore 	int err;
5490ed6d76e4SPaul Moore 
54916b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
54926b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5493ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5494ed6d76e4SPaul Moore 	if (err)
5495ed6d76e4SPaul Moore 		return err;
54966b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
54976b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5498ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5499ed6d76e4SPaul Moore 	if (err)
5500ed6d76e4SPaul Moore 		return err;
55015dbbaf2dSPaul Moore 	tunsec->sid = sid;
5502ed6d76e4SPaul Moore 
5503ed6d76e4SPaul Moore 	return 0;
5504ed6d76e4SPaul Moore }
5505ed6d76e4SPaul Moore 
55061da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
55071da177e4SLinus Torvalds {
55081da177e4SLinus Torvalds 	int err = 0;
55091da177e4SLinus Torvalds 	u32 perm;
55101da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5511253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
55121da177e4SLinus Torvalds 
551377954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
55141da177e4SLinus Torvalds 		err = -EINVAL;
55151da177e4SLinus Torvalds 		goto out;
55161da177e4SLinus Torvalds 	}
5517b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
55181da177e4SLinus Torvalds 
5519253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
55201da177e4SLinus Torvalds 	if (err) {
55211da177e4SLinus Torvalds 		if (err == -EINVAL) {
552276319946SVladis Dronov 			pr_warn_ratelimited("SELinux: unrecognized netlink"
552376319946SVladis Dronov 			       " message: protocol=%hu nlmsg_type=%hu sclass=%s"
552476319946SVladis Dronov 			       " pig=%d comm=%s\n",
5525cded3fffSMarek Milkovic 			       sk->sk_protocol, nlh->nlmsg_type,
552676319946SVladis Dronov 			       secclass_map[sksec->sclass - 1].name,
552776319946SVladis Dronov 			       task_pid_nr(current), current->comm);
5528e5a5ca96SPaul Moore 			if (!enforcing_enabled(&selinux_state) ||
5529aa8e712cSStephen Smalley 			    security_get_allow_unknown(&selinux_state))
55301da177e4SLinus Torvalds 				err = 0;
55311da177e4SLinus Torvalds 		}
55321da177e4SLinus Torvalds 
55331da177e4SLinus Torvalds 		/* Ignore */
55341da177e4SLinus Torvalds 		if (err == -ENOENT)
55351da177e4SLinus Torvalds 			err = 0;
55361da177e4SLinus Torvalds 		goto out;
55371da177e4SLinus Torvalds 	}
55381da177e4SLinus Torvalds 
5539be0554c9SStephen Smalley 	err = sock_has_perm(sk, perm);
55401da177e4SLinus Torvalds out:
55411da177e4SLinus Torvalds 	return err;
55421da177e4SLinus Torvalds }
55431da177e4SLinus Torvalds 
55441da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
55451da177e4SLinus Torvalds 
5546cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5547cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5548effad8dfSPaul Moore 				       u16 family)
55491da177e4SLinus Torvalds {
5550dfaebe98SPaul Moore 	int err;
5551effad8dfSPaul Moore 	char *addrp;
5552effad8dfSPaul Moore 	u32 peer_sid;
55532bf49690SThomas Liu 	struct common_audit_data ad;
555448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5555effad8dfSPaul Moore 	u8 secmark_active;
5556948bf85cSPaul Moore 	u8 netlbl_active;
5557effad8dfSPaul Moore 	u8 peerlbl_active;
55584237c75cSVenkat Yekkirala 
5559aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5560effad8dfSPaul Moore 		return NF_ACCEPT;
55614237c75cSVenkat Yekkirala 
5562effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5563948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
55642be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5565effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5566effad8dfSPaul Moore 		return NF_ACCEPT;
55674237c75cSVenkat Yekkirala 
5568d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5569d8395c87SPaul Moore 		return NF_DROP;
5570d8395c87SPaul Moore 
557150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
557248c62af6SEric Paris 	ad.u.net = &net;
5573cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
557448c62af6SEric Paris 	ad.u.net->family = family;
5575effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5576effad8dfSPaul Moore 		return NF_DROP;
55771da177e4SLinus Torvalds 
5578dfaebe98SPaul Moore 	if (peerlbl_active) {
5579cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5580cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5581dfaebe98SPaul Moore 		if (err) {
5582a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5583effad8dfSPaul Moore 			return NF_DROP;
5584dfaebe98SPaul Moore 		}
5585dfaebe98SPaul Moore 	}
5586effad8dfSPaul Moore 
5587effad8dfSPaul Moore 	if (secmark_active)
55886b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
55896b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5590effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5591effad8dfSPaul Moore 			return NF_DROP;
5592effad8dfSPaul Moore 
5593948bf85cSPaul Moore 	if (netlbl_active)
5594948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5595948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5596948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5597948bf85cSPaul Moore 		 * protection */
5598948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5599948bf85cSPaul Moore 			return NF_DROP;
5600948bf85cSPaul Moore 
5601effad8dfSPaul Moore 	return NF_ACCEPT;
5602effad8dfSPaul Moore }
5603effad8dfSPaul Moore 
560406198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5605effad8dfSPaul Moore 					 struct sk_buff *skb,
5606238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5607effad8dfSPaul Moore {
5608238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5609effad8dfSPaul Moore }
5610effad8dfSPaul Moore 
56111a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
561206198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5613effad8dfSPaul Moore 					 struct sk_buff *skb,
5614238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5615effad8dfSPaul Moore {
5616238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5617effad8dfSPaul Moore }
5618effad8dfSPaul Moore #endif	/* IPV6 */
5619effad8dfSPaul Moore 
5620948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5621948bf85cSPaul Moore 				      u16 family)
5622948bf85cSPaul Moore {
562347180068SPaul Moore 	struct sock *sk;
5624948bf85cSPaul Moore 	u32 sid;
5625948bf85cSPaul Moore 
5626948bf85cSPaul Moore 	if (!netlbl_enabled())
5627948bf85cSPaul Moore 		return NF_ACCEPT;
5628948bf85cSPaul Moore 
5629948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5630948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5631948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
563247180068SPaul Moore 	sk = skb->sk;
563347180068SPaul Moore 	if (sk) {
563447180068SPaul Moore 		struct sk_security_struct *sksec;
563547180068SPaul Moore 
5636e446f9dfSEric Dumazet 		if (sk_listener(sk))
563747180068SPaul Moore 			/* if the socket is the listening state then this
563847180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
563947180068SPaul Moore 			 * be labeled based on the connection/request_sock and
564047180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
564147180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
564247180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
564347180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
564447180068SPaul Moore 			 * as any IP option based labeling should be copied
564547180068SPaul Moore 			 * from the initial connection request (in the IP
564647180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
564747180068SPaul Moore 			 * security label in the packet itself this is the
564847180068SPaul Moore 			 * best we can do. */
564947180068SPaul Moore 			return NF_ACCEPT;
565047180068SPaul Moore 
565147180068SPaul Moore 		/* standard practice, label using the parent socket */
565247180068SPaul Moore 		sksec = sk->sk_security;
5653948bf85cSPaul Moore 		sid = sksec->sid;
5654948bf85cSPaul Moore 	} else
5655948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5656948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5657948bf85cSPaul Moore 		return NF_DROP;
5658948bf85cSPaul Moore 
5659948bf85cSPaul Moore 	return NF_ACCEPT;
5660948bf85cSPaul Moore }
5661948bf85cSPaul Moore 
566206198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5663948bf85cSPaul Moore 					struct sk_buff *skb,
5664238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5665948bf85cSPaul Moore {
5666948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5667948bf85cSPaul Moore }
5668948bf85cSPaul Moore 
56691a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
56702917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
56712917f57bSHuw Davies 					struct sk_buff *skb,
56722917f57bSHuw Davies 					const struct nf_hook_state *state)
56732917f57bSHuw Davies {
56742917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
56752917f57bSHuw Davies }
56762917f57bSHuw Davies #endif	/* IPV6 */
56772917f57bSHuw Davies 
5678effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5679effad8dfSPaul Moore 						int ifindex,
5680d8395c87SPaul Moore 						u16 family)
56814e5ab4cbSJames Morris {
568254abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
56834237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
56842bf49690SThomas Liu 	struct common_audit_data ad;
568548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5686d8395c87SPaul Moore 	char *addrp;
5687d8395c87SPaul Moore 	u8 proto;
56884e5ab4cbSJames Morris 
5689effad8dfSPaul Moore 	if (sk == NULL)
5690effad8dfSPaul Moore 		return NF_ACCEPT;
56914237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
56924e5ab4cbSJames Morris 
569350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
569448c62af6SEric Paris 	ad.u.net = &net;
569548c62af6SEric Paris 	ad.u.net->netif = ifindex;
569648c62af6SEric Paris 	ad.u.net->family = family;
5697d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5698d8395c87SPaul Moore 		return NF_DROP;
5699d8395c87SPaul Moore 
570058bfbb51SPaul Moore 	if (selinux_secmark_enabled())
57016b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57026b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5703d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
57042fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
57051da177e4SLinus Torvalds 
5706d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
57072fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5708effad8dfSPaul Moore 
5709effad8dfSPaul Moore 	return NF_ACCEPT;
5710effad8dfSPaul Moore }
5711effad8dfSPaul Moore 
5712cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5713cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5714effad8dfSPaul Moore 					 u16 family)
5715effad8dfSPaul Moore {
5716effad8dfSPaul Moore 	u32 secmark_perm;
5717effad8dfSPaul Moore 	u32 peer_sid;
5718cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5719effad8dfSPaul Moore 	struct sock *sk;
57202bf49690SThomas Liu 	struct common_audit_data ad;
572148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5722effad8dfSPaul Moore 	char *addrp;
5723effad8dfSPaul Moore 	u8 secmark_active;
5724effad8dfSPaul Moore 	u8 peerlbl_active;
5725effad8dfSPaul Moore 
5726effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5727effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5728effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5729effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5730aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5731d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5732c0828e50SPaul Moore 
5733effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
57342be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5735effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5736effad8dfSPaul Moore 		return NF_ACCEPT;
5737effad8dfSPaul Moore 
573854abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5739c0828e50SPaul Moore 
5740effad8dfSPaul Moore #ifdef CONFIG_XFRM
5741effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5742effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5743effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5744effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5745effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5746c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5747c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5748c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5749c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5750c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5751c0828e50SPaul Moore 	 *       connection. */
5752c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5753e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5754effad8dfSPaul Moore 		return NF_ACCEPT;
5755effad8dfSPaul Moore #endif
5756effad8dfSPaul Moore 
5757d8395c87SPaul Moore 	if (sk == NULL) {
5758446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5759446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5760446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5761446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
57624a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5763d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5764d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
576504f6d70fSEric Paris 				return NF_DROP;
57664a7ab3dcSSteffen Klassert 		} else {
57674a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5768d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
57694a7ab3dcSSteffen Klassert 		}
5770e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5771446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5772446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5773446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5774446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5775446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5776446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5777446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5778446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5779446b8024SPaul Moore 		 * for similar problems. */
5780446b8024SPaul Moore 		u32 skb_sid;
5781e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5782e446f9dfSEric Dumazet 
5783e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5784446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5785446b8024SPaul Moore 			return NF_DROP;
5786c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5787c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5788c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5789c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5790c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5791c0828e50SPaul Moore 		 * pass the packet. */
5792c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5793c0828e50SPaul Moore 			switch (family) {
5794c0828e50SPaul Moore 			case PF_INET:
5795c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5796c0828e50SPaul Moore 					return NF_ACCEPT;
5797c0828e50SPaul Moore 				break;
5798c0828e50SPaul Moore 			case PF_INET6:
5799c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5800c0828e50SPaul Moore 					return NF_ACCEPT;
5801a7a91a19SPaul Moore 				break;
5802c0828e50SPaul Moore 			default:
5803c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5804c0828e50SPaul Moore 			}
5805c0828e50SPaul Moore 		}
5806446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5807446b8024SPaul Moore 			return NF_DROP;
5808446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5809d8395c87SPaul Moore 	} else {
5810446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5811446b8024SPaul Moore 		 * associated socket. */
5812effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5813effad8dfSPaul Moore 		peer_sid = sksec->sid;
5814effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5815effad8dfSPaul Moore 	}
5816effad8dfSPaul Moore 
581750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
581848c62af6SEric Paris 	ad.u.net = &net;
581948c62af6SEric Paris 	ad.u.net->netif = ifindex;
582048c62af6SEric Paris 	ad.u.net->family = family;
5821d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
582204f6d70fSEric Paris 		return NF_DROP;
5823d8395c87SPaul Moore 
5824effad8dfSPaul Moore 	if (secmark_active)
58256b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58266b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5827effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
58281f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5829effad8dfSPaul Moore 
5830effad8dfSPaul Moore 	if (peerlbl_active) {
5831effad8dfSPaul Moore 		u32 if_sid;
5832effad8dfSPaul Moore 		u32 node_sid;
5833effad8dfSPaul Moore 
5834cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
583504f6d70fSEric Paris 			return NF_DROP;
58366b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58376b6bc620SStephen Smalley 				 peer_sid, if_sid,
5838effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
58391f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5840effad8dfSPaul Moore 
5841effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
584204f6d70fSEric Paris 			return NF_DROP;
58436b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58446b6bc620SStephen Smalley 				 peer_sid, node_sid,
5845effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
58461f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5847effad8dfSPaul Moore 	}
5848effad8dfSPaul Moore 
5849effad8dfSPaul Moore 	return NF_ACCEPT;
5850effad8dfSPaul Moore }
5851effad8dfSPaul Moore 
585206198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5853a224be76SDavid S. Miller 					   struct sk_buff *skb,
5854238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
58551da177e4SLinus Torvalds {
5856238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
58571da177e4SLinus Torvalds }
58581da177e4SLinus Torvalds 
58591a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
586006198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5861a224be76SDavid S. Miller 					   struct sk_buff *skb,
5862238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
58631da177e4SLinus Torvalds {
5864238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
58651da177e4SLinus Torvalds }
58661da177e4SLinus Torvalds #endif	/* IPV6 */
58671da177e4SLinus Torvalds 
58681da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
58691da177e4SLinus Torvalds 
58701da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
58711da177e4SLinus Torvalds {
5872941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
58731da177e4SLinus Torvalds }
58741da177e4SLinus Torvalds 
5875be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm,
58761da177e4SLinus Torvalds 			      u16 sclass)
58771da177e4SLinus Torvalds {
58781da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58791da177e4SLinus Torvalds 
588089d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
58811da177e4SLinus Torvalds 	if (!isec)
58821da177e4SLinus Torvalds 		return -ENOMEM;
58831da177e4SLinus Torvalds 
58841da177e4SLinus Torvalds 	isec->sclass = sclass;
5885be0554c9SStephen Smalley 	isec->sid = current_sid();
58861da177e4SLinus Torvalds 	perm->security = isec;
58871da177e4SLinus Torvalds 
58881da177e4SLinus Torvalds 	return 0;
58891da177e4SLinus Torvalds }
58901da177e4SLinus Torvalds 
58911da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
58921da177e4SLinus Torvalds {
58931da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
58941da177e4SLinus Torvalds 	perm->security = NULL;
58951da177e4SLinus Torvalds 	kfree(isec);
58961da177e4SLinus Torvalds }
58971da177e4SLinus Torvalds 
58981da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
58991da177e4SLinus Torvalds {
59001da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59011da177e4SLinus Torvalds 
590289d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
59031da177e4SLinus Torvalds 	if (!msec)
59041da177e4SLinus Torvalds 		return -ENOMEM;
59051da177e4SLinus Torvalds 
59061da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
59071da177e4SLinus Torvalds 	msg->security = msec;
59081da177e4SLinus Torvalds 
59091da177e4SLinus Torvalds 	return 0;
59101da177e4SLinus Torvalds }
59111da177e4SLinus Torvalds 
59121da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
59131da177e4SLinus Torvalds {
59141da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
59151da177e4SLinus Torvalds 
59161da177e4SLinus Torvalds 	msg->security = NULL;
59171da177e4SLinus Torvalds 	kfree(msec);
59181da177e4SLinus Torvalds }
59191da177e4SLinus Torvalds 
59201da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
59216af963f1SStephen Smalley 			u32 perms)
59221da177e4SLinus Torvalds {
59231da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59242bf49690SThomas Liu 	struct common_audit_data ad;
5925275bb41eSDavid Howells 	u32 sid = current_sid();
59261da177e4SLinus Torvalds 
59271da177e4SLinus Torvalds 	isec = ipc_perms->security;
59281da177e4SLinus Torvalds 
592950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59301da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
59311da177e4SLinus Torvalds 
59326b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59336b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
59341da177e4SLinus Torvalds }
59351da177e4SLinus Torvalds 
59361da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
59371da177e4SLinus Torvalds {
59381da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
59391da177e4SLinus Torvalds }
59401da177e4SLinus Torvalds 
59411da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
59421da177e4SLinus Torvalds {
59431da177e4SLinus Torvalds 	msg_msg_free_security(msg);
59441da177e4SLinus Torvalds }
59451da177e4SLinus Torvalds 
59461da177e4SLinus Torvalds /* message queue security operations */
5947d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
59481da177e4SLinus Torvalds {
59491da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59502bf49690SThomas Liu 	struct common_audit_data ad;
5951275bb41eSDavid Howells 	u32 sid = current_sid();
59521da177e4SLinus Torvalds 	int rc;
59531da177e4SLinus Torvalds 
5954d8c6e854SEric W. Biederman 	rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
59551da177e4SLinus Torvalds 	if (rc)
59561da177e4SLinus Torvalds 		return rc;
59571da177e4SLinus Torvalds 
5958d8c6e854SEric W. Biederman 	isec = msq->security;
59591da177e4SLinus Torvalds 
596050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5961d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59621da177e4SLinus Torvalds 
59636b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59646b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
59651da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
59661da177e4SLinus Torvalds 	if (rc) {
5967d8c6e854SEric W. Biederman 		ipc_free_security(msq);
59681da177e4SLinus Torvalds 		return rc;
59691da177e4SLinus Torvalds 	}
59701da177e4SLinus Torvalds 	return 0;
59711da177e4SLinus Torvalds }
59721da177e4SLinus Torvalds 
5973d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
59741da177e4SLinus Torvalds {
5975d8c6e854SEric W. Biederman 	ipc_free_security(msq);
59761da177e4SLinus Torvalds }
59771da177e4SLinus Torvalds 
5978d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
59791da177e4SLinus Torvalds {
59801da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59812bf49690SThomas Liu 	struct common_audit_data ad;
5982275bb41eSDavid Howells 	u32 sid = current_sid();
59831da177e4SLinus Torvalds 
5984d8c6e854SEric W. Biederman 	isec = msq->security;
59851da177e4SLinus Torvalds 
598650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5987d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59881da177e4SLinus Torvalds 
59896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59906b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
59911da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
59921da177e4SLinus Torvalds }
59931da177e4SLinus Torvalds 
5994d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
59951da177e4SLinus Torvalds {
59961da177e4SLinus Torvalds 	int err;
59971da177e4SLinus Torvalds 	int perms;
59981da177e4SLinus Torvalds 
59991da177e4SLinus Torvalds 	switch (cmd) {
60001da177e4SLinus Torvalds 	case IPC_INFO:
60011da177e4SLinus Torvalds 	case MSG_INFO:
60021da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60036b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60046b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6005be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60061da177e4SLinus Torvalds 	case IPC_STAT:
60071da177e4SLinus Torvalds 	case MSG_STAT:
600823c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
60091da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
60101da177e4SLinus Torvalds 		break;
60111da177e4SLinus Torvalds 	case IPC_SET:
60121da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
60131da177e4SLinus Torvalds 		break;
60141da177e4SLinus Torvalds 	case IPC_RMID:
60151da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
60161da177e4SLinus Torvalds 		break;
60171da177e4SLinus Torvalds 	default:
60181da177e4SLinus Torvalds 		return 0;
60191da177e4SLinus Torvalds 	}
60201da177e4SLinus Torvalds 
6021d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
60221da177e4SLinus Torvalds 	return err;
60231da177e4SLinus Torvalds }
60241da177e4SLinus Torvalds 
6025d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
60261da177e4SLinus Torvalds {
60271da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60281da177e4SLinus Torvalds 	struct msg_security_struct *msec;
60292bf49690SThomas Liu 	struct common_audit_data ad;
6030275bb41eSDavid Howells 	u32 sid = current_sid();
60311da177e4SLinus Torvalds 	int rc;
60321da177e4SLinus Torvalds 
6033d8c6e854SEric W. Biederman 	isec = msq->security;
60341da177e4SLinus Torvalds 	msec = msg->security;
60351da177e4SLinus Torvalds 
60361da177e4SLinus Torvalds 	/*
60371da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
60381da177e4SLinus Torvalds 	 */
60391da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
60401da177e4SLinus Torvalds 		/*
60411da177e4SLinus Torvalds 		 * Compute new sid based on current process and
60421da177e4SLinus Torvalds 		 * message queue this message will be stored in
60431da177e4SLinus Torvalds 		 */
6044aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
6045aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
60461da177e4SLinus Torvalds 		if (rc)
60471da177e4SLinus Torvalds 			return rc;
60481da177e4SLinus Torvalds 	}
60491da177e4SLinus Torvalds 
605050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6051d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60521da177e4SLinus Torvalds 
60531da177e4SLinus Torvalds 	/* Can this process write to the queue? */
60546b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60556b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
60561da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
60571da177e4SLinus Torvalds 	if (!rc)
60581da177e4SLinus Torvalds 		/* Can this process send the message */
60596b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
60606b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
6061275bb41eSDavid Howells 				  MSG__SEND, &ad);
60621da177e4SLinus Torvalds 	if (!rc)
60631da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
60646b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
60656b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
6066275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
60671da177e4SLinus Torvalds 
60681da177e4SLinus Torvalds 	return rc;
60691da177e4SLinus Torvalds }
60701da177e4SLinus Torvalds 
6071d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
60721da177e4SLinus Torvalds 				    struct task_struct *target,
60731da177e4SLinus Torvalds 				    long type, int mode)
60741da177e4SLinus Torvalds {
60751da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60761da177e4SLinus Torvalds 	struct msg_security_struct *msec;
60772bf49690SThomas Liu 	struct common_audit_data ad;
6078275bb41eSDavid Howells 	u32 sid = task_sid(target);
60791da177e4SLinus Torvalds 	int rc;
60801da177e4SLinus Torvalds 
6081d8c6e854SEric W. Biederman 	isec = msq->security;
60821da177e4SLinus Torvalds 	msec = msg->security;
60831da177e4SLinus Torvalds 
608450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6085d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60861da177e4SLinus Torvalds 
60876b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60886b6bc620SStephen Smalley 			  sid, isec->sid,
60891da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
60901da177e4SLinus Torvalds 	if (!rc)
60916b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
60926b6bc620SStephen Smalley 				  sid, msec->sid,
60931da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
60941da177e4SLinus Torvalds 	return rc;
60951da177e4SLinus Torvalds }
60961da177e4SLinus Torvalds 
60971da177e4SLinus Torvalds /* Shared Memory security operations */
60987191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
60991da177e4SLinus Torvalds {
61001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61012bf49690SThomas Liu 	struct common_audit_data ad;
6102275bb41eSDavid Howells 	u32 sid = current_sid();
61031da177e4SLinus Torvalds 	int rc;
61041da177e4SLinus Torvalds 
61057191adffSEric W. Biederman 	rc = ipc_alloc_security(shp, SECCLASS_SHM);
61061da177e4SLinus Torvalds 	if (rc)
61071da177e4SLinus Torvalds 		return rc;
61081da177e4SLinus Torvalds 
61097191adffSEric W. Biederman 	isec = shp->security;
61101da177e4SLinus Torvalds 
611150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61127191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61131da177e4SLinus Torvalds 
61146b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61156b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
61161da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
61171da177e4SLinus Torvalds 	if (rc) {
61187191adffSEric W. Biederman 		ipc_free_security(shp);
61191da177e4SLinus Torvalds 		return rc;
61201da177e4SLinus Torvalds 	}
61211da177e4SLinus Torvalds 	return 0;
61221da177e4SLinus Torvalds }
61231da177e4SLinus Torvalds 
61247191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp)
61251da177e4SLinus Torvalds {
61267191adffSEric W. Biederman 	ipc_free_security(shp);
61271da177e4SLinus Torvalds }
61281da177e4SLinus Torvalds 
61297191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
61301da177e4SLinus Torvalds {
61311da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61322bf49690SThomas Liu 	struct common_audit_data ad;
6133275bb41eSDavid Howells 	u32 sid = current_sid();
61341da177e4SLinus Torvalds 
61357191adffSEric W. Biederman 	isec = shp->security;
61361da177e4SLinus Torvalds 
613750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61387191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61391da177e4SLinus Torvalds 
61406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
61416b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
61421da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
61431da177e4SLinus Torvalds }
61441da177e4SLinus Torvalds 
61451da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
61467191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
61471da177e4SLinus Torvalds {
61481da177e4SLinus Torvalds 	int perms;
61491da177e4SLinus Torvalds 	int err;
61501da177e4SLinus Torvalds 
61511da177e4SLinus Torvalds 	switch (cmd) {
61521da177e4SLinus Torvalds 	case IPC_INFO:
61531da177e4SLinus Torvalds 	case SHM_INFO:
61541da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
61556b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
61566b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6157be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
61581da177e4SLinus Torvalds 	case IPC_STAT:
61591da177e4SLinus Torvalds 	case SHM_STAT:
6160c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
61611da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
61621da177e4SLinus Torvalds 		break;
61631da177e4SLinus Torvalds 	case IPC_SET:
61641da177e4SLinus Torvalds 		perms = SHM__SETATTR;
61651da177e4SLinus Torvalds 		break;
61661da177e4SLinus Torvalds 	case SHM_LOCK:
61671da177e4SLinus Torvalds 	case SHM_UNLOCK:
61681da177e4SLinus Torvalds 		perms = SHM__LOCK;
61691da177e4SLinus Torvalds 		break;
61701da177e4SLinus Torvalds 	case IPC_RMID:
61711da177e4SLinus Torvalds 		perms = SHM__DESTROY;
61721da177e4SLinus Torvalds 		break;
61731da177e4SLinus Torvalds 	default:
61741da177e4SLinus Torvalds 		return 0;
61751da177e4SLinus Torvalds 	}
61761da177e4SLinus Torvalds 
61777191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
61781da177e4SLinus Torvalds 	return err;
61791da177e4SLinus Torvalds }
61801da177e4SLinus Torvalds 
61817191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
61821da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
61831da177e4SLinus Torvalds {
61841da177e4SLinus Torvalds 	u32 perms;
61851da177e4SLinus Torvalds 
61861da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
61871da177e4SLinus Torvalds 		perms = SHM__READ;
61881da177e4SLinus Torvalds 	else
61891da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
61901da177e4SLinus Torvalds 
61917191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
61921da177e4SLinus Torvalds }
61931da177e4SLinus Torvalds 
61941da177e4SLinus Torvalds /* Semaphore security operations */
6195aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
61961da177e4SLinus Torvalds {
61971da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61982bf49690SThomas Liu 	struct common_audit_data ad;
6199275bb41eSDavid Howells 	u32 sid = current_sid();
62001da177e4SLinus Torvalds 	int rc;
62011da177e4SLinus Torvalds 
6202aefad959SEric W. Biederman 	rc = ipc_alloc_security(sma, SECCLASS_SEM);
62031da177e4SLinus Torvalds 	if (rc)
62041da177e4SLinus Torvalds 		return rc;
62051da177e4SLinus Torvalds 
6206aefad959SEric W. Biederman 	isec = sma->security;
62071da177e4SLinus Torvalds 
620850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6209aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62101da177e4SLinus Torvalds 
62116b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
62126b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
62131da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
62141da177e4SLinus Torvalds 	if (rc) {
6215aefad959SEric W. Biederman 		ipc_free_security(sma);
62161da177e4SLinus Torvalds 		return rc;
62171da177e4SLinus Torvalds 	}
62181da177e4SLinus Torvalds 	return 0;
62191da177e4SLinus Torvalds }
62201da177e4SLinus Torvalds 
6221aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma)
62221da177e4SLinus Torvalds {
6223aefad959SEric W. Biederman 	ipc_free_security(sma);
62241da177e4SLinus Torvalds }
62251da177e4SLinus Torvalds 
6226aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
62271da177e4SLinus Torvalds {
62281da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62292bf49690SThomas Liu 	struct common_audit_data ad;
6230275bb41eSDavid Howells 	u32 sid = current_sid();
62311da177e4SLinus Torvalds 
6232aefad959SEric W. Biederman 	isec = sma->security;
62331da177e4SLinus Torvalds 
623450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6235aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62361da177e4SLinus Torvalds 
62376b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
62386b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
62391da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
62401da177e4SLinus Torvalds }
62411da177e4SLinus Torvalds 
62421da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6243aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
62441da177e4SLinus Torvalds {
62451da177e4SLinus Torvalds 	int err;
62461da177e4SLinus Torvalds 	u32 perms;
62471da177e4SLinus Torvalds 
62481da177e4SLinus Torvalds 	switch (cmd) {
62491da177e4SLinus Torvalds 	case IPC_INFO:
62501da177e4SLinus Torvalds 	case SEM_INFO:
62511da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
62526b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
62536b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6254be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
62551da177e4SLinus Torvalds 	case GETPID:
62561da177e4SLinus Torvalds 	case GETNCNT:
62571da177e4SLinus Torvalds 	case GETZCNT:
62581da177e4SLinus Torvalds 		perms = SEM__GETATTR;
62591da177e4SLinus Torvalds 		break;
62601da177e4SLinus Torvalds 	case GETVAL:
62611da177e4SLinus Torvalds 	case GETALL:
62621da177e4SLinus Torvalds 		perms = SEM__READ;
62631da177e4SLinus Torvalds 		break;
62641da177e4SLinus Torvalds 	case SETVAL:
62651da177e4SLinus Torvalds 	case SETALL:
62661da177e4SLinus Torvalds 		perms = SEM__WRITE;
62671da177e4SLinus Torvalds 		break;
62681da177e4SLinus Torvalds 	case IPC_RMID:
62691da177e4SLinus Torvalds 		perms = SEM__DESTROY;
62701da177e4SLinus Torvalds 		break;
62711da177e4SLinus Torvalds 	case IPC_SET:
62721da177e4SLinus Torvalds 		perms = SEM__SETATTR;
62731da177e4SLinus Torvalds 		break;
62741da177e4SLinus Torvalds 	case IPC_STAT:
62751da177e4SLinus Torvalds 	case SEM_STAT:
6276a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
62771da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
62781da177e4SLinus Torvalds 		break;
62791da177e4SLinus Torvalds 	default:
62801da177e4SLinus Torvalds 		return 0;
62811da177e4SLinus Torvalds 	}
62821da177e4SLinus Torvalds 
6283aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
62841da177e4SLinus Torvalds 	return err;
62851da177e4SLinus Torvalds }
62861da177e4SLinus Torvalds 
6287aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
62881da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
62891da177e4SLinus Torvalds {
62901da177e4SLinus Torvalds 	u32 perms;
62911da177e4SLinus Torvalds 
62921da177e4SLinus Torvalds 	if (alter)
62931da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
62941da177e4SLinus Torvalds 	else
62951da177e4SLinus Torvalds 		perms = SEM__READ;
62961da177e4SLinus Torvalds 
6297aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
62981da177e4SLinus Torvalds }
62991da177e4SLinus Torvalds 
63001da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
63011da177e4SLinus Torvalds {
63021da177e4SLinus Torvalds 	u32 av = 0;
63031da177e4SLinus Torvalds 
63041da177e4SLinus Torvalds 	av = 0;
63051da177e4SLinus Torvalds 	if (flag & S_IRUGO)
63061da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
63071da177e4SLinus Torvalds 	if (flag & S_IWUGO)
63081da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
63091da177e4SLinus Torvalds 
63101da177e4SLinus Torvalds 	if (av == 0)
63111da177e4SLinus Torvalds 		return 0;
63121da177e4SLinus Torvalds 
63136af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
63141da177e4SLinus Torvalds }
63151da177e4SLinus Torvalds 
6316713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6317713a04aeSAhmed S. Darwish {
6318713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
6319713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6320713a04aeSAhmed S. Darwish }
6321713a04aeSAhmed S. Darwish 
63221da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
63231da177e4SLinus Torvalds {
63241da177e4SLinus Torvalds 	if (inode)
63251da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
63261da177e4SLinus Torvalds }
63271da177e4SLinus Torvalds 
63281da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
632904ff9708SAl Viro 			       char *name, char **value)
63301da177e4SLinus Torvalds {
6331275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
63328c8570fbSDustin Kirkland 	u32 sid;
63331da177e4SLinus Torvalds 	int error;
633404ff9708SAl Viro 	unsigned len;
63351da177e4SLinus Torvalds 
6336275bb41eSDavid Howells 	rcu_read_lock();
6337275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
63381da177e4SLinus Torvalds 
6339be0554c9SStephen Smalley 	if (current != p) {
63406b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63416b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6342be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6343be0554c9SStephen Smalley 		if (error)
6344be0554c9SStephen Smalley 			goto bad;
6345be0554c9SStephen Smalley 	}
6346be0554c9SStephen Smalley 
63471da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6348275bb41eSDavid Howells 		sid = __tsec->sid;
63491da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6350275bb41eSDavid Howells 		sid = __tsec->osid;
63511da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6352275bb41eSDavid Howells 		sid = __tsec->exec_sid;
63531da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6354275bb41eSDavid Howells 		sid = __tsec->create_sid;
63554eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6356275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
635742c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6358275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6359be0554c9SStephen Smalley 	else {
6360be0554c9SStephen Smalley 		error = -EINVAL;
6361be0554c9SStephen Smalley 		goto bad;
6362be0554c9SStephen Smalley 	}
6363275bb41eSDavid Howells 	rcu_read_unlock();
63641da177e4SLinus Torvalds 
63651da177e4SLinus Torvalds 	if (!sid)
63661da177e4SLinus Torvalds 		return 0;
63671da177e4SLinus Torvalds 
6368aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
636904ff9708SAl Viro 	if (error)
637004ff9708SAl Viro 		return error;
637104ff9708SAl Viro 	return len;
6372275bb41eSDavid Howells 
6373be0554c9SStephen Smalley bad:
6374275bb41eSDavid Howells 	rcu_read_unlock();
6375be0554c9SStephen Smalley 	return error;
63761da177e4SLinus Torvalds }
63771da177e4SLinus Torvalds 
6378b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
63791da177e4SLinus Torvalds {
63801da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6381d84f4f99SDavid Howells 	struct cred *new;
6382be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
63831da177e4SLinus Torvalds 	int error;
63841da177e4SLinus Torvalds 	char *str = value;
63851da177e4SLinus Torvalds 
63861da177e4SLinus Torvalds 	/*
63871da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
63881da177e4SLinus Torvalds 	 */
63891da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
63906b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63916b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6392be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
63931da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
63946b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63956b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6396be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
63974eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
63986b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63996b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6400be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
640142c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
64026b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64036b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6404be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
64051da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
64066b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64076b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6408be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
64091da177e4SLinus Torvalds 	else
64101da177e4SLinus Torvalds 		error = -EINVAL;
64111da177e4SLinus Torvalds 	if (error)
64121da177e4SLinus Torvalds 		return error;
64131da177e4SLinus Torvalds 
64141da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6415a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
64161da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
64171da177e4SLinus Torvalds 			str[size-1] = 0;
64181da177e4SLinus Torvalds 			size--;
64191da177e4SLinus Torvalds 		}
6420aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6421aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
642212b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6423db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6424d6ea83ecSEric Paris 				struct audit_buffer *ab;
6425d6ea83ecSEric Paris 				size_t audit_size;
6426d6ea83ecSEric Paris 
6427d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6428d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6429d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6430d6ea83ecSEric Paris 					audit_size = size - 1;
6431d6ea83ecSEric Paris 				else
6432d6ea83ecSEric Paris 					audit_size = size;
6433d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
6434d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6435d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6436d6ea83ecSEric Paris 				audit_log_end(ab);
6437d6ea83ecSEric Paris 
643812b29f34SStephen Smalley 				return error;
6439d6ea83ecSEric Paris 			}
6440aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6441aa8e712cSStephen Smalley 						      &selinux_state,
6442aa8e712cSStephen Smalley 						      value, size, &sid);
644312b29f34SStephen Smalley 		}
64441da177e4SLinus Torvalds 		if (error)
64451da177e4SLinus Torvalds 			return error;
64461da177e4SLinus Torvalds 	}
64471da177e4SLinus Torvalds 
6448d84f4f99SDavid Howells 	new = prepare_creds();
6449d84f4f99SDavid Howells 	if (!new)
6450d84f4f99SDavid Howells 		return -ENOMEM;
6451d84f4f99SDavid Howells 
64521da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
64531da177e4SLinus Torvalds 	   performed during the actual operation (execve,
64541da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6455d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
64561da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
64571da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
6458d84f4f99SDavid Howells 	tsec = new->security;
6459d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
64601da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6461d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
64621da177e4SLinus Torvalds 		tsec->create_sid = sid;
6463d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
64646b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64656b6bc620SStephen Smalley 				     mysid, sid, SECCLASS_KEY, KEY__CREATE,
6466be0554c9SStephen Smalley 				     NULL);
64674eb582cfSMichael LeMay 		if (error)
6468d84f4f99SDavid Howells 			goto abort_change;
64694eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6470d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
647142c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6472d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6473d84f4f99SDavid Howells 		error = -EINVAL;
64741da177e4SLinus Torvalds 		if (sid == 0)
6475d84f4f99SDavid Howells 			goto abort_change;
6476d9250deaSKaiGai Kohei 
6477d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6478d84f4f99SDavid Howells 		error = -EPERM;
64795bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6480aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6481aa8e712cSStephen Smalley 							    tsec->sid, sid);
6482d84f4f99SDavid Howells 			if (error)
6483d84f4f99SDavid Howells 				goto abort_change;
64841da177e4SLinus Torvalds 		}
64851da177e4SLinus Torvalds 
64861da177e4SLinus Torvalds 		/* Check permissions for the transition. */
64876b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64886b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
64891da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
64901da177e4SLinus Torvalds 		if (error)
6491d84f4f99SDavid Howells 			goto abort_change;
64921da177e4SLinus Torvalds 
64931da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
64941da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6495be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
64960c6181cbSPaul Moore 		if (ptsid != 0) {
64976b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
64986b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6499d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6500d84f4f99SDavid Howells 			if (error)
6501d84f4f99SDavid Howells 				goto abort_change;
6502d84f4f99SDavid Howells 		}
6503d84f4f99SDavid Howells 
6504d84f4f99SDavid Howells 		tsec->sid = sid;
6505d84f4f99SDavid Howells 	} else {
6506d84f4f99SDavid Howells 		error = -EINVAL;
6507d84f4f99SDavid Howells 		goto abort_change;
6508d84f4f99SDavid Howells 	}
6509d84f4f99SDavid Howells 
6510d84f4f99SDavid Howells 	commit_creds(new);
65111da177e4SLinus Torvalds 	return size;
6512d84f4f99SDavid Howells 
6513d84f4f99SDavid Howells abort_change:
6514d84f4f99SDavid Howells 	abort_creds(new);
6515d84f4f99SDavid Howells 	return error;
65161da177e4SLinus Torvalds }
65171da177e4SLinus Torvalds 
6518746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6519746df9b5SDavid Quigley {
6520746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6521746df9b5SDavid Quigley }
6522746df9b5SDavid Quigley 
6523dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6524dc49c1f9SCatherine Zhang {
6525aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6526aa8e712cSStephen Smalley 				       secdata, seclen);
6527dc49c1f9SCatherine Zhang }
6528dc49c1f9SCatherine Zhang 
65297bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
653063cb3449SDavid Howells {
6531aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6532aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
653363cb3449SDavid Howells }
653463cb3449SDavid Howells 
6535dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6536dc49c1f9SCatherine Zhang {
6537dc49c1f9SCatherine Zhang 	kfree(secdata);
6538dc49c1f9SCatherine Zhang }
6539dc49c1f9SCatherine Zhang 
65406f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
65416f3be9f5SAndreas Gruenbacher {
65426f3be9f5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
65436f3be9f5SAndreas Gruenbacher 
65449287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
65456f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
65469287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
65476f3be9f5SAndreas Gruenbacher }
65486f3be9f5SAndreas Gruenbacher 
65491ee65e37SDavid P. Quigley /*
65501ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
65511ee65e37SDavid P. Quigley  */
65521ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
65531ee65e37SDavid P. Quigley {
65541ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
65551ee65e37SDavid P. Quigley }
65561ee65e37SDavid P. Quigley 
65571ee65e37SDavid P. Quigley /*
65581ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
65591ee65e37SDavid P. Quigley  */
65601ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
65611ee65e37SDavid P. Quigley {
65621ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
65631ee65e37SDavid P. Quigley }
65641ee65e37SDavid P. Quigley 
65651ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
65661ee65e37SDavid P. Quigley {
65671ee65e37SDavid P. Quigley 	int len = 0;
65681ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
65691ee65e37SDavid P. Quigley 						ctx, true);
65701ee65e37SDavid P. Quigley 	if (len < 0)
65711ee65e37SDavid P. Quigley 		return len;
65721ee65e37SDavid P. Quigley 	*ctxlen = len;
65731ee65e37SDavid P. Quigley 	return 0;
65741ee65e37SDavid P. Quigley }
6575d720024eSMichael LeMay #ifdef CONFIG_KEYS
6576d720024eSMichael LeMay 
6577d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
65787e047ef5SDavid Howells 			     unsigned long flags)
6579d720024eSMichael LeMay {
6580d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6581d720024eSMichael LeMay 	struct key_security_struct *ksec;
6582d720024eSMichael LeMay 
6583d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6584d720024eSMichael LeMay 	if (!ksec)
6585d720024eSMichael LeMay 		return -ENOMEM;
6586d720024eSMichael LeMay 
6587d84f4f99SDavid Howells 	tsec = cred->security;
6588d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6589d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
65904eb582cfSMichael LeMay 	else
6591d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6592d720024eSMichael LeMay 
6593275bb41eSDavid Howells 	k->security = ksec;
6594d720024eSMichael LeMay 	return 0;
6595d720024eSMichael LeMay }
6596d720024eSMichael LeMay 
6597d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6598d720024eSMichael LeMay {
6599d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6600d720024eSMichael LeMay 
6601d720024eSMichael LeMay 	k->security = NULL;
6602d720024eSMichael LeMay 	kfree(ksec);
6603d720024eSMichael LeMay }
6604d720024eSMichael LeMay 
6605d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6606d84f4f99SDavid Howells 				  const struct cred *cred,
6607f5895943SDavid Howells 				  unsigned perm)
6608d720024eSMichael LeMay {
6609d720024eSMichael LeMay 	struct key *key;
6610d720024eSMichael LeMay 	struct key_security_struct *ksec;
6611275bb41eSDavid Howells 	u32 sid;
6612d720024eSMichael LeMay 
6613d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
6614d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
6615d720024eSMichael LeMay 	   appear to be created. */
6616d720024eSMichael LeMay 	if (perm == 0)
6617d720024eSMichael LeMay 		return 0;
6618d720024eSMichael LeMay 
6619d84f4f99SDavid Howells 	sid = cred_sid(cred);
6620275bb41eSDavid Howells 
6621275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6622275bb41eSDavid Howells 	ksec = key->security;
6623275bb41eSDavid Howells 
66246b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66256b6bc620SStephen Smalley 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6626d720024eSMichael LeMay }
6627d720024eSMichael LeMay 
662870a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
662970a5bb72SDavid Howells {
663070a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
663170a5bb72SDavid Howells 	char *context = NULL;
663270a5bb72SDavid Howells 	unsigned len;
663370a5bb72SDavid Howells 	int rc;
663470a5bb72SDavid Howells 
6635aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6636aa8e712cSStephen Smalley 				     &context, &len);
663770a5bb72SDavid Howells 	if (!rc)
663870a5bb72SDavid Howells 		rc = len;
663970a5bb72SDavid Howells 	*_buffer = context;
664070a5bb72SDavid Howells 	return rc;
664170a5bb72SDavid Howells }
66423a976fa6SDaniel Jurgens #endif
664370a5bb72SDavid Howells 
66443a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6645cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6646cfc4d882SDaniel Jurgens {
6647cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6648cfc4d882SDaniel Jurgens 	int err;
6649cfc4d882SDaniel Jurgens 	u32 sid = 0;
6650cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6651cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6652cfc4d882SDaniel Jurgens 
6653409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6654cfc4d882SDaniel Jurgens 	if (err)
6655cfc4d882SDaniel Jurgens 		return err;
6656cfc4d882SDaniel Jurgens 
6657cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6658cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6659cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6660cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
66616b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66626b6bc620SStephen Smalley 			    sec->sid, sid,
6663cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6664cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6665cfc4d882SDaniel Jurgens }
6666cfc4d882SDaniel Jurgens 
6667ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6668ab861dfcSDaniel Jurgens 					    u8 port_num)
6669ab861dfcSDaniel Jurgens {
6670ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6671ab861dfcSDaniel Jurgens 	int err;
6672ab861dfcSDaniel Jurgens 	u32 sid = 0;
6673ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6674ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6675ab861dfcSDaniel Jurgens 
6676aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6677aa8e712cSStephen Smalley 				      &sid);
6678ab861dfcSDaniel Jurgens 
6679ab861dfcSDaniel Jurgens 	if (err)
6680ab861dfcSDaniel Jurgens 		return err;
6681ab861dfcSDaniel Jurgens 
6682ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6683ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6684ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6685ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
66866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66876b6bc620SStephen Smalley 			    sec->sid, sid,
6688ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6689ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6690ab861dfcSDaniel Jurgens }
6691ab861dfcSDaniel Jurgens 
66923a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
66933a976fa6SDaniel Jurgens {
66943a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
66953a976fa6SDaniel Jurgens 
66963a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
66973a976fa6SDaniel Jurgens 	if (!sec)
66983a976fa6SDaniel Jurgens 		return -ENOMEM;
66993a976fa6SDaniel Jurgens 	sec->sid = current_sid();
67003a976fa6SDaniel Jurgens 
67013a976fa6SDaniel Jurgens 	*ib_sec = sec;
67023a976fa6SDaniel Jurgens 	return 0;
67033a976fa6SDaniel Jurgens }
67043a976fa6SDaniel Jurgens 
67053a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
67063a976fa6SDaniel Jurgens {
67073a976fa6SDaniel Jurgens 	kfree(ib_sec);
67083a976fa6SDaniel Jurgens }
6709d720024eSMichael LeMay #endif
6710d720024eSMichael LeMay 
6711ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6712ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6713ec27c356SChenbo Feng 				     unsigned int size)
6714ec27c356SChenbo Feng {
6715ec27c356SChenbo Feng 	u32 sid = current_sid();
6716ec27c356SChenbo Feng 	int ret;
6717ec27c356SChenbo Feng 
6718ec27c356SChenbo Feng 	switch (cmd) {
6719ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
67206b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67216b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6722ec27c356SChenbo Feng 				   NULL);
6723ec27c356SChenbo Feng 		break;
6724ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
67256b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67266b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6727ec27c356SChenbo Feng 				   NULL);
6728ec27c356SChenbo Feng 		break;
6729ec27c356SChenbo Feng 	default:
6730ec27c356SChenbo Feng 		ret = 0;
6731ec27c356SChenbo Feng 		break;
6732ec27c356SChenbo Feng 	}
6733ec27c356SChenbo Feng 
6734ec27c356SChenbo Feng 	return ret;
6735ec27c356SChenbo Feng }
6736ec27c356SChenbo Feng 
6737ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6738ec27c356SChenbo Feng {
6739ec27c356SChenbo Feng 	u32 av = 0;
6740ec27c356SChenbo Feng 
6741ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6742ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6743ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6744ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6745ec27c356SChenbo Feng 	return av;
6746ec27c356SChenbo Feng }
6747ec27c356SChenbo Feng 
6748f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6749f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6750f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6751f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6752f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6753f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6754f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6755f66e448cSChenbo Feng  */
6756f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6757f66e448cSChenbo Feng {
6758f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6759f66e448cSChenbo Feng 	struct bpf_prog *prog;
6760f66e448cSChenbo Feng 	struct bpf_map *map;
6761f66e448cSChenbo Feng 	int ret;
6762f66e448cSChenbo Feng 
6763f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6764f66e448cSChenbo Feng 		map = file->private_data;
6765f66e448cSChenbo Feng 		bpfsec = map->security;
67666b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67676b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6768f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6769f66e448cSChenbo Feng 		if (ret)
6770f66e448cSChenbo Feng 			return ret;
6771f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6772f66e448cSChenbo Feng 		prog = file->private_data;
6773f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
67746b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67756b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6776f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6777f66e448cSChenbo Feng 		if (ret)
6778f66e448cSChenbo Feng 			return ret;
6779f66e448cSChenbo Feng 	}
6780f66e448cSChenbo Feng 	return 0;
6781f66e448cSChenbo Feng }
6782f66e448cSChenbo Feng 
6783ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6784ec27c356SChenbo Feng {
6785ec27c356SChenbo Feng 	u32 sid = current_sid();
6786ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6787ec27c356SChenbo Feng 
6788ec27c356SChenbo Feng 	bpfsec = map->security;
67896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67906b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6791ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6792ec27c356SChenbo Feng }
6793ec27c356SChenbo Feng 
6794ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6795ec27c356SChenbo Feng {
6796ec27c356SChenbo Feng 	u32 sid = current_sid();
6797ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6798ec27c356SChenbo Feng 
6799ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
68006b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68016b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6802ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6803ec27c356SChenbo Feng }
6804ec27c356SChenbo Feng 
6805ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6806ec27c356SChenbo Feng {
6807ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6808ec27c356SChenbo Feng 
6809ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6810ec27c356SChenbo Feng 	if (!bpfsec)
6811ec27c356SChenbo Feng 		return -ENOMEM;
6812ec27c356SChenbo Feng 
6813ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6814ec27c356SChenbo Feng 	map->security = bpfsec;
6815ec27c356SChenbo Feng 
6816ec27c356SChenbo Feng 	return 0;
6817ec27c356SChenbo Feng }
6818ec27c356SChenbo Feng 
6819ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6820ec27c356SChenbo Feng {
6821ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6822ec27c356SChenbo Feng 
6823ec27c356SChenbo Feng 	map->security = NULL;
6824ec27c356SChenbo Feng 	kfree(bpfsec);
6825ec27c356SChenbo Feng }
6826ec27c356SChenbo Feng 
6827ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6828ec27c356SChenbo Feng {
6829ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6830ec27c356SChenbo Feng 
6831ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6832ec27c356SChenbo Feng 	if (!bpfsec)
6833ec27c356SChenbo Feng 		return -ENOMEM;
6834ec27c356SChenbo Feng 
6835ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6836ec27c356SChenbo Feng 	aux->security = bpfsec;
6837ec27c356SChenbo Feng 
6838ec27c356SChenbo Feng 	return 0;
6839ec27c356SChenbo Feng }
6840ec27c356SChenbo Feng 
6841ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6842ec27c356SChenbo Feng {
6843ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6844ec27c356SChenbo Feng 
6845ec27c356SChenbo Feng 	aux->security = NULL;
6846ec27c356SChenbo Feng 	kfree(bpfsec);
6847ec27c356SChenbo Feng }
6848ec27c356SChenbo Feng #endif
6849ec27c356SChenbo Feng 
6850ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6851e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6852e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6853e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6854e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6855076c54c5SAhmed S. Darwish 
6856e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6857e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6858e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
6859e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
6860e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
6861e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
6862e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
6863e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
6864e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
686579af7307SStephen Smalley 
6866e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
68671da177e4SLinus Torvalds 
6868e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6869e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6870e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
68711da177e4SLinus Torvalds 
6872e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6873e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6874e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6875e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6876e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6877e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6878e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6879e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
6880e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
6881e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6882e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6883e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
68841da177e4SLinus Torvalds 
6885e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6886a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6887e0007529SEric Paris 
6888e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6889e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6890e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6891e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
6892e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
6893e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6894e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6895e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6896e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6897e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6898e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6899e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6900e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6901e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6902e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6903e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6904e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6906e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6907e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6908e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6912e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
691356909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
691419472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
69151da177e4SLinus Torvalds 
6916e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
6917e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6918e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6919e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6920e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6921e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6922e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6923e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
6924e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6925e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6926e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6927e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
69281da177e4SLinus Torvalds 
6929e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
69301da177e4SLinus Torvalds 
6931a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6932e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6933e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, selinux_cred_free),
6934e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6935e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
69363ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6937e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6938e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6939e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
694061d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6941e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6942e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6943e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6944e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6945e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6946e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6947e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6948791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6949e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6950e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6951e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6952e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6953e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
6954e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6955788e7dd4SYuichi Nakamura 
6956e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6957e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
69581da177e4SLinus Torvalds 
6959e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6960e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
69611da177e4SLinus Torvalds 
6962e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_alloc_security,
6963e20b043aSCasey Schaufler 			selinux_msg_queue_alloc_security),
6964e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6965e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6966e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6967e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6968e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
69691da177e4SLinus Torvalds 
6970e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6971e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6972e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6973e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6974e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
69751da177e4SLinus Torvalds 
6976e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6977e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6978e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6979e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6980e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
69811da177e4SLinus Torvalds 
6982e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
69831da177e4SLinus Torvalds 
6984e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6985e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
69861da177e4SLinus Torvalds 
6987e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6988e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6989e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6990e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
69916f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6992e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6993e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6994e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
69951da177e4SLinus Torvalds 
6996e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6997e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6998dc49c1f9SCatherine Zhang 
6999e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
7000e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
7001e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7002e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7003e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7004e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7005e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7006e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7007e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7008e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7009e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7010e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7011e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7012e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7013e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
7014e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
7015e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7016e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7017e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7018e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7019e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7020e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7021d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7022d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7023d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7024e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7025e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7026e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7027e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7028e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7029e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7030e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7031e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7032e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7033e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7034e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7035e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7036e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
70373a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
7038cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7039ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
7040ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
70413a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
70423a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
70433a976fa6SDaniel Jurgens #endif
7044d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
7045e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7046e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7047e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7048e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7049e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7050e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7051e20b043aSCasey Schaufler 			selinux_xfrm_state_alloc_acquire),
7052e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7053e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7054e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7055e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7056e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
7057e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
70581da177e4SLinus Torvalds #endif
7059d720024eSMichael LeMay 
7060d720024eSMichael LeMay #ifdef CONFIG_KEYS
7061e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7062e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
7063e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
7064e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7065d720024eSMichael LeMay #endif
70669d57a7f9SAhmed S. Darwish 
70679d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
7068e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7069e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7070e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7071e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
70729d57a7f9SAhmed S. Darwish #endif
7073ec27c356SChenbo Feng 
7074ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
7075ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
7076ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7077ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7078ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7079ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7080ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7081ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7082ec27c356SChenbo Feng #endif
70831da177e4SLinus Torvalds };
70841da177e4SLinus Torvalds 
70851da177e4SLinus Torvalds static __init int selinux_init(void)
70861da177e4SLinus Torvalds {
7087b1d9e6b0SCasey Schaufler 	if (!security_module_enable("selinux")) {
7088076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
7089076c54c5SAhmed S. Darwish 		return 0;
7090076c54c5SAhmed S. Darwish 	}
7091076c54c5SAhmed S. Darwish 
70921da177e4SLinus Torvalds 	if (!selinux_enabled) {
70931da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
70941da177e4SLinus Torvalds 		return 0;
70951da177e4SLinus Torvalds 	}
70961da177e4SLinus Torvalds 
70971da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
70981da177e4SLinus Torvalds 
7099aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
7100e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
7101aa8e712cSStephen Smalley 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
7102aa8e712cSStephen Smalley 	selinux_ss_init(&selinux_state.ss);
71036b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
7104aa8e712cSStephen Smalley 
71051da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
7106d84f4f99SDavid Howells 	cred_init_security();
71071da177e4SLinus Torvalds 
7108fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7109fcaaade1SStephen Smalley 
71107cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
71117cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
711220c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
711363205654SSangwoo 	file_security_cache = kmem_cache_create("selinux_file_security",
711463205654SSangwoo 					    sizeof(struct file_security_struct),
711563205654SSangwoo 					    0, SLAB_PANIC, NULL);
71161da177e4SLinus Torvalds 	avc_init();
71171da177e4SLinus Torvalds 
7118aa8e712cSStephen Smalley 	avtab_cache_init();
7119aa8e712cSStephen Smalley 
7120aa8e712cSStephen Smalley 	ebitmap_cache_init();
7121aa8e712cSStephen Smalley 
7122aa8e712cSStephen Smalley 	hashtab_cache_init();
7123aa8e712cSStephen Smalley 
7124d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
71251da177e4SLinus Torvalds 
7126615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7127615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7128615e51fdSPaul Moore 
71298f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
71308f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
71318f408ab6SDaniel Jurgens 
7132aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7133fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
7134828dfe1dSEric Paris 	else
7135fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
7136d720024eSMichael LeMay 
71371da177e4SLinus Torvalds 	return 0;
71381da177e4SLinus Torvalds }
71391da177e4SLinus Torvalds 
7140e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7141e8c26255SAl Viro {
7142e8c26255SAl Viro 	superblock_doinit(sb, NULL);
7143e8c26255SAl Viro }
7144e8c26255SAl Viro 
71451da177e4SLinus Torvalds void selinux_complete_init(void)
71461da177e4SLinus Torvalds {
7147fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
71481da177e4SLinus Torvalds 
71491da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7150fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
7151e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
71521da177e4SLinus Torvalds }
71531da177e4SLinus Torvalds 
71541da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
71551da177e4SLinus Torvalds    all processes and objects when they are created. */
71561da177e4SLinus Torvalds security_initcall(selinux_init);
71571da177e4SLinus Torvalds 
7158c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
71591da177e4SLinus Torvalds 
7160591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7161effad8dfSPaul Moore 	{
7162effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
71632597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
71646e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
71651da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7166effad8dfSPaul Moore 	},
7167effad8dfSPaul Moore 	{
7168effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
71692597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7170effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7171effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7172948bf85cSPaul Moore 	},
7173948bf85cSPaul Moore 	{
7174948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
71752597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7176948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7177948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
717825db6beaSJiri Pirko 	},
71791a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7180effad8dfSPaul Moore 	{
7181effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
71822597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
71836e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
71841da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7185effad8dfSPaul Moore 	},
7186effad8dfSPaul Moore 	{
7187effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
71882597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7189effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7190effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
719125db6beaSJiri Pirko 	},
71922917f57bSHuw Davies 	{
71932917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
71942917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
71952917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
71962917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
71972917f57bSHuw Davies 	},
71981da177e4SLinus Torvalds #endif	/* IPV6 */
719925db6beaSJiri Pirko };
72001da177e4SLinus Torvalds 
72018e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
72028e71bf75SFlorian Westphal {
72038e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
72048e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
72058e71bf75SFlorian Westphal }
72068e71bf75SFlorian Westphal 
72078e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
72088e71bf75SFlorian Westphal {
72098e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
72108e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
72118e71bf75SFlorian Westphal }
72128e71bf75SFlorian Westphal 
72138e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
72148e71bf75SFlorian Westphal 	.init = selinux_nf_register,
72158e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
72168e71bf75SFlorian Westphal };
72178e71bf75SFlorian Westphal 
72181da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
72191da177e4SLinus Torvalds {
722025db6beaSJiri Pirko 	int err;
72211da177e4SLinus Torvalds 
72221da177e4SLinus Torvalds 	if (!selinux_enabled)
722325db6beaSJiri Pirko 		return 0;
72241da177e4SLinus Torvalds 
7225fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
72261da177e4SLinus Torvalds 
72278e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
72281da177e4SLinus Torvalds 	if (err)
72298e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
72301da177e4SLinus Torvalds 
723125db6beaSJiri Pirko 	return 0;
72321da177e4SLinus Torvalds }
72331da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
72341da177e4SLinus Torvalds 
72351da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
72361da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
72371da177e4SLinus Torvalds {
7238fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
72391da177e4SLinus Torvalds 
72408e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
72411da177e4SLinus Torvalds }
72421da177e4SLinus Torvalds #endif
72431da177e4SLinus Torvalds 
7244c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
72451da177e4SLinus Torvalds 
72461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
72471da177e4SLinus Torvalds #define selinux_nf_ip_exit()
72481da177e4SLinus Torvalds #endif
72491da177e4SLinus Torvalds 
7250c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
72511da177e4SLinus Torvalds 
72521da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7253aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
72541da177e4SLinus Torvalds {
7255aa8e712cSStephen Smalley 	if (state->initialized) {
72561da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
72571da177e4SLinus Torvalds 		return -EINVAL;
72581da177e4SLinus Torvalds 	}
72591da177e4SLinus Torvalds 
7260aa8e712cSStephen Smalley 	if (state->disabled) {
72611da177e4SLinus Torvalds 		/* Only do this once. */
72621da177e4SLinus Torvalds 		return -EINVAL;
72631da177e4SLinus Torvalds 	}
72641da177e4SLinus Torvalds 
7265aa8e712cSStephen Smalley 	state->disabled = 1;
7266aa8e712cSStephen Smalley 
72671da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
72681da177e4SLinus Torvalds 
726930d55280SStephen Smalley 	selinux_enabled = 0;
72701da177e4SLinus Torvalds 
7271b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
72721da177e4SLinus Torvalds 
7273af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7274af8ff049SEric Paris 	avc_disable();
7275af8ff049SEric Paris 
72761da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
72771da177e4SLinus Torvalds 	selinux_nf_ip_exit();
72781da177e4SLinus Torvalds 
72791da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
72801da177e4SLinus Torvalds 	exit_sel_fs();
72811da177e4SLinus Torvalds 
72821da177e4SLinus Torvalds 	return 0;
72831da177e4SLinus Torvalds }
72841da177e4SLinus Torvalds #endif
7285