xref: /openbmc/linux/security/selinux/hooks.c (revision bd3236557bb256d6491df125e5e9d0393c70e4d2)
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>
91e262e32dSDavid Howells #include <uapi/linux/mount.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include "avc.h"
941da177e4SLinus Torvalds #include "objsec.h"
951da177e4SLinus Torvalds #include "netif.h"
96224dfbd8SPaul Moore #include "netnode.h"
973e112172SPaul Moore #include "netport.h"
98409dcf31SDaniel Jurgens #include "ibpkey.h"
99d28d1e08STrent Jaeger #include "xfrm.h"
100c60475bfSPaul Moore #include "netlabel.h"
1019d57a7f9SAhmed S. Darwish #include "audit.h"
1027b98a585SJames Morris #include "avc_ss.h"
1031da177e4SLinus Torvalds 
104aa8e712cSStephen Smalley struct selinux_state selinux_state;
105aa8e712cSStephen Smalley 
106d621d35eSPaul Moore /* SECMARK reference count */
10756a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
108d621d35eSPaul Moore 
1091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
110aa8e712cSStephen Smalley static int selinux_enforcing_boot;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1131da177e4SLinus Torvalds {
114f5269710SEric Paris 	unsigned long enforcing;
11529707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
116aa8e712cSStephen Smalley 		selinux_enforcing_boot = enforcing ? 1 : 0;
1171da177e4SLinus Torvalds 	return 1;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
120aa8e712cSStephen Smalley #else
121aa8e712cSStephen Smalley #define selinux_enforcing_boot 1
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1251da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1281da177e4SLinus Torvalds {
129f5269710SEric Paris 	unsigned long enabled;
13029707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
131f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1321da177e4SLinus Torvalds 	return 1;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
13530d55280SStephen Smalley #else
13630d55280SStephen Smalley int selinux_enabled = 1;
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
139aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot =
140aa8e712cSStephen Smalley 	CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141aa8e712cSStephen Smalley 
142aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str)
143aa8e712cSStephen Smalley {
144aa8e712cSStephen Smalley 	unsigned long checkreqprot;
145aa8e712cSStephen Smalley 
146aa8e712cSStephen Smalley 	if (!kstrtoul(str, 0, &checkreqprot))
147aa8e712cSStephen Smalley 		selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148aa8e712cSStephen Smalley 	return 1;
149aa8e712cSStephen Smalley }
150aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup);
151aa8e712cSStephen Smalley 
152e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
15363205654SSangwoo static struct kmem_cache *file_security_cache;
1547cae7e26SJames Morris 
155d621d35eSPaul Moore /**
156d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157d621d35eSPaul Moore  *
158d621d35eSPaul Moore  * Description:
159d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
160d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
161d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1622be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1632be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
164d621d35eSPaul Moore  *
165d621d35eSPaul Moore  */
166d621d35eSPaul Moore static int selinux_secmark_enabled(void)
167d621d35eSPaul Moore {
168aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
169aa8e712cSStephen Smalley 		atomic_read(&selinux_secmark_refcount));
1702be4d74fSChris PeBenito }
1712be4d74fSChris PeBenito 
1722be4d74fSChris PeBenito /**
1732be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1742be4d74fSChris PeBenito  *
1752be4d74fSChris PeBenito  * Description:
1762be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1772be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1782be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1792be4d74fSChris PeBenito  * is always considered enabled.
1802be4d74fSChris PeBenito  *
1812be4d74fSChris PeBenito  */
1822be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1832be4d74fSChris PeBenito {
184aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
185aa8e712cSStephen Smalley 		netlbl_enabled() || selinux_xfrm_enabled());
186d621d35eSPaul Moore }
187d621d35eSPaul Moore 
188615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
189615e51fdSPaul Moore {
190615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
191615e51fdSPaul Moore 		sel_netif_flush();
192615e51fdSPaul Moore 		sel_netnode_flush();
193615e51fdSPaul Moore 		sel_netport_flush();
194615e51fdSPaul Moore 		synchronize_net();
195615e51fdSPaul Moore 	}
196615e51fdSPaul Moore 	return 0;
197615e51fdSPaul Moore }
198615e51fdSPaul Moore 
1998f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event)
2008f408ab6SDaniel Jurgens {
201409dcf31SDaniel Jurgens 	if (event == AVC_CALLBACK_RESET) {
202409dcf31SDaniel Jurgens 		sel_ib_pkey_flush();
2038f408ab6SDaniel Jurgens 		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
204409dcf31SDaniel Jurgens 	}
2058f408ab6SDaniel Jurgens 
2068f408ab6SDaniel Jurgens 	return 0;
2078f408ab6SDaniel Jurgens }
2088f408ab6SDaniel Jurgens 
209d84f4f99SDavid Howells /*
210d84f4f99SDavid Howells  * initialise the security for the init task
211d84f4f99SDavid Howells  */
212d84f4f99SDavid Howells static void cred_init_security(void)
2131da177e4SLinus Torvalds {
2143b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
2151da177e4SLinus Torvalds 	struct task_security_struct *tsec;
2161da177e4SLinus Torvalds 
21789d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
2181da177e4SLinus Torvalds 	if (!tsec)
219d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
2201da177e4SLinus Torvalds 
221d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
222f1752eecSDavid Howells 	cred->security = tsec;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225275bb41eSDavid Howells /*
22688e67f3bSDavid Howells  * get the security ID of a set of credentials
22788e67f3bSDavid Howells  */
22888e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
22988e67f3bSDavid Howells {
23088e67f3bSDavid Howells 	const struct task_security_struct *tsec;
23188e67f3bSDavid Howells 
23288e67f3bSDavid Howells 	tsec = cred->security;
23388e67f3bSDavid Howells 	return tsec->sid;
23488e67f3bSDavid Howells }
23588e67f3bSDavid Howells 
23688e67f3bSDavid Howells /*
2373b11a1deSDavid Howells  * get the objective security ID of a task
238275bb41eSDavid Howells  */
239275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
240275bb41eSDavid Howells {
241275bb41eSDavid Howells 	u32 sid;
242275bb41eSDavid Howells 
243275bb41eSDavid Howells 	rcu_read_lock();
24488e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
245275bb41eSDavid Howells 	rcu_read_unlock();
246275bb41eSDavid Howells 	return sid;
247275bb41eSDavid Howells }
248275bb41eSDavid Howells 
24988e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
25088e67f3bSDavid Howells 
2511da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	struct inode_security_struct *isec;
254275bb41eSDavid Howells 	u32 sid = current_sid();
2551da177e4SLinus Torvalds 
256a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2571da177e4SLinus Torvalds 	if (!isec)
2581da177e4SLinus Torvalds 		return -ENOMEM;
2591da177e4SLinus Torvalds 
2609287aed2SAndreas Gruenbacher 	spin_lock_init(&isec->lock);
2611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2621da177e4SLinus Torvalds 	isec->inode = inode;
2631da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2641da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
265275bb41eSDavid Howells 	isec->task_sid = sid;
26642059112SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
2671da177e4SLinus Torvalds 	inode->i_security = isec;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return 0;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2725d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2735d226df4SAndreas Gruenbacher 
2745d226df4SAndreas Gruenbacher /*
2755d226df4SAndreas Gruenbacher  * Try reloading inode security labels that have been marked as invalid.  The
2765d226df4SAndreas Gruenbacher  * @may_sleep parameter indicates when sleeping and thus reloading labels is
27742059112SAndreas Gruenbacher  * allowed; when set to false, returns -ECHILD when the label is
278e9193288SAl Viro  * invalid.  The @dentry parameter should be set to a dentry of the inode.
2795d226df4SAndreas Gruenbacher  */
2805d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode,
281e9193288SAl Viro 				       struct dentry *dentry,
2825d226df4SAndreas Gruenbacher 				       bool may_sleep)
2835d226df4SAndreas Gruenbacher {
2845d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
2855d226df4SAndreas Gruenbacher 
2865d226df4SAndreas Gruenbacher 	might_sleep_if(may_sleep);
2875d226df4SAndreas Gruenbacher 
288aa8e712cSStephen Smalley 	if (selinux_state.initialized &&
289aa8e712cSStephen Smalley 	    isec->initialized != LABEL_INITIALIZED) {
2905d226df4SAndreas Gruenbacher 		if (!may_sleep)
2915d226df4SAndreas Gruenbacher 			return -ECHILD;
2925d226df4SAndreas Gruenbacher 
2935d226df4SAndreas Gruenbacher 		/*
2945d226df4SAndreas Gruenbacher 		 * Try reloading the inode security label.  This will fail if
2955d226df4SAndreas Gruenbacher 		 * @opt_dentry is NULL and no dentry for this inode can be
2965d226df4SAndreas Gruenbacher 		 * found; in that case, continue using the old label.
2975d226df4SAndreas Gruenbacher 		 */
298e9193288SAl Viro 		inode_doinit_with_dentry(inode, dentry);
2995d226df4SAndreas Gruenbacher 	}
3005d226df4SAndreas Gruenbacher 	return 0;
3015d226df4SAndreas Gruenbacher }
3025d226df4SAndreas Gruenbacher 
3035d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
3045d226df4SAndreas Gruenbacher {
3055d226df4SAndreas Gruenbacher 	return inode->i_security;
3065d226df4SAndreas Gruenbacher }
3075d226df4SAndreas Gruenbacher 
3085d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
3095d226df4SAndreas Gruenbacher {
3105d226df4SAndreas Gruenbacher 	int error;
3115d226df4SAndreas Gruenbacher 
3125d226df4SAndreas Gruenbacher 	error = __inode_security_revalidate(inode, NULL, !rcu);
3135d226df4SAndreas Gruenbacher 	if (error)
3145d226df4SAndreas Gruenbacher 		return ERR_PTR(error);
3155d226df4SAndreas Gruenbacher 	return inode->i_security;
3165d226df4SAndreas Gruenbacher }
3175d226df4SAndreas Gruenbacher 
31883da53c5SAndreas Gruenbacher /*
31983da53c5SAndreas Gruenbacher  * Get the security label of an inode.
32083da53c5SAndreas Gruenbacher  */
32183da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode)
32283da53c5SAndreas Gruenbacher {
3235d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, NULL, true);
32483da53c5SAndreas Gruenbacher 	return inode->i_security;
32583da53c5SAndreas Gruenbacher }
32683da53c5SAndreas Gruenbacher 
3272c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
3282c97165bSPaul Moore {
3292c97165bSPaul Moore 	struct inode *inode = d_backing_inode(dentry);
3302c97165bSPaul Moore 
3312c97165bSPaul Moore 	return inode->i_security;
3322c97165bSPaul Moore }
3332c97165bSPaul Moore 
33483da53c5SAndreas Gruenbacher /*
33583da53c5SAndreas Gruenbacher  * Get the security label of a dentry's backing inode.
33683da53c5SAndreas Gruenbacher  */
33783da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
33883da53c5SAndreas Gruenbacher {
33983da53c5SAndreas Gruenbacher 	struct inode *inode = d_backing_inode(dentry);
34083da53c5SAndreas Gruenbacher 
3415d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
34283da53c5SAndreas Gruenbacher 	return inode->i_security;
34383da53c5SAndreas Gruenbacher }
34483da53c5SAndreas Gruenbacher 
3453dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head)
3463dc91d43SSteven Rostedt {
3473dc91d43SSteven Rostedt 	struct inode_security_struct *isec;
3483dc91d43SSteven Rostedt 
3493dc91d43SSteven Rostedt 	isec = container_of(head, struct inode_security_struct, rcu);
3503dc91d43SSteven Rostedt 	kmem_cache_free(sel_inode_cache, isec);
3513dc91d43SSteven Rostedt }
3523dc91d43SSteven Rostedt 
3531da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3561da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3571da177e4SLinus Torvalds 
3589629d04aSWaiman Long 	/*
3599629d04aSWaiman Long 	 * As not all inode security structures are in a list, we check for
3609629d04aSWaiman Long 	 * empty list outside of the lock to make sure that we won't waste
3619629d04aSWaiman Long 	 * time taking a lock doing nothing.
3629629d04aSWaiman Long 	 *
3639629d04aSWaiman Long 	 * The list_del_init() function can be safely called more than once.
3649629d04aSWaiman Long 	 * It should not be possible for this function to be called with
3659629d04aSWaiman Long 	 * concurrent list_add(), but for better safety against future changes
3669629d04aSWaiman Long 	 * in the code, we use list_empty_careful() here.
3679629d04aSWaiman Long 	 */
3689629d04aSWaiman Long 	if (!list_empty_careful(&isec->list)) {
3691da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
3701da177e4SLinus Torvalds 		list_del_init(&isec->list);
3711da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
3729629d04aSWaiman Long 	}
3731da177e4SLinus Torvalds 
3743dc91d43SSteven Rostedt 	/*
3753dc91d43SSteven Rostedt 	 * The inode may still be referenced in a path walk and
3763dc91d43SSteven Rostedt 	 * a call to selinux_inode_permission() can be made
3773dc91d43SSteven Rostedt 	 * after inode_free_security() is called. Ideally, the VFS
3783dc91d43SSteven Rostedt 	 * wouldn't do this, but fixing that is a much harder
3793dc91d43SSteven Rostedt 	 * job. For now, simply free the i_security via RCU, and
3803dc91d43SSteven Rostedt 	 * leave the current inode->i_security pointer intact.
3813dc91d43SSteven Rostedt 	 * The inode will be freed after the RCU grace period too.
3823dc91d43SSteven Rostedt 	 */
3833dc91d43SSteven Rostedt 	call_rcu(&isec->rcu, inode_free_rcu);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct file_security_struct *fsec;
389275bb41eSDavid Howells 	u32 sid = current_sid();
3901da177e4SLinus Torvalds 
39163205654SSangwoo 	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
3921da177e4SLinus Torvalds 	if (!fsec)
3931da177e4SLinus Torvalds 		return -ENOMEM;
3941da177e4SLinus Torvalds 
395275bb41eSDavid Howells 	fsec->sid = sid;
396275bb41eSDavid Howells 	fsec->fown_sid = sid;
3971da177e4SLinus Torvalds 	file->f_security = fsec;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	return 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static void file_free_security(struct file *file)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
4051da177e4SLinus Torvalds 	file->f_security = NULL;
40663205654SSangwoo 	kmem_cache_free(file_security_cache, fsec);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
4121da177e4SLinus Torvalds 
41389d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
4141da177e4SLinus Torvalds 	if (!sbsec)
4151da177e4SLinus Torvalds 		return -ENOMEM;
4161da177e4SLinus Torvalds 
417bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
4181da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
4191da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
4201da177e4SLinus Torvalds 	sbsec->sb = sb;
4211da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
4221da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
423c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
4241da177e4SLinus Torvalds 	sb->s_security = sbsec;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4321da177e4SLinus Torvalds 	sb->s_security = NULL;
4331da177e4SLinus Torvalds 	kfree(sbsec);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436*bd323655SAl Viro struct selinux_mnt_opts {
437*bd323655SAl Viro 	const char *fscontext, *context, *rootcontext, *defcontext;
438*bd323655SAl Viro };
439*bd323655SAl Viro 
440204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts)
441204cc0ccSAl Viro {
442*bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
443*bd323655SAl Viro 	kfree(opts->fscontext);
444*bd323655SAl Viro 	kfree(opts->context);
445*bd323655SAl Viro 	kfree(opts->rootcontext);
446*bd323655SAl Viro 	kfree(opts->defcontext);
447204cc0ccSAl Viro 	kfree(opts);
448204cc0ccSAl Viro }
449204cc0ccSAl Viro 
4501da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds enum {
45631e87930SEric Paris 	Opt_error = -1,
4571da177e4SLinus Torvalds 	Opt_context = 1,
4581da177e4SLinus Torvalds 	Opt_fscontext = 2,
459c9180a57SEric Paris 	Opt_defcontext = 3,
460c9180a57SEric Paris 	Opt_rootcontext = 4,
46111689d47SDavid P. Quigley 	Opt_labelsupport = 5,
462d355987fSEric Paris 	Opt_nextmntopt = 6,
4631da177e4SLinus Torvalds };
4641da177e4SLinus Torvalds 
465d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
466d355987fSEric Paris 
467a447c093SSteven Whitehouse static const match_table_t tokens = {
468832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
469832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
470832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
471832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
47211689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
47331e87930SEric Paris 	{Opt_error, NULL},
4741da177e4SLinus Torvalds };
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4771da177e4SLinus Torvalds 
478c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
479c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
480275bb41eSDavid Howells 			const struct cred *cred)
481c312feb2SEric Paris {
482275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
483c312feb2SEric Paris 	int rc;
484c312feb2SEric Paris 
4856b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4866b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
487c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
488c312feb2SEric Paris 	if (rc)
489c312feb2SEric Paris 		return rc;
490c312feb2SEric Paris 
4916b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4926b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
493c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
494c312feb2SEric Paris 	return rc;
495c312feb2SEric Paris }
496c312feb2SEric Paris 
4970808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4980808925eSEric Paris 			struct superblock_security_struct *sbsec,
499275bb41eSDavid Howells 			const struct cred *cred)
5000808925eSEric Paris {
501275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
5020808925eSEric Paris 	int rc;
5036b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5046b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
5050808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
5060808925eSEric Paris 	if (rc)
5070808925eSEric Paris 		return rc;
5080808925eSEric Paris 
5096b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5106b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
5110808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
5120808925eSEric Paris 	return rc;
5130808925eSEric Paris }
5140808925eSEric Paris 
515b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
516b43e725dSEric Paris {
517b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
518b43e725dSEric Paris 
519d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
520b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
521d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
5229fc2b4b4SJ. Bruce Fields 		sbsec->behavior == SECURITY_FS_USE_NATIVE ||
523d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
524d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
525d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
526d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
527a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
5282651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
529aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
5302651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
5312651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
532b43e725dSEric Paris }
533b43e725dSEric Paris 
534c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5371da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
538c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5391da177e4SLinus Torvalds 	int rc = 0;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
5421da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
5431da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
5441da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
5451da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
5461da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
5475d6c3191SAndreas Gruenbacher 		if (!(root_inode->i_opflags & IOP_XATTR)) {
548c103a91eSpeter enderborg 			pr_warn("SELinux: (dev %s, type %s) has no "
54929b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
5501da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5511da177e4SLinus Torvalds 			goto out;
5521da177e4SLinus Torvalds 		}
5535d6c3191SAndreas Gruenbacher 
5545d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
5551da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5561da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
557c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
55829b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
55929b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5601da177e4SLinus Torvalds 			else
561c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
56229b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
56329b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
5641da177e4SLinus Torvalds 			goto out;
5651da177e4SLinus Torvalds 		}
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
568eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5690b4d3452SScott Mayhew 
5700b4d3452SScott Mayhew 	/*
5710b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5720b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5730b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5740b4d3452SScott Mayhew 	 */
575b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
57612f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5770b4d3452SScott Mayhew 	else
5780b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
579ddd29ec6SDavid P. Quigley 
5801da177e4SLinus Torvalds 	/* Initialize the root inode. */
581c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5841da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5851da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5861da177e4SLinus Torvalds 	   populates itself. */
5871da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5888d64124aSAl Viro 	while (!list_empty(&sbsec->isec_head)) {
5891da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5908d64124aSAl Viro 				list_first_entry(&sbsec->isec_head,
5911da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5921da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
593923190d3SStephen Smalley 		list_del_init(&isec->list);
5941da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5951da177e4SLinus Torvalds 		inode = igrab(inode);
5961da177e4SLinus Torvalds 		if (inode) {
5971da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
5981da177e4SLinus Torvalds 				inode_doinit(inode);
5991da177e4SLinus Torvalds 			iput(inode);
6001da177e4SLinus Torvalds 		}
6011da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
6041da177e4SLinus Torvalds out:
605c9180a57SEric Paris 	return rc;
606c9180a57SEric Paris }
607c9180a57SEric Paris 
608c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
609c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
610c9180a57SEric Paris {
6110d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
6120d90a7ecSDavid P. Quigley 
613c9180a57SEric Paris 	/* check if the old mount command had the same options */
6140d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
615c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
616c9180a57SEric Paris 		    (old_sid != new_sid))
617c9180a57SEric Paris 			return 1;
618c9180a57SEric Paris 
619c9180a57SEric Paris 	/* check if we were passed the same options twice,
620c9180a57SEric Paris 	 * aka someone passed context=a,context=b
621c9180a57SEric Paris 	 */
6220d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
6230d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
624c9180a57SEric Paris 			return 1;
625c9180a57SEric Paris 	return 0;
626c9180a57SEric Paris }
627e0007529SEric Paris 
628*bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
629*bd323655SAl Viro {
630*bd323655SAl Viro 	int rc = security_context_str_to_sid(&selinux_state, s,
631*bd323655SAl Viro 					     sid, GFP_KERNEL);
632*bd323655SAl Viro 	if (rc)
633*bd323655SAl Viro 		pr_warn("SELinux: security_context_str_to_sid"
634*bd323655SAl Viro 		       "(%s) failed for (dev %s, type %s) errno=%d\n",
635*bd323655SAl Viro 		       s, sb->s_id, sb->s_type->name, rc);
636*bd323655SAl Viro 	return rc;
637*bd323655SAl Viro }
638*bd323655SAl Viro 
639c9180a57SEric Paris /*
640c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
641c9180a57SEric Paris  * labeling information.
642c9180a57SEric Paris  */
643e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
644204cc0ccSAl Viro 				void *mnt_opts,
645649f6e77SDavid Quigley 				unsigned long kern_flags,
646649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
647c9180a57SEric Paris {
648275bb41eSDavid Howells 	const struct cred *cred = current_cred();
649c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
65083da53c5SAndreas Gruenbacher 	struct dentry *root = sbsec->sb->s_root;
651*bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
6522c97165bSPaul Moore 	struct inode_security_struct *root_isec;
653c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
654c9180a57SEric Paris 	u32 defcontext_sid = 0;
655*bd323655SAl Viro 	int rc = 0;
656c9180a57SEric Paris 
657c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
658c9180a57SEric Paris 
659aa8e712cSStephen Smalley 	if (!selinux_state.initialized) {
660*bd323655SAl Viro 		if (!opts) {
661c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
662c9180a57SEric Paris 			   after the initial policy is loaded and the security
663c9180a57SEric Paris 			   server is ready to handle calls. */
664c9180a57SEric Paris 			goto out;
665c9180a57SEric Paris 		}
666c9180a57SEric Paris 		rc = -EINVAL;
667c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
668744ba35eSEric Paris 			"before the security server is initialized\n");
669c9180a57SEric Paris 		goto out;
670c9180a57SEric Paris 	}
671649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
672649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
673649f6e77SDavid Quigley 		 * place the results is not allowed */
674649f6e77SDavid Quigley 		rc = -EINVAL;
675649f6e77SDavid Quigley 		goto out;
676649f6e77SDavid Quigley 	}
677c9180a57SEric Paris 
678c9180a57SEric Paris 	/*
679e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
680e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
681e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
682e0007529SEric Paris 	 * we need to skip the double mount verification.
683e0007529SEric Paris 	 *
684e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
685e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
686e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
687e0007529SEric Paris 	 * will be used for both mounts)
688e0007529SEric Paris 	 */
6890d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
690*bd323655SAl Viro 	    && !opts)
691e0007529SEric Paris 		goto out;
692e0007529SEric Paris 
6932c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
6942c97165bSPaul Moore 
695e0007529SEric Paris 	/*
696c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
697c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
698c9180a57SEric Paris 	 * than once with different security options.
699c9180a57SEric Paris 	 */
700*bd323655SAl Viro 	if (opts) {
701*bd323655SAl Viro 		if (opts->fscontext) {
702*bd323655SAl Viro 			rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
703*bd323655SAl Viro 			if (rc)
704c9180a57SEric Paris 				goto out;
705c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
706c9180a57SEric Paris 					fscontext_sid))
707c9180a57SEric Paris 				goto out_double_mount;
708c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
709*bd323655SAl Viro 		}
710*bd323655SAl Viro 		if (opts->context) {
711*bd323655SAl Viro 			rc = parse_sid(sb, opts->context, &context_sid);
712*bd323655SAl Viro 			if (rc)
713*bd323655SAl Viro 				goto out;
714c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
715c9180a57SEric Paris 					context_sid))
716c9180a57SEric Paris 				goto out_double_mount;
717c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
718*bd323655SAl Viro 		}
719*bd323655SAl Viro 		if (opts->rootcontext) {
720*bd323655SAl Viro 			rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
721*bd323655SAl Viro 			if (rc)
722*bd323655SAl Viro 				goto out;
723c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
724c9180a57SEric Paris 					rootcontext_sid))
725c9180a57SEric Paris 				goto out_double_mount;
726c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
727*bd323655SAl Viro 		}
728*bd323655SAl Viro 		if (opts->defcontext) {
729*bd323655SAl Viro 			rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
730*bd323655SAl Viro 			if (rc)
731*bd323655SAl Viro 				goto out;
732c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
733c9180a57SEric Paris 					defcontext_sid))
734c9180a57SEric Paris 				goto out_double_mount;
735c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
736c9180a57SEric Paris 		}
737c9180a57SEric Paris 	}
738c9180a57SEric Paris 
7390d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
740c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
741*bd323655SAl Viro 		if ((sbsec->flags & SE_MNTMASK) && !opts)
742c9180a57SEric Paris 			goto out_double_mount;
743c9180a57SEric Paris 		rc = 0;
744c9180a57SEric Paris 		goto out;
745c9180a57SEric Paris 	}
746c9180a57SEric Paris 
747089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
748134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
749134509d5SStephen Smalley 
7508e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
7516a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
7528e014720SStephen Smalley 	    !strcmp(sb->s_type->name, "sysfs") ||
753901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "pstore") ||
754901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
755901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
756134509d5SStephen Smalley 		sbsec->flags |= SE_SBGENFS;
757c9180a57SEric Paris 
758eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
759eb9ae686SDavid Quigley 		/*
760eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
761eb9ae686SDavid Quigley 		 * filesystem type.
762eb9ae686SDavid Quigley 		 */
763aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
764c9180a57SEric Paris 		if (rc) {
765c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
766089be43eSJames Morris 					__func__, sb->s_type->name, rc);
767c9180a57SEric Paris 			goto out;
768c9180a57SEric Paris 		}
769eb9ae686SDavid Quigley 	}
770aad82892SSeth Forshee 
771aad82892SSeth Forshee 	/*
77201593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
77301593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
77401593d32SStephen Smalley 	 * line and security labels must be ignored.
775aad82892SSeth Forshee 	 */
77601593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
77701593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
77801593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
77901593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
780aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
781aad82892SSeth Forshee 		    defcontext_sid) {
782aad82892SSeth Forshee 			rc = -EACCES;
783aad82892SSeth Forshee 			goto out;
784aad82892SSeth Forshee 		}
785aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
786aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
787aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
788aa8e712cSStephen Smalley 						     current_sid(),
789aa8e712cSStephen Smalley 						     current_sid(),
790aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
791aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
792aad82892SSeth Forshee 			if (rc)
793aad82892SSeth Forshee 				goto out;
794aad82892SSeth Forshee 		}
795aad82892SSeth Forshee 		goto out_set_opts;
796aad82892SSeth Forshee 	}
797aad82892SSeth Forshee 
798c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
799c9180a57SEric Paris 	if (fscontext_sid) {
800275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
801c9180a57SEric Paris 		if (rc)
802c9180a57SEric Paris 			goto out;
803c9180a57SEric Paris 
804c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
805c9180a57SEric Paris 	}
806c9180a57SEric Paris 
807c9180a57SEric Paris 	/*
808c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
809c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
810c9180a57SEric Paris 	 * the superblock context if not already set.
811c9180a57SEric Paris 	 */
812eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
813eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
814eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
815eb9ae686SDavid Quigley 	}
816eb9ae686SDavid Quigley 
817c9180a57SEric Paris 	if (context_sid) {
818c9180a57SEric Paris 		if (!fscontext_sid) {
819275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
820275bb41eSDavid Howells 							  cred);
821c9180a57SEric Paris 			if (rc)
822c9180a57SEric Paris 				goto out;
823c9180a57SEric Paris 			sbsec->sid = context_sid;
824c9180a57SEric Paris 		} else {
825275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
826275bb41eSDavid Howells 							     cred);
827c9180a57SEric Paris 			if (rc)
828c9180a57SEric Paris 				goto out;
829c9180a57SEric Paris 		}
830c9180a57SEric Paris 		if (!rootcontext_sid)
831c9180a57SEric Paris 			rootcontext_sid = context_sid;
832c9180a57SEric Paris 
833c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
834c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
835c9180a57SEric Paris 	}
836c9180a57SEric Paris 
837c9180a57SEric Paris 	if (rootcontext_sid) {
838275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
839275bb41eSDavid Howells 						     cred);
840c9180a57SEric Paris 		if (rc)
841c9180a57SEric Paris 			goto out;
842c9180a57SEric Paris 
843c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
8446f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
845c9180a57SEric Paris 	}
846c9180a57SEric Paris 
847c9180a57SEric Paris 	if (defcontext_sid) {
848eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
849eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
850c9180a57SEric Paris 			rc = -EINVAL;
851c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
852c9180a57SEric Paris 			       "invalid for this filesystem type\n");
853c9180a57SEric Paris 			goto out;
854c9180a57SEric Paris 		}
855c9180a57SEric Paris 
856c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
857c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
858275bb41eSDavid Howells 							     sbsec, cred);
859c9180a57SEric Paris 			if (rc)
860c9180a57SEric Paris 				goto out;
861c9180a57SEric Paris 		}
862c9180a57SEric Paris 
863c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
864c9180a57SEric Paris 	}
865c9180a57SEric Paris 
866aad82892SSeth Forshee out_set_opts:
867c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
868c9180a57SEric Paris out:
869bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8701da177e4SLinus Torvalds 	return rc;
871c9180a57SEric Paris out_double_mount:
872c9180a57SEric Paris 	rc = -EINVAL;
873c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
874*bd323655SAl Viro 	       "security settings for (dev %s, type %s)\n", sb->s_id,
875*bd323655SAl Viro 	       sb->s_type->name);
876c9180a57SEric Paris 	goto out;
877c9180a57SEric Paris }
878c9180a57SEric Paris 
879094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
880094f7b69SJeff Layton 				    const struct super_block *newsb)
881094f7b69SJeff Layton {
882094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
883094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
884094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
885094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
886094f7b69SJeff Layton 
887094f7b69SJeff Layton 	if (oldflags != newflags)
888094f7b69SJeff Layton 		goto mismatch;
889094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
890094f7b69SJeff Layton 		goto mismatch;
891094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
892094f7b69SJeff Layton 		goto mismatch;
893094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
894094f7b69SJeff Layton 		goto mismatch;
895094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
89683da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
89783da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
898094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
899094f7b69SJeff Layton 			goto mismatch;
900094f7b69SJeff Layton 	}
901094f7b69SJeff Layton 	return 0;
902094f7b69SJeff Layton mismatch:
903c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
904094f7b69SJeff Layton 			    "different security settings for (dev %s, "
905094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
906094f7b69SJeff Layton 	return -EBUSY;
907094f7b69SJeff Layton }
908094f7b69SJeff Layton 
909094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
9100b4d3452SScott Mayhew 					struct super_block *newsb,
9110b4d3452SScott Mayhew 					unsigned long kern_flags,
9120b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
913c9180a57SEric Paris {
9140b4d3452SScott Mayhew 	int rc = 0;
915c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
916c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
917c9180a57SEric Paris 
918c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
919c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
920c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
921c9180a57SEric Paris 
9220f5e6420SEric Paris 	/*
9230f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
924e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
9250f5e6420SEric Paris 	 */
926aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
927094f7b69SJeff Layton 		return 0;
928c9180a57SEric Paris 
9290b4d3452SScott Mayhew 	/*
9300b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
9310b4d3452SScott Mayhew 	 * place the results is not allowed.
9320b4d3452SScott Mayhew 	 */
9330b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
9340b4d3452SScott Mayhew 		return -EINVAL;
9350b4d3452SScott Mayhew 
936c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
9370d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
938c9180a57SEric Paris 
939094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
9400d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
941094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
9425a552617SEric Paris 
943c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
944c9180a57SEric Paris 
945c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
946c9180a57SEric Paris 
947c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
948c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
949c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
950c9180a57SEric Paris 
9510b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
9520b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
953aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
9540b4d3452SScott Mayhew 		if (rc)
9550b4d3452SScott Mayhew 			goto out;
9560b4d3452SScott Mayhew 	}
9570b4d3452SScott Mayhew 
9580b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
9590b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
9600b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
9610b4d3452SScott Mayhew 	}
9620b4d3452SScott Mayhew 
963c9180a57SEric Paris 	if (set_context) {
964c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
965c9180a57SEric Paris 
966c9180a57SEric Paris 		if (!set_fscontext)
967c9180a57SEric Paris 			newsbsec->sid = sid;
968c9180a57SEric Paris 		if (!set_rootcontext) {
96983da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
970c9180a57SEric Paris 			newisec->sid = sid;
971c9180a57SEric Paris 		}
972c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
973c9180a57SEric Paris 	}
974c9180a57SEric Paris 	if (set_rootcontext) {
97583da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
97683da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
977c9180a57SEric Paris 
978c9180a57SEric Paris 		newisec->sid = oldisec->sid;
979c9180a57SEric Paris 	}
980c9180a57SEric Paris 
981c9180a57SEric Paris 	sb_finish_set_opts(newsb);
9820b4d3452SScott Mayhew out:
983c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
9840b4d3452SScott Mayhew 	return rc;
985c9180a57SEric Paris }
986c9180a57SEric Paris 
9872e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
988204cc0ccSAl Viro 				  void **mnt_opts)
989c9180a57SEric Paris {
990*bd323655SAl Viro 	struct selinux_mnt_opts *opts = *mnt_opts;
991e0007529SEric Paris 	char *p;
992*bd323655SAl Viro 	int rc;
993c9180a57SEric Paris 
994c9180a57SEric Paris 	/* Standard string-based options. */
995c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
996c9180a57SEric Paris 		int token;
997c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
998c9180a57SEric Paris 
999c9180a57SEric Paris 		if (!*p)
1000c9180a57SEric Paris 			continue;
1001c9180a57SEric Paris 
1002c9180a57SEric Paris 		token = match_token(p, tokens, args);
1003c9180a57SEric Paris 
1004*bd323655SAl Viro 		if (!opts) {
1005*bd323655SAl Viro 			opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1006*bd323655SAl Viro 			if (!opts)
1007*bd323655SAl Viro 				return -ENOMEM;
1008*bd323655SAl Viro 		}
1009*bd323655SAl Viro 
1010c9180a57SEric Paris 		switch (token) {
1011c9180a57SEric Paris 		case Opt_context:
1012*bd323655SAl Viro 			if (opts->context || opts->defcontext) {
1013c9180a57SEric Paris 				rc = -EINVAL;
1014c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1015c9180a57SEric Paris 				goto out_err;
1016c9180a57SEric Paris 			}
1017*bd323655SAl Viro 			opts->context = match_strdup(&args[0]);
1018*bd323655SAl Viro 			if (!opts->context) {
1019c9180a57SEric Paris 				rc = -ENOMEM;
1020c9180a57SEric Paris 				goto out_err;
1021c9180a57SEric Paris 			}
1022c9180a57SEric Paris 			break;
1023c9180a57SEric Paris 
1024c9180a57SEric Paris 		case Opt_fscontext:
1025*bd323655SAl Viro 			if (opts->fscontext) {
1026c9180a57SEric Paris 				rc = -EINVAL;
1027c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1028c9180a57SEric Paris 				goto out_err;
1029c9180a57SEric Paris 			}
1030*bd323655SAl Viro 			opts->fscontext = match_strdup(&args[0]);
1031*bd323655SAl Viro 			if (!opts->fscontext) {
1032c9180a57SEric Paris 				rc = -ENOMEM;
1033c9180a57SEric Paris 				goto out_err;
1034c9180a57SEric Paris 			}
1035c9180a57SEric Paris 			break;
1036c9180a57SEric Paris 
1037c9180a57SEric Paris 		case Opt_rootcontext:
1038*bd323655SAl Viro 			if (opts->rootcontext) {
1039c9180a57SEric Paris 				rc = -EINVAL;
1040c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1041c9180a57SEric Paris 				goto out_err;
1042c9180a57SEric Paris 			}
1043*bd323655SAl Viro 			opts->rootcontext = match_strdup(&args[0]);
1044*bd323655SAl Viro 			if (!opts->rootcontext) {
1045c9180a57SEric Paris 				rc = -ENOMEM;
1046c9180a57SEric Paris 				goto out_err;
1047c9180a57SEric Paris 			}
1048c9180a57SEric Paris 			break;
1049c9180a57SEric Paris 
1050c9180a57SEric Paris 		case Opt_defcontext:
1051*bd323655SAl Viro 			if (opts->context || opts->defcontext) {
1052c9180a57SEric Paris 				rc = -EINVAL;
1053c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1054c9180a57SEric Paris 				goto out_err;
1055c9180a57SEric Paris 			}
1056*bd323655SAl Viro 			opts->defcontext = match_strdup(&args[0]);
1057*bd323655SAl Viro 			if (!opts->defcontext) {
1058c9180a57SEric Paris 				rc = -ENOMEM;
1059c9180a57SEric Paris 				goto out_err;
1060c9180a57SEric Paris 			}
1061c9180a57SEric Paris 			break;
106211689d47SDavid P. Quigley 		case Opt_labelsupport:
106311689d47SDavid P. Quigley 			break;
1064c9180a57SEric Paris 		default:
1065c9180a57SEric Paris 			rc = -EINVAL;
1066c103a91eSpeter enderborg 			pr_warn("SELinux:  unknown mount option\n");
1067c9180a57SEric Paris 			goto out_err;
1068c9180a57SEric Paris 
1069c9180a57SEric Paris 		}
1070c9180a57SEric Paris 	}
1071*bd323655SAl Viro 	*mnt_opts = opts;
1072e0007529SEric Paris 	return 0;
1073e0007529SEric Paris 
1074c9180a57SEric Paris out_err:
1075*bd323655SAl Viro 	if (opts)
1076*bd323655SAl Viro 		selinux_free_mnt_opts(opts);
1077c9180a57SEric Paris 	return rc;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
1080e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid)
10812069f457SEric Paris {
1082e3489f89SAl Viro 	char *context = NULL;
1083e3489f89SAl Viro 	u32 len;
1084e3489f89SAl Viro 	int rc;
10852069f457SEric Paris 
1086e3489f89SAl Viro 	rc = security_sid_to_context(&selinux_state, sid,
1087e3489f89SAl Viro 					     &context, &len);
1088e3489f89SAl Viro 	if (!rc) {
1089e3489f89SAl Viro 		bool has_comma = context && strchr(context, ',');
109011689d47SDavid P. Quigley 
10912069f457SEric Paris 		if (has_comma)
10922069f457SEric Paris 			seq_putc(m, '\"');
1093e3489f89SAl Viro 		seq_escape(m, context, "\"\n\\");
10942069f457SEric Paris 		if (has_comma)
10952069f457SEric Paris 			seq_putc(m, '\"');
10962069f457SEric Paris 	}
1097e3489f89SAl Viro 	kfree(context);
1098e3489f89SAl Viro 	return rc;
10992069f457SEric Paris }
11002069f457SEric Paris 
11012069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
11022069f457SEric Paris {
1103e3489f89SAl Viro 	struct superblock_security_struct *sbsec = sb->s_security;
11042069f457SEric Paris 	int rc;
11052069f457SEric Paris 
1106e3489f89SAl Viro 	if (!(sbsec->flags & SE_SBINITIALIZED))
1107e3489f89SAl Viro 		return 0;
1108e3489f89SAl Viro 
1109e3489f89SAl Viro 	if (!selinux_state.initialized)
1110e3489f89SAl Viro 		return 0;
1111e3489f89SAl Viro 
1112e3489f89SAl Viro 	if (sbsec->flags & FSCONTEXT_MNT) {
1113e3489f89SAl Viro 		seq_putc(m, ',');
1114e3489f89SAl Viro 		seq_puts(m, FSCONTEXT_STR);
1115e3489f89SAl Viro 		rc = show_sid(m, sbsec->sid);
1116e3489f89SAl Viro 		if (rc)
11172069f457SEric Paris 			return rc;
1118383795c2SEric Paris 	}
1119e3489f89SAl Viro 	if (sbsec->flags & CONTEXT_MNT) {
1120e3489f89SAl Viro 		seq_putc(m, ',');
1121e3489f89SAl Viro 		seq_puts(m, CONTEXT_STR);
1122e3489f89SAl Viro 		rc = show_sid(m, sbsec->mntpoint_sid);
1123e3489f89SAl Viro 		if (rc)
11242069f457SEric Paris 			return rc;
11252069f457SEric Paris 	}
1126e3489f89SAl Viro 	if (sbsec->flags & DEFCONTEXT_MNT) {
1127e3489f89SAl Viro 		seq_putc(m, ',');
1128e3489f89SAl Viro 		seq_puts(m, DEFCONTEXT_STR);
1129e3489f89SAl Viro 		rc = show_sid(m, sbsec->def_sid);
1130e3489f89SAl Viro 		if (rc)
1131e3489f89SAl Viro 			return rc;
1132e3489f89SAl Viro 	}
1133e3489f89SAl Viro 	if (sbsec->flags & ROOTCONTEXT_MNT) {
1134e3489f89SAl Viro 		struct dentry *root = sbsec->sb->s_root;
1135e3489f89SAl Viro 		struct inode_security_struct *isec = backing_inode_security(root);
1136e3489f89SAl Viro 		seq_putc(m, ',');
1137e3489f89SAl Viro 		seq_puts(m, ROOTCONTEXT_STR);
1138e3489f89SAl Viro 		rc = show_sid(m, isec->sid);
1139e3489f89SAl Viro 		if (rc)
1140e3489f89SAl Viro 			return rc;
1141e3489f89SAl Viro 	}
1142e3489f89SAl Viro 	if (sbsec->flags & SBLABEL_MNT) {
1143e3489f89SAl Viro 		seq_putc(m, ',');
1144e3489f89SAl Viro 		seq_puts(m, LABELSUPP_STR);
1145e3489f89SAl Viro 	}
1146e3489f89SAl Viro 	return 0;
1147e3489f89SAl Viro }
11482069f457SEric Paris 
11491da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11521da177e4SLinus Torvalds 	case S_IFSOCK:
11531da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11541da177e4SLinus Torvalds 	case S_IFLNK:
11551da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11561da177e4SLinus Torvalds 	case S_IFREG:
11571da177e4SLinus Torvalds 		return SECCLASS_FILE;
11581da177e4SLinus Torvalds 	case S_IFBLK:
11591da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11601da177e4SLinus Torvalds 	case S_IFDIR:
11611da177e4SLinus Torvalds 		return SECCLASS_DIR;
11621da177e4SLinus Torvalds 	case S_IFCHR:
11631da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11641da177e4SLinus Torvalds 	case S_IFIFO:
11651da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	return SECCLASS_FILE;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
117213402580SJames Morris static inline int default_protocol_stream(int protocol)
117313402580SJames Morris {
117413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
117513402580SJames Morris }
117613402580SJames Morris 
117713402580SJames Morris static inline int default_protocol_dgram(int protocol)
117813402580SJames Morris {
117913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
118013402580SJames Morris }
118113402580SJames Morris 
11821da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11831da177e4SLinus Torvalds {
1184aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1185da69a530SStephen Smalley 
11861da177e4SLinus Torvalds 	switch (family) {
11871da177e4SLinus Torvalds 	case PF_UNIX:
11881da177e4SLinus Torvalds 		switch (type) {
11891da177e4SLinus Torvalds 		case SOCK_STREAM:
11901da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11911da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11921da177e4SLinus Torvalds 		case SOCK_DGRAM:
11932a764b52SLuis Ressel 		case SOCK_RAW:
11941da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		break;
11971da177e4SLinus Torvalds 	case PF_INET:
11981da177e4SLinus Torvalds 	case PF_INET6:
11991da177e4SLinus Torvalds 		switch (type) {
12001da177e4SLinus Torvalds 		case SOCK_STREAM:
1201da69a530SStephen Smalley 		case SOCK_SEQPACKET:
120213402580SJames Morris 			if (default_protocol_stream(protocol))
12031da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1204da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1205da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
120613402580SJames Morris 			else
120713402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
12081da177e4SLinus Torvalds 		case SOCK_DGRAM:
120913402580SJames Morris 			if (default_protocol_dgram(protocol))
12101da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1211ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1212ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1213da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
121413402580SJames Morris 			else
121513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
12162ee92d46SJames Morris 		case SOCK_DCCP:
12172ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
121813402580SJames Morris 		default:
12191da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
12201da177e4SLinus Torvalds 		}
12211da177e4SLinus Torvalds 		break;
12221da177e4SLinus Torvalds 	case PF_NETLINK:
12231da177e4SLinus Torvalds 		switch (protocol) {
12241da177e4SLinus Torvalds 		case NETLINK_ROUTE:
12251da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
12267f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
12271da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
12281da177e4SLinus Torvalds 		case NETLINK_NFLOG:
12291da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
12301da177e4SLinus Torvalds 		case NETLINK_XFRM:
12311da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
12321da177e4SLinus Torvalds 		case NETLINK_SELINUX:
12331da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
12346c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
12356c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
12361da177e4SLinus Torvalds 		case NETLINK_AUDIT:
12371da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
12386c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
12396c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
12406c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
12416c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
12426c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
12436c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
12441da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12451da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12460c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12470c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12486c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
12496c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
12506c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
12516c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
12526c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
12536c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
12546c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
12556c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
12561da177e4SLinus Torvalds 		default:
12571da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 	case PF_PACKET:
12601da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12611da177e4SLinus Torvalds 	case PF_KEY:
12621da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12633e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12643e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12651da177e4SLinus Torvalds 	}
12661da177e4SLinus Torvalds 
1267da69a530SStephen Smalley 	if (extsockclass) {
1268da69a530SStephen Smalley 		switch (family) {
1269da69a530SStephen Smalley 		case PF_AX25:
1270da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1271da69a530SStephen Smalley 		case PF_IPX:
1272da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1273da69a530SStephen Smalley 		case PF_NETROM:
1274da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1275da69a530SStephen Smalley 		case PF_ATMPVC:
1276da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1277da69a530SStephen Smalley 		case PF_X25:
1278da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1279da69a530SStephen Smalley 		case PF_ROSE:
1280da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1281da69a530SStephen Smalley 		case PF_DECnet:
1282da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1283da69a530SStephen Smalley 		case PF_ATMSVC:
1284da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1285da69a530SStephen Smalley 		case PF_RDS:
1286da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1287da69a530SStephen Smalley 		case PF_IRDA:
1288da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1289da69a530SStephen Smalley 		case PF_PPPOX:
1290da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1291da69a530SStephen Smalley 		case PF_LLC:
1292da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1293da69a530SStephen Smalley 		case PF_CAN:
1294da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1295da69a530SStephen Smalley 		case PF_TIPC:
1296da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1297da69a530SStephen Smalley 		case PF_BLUETOOTH:
1298da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1299da69a530SStephen Smalley 		case PF_IUCV:
1300da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1301da69a530SStephen Smalley 		case PF_RXRPC:
1302da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1303da69a530SStephen Smalley 		case PF_ISDN:
1304da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1305da69a530SStephen Smalley 		case PF_PHONET:
1306da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1307da69a530SStephen Smalley 		case PF_IEEE802154:
1308da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1309da69a530SStephen Smalley 		case PF_CAIF:
1310da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1311da69a530SStephen Smalley 		case PF_ALG:
1312da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1313da69a530SStephen Smalley 		case PF_NFC:
1314da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1315da69a530SStephen Smalley 		case PF_VSOCK:
1316da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1317da69a530SStephen Smalley 		case PF_KCM:
1318da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1319da69a530SStephen Smalley 		case PF_QIPCRTR:
1320da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
13213051bf36SLinus Torvalds 		case PF_SMC:
13223051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
132368e8b849SBjörn Töpel 		case PF_XDP:
132468e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
132568e8b849SBjörn Töpel #if PF_MAX > 45
1326da69a530SStephen Smalley #error New address family defined, please update this function.
1327da69a530SStephen Smalley #endif
1328da69a530SStephen Smalley 		}
1329da69a530SStephen Smalley 	}
1330da69a530SStephen Smalley 
13311da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
1334134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
13351da177e4SLinus Torvalds 				 u16 tclass,
1336134509d5SStephen Smalley 				 u16 flags,
13371da177e4SLinus Torvalds 				 u32 *sid)
13381da177e4SLinus Torvalds {
13398e6c9693SLucian Adrian Grijincu 	int rc;
1340fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
13418e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
13441da177e4SLinus Torvalds 	if (!buffer)
13451da177e4SLinus Torvalds 		return -ENOMEM;
13461da177e4SLinus Torvalds 
13478e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
13488e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
13498e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
13508e6c9693SLucian Adrian Grijincu 	else {
1351134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
13528e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
13538e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
13548e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
13558e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
13568e6c9693SLucian Adrian Grijincu 				path[1] = '/';
13578e6c9693SLucian Adrian Grijincu 				path++;
13581da177e4SLinus Torvalds 			}
1359134509d5SStephen Smalley 		}
1360aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1361aa8e712cSStephen Smalley 					path, tclass, sid);
13627bb185edSStephen Smalley 		if (rc == -ENOENT) {
13637bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
13647bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
13657bb185edSStephen Smalley 			rc = 0;
13667bb185edSStephen Smalley 		}
13678e6c9693SLucian Adrian Grijincu 	}
13681da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
13691da177e4SLinus Torvalds 	return rc;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
13731da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
13761da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
13779287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
13789287aed2SAndreas Gruenbacher 	u16 sclass;
13791da177e4SLinus Torvalds 	struct dentry *dentry;
13801da177e4SLinus Torvalds #define INITCONTEXTLEN 255
13811da177e4SLinus Torvalds 	char *context = NULL;
13821da177e4SLinus Torvalds 	unsigned len = 0;
13831da177e4SLinus Torvalds 	int rc = 0;
13841da177e4SLinus Torvalds 
13856f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
138613457d07SAndreas Gruenbacher 		return 0;
13871da177e4SLinus Torvalds 
13889287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
13896f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
139023970741SEric Paris 		goto out_unlock;
13911da177e4SLinus Torvalds 
139213457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
139313457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
139413457d07SAndreas Gruenbacher 
13951da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
13960d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
13971da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
13981da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
13991da177e4SLinus Torvalds 		   server is ready to handle calls. */
14001da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
14011da177e4SLinus Torvalds 		if (list_empty(&isec->list))
14021da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
14031da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
140423970741SEric Paris 		goto out_unlock;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
14079287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
14089287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
14099287aed2SAndreas Gruenbacher 	sid = isec->sid;
14109287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
14119287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
14129287aed2SAndreas Gruenbacher 
14131da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1414eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1415eb9ae686SDavid Quigley 		break;
14161da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
14175d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
14189287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
14191da177e4SLinus Torvalds 			break;
14201da177e4SLinus Torvalds 		}
14211da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
14221da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
14231da177e4SLinus Torvalds 		if (opt_dentry) {
14241da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
14251da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
14261da177e4SLinus Torvalds 		} else {
1427b127125dSAl Viro 			/*
1428b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1429b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1430b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1431b127125dSAl Viro 			 * two, depending upon that...
1432b127125dSAl Viro 			 */
14331da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1434b127125dSAl Viro 			if (!dentry)
1435b127125dSAl Viro 				dentry = d_find_any_alias(inode);
14361da177e4SLinus Torvalds 		}
14371da177e4SLinus Torvalds 		if (!dentry) {
1438df7f54c0SEric Paris 			/*
1439df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1440df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1441df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1442df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1443df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1444df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1445df7f54c0SEric Paris 			 * be used again by userspace.
1446df7f54c0SEric Paris 			 */
14479287aed2SAndreas Gruenbacher 			goto out;
14481da177e4SLinus Torvalds 		}
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
14514cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
14521da177e4SLinus Torvalds 		if (!context) {
14531da177e4SLinus Torvalds 			rc = -ENOMEM;
14541da177e4SLinus Torvalds 			dput(dentry);
14559287aed2SAndreas Gruenbacher 			goto out;
14561da177e4SLinus Torvalds 		}
14574cb912f1SEric Paris 		context[len] = '\0';
14585d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
14591da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1460314dabb8SJames Morris 			kfree(context);
1461314dabb8SJames Morris 
14621da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
14635d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
14641da177e4SLinus Torvalds 			if (rc < 0) {
14651da177e4SLinus Torvalds 				dput(dentry);
14669287aed2SAndreas Gruenbacher 				goto out;
14671da177e4SLinus Torvalds 			}
14681da177e4SLinus Torvalds 			len = rc;
14694cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
14701da177e4SLinus Torvalds 			if (!context) {
14711da177e4SLinus Torvalds 				rc = -ENOMEM;
14721da177e4SLinus Torvalds 				dput(dentry);
14739287aed2SAndreas Gruenbacher 				goto out;
14741da177e4SLinus Torvalds 			}
14754cb912f1SEric Paris 			context[len] = '\0';
14765d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 		dput(dentry);
14791da177e4SLinus Torvalds 		if (rc < 0) {
14801da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1481c103a91eSpeter enderborg 				pr_warn("SELinux: %s:  getxattr returned "
1482dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
14831da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
14841da177e4SLinus Torvalds 				kfree(context);
14859287aed2SAndreas Gruenbacher 				goto out;
14861da177e4SLinus Torvalds 			}
14871da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
14881da177e4SLinus Torvalds 			sid = sbsec->def_sid;
14891da177e4SLinus Torvalds 			rc = 0;
14901da177e4SLinus Torvalds 		} else {
1491aa8e712cSStephen Smalley 			rc = security_context_to_sid_default(&selinux_state,
1492aa8e712cSStephen Smalley 							     context, rc, &sid,
1493869ab514SStephen Smalley 							     sbsec->def_sid,
1494869ab514SStephen Smalley 							     GFP_NOFS);
14951da177e4SLinus Torvalds 			if (rc) {
14964ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
14974ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
14984ba0a8adSEric Paris 
14994ba0a8adSEric Paris 				if (rc == -EINVAL) {
15004ba0a8adSEric Paris 					if (printk_ratelimit())
1501c103a91eSpeter enderborg 						pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
15024ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
15034ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
15044ba0a8adSEric Paris 				} else {
1505c103a91eSpeter enderborg 					pr_warn("SELinux: %s:  context_to_sid(%s) "
15061da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
15074ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
15084ba0a8adSEric Paris 				}
15091da177e4SLinus Torvalds 				kfree(context);
15101da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
15111da177e4SLinus Torvalds 				rc = 0;
15121da177e4SLinus Torvalds 				break;
15131da177e4SLinus Torvalds 			}
15141da177e4SLinus Torvalds 		}
15151da177e4SLinus Torvalds 		kfree(context);
15161da177e4SLinus Torvalds 		break;
15171da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
15189287aed2SAndreas Gruenbacher 		sid = task_sid;
15191da177e4SLinus Torvalds 		break;
15201da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
15211da177e4SLinus Torvalds 		/* Default to the fs SID. */
15229287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1525aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1526aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
15271da177e4SLinus Torvalds 		if (rc)
15289287aed2SAndreas Gruenbacher 			goto out;
15291da177e4SLinus Torvalds 		break;
1530c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
15319287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1532c312feb2SEric Paris 		break;
15331da177e4SLinus Torvalds 	default:
1534c312feb2SEric Paris 		/* Default to the fs superblock SID. */
15359287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
15361da177e4SLinus Torvalds 
1537134509d5SStephen Smalley 		if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1538f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1539f64410ecSPaul Moore 			 * procfs inodes */
1540b127125dSAl Viro 			if (opt_dentry) {
1541f64410ecSPaul Moore 				/* Called from d_instantiate or
1542f64410ecSPaul Moore 				 * d_splice_alias. */
1543f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1544b127125dSAl Viro 			} else {
1545f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1546b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1547b127125dSAl Viro 				 * a connected one, so try that first.
1548b127125dSAl Viro 				 */
1549f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1550b127125dSAl Viro 				if (!dentry)
1551b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1552b127125dSAl Viro 			}
1553f64410ecSPaul Moore 			/*
1554f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1555f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1556f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1557f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1558f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1559f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1560f64410ecSPaul Moore 			 * could be used again by userspace.
1561f64410ecSPaul Moore 			 */
1562f64410ecSPaul Moore 			if (!dentry)
15639287aed2SAndreas Gruenbacher 				goto out;
15649287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1565134509d5SStephen Smalley 						   sbsec->flags, &sid);
1566f64410ecSPaul Moore 			dput(dentry);
15671da177e4SLinus Torvalds 			if (rc)
15689287aed2SAndreas Gruenbacher 				goto out;
15691da177e4SLinus Torvalds 		}
15701da177e4SLinus Torvalds 		break;
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds 
15739287aed2SAndreas Gruenbacher out:
15749287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15759287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
15769287aed2SAndreas Gruenbacher 		if (!sid || rc) {
15779287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
15789287aed2SAndreas Gruenbacher 			goto out_unlock;
15799287aed2SAndreas Gruenbacher 		}
15809287aed2SAndreas Gruenbacher 
15816f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
15829287aed2SAndreas Gruenbacher 		isec->sid = sid;
15839287aed2SAndreas Gruenbacher 	}
15841da177e4SLinus Torvalds 
158523970741SEric Paris out_unlock:
15869287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15871da177e4SLinus Torvalds 	return rc;
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
15911da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	u32 perm = 0;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	switch (sig) {
15961da177e4SLinus Torvalds 	case SIGCHLD:
15971da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
15981da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
15991da177e4SLinus Torvalds 		break;
16001da177e4SLinus Torvalds 	case SIGKILL:
16011da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
16021da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
16031da177e4SLinus Torvalds 		break;
16041da177e4SLinus Torvalds 	case SIGSTOP:
16051da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
16061da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
16071da177e4SLinus Torvalds 		break;
16081da177e4SLinus Torvalds 	default:
16091da177e4SLinus Torvalds 		/* All other signals. */
16101da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
16111da177e4SLinus Torvalds 		break;
16121da177e4SLinus Torvalds 	}
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	return perm;
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
1617b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1618b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1619b68e418cSStephen Smalley #endif
1620b68e418cSStephen Smalley 
16211da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
16226a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
16238e4ff6f2SStephen Smalley 			       int cap, int audit, bool initns)
16241da177e4SLinus Torvalds {
16252bf49690SThomas Liu 	struct common_audit_data ad;
162606112163SEric Paris 	struct av_decision avd;
1627b68e418cSStephen Smalley 	u16 sclass;
16283699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1629b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
163006112163SEric Paris 	int rc;
16311da177e4SLinus Torvalds 
163250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
16331da177e4SLinus Torvalds 	ad.u.cap = cap;
16341da177e4SLinus Torvalds 
1635b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1636b68e418cSStephen Smalley 	case 0:
16378e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1638b68e418cSStephen Smalley 		break;
1639b68e418cSStephen Smalley 	case 1:
16408e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1641b68e418cSStephen Smalley 		break;
1642b68e418cSStephen Smalley 	default:
1643c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1644b68e418cSStephen Smalley 		BUG();
1645a35c6c83SEric Paris 		return -EINVAL;
1646b68e418cSStephen Smalley 	}
164706112163SEric Paris 
16486b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
16496b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
16509ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
16516b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
16526b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
16539ade0cf4SEric Paris 		if (rc2)
16549ade0cf4SEric Paris 			return rc2;
16559ade0cf4SEric Paris 	}
165606112163SEric Paris 	return rc;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
16601da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
16611da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
166288e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
16631da177e4SLinus Torvalds 			  struct inode *inode,
16641da177e4SLinus Torvalds 			  u32 perms,
166519e49834SLinus Torvalds 			  struct common_audit_data *adp)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1668275bb41eSDavid Howells 	u32 sid;
16691da177e4SLinus Torvalds 
1670e0e81739SDavid Howells 	validate_creds(cred);
1671e0e81739SDavid Howells 
1672bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1673bbaca6c2SStephen Smalley 		return 0;
1674bbaca6c2SStephen Smalley 
167588e67f3bSDavid Howells 	sid = cred_sid(cred);
16761da177e4SLinus Torvalds 	isec = inode->i_security;
16771da177e4SLinus Torvalds 
16786b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
16796b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16831da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16841da177e4SLinus Torvalds    pathname if needed. */
168588e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16861da177e4SLinus Torvalds 				  struct dentry *dentry,
16871da177e4SLinus Torvalds 				  u32 av)
16881da177e4SLinus Torvalds {
1689c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16902bf49690SThomas Liu 	struct common_audit_data ad;
169188e67f3bSDavid Howells 
169250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16932875fa00SEric Paris 	ad.u.dentry = dentry;
16945d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
169519e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16962875fa00SEric Paris }
16972875fa00SEric Paris 
16982875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16992875fa00SEric Paris    the path to help the auditing code to more easily generate the
17002875fa00SEric Paris    pathname if needed. */
17012875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
17023f7036a0SAl Viro 				const struct path *path,
17032875fa00SEric Paris 				u32 av)
17042875fa00SEric Paris {
1705c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
17062875fa00SEric Paris 	struct common_audit_data ad;
17072875fa00SEric Paris 
170850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
17092875fa00SEric Paris 	ad.u.path = *path;
17105d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
171119e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
17121da177e4SLinus Torvalds }
17131da177e4SLinus Torvalds 
171413f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
171513f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
171613f8e981SDavid Howells 				     struct file *file,
171713f8e981SDavid Howells 				     u32 av)
171813f8e981SDavid Howells {
171913f8e981SDavid Howells 	struct common_audit_data ad;
172013f8e981SDavid Howells 
172143af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
172243af5de7SVivek Goyal 	ad.u.file = file;
172319e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
172413f8e981SDavid Howells }
172513f8e981SDavid Howells 
1726f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1727f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1728f66e448cSChenbo Feng #endif
1729f66e448cSChenbo Feng 
17301da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
17311da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
17321da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
17331da177e4SLinus Torvalds    check a particular permission to the file.
17341da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
17351da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
17361da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
17371da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
173888e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
17391da177e4SLinus Torvalds 			 struct file *file,
17401da177e4SLinus Torvalds 			 u32 av)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1743496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17442bf49690SThomas Liu 	struct common_audit_data ad;
174588e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17461da177e4SLinus Torvalds 	int rc;
17471da177e4SLinus Torvalds 
174843af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
174943af5de7SVivek Goyal 	ad.u.file = file;
17501da177e4SLinus Torvalds 
1751275bb41eSDavid Howells 	if (sid != fsec->sid) {
17526b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
17536b6bc620SStephen Smalley 				  sid, fsec->sid,
17541da177e4SLinus Torvalds 				  SECCLASS_FD,
17551da177e4SLinus Torvalds 				  FD__USE,
17561da177e4SLinus Torvalds 				  &ad);
17571da177e4SLinus Torvalds 		if (rc)
175888e67f3bSDavid Howells 			goto out;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 
1761f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1762f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1763f66e448cSChenbo Feng 	if (rc)
1764f66e448cSChenbo Feng 		return rc;
1765f66e448cSChenbo Feng #endif
1766f66e448cSChenbo Feng 
17671da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
176888e67f3bSDavid Howells 	rc = 0;
17691da177e4SLinus Torvalds 	if (av)
177019e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
17711da177e4SLinus Torvalds 
177288e67f3bSDavid Howells out:
177388e67f3bSDavid Howells 	return rc;
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
1776c3c188b2SDavid Howells /*
1777c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1778c3c188b2SDavid Howells  */
1779c957f6dfSVivek Goyal static int
1780c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1781c957f6dfSVivek Goyal 				 struct inode *dir,
1782c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1783c3c188b2SDavid Howells 				 u32 *_new_isid)
1784c3c188b2SDavid Howells {
1785c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1786c3c188b2SDavid Howells 
1787c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1788c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1789c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1790c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1791c3c188b2SDavid Howells 		   tsec->create_sid) {
1792c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1793c3c188b2SDavid Howells 	} else {
179420cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1795aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1796aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1797c3c188b2SDavid Howells 					       name, _new_isid);
1798c3c188b2SDavid Howells 	}
1799c3c188b2SDavid Howells 
1800c3c188b2SDavid Howells 	return 0;
1801c3c188b2SDavid Howells }
1802c3c188b2SDavid Howells 
18031da177e4SLinus Torvalds /* Check whether a task can create a file. */
18041da177e4SLinus Torvalds static int may_create(struct inode *dir,
18051da177e4SLinus Torvalds 		      struct dentry *dentry,
18061da177e4SLinus Torvalds 		      u16 tclass)
18071da177e4SLinus Torvalds {
18085fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
18091da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
18101da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1811275bb41eSDavid Howells 	u32 sid, newsid;
18122bf49690SThomas Liu 	struct common_audit_data ad;
18131da177e4SLinus Torvalds 	int rc;
18141da177e4SLinus Torvalds 
181583da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
18161da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
18171da177e4SLinus Torvalds 
1818275bb41eSDavid Howells 	sid = tsec->sid;
1819275bb41eSDavid Howells 
182050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1821a269434dSEric Paris 	ad.u.dentry = dentry;
18221da177e4SLinus Torvalds 
18236b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18246b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
18251da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
18261da177e4SLinus Torvalds 			  &ad);
18271da177e4SLinus Torvalds 	if (rc)
18281da177e4SLinus Torvalds 		return rc;
18291da177e4SLinus Torvalds 
1830c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(), dir,
1831c957f6dfSVivek Goyal 					   &dentry->d_name, tclass, &newsid);
18321da177e4SLinus Torvalds 	if (rc)
18331da177e4SLinus Torvalds 		return rc;
18341da177e4SLinus Torvalds 
18356b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18366b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
18371da177e4SLinus Torvalds 	if (rc)
18381da177e4SLinus Torvalds 		return rc;
18391da177e4SLinus Torvalds 
18406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18416b6bc620SStephen Smalley 			    newsid, sbsec->sid,
18421da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
18431da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds #define MAY_LINK	0
18471da177e4SLinus Torvalds #define MAY_UNLINK	1
18481da177e4SLinus Torvalds #define MAY_RMDIR	2
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
18511da177e4SLinus Torvalds static int may_link(struct inode *dir,
18521da177e4SLinus Torvalds 		    struct dentry *dentry,
18531da177e4SLinus Torvalds 		    int kind)
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds {
18561da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
18572bf49690SThomas Liu 	struct common_audit_data ad;
1858275bb41eSDavid Howells 	u32 sid = current_sid();
18591da177e4SLinus Torvalds 	u32 av;
18601da177e4SLinus Torvalds 	int rc;
18611da177e4SLinus Torvalds 
186283da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
186383da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
18641da177e4SLinus Torvalds 
186550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1866a269434dSEric Paris 	ad.u.dentry = dentry;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	av = DIR__SEARCH;
18691da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
18706b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18716b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
18721da177e4SLinus Torvalds 	if (rc)
18731da177e4SLinus Torvalds 		return rc;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	switch (kind) {
18761da177e4SLinus Torvalds 	case MAY_LINK:
18771da177e4SLinus Torvalds 		av = FILE__LINK;
18781da177e4SLinus Torvalds 		break;
18791da177e4SLinus Torvalds 	case MAY_UNLINK:
18801da177e4SLinus Torvalds 		av = FILE__UNLINK;
18811da177e4SLinus Torvalds 		break;
18821da177e4SLinus Torvalds 	case MAY_RMDIR:
18831da177e4SLinus Torvalds 		av = DIR__RMDIR;
18841da177e4SLinus Torvalds 		break;
18851da177e4SLinus Torvalds 	default:
1886c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
1887744ba35eSEric Paris 			__func__, kind);
18881da177e4SLinus Torvalds 		return 0;
18891da177e4SLinus Torvalds 	}
18901da177e4SLinus Torvalds 
18916b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18926b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
18931da177e4SLinus Torvalds 	return rc;
18941da177e4SLinus Torvalds }
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
18971da177e4SLinus Torvalds 			     struct dentry *old_dentry,
18981da177e4SLinus Torvalds 			     struct inode *new_dir,
18991da177e4SLinus Torvalds 			     struct dentry *new_dentry)
19001da177e4SLinus Torvalds {
19011da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
19022bf49690SThomas Liu 	struct common_audit_data ad;
1903275bb41eSDavid Howells 	u32 sid = current_sid();
19041da177e4SLinus Torvalds 	u32 av;
19051da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
19061da177e4SLinus Torvalds 	int rc;
19071da177e4SLinus Torvalds 
190883da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
190983da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
1910e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
191183da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
19121da177e4SLinus Torvalds 
191350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
19141da177e4SLinus Torvalds 
1915a269434dSEric Paris 	ad.u.dentry = old_dentry;
19166b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19176b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
19181da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
19191da177e4SLinus Torvalds 	if (rc)
19201da177e4SLinus Torvalds 		return rc;
19216b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19226b6bc620SStephen Smalley 			  sid, old_isec->sid,
19231da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
19241da177e4SLinus Torvalds 	if (rc)
19251da177e4SLinus Torvalds 		return rc;
19261da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
19276b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19286b6bc620SStephen Smalley 				  sid, old_isec->sid,
19291da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
19301da177e4SLinus Torvalds 		if (rc)
19311da177e4SLinus Torvalds 			return rc;
19321da177e4SLinus Torvalds 	}
19331da177e4SLinus Torvalds 
1934a269434dSEric Paris 	ad.u.dentry = new_dentry;
19351da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
19362c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
19371da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
19386b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19396b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
19401da177e4SLinus Torvalds 	if (rc)
19411da177e4SLinus Torvalds 		return rc;
19422c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
194383da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
1944e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
19456b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19466b6bc620SStephen Smalley 				  sid, new_isec->sid,
19471da177e4SLinus Torvalds 				  new_isec->sclass,
19481da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
19491da177e4SLinus Torvalds 		if (rc)
19501da177e4SLinus Torvalds 			return rc;
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	return 0;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
195788e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
19581da177e4SLinus Torvalds 			       struct super_block *sb,
19591da177e4SLinus Torvalds 			       u32 perms,
19602bf49690SThomas Liu 			       struct common_audit_data *ad)
19611da177e4SLinus Torvalds {
19621da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
196388e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	sbsec = sb->s_security;
19666b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19676b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
19711da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
19721da177e4SLinus Torvalds {
19731da177e4SLinus Torvalds 	u32 av = 0;
19741da177e4SLinus Torvalds 
1975dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
19761da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19771da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
19781da177e4SLinus Torvalds 		if (mask & MAY_READ)
19791da177e4SLinus Torvalds 			av |= FILE__READ;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
19821da177e4SLinus Torvalds 			av |= FILE__APPEND;
19831da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
19841da177e4SLinus Torvalds 			av |= FILE__WRITE;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	} else {
19871da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19881da177e4SLinus Torvalds 			av |= DIR__SEARCH;
19891da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
19901da177e4SLinus Torvalds 			av |= DIR__WRITE;
19911da177e4SLinus Torvalds 		if (mask & MAY_READ)
19921da177e4SLinus Torvalds 			av |= DIR__READ;
19931da177e4SLinus Torvalds 	}
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds 	return av;
19961da177e4SLinus Torvalds }
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
19991da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	u32 av = 0;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
20041da177e4SLinus Torvalds 		av |= FILE__READ;
20051da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
20061da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
20071da177e4SLinus Torvalds 			av |= FILE__APPEND;
20081da177e4SLinus Torvalds 		else
20091da177e4SLinus Torvalds 			av |= FILE__WRITE;
20101da177e4SLinus Torvalds 	}
20110794c66dSStephen Smalley 	if (!av) {
20120794c66dSStephen Smalley 		/*
20130794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
20140794c66dSStephen Smalley 		 */
20150794c66dSStephen Smalley 		av = FILE__IOCTL;
20160794c66dSStephen Smalley 	}
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	return av;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20218b6a5a37SEric Paris /*
20228b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
20238b6a5a37SEric Paris  * open permission.
20248b6a5a37SEric Paris  */
20258b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
20268b6a5a37SEric Paris {
20278b6a5a37SEric Paris 	u32 av = file_to_av(file);
2028ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
20298b6a5a37SEric Paris 
2030aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
2031aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
20328b6a5a37SEric Paris 		av |= FILE__OPEN;
203349b7b8deSEric Paris 
20348b6a5a37SEric Paris 	return av;
20358b6a5a37SEric Paris }
20368b6a5a37SEric Paris 
20371da177e4SLinus Torvalds /* Hook functions begin here. */
20381da177e4SLinus Torvalds 
203979af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
204079af7307SStephen Smalley {
204179af7307SStephen Smalley 	u32 mysid = current_sid();
204279af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
204379af7307SStephen Smalley 
20446b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20456b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
204679af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
204779af7307SStephen Smalley }
204879af7307SStephen Smalley 
204979af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
205079af7307SStephen Smalley 				      struct task_struct *to)
205179af7307SStephen Smalley {
205279af7307SStephen Smalley 	u32 mysid = current_sid();
205379af7307SStephen Smalley 	u32 fromsid = task_sid(from);
205479af7307SStephen Smalley 	u32 tosid = task_sid(to);
205579af7307SStephen Smalley 	int rc;
205679af7307SStephen Smalley 
205779af7307SStephen Smalley 	if (mysid != fromsid) {
20586b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20596b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
206079af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
206179af7307SStephen Smalley 		if (rc)
206279af7307SStephen Smalley 			return rc;
206379af7307SStephen Smalley 	}
206479af7307SStephen Smalley 
20656b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20666b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
206779af7307SStephen Smalley 			    NULL);
206879af7307SStephen Smalley }
206979af7307SStephen Smalley 
207079af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
207179af7307SStephen Smalley 					  struct task_struct *to)
207279af7307SStephen Smalley {
207379af7307SStephen Smalley 	u32 fromsid = task_sid(from);
207479af7307SStephen Smalley 	u32 tosid = task_sid(to);
207579af7307SStephen Smalley 
20766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20776b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
207879af7307SStephen Smalley 			    NULL);
207979af7307SStephen Smalley }
208079af7307SStephen Smalley 
208179af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
208279af7307SStephen Smalley 					struct task_struct *to,
208379af7307SStephen Smalley 					struct file *file)
208479af7307SStephen Smalley {
208579af7307SStephen Smalley 	u32 sid = task_sid(to);
208679af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
208783da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
208820cdef8dSPaul Moore 	struct inode_security_struct *isec;
208979af7307SStephen Smalley 	struct common_audit_data ad;
209079af7307SStephen Smalley 	int rc;
209179af7307SStephen Smalley 
209279af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
209379af7307SStephen Smalley 	ad.u.path = file->f_path;
209479af7307SStephen Smalley 
209579af7307SStephen Smalley 	if (sid != fsec->sid) {
20966b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20976b6bc620SStephen Smalley 				  sid, fsec->sid,
209879af7307SStephen Smalley 				  SECCLASS_FD,
209979af7307SStephen Smalley 				  FD__USE,
210079af7307SStephen Smalley 				  &ad);
210179af7307SStephen Smalley 		if (rc)
210279af7307SStephen Smalley 			return rc;
210379af7307SStephen Smalley 	}
210479af7307SStephen Smalley 
2105f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2106f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2107f66e448cSChenbo Feng 	if (rc)
2108f66e448cSChenbo Feng 		return rc;
2109f66e448cSChenbo Feng #endif
2110f66e448cSChenbo Feng 
211183da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
211279af7307SStephen Smalley 		return 0;
211379af7307SStephen Smalley 
211420cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
21156b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21166b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
211779af7307SStephen Smalley 			    &ad);
211879af7307SStephen Smalley }
211979af7307SStephen Smalley 
21209e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2121006ebb40SStephen Smalley 				     unsigned int mode)
21221da177e4SLinus Torvalds {
2123275bb41eSDavid Howells 	u32 sid = current_sid();
2124275bb41eSDavid Howells 	u32 csid = task_sid(child);
2125006ebb40SStephen Smalley 
2126be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
21276b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21286b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2129be0554c9SStephen Smalley 
21306b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21316b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
21325cd9c58fSDavid Howells }
21335cd9c58fSDavid Howells 
21345cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
21355cd9c58fSDavid Howells {
21366b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21376b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2138be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
21421da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
21431da177e4SLinus Torvalds {
21446b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21456b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2146be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
2149d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2150d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
215115a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
215215a2460eSDavid Howells 			  const kernel_cap_t *permitted)
21531da177e4SLinus Torvalds {
21546b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21556b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2156be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21595626d3e8SJames Morris /*
21605626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
21615626d3e8SJames Morris  * which was removed).
21625626d3e8SJames Morris  *
21635626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
21645626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
21655626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
21665626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
21675626d3e8SJames Morris  */
21685626d3e8SJames Morris 
21696a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
21706a9de491SEric Paris 			   int cap, int audit)
21711da177e4SLinus Torvalds {
21728e4ff6f2SStephen Smalley 	return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
21761da177e4SLinus Torvalds {
217788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
21781da177e4SLinus Torvalds 	int rc = 0;
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	if (!sb)
21811da177e4SLinus Torvalds 		return 0;
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	switch (cmds) {
21841da177e4SLinus Torvalds 	case Q_SYNC:
21851da177e4SLinus Torvalds 	case Q_QUOTAON:
21861da177e4SLinus Torvalds 	case Q_QUOTAOFF:
21871da177e4SLinus Torvalds 	case Q_SETINFO:
21881da177e4SLinus Torvalds 	case Q_SETQUOTA:
218988e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
21901da177e4SLinus Torvalds 		break;
21911da177e4SLinus Torvalds 	case Q_GETFMT:
21921da177e4SLinus Torvalds 	case Q_GETINFO:
21931da177e4SLinus Torvalds 	case Q_GETQUOTA:
219488e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
21951da177e4SLinus Torvalds 		break;
21961da177e4SLinus Torvalds 	default:
21971da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
21981da177e4SLinus Torvalds 		break;
21991da177e4SLinus Torvalds 	}
22001da177e4SLinus Torvalds 	return rc;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
22041da177e4SLinus Torvalds {
220588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
220688e67f3bSDavid Howells 
22072875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
221012b3052cSEric Paris static int selinux_syslog(int type)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	switch (type) {
2213d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2214d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
22156b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22166b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2217be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2218d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2219d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2220d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2221d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
22226b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22236b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2224be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2225be0554c9SStephen Smalley 				    NULL);
22261da177e4SLinus Torvalds 	}
2227be0554c9SStephen Smalley 	/* All other syslog types */
22286b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22296b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2230be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
22311da177e4SLinus Torvalds }
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds /*
22341da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
22351da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
22361da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
22371da177e4SLinus Torvalds  *
22381da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
22391da177e4SLinus Torvalds  * processes that allocate mappings.
22401da177e4SLinus Torvalds  */
224134b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
22441da177e4SLinus Torvalds 
2245b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
22468e4ff6f2SStephen Smalley 				 SECURITY_CAP_NOAUDIT, true);
22471da177e4SLinus Torvalds 	if (rc == 0)
22481da177e4SLinus Torvalds 		cap_sys_admin = 1;
22491da177e4SLinus Torvalds 
2250b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds /* binprm security operations */
22541da177e4SLinus Torvalds 
2255be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
22560c6181cbSPaul Moore {
22570c6181cbSPaul Moore 	u32 sid = 0;
22580c6181cbSPaul Moore 	struct task_struct *tracer;
22590c6181cbSPaul Moore 
22600c6181cbSPaul Moore 	rcu_read_lock();
2261be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
22620c6181cbSPaul Moore 	if (tracer)
22630c6181cbSPaul Moore 		sid = task_sid(tracer);
22640c6181cbSPaul Moore 	rcu_read_unlock();
22650c6181cbSPaul Moore 
22660c6181cbSPaul Moore 	return sid;
22670c6181cbSPaul Moore }
22680c6181cbSPaul Moore 
22697b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
22707b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
22717b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
22727b0d0b40SStephen Smalley {
22737b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2274380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
22757b0d0b40SStephen Smalley 	int rc;
2276af63f419SStephen Smalley 	u32 av;
22777b0d0b40SStephen Smalley 
22787b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
22797b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
22807b0d0b40SStephen Smalley 
22817b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
22827b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
22837b0d0b40SStephen Smalley 
22847b0d0b40SStephen Smalley 	/*
2285af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2286af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2287af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2288af63f419SStephen Smalley 	 * the old and new contexts.
2289af63f419SStephen Smalley 	 */
2290aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2291af63f419SStephen Smalley 		av = 0;
2292af63f419SStephen Smalley 		if (nnp)
2293af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2294af63f419SStephen Smalley 		if (nosuid)
2295af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
22966b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22976b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2298af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2299af63f419SStephen Smalley 		if (!rc)
2300af63f419SStephen Smalley 			return 0;
2301af63f419SStephen Smalley 	}
2302af63f419SStephen Smalley 
2303af63f419SStephen Smalley 	/*
2304af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2305af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2306af63f419SStephen Smalley 	 * of the permissions of the current SID.
23077b0d0b40SStephen Smalley 	 */
2308aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2309aa8e712cSStephen Smalley 					 new_tsec->sid);
2310af63f419SStephen Smalley 	if (!rc)
2311af63f419SStephen Smalley 		return 0;
2312af63f419SStephen Smalley 
23137b0d0b40SStephen Smalley 	/*
23147b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
23157b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
23167b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
23177b0d0b40SStephen Smalley 	 */
23187b0d0b40SStephen Smalley 	if (nnp)
23197b0d0b40SStephen Smalley 		return -EPERM;
23207b0d0b40SStephen Smalley 	return -EACCES;
23217b0d0b40SStephen Smalley }
23227b0d0b40SStephen Smalley 
2323a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
23241da177e4SLinus Torvalds {
2325a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2326a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23271da177e4SLinus Torvalds 	struct inode_security_struct *isec;
23282bf49690SThomas Liu 	struct common_audit_data ad;
2329496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
23301da177e4SLinus Torvalds 	int rc;
23311da177e4SLinus Torvalds 
2332a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2333a6f76f23SDavid Howells 	 * the script interpreter */
2334ddb4a144SKees Cook 	if (bprm->called_set_creds)
23351da177e4SLinus Torvalds 		return 0;
23361da177e4SLinus Torvalds 
2337a6f76f23SDavid Howells 	old_tsec = current_security();
2338a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
233983da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	/* Default to the current task SID. */
2342a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2343a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
23441da177e4SLinus Torvalds 
234528eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2346a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2347a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2348a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
23491da177e4SLinus Torvalds 
2350a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2351a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
23521da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2353a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2354259e5e6cSAndy Lutomirski 
23557b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
23567b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23577b0d0b40SStephen Smalley 		if (rc)
23587b0d0b40SStephen Smalley 			return rc;
23591da177e4SLinus Torvalds 	} else {
23601da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2361aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2362aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2363652bb9b0SEric Paris 					     &new_tsec->sid);
23641da177e4SLinus Torvalds 		if (rc)
23651da177e4SLinus Torvalds 			return rc;
23667b0d0b40SStephen Smalley 
23677b0d0b40SStephen Smalley 		/*
23687b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
23697b0d0b40SStephen Smalley 		 * transition.
23707b0d0b40SStephen Smalley 		 */
23717b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23727b0d0b40SStephen Smalley 		if (rc)
23737b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
23741da177e4SLinus Torvalds 	}
23751da177e4SLinus Torvalds 
237643af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
237743af5de7SVivek Goyal 	ad.u.file = bprm->file;
23781da177e4SLinus Torvalds 
2379a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
23806b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23816b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
23821da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
23831da177e4SLinus Torvalds 		if (rc)
23841da177e4SLinus Torvalds 			return rc;
23851da177e4SLinus Torvalds 	} else {
23861da177e4SLinus Torvalds 		/* Check permissions for the transition. */
23876b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23886b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
23891da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
23901da177e4SLinus Torvalds 		if (rc)
23911da177e4SLinus Torvalds 			return rc;
23921da177e4SLinus Torvalds 
23936b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23946b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
23951da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
23961da177e4SLinus Torvalds 		if (rc)
23971da177e4SLinus Torvalds 			return rc;
23981da177e4SLinus Torvalds 
2399a6f76f23SDavid Howells 		/* Check for shared state */
2400a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
24016b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
24026b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2403a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2404a6f76f23SDavid Howells 					  NULL);
2405a6f76f23SDavid Howells 			if (rc)
2406a6f76f23SDavid Howells 				return -EPERM;
24071da177e4SLinus Torvalds 		}
24081da177e4SLinus Torvalds 
2409a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2410a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
24119227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2412be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2413a6f76f23SDavid Howells 			if (ptsid != 0) {
24146b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
24156b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2416a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2417a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2418a6f76f23SDavid Howells 				if (rc)
2419a6f76f23SDavid Howells 					return -EPERM;
2420a6f76f23SDavid Howells 			}
2421a6f76f23SDavid Howells 		}
2422a6f76f23SDavid Howells 
2423a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2424a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2425a6f76f23SDavid Howells 
24261da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
24271da177e4SLinus Torvalds 		   the noatsecure permission is granted between
24281da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
24296b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
24306b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
243162874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
243262874c3aSKees Cook 				  NULL);
243362874c3aSKees Cook 		bprm->secureexec |= !!rc;
24341da177e4SLinus Torvalds 	}
24351da177e4SLinus Torvalds 
243662874c3aSKees Cook 	return 0;
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
2439c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2440c3c073f8SAl Viro {
2441c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2442c3c073f8SAl Viro }
2443c3c073f8SAl Viro 
24441da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2445745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2446745ca247SDavid Howells 					    struct files_struct *files)
24471da177e4SLinus Torvalds {
24481da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2449b20c8122SStephen Smalley 	struct tty_struct *tty;
245024ec839cSPeter Zijlstra 	int drop_tty = 0;
2451c3c073f8SAl Viro 	unsigned n;
24521da177e4SLinus Torvalds 
245324ec839cSPeter Zijlstra 	tty = get_current_tty();
24541da177e4SLinus Torvalds 	if (tty) {
24554a510969SPeter Hurley 		spin_lock(&tty->files_lock);
245637dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2457d996b62aSNick Piggin 			struct tty_file_private *file_priv;
245837dd0bd0SEric Paris 
24591da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
246013f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
246113f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
246213f8e981SDavid Howells 			   open file may belong to another process and we are
246313f8e981SDavid Howells 			   only interested in the inode-based check here. */
2464d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2465d996b62aSNick Piggin 						struct tty_file_private, list);
2466d996b62aSNick Piggin 			file = file_priv->file;
246713f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
246824ec839cSPeter Zijlstra 				drop_tty = 1;
24691da177e4SLinus Torvalds 		}
24704a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2471452a00d2SAlan Cox 		tty_kref_put(tty);
24721da177e4SLinus Torvalds 	}
247398a27ba4SEric W. Biederman 	/* Reset controlling tty. */
247498a27ba4SEric W. Biederman 	if (drop_tty)
247598a27ba4SEric W. Biederman 		no_tty();
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2478c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2479c3c073f8SAl Viro 	if (!n) /* none found? */
2480c3c073f8SAl Viro 		return;
24811da177e4SLinus Torvalds 
2482c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
248345525b26SAl Viro 	if (IS_ERR(devnull))
248445525b26SAl Viro 		devnull = NULL;
2485c3c073f8SAl Viro 	/* replace all the matching ones with this */
2486c3c073f8SAl Viro 	do {
248745525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2488c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
248945525b26SAl Viro 	if (devnull)
2490c3c073f8SAl Viro 		fput(devnull);
24911da177e4SLinus Torvalds }
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds /*
2494a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
24951da177e4SLinus Torvalds  */
2496a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
24971da177e4SLinus Torvalds {
2498a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24991da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
25001da177e4SLinus Torvalds 	int rc, i;
25011da177e4SLinus Torvalds 
2502a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2503a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
25041da177e4SLinus Torvalds 		return;
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2507a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
25081da177e4SLinus Torvalds 
2509a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2510a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2511a6f76f23SDavid Howells 
2512a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2513a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2514a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2515a6f76f23SDavid Howells 	 *
2516a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2517a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2518a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2519a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2520a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2521a6f76f23SDavid Howells 	 */
25226b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25236b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2524a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2525a6f76f23SDavid Howells 	if (rc) {
2526eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2527eb2d55a3SOleg Nesterov 		task_lock(current);
2528a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2529a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2530a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2531a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2532a6f76f23SDavid Howells 		}
2533eb2d55a3SOleg Nesterov 		task_unlock(current);
2534baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2535eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2536a6f76f23SDavid Howells 	}
2537a6f76f23SDavid Howells }
2538a6f76f23SDavid Howells 
2539a6f76f23SDavid Howells /*
2540a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2541a6f76f23SDavid Howells  * due to exec
2542a6f76f23SDavid Howells  */
2543a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2544a6f76f23SDavid Howells {
2545a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2546a6f76f23SDavid Howells 	struct itimerval itimer;
2547a6f76f23SDavid Howells 	u32 osid, sid;
2548a6f76f23SDavid Howells 	int rc, i;
2549a6f76f23SDavid Howells 
2550a6f76f23SDavid Howells 	osid = tsec->osid;
2551a6f76f23SDavid Howells 	sid = tsec->sid;
2552a6f76f23SDavid Howells 
2553a6f76f23SDavid Howells 	if (sid == osid)
2554a6f76f23SDavid Howells 		return;
2555a6f76f23SDavid Howells 
2556a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2557a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2558a6f76f23SDavid Howells 	 * flush and unblock signals.
2559a6f76f23SDavid Howells 	 *
2560a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2561a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2562a6f76f23SDavid Howells 	 */
25636b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25646b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
25651da177e4SLinus Torvalds 	if (rc) {
2566baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
25671da177e4SLinus Torvalds 			memset(&itimer, 0, sizeof itimer);
25681da177e4SLinus Torvalds 			for (i = 0; i < 3; i++)
25691da177e4SLinus Torvalds 				do_setitimer(i, &itimer, NULL);
2570baa73d9eSNicolas Pitre 		}
25711da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
25729e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
25739e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
25749e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
25751da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
25761da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
25779e7c8f8cSOleg Nesterov 			recalc_sigpending();
25783bcac026SDavid Howells 		}
25791da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
25801da177e4SLinus Torvalds 	}
25811da177e4SLinus Torvalds 
2582a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2583a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2584ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
25850b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2586ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
25871da177e4SLinus Torvalds }
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds /* superblock security operations */
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
25921da177e4SLinus Torvalds {
25931da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
25941da177e4SLinus Torvalds }
25951da177e4SLinus Torvalds 
25961da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
25971da177e4SLinus Torvalds {
25981da177e4SLinus Torvalds 	superblock_free_security(sb);
25991da177e4SLinus Torvalds }
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
26021da177e4SLinus Torvalds {
26031da177e4SLinus Torvalds 	if (plen > olen)
26041da177e4SLinus Torvalds 		return 0;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
26101da177e4SLinus Torvalds {
2611832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2612832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2613832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
261411689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
261511689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
26191da177e4SLinus Torvalds {
26201da177e4SLinus Torvalds 	if (!*first) {
26211da177e4SLinus Torvalds 		**to = ',';
26221da177e4SLinus Torvalds 		*to += 1;
26233528a953SCory Olmo 	} else
26241da177e4SLinus Torvalds 		*first = 0;
26251da177e4SLinus Torvalds 	memcpy(*to, from, len);
26261da177e4SLinus Torvalds 	*to += len;
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26293528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
26303528a953SCory Olmo 				       int len)
26313528a953SCory Olmo {
26323528a953SCory Olmo 	int current_size = 0;
26333528a953SCory Olmo 
26343528a953SCory Olmo 	if (!*first) {
26353528a953SCory Olmo 		**to = '|';
26363528a953SCory Olmo 		*to += 1;
2637828dfe1dSEric Paris 	} else
26383528a953SCory Olmo 		*first = 0;
26393528a953SCory Olmo 
26403528a953SCory Olmo 	while (current_size < len) {
26413528a953SCory Olmo 		if (*from != '"') {
26423528a953SCory Olmo 			**to = *from;
26433528a953SCory Olmo 			*to += 1;
26443528a953SCory Olmo 		}
26453528a953SCory Olmo 		from += 1;
26463528a953SCory Olmo 		current_size += 1;
26473528a953SCory Olmo 	}
26483528a953SCory Olmo }
26493528a953SCory Olmo 
2650e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
26511da177e4SLinus Torvalds {
26521da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
26531da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
26541da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
26553528a953SCory Olmo 	int open_quote = 0;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 	in_curr = orig;
26581da177e4SLinus Torvalds 	sec_curr = copy;
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
26611da177e4SLinus Torvalds 	if (!nosec) {
26621da177e4SLinus Torvalds 		rc = -ENOMEM;
26631da177e4SLinus Torvalds 		goto out;
26641da177e4SLinus Torvalds 	}
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	nosec_save = nosec;
26671da177e4SLinus Torvalds 	fnosec = fsec = 1;
26681da177e4SLinus Torvalds 	in_save = in_end = orig;
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	do {
26713528a953SCory Olmo 		if (*in_end == '"')
26723528a953SCory Olmo 			open_quote = !open_quote;
26733528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
26743528a953SCory Olmo 				*in_end == '\0') {
26751da177e4SLinus Torvalds 			int len = in_end - in_curr;
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
26783528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
26791da177e4SLinus Torvalds 			else
26801da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
26811da177e4SLinus Torvalds 
26821da177e4SLinus Torvalds 			in_curr = in_end + 1;
26831da177e4SLinus Torvalds 		}
26841da177e4SLinus Torvalds 	} while (*in_end++);
26851da177e4SLinus Torvalds 
26866931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2687da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
26881da177e4SLinus Torvalds out:
26891da177e4SLinus Torvalds 	return rc;
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds 
2692204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26935b400239SAl Viro {
26945b400239SAl Viro 	char *s = (char *)get_zeroed_page(GFP_KERNEL);
26955b400239SAl Viro 	int err;
26965b400239SAl Viro 
26975b400239SAl Viro 	if (!s)
26985b400239SAl Viro 		return -ENOMEM;
26995b400239SAl Viro 	err = selinux_sb_copy_data(options, s);
27005b400239SAl Viro 	if (!err)
2701204cc0ccSAl Viro 		err = selinux_parse_opts_str(s, mnt_opts);
27025b400239SAl Viro 	free_page((unsigned long)s);
27035b400239SAl Viro 	return err;
27045b400239SAl Viro }
27055b400239SAl Viro 
2706204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2707026eb167SEric Paris {
2708*bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
2709026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2710*bd323655SAl Viro 	u32 sid;
2711*bd323655SAl Viro 	int rc;
2712026eb167SEric Paris 
2713026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2714026eb167SEric Paris 		return 0;
2715026eb167SEric Paris 
2716204cc0ccSAl Viro 	if (!opts)
2717204cc0ccSAl Viro 		return 0;
2718204cc0ccSAl Viro 
2719*bd323655SAl Viro 	if (opts->fscontext) {
2720*bd323655SAl Viro 		rc = parse_sid(sb, opts->fscontext, &sid);
2721*bd323655SAl Viro 		if (rc)
2722c039bc3cSAl Viro 			return rc;
2723026eb167SEric Paris 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2724026eb167SEric Paris 			goto out_bad_option;
2725*bd323655SAl Viro 	}
2726*bd323655SAl Viro 	if (opts->context) {
2727*bd323655SAl Viro 		rc = parse_sid(sb, opts->context, &sid);
2728*bd323655SAl Viro 		if (rc)
2729*bd323655SAl Viro 			return rc;
2730026eb167SEric Paris 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2731026eb167SEric Paris 			goto out_bad_option;
2732*bd323655SAl Viro 	}
2733*bd323655SAl Viro 	if (opts->rootcontext) {
2734026eb167SEric Paris 		struct inode_security_struct *root_isec;
273583da53c5SAndreas Gruenbacher 		root_isec = backing_inode_security(sb->s_root);
2736*bd323655SAl Viro 		rc = parse_sid(sb, opts->rootcontext, &sid);
2737*bd323655SAl Viro 		if (rc)
2738*bd323655SAl Viro 			return rc;
2739026eb167SEric Paris 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2740026eb167SEric Paris 			goto out_bad_option;
2741026eb167SEric Paris 	}
2742*bd323655SAl Viro 	if (opts->defcontext) {
2743*bd323655SAl Viro 		rc = parse_sid(sb, opts->defcontext, &sid);
2744*bd323655SAl Viro 		if (rc)
2745*bd323655SAl Viro 			return rc;
2746026eb167SEric Paris 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2747026eb167SEric Paris 			goto out_bad_option;
2748026eb167SEric Paris 	}
2749c039bc3cSAl Viro 	return 0;
2750026eb167SEric Paris 
2751026eb167SEric Paris out_bad_option:
2752c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
275329b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
275429b1deb2SLinus Torvalds 	       sb->s_type->name);
2755c039bc3cSAl Viro 	return -EINVAL;
2756026eb167SEric Paris }
2757026eb167SEric Paris 
2758a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb)
27591da177e4SLinus Torvalds {
276088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27612bf49690SThomas Liu 	struct common_audit_data ad;
276274192246SJames Morris 
276350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2764a269434dSEric Paris 	ad.u.dentry = sb->s_root;
276588e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
2768726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
27691da177e4SLinus Torvalds {
277088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27712bf49690SThomas Liu 	struct common_audit_data ad;
27721da177e4SLinus Torvalds 
277350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2774a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
277588e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
2778808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
27798a04c43bSAl Viro 			 const struct path *path,
2780808d4e3cSAl Viro 			 const char *type,
27811da177e4SLinus Torvalds 			 unsigned long flags,
27821da177e4SLinus Torvalds 			 void *data)
27831da177e4SLinus Torvalds {
278488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2787d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
27881da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
27891da177e4SLinus Torvalds 	else
27902875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27911da177e4SLinus Torvalds }
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27941da177e4SLinus Torvalds {
279588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27961da177e4SLinus Torvalds 
279788e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27981da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds /* inode security operations */
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
28041da177e4SLinus Torvalds {
28051da177e4SLinus Torvalds 	return inode_alloc_security(inode);
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	inode_free_security(inode);
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
2813d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
28144f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2815d47be3dfSDavid Quigley 					u32 *ctxlen)
2816d47be3dfSDavid Quigley {
2817d47be3dfSDavid Quigley 	u32 newsid;
2818d47be3dfSDavid Quigley 	int rc;
2819d47be3dfSDavid Quigley 
2820c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2821c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2822d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2823d47be3dfSDavid Quigley 					   &newsid);
2824c3c188b2SDavid Howells 	if (rc)
2825d47be3dfSDavid Quigley 		return rc;
2826d47be3dfSDavid Quigley 
2827aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2828aa8e712cSStephen Smalley 				       ctxlen);
2829d47be3dfSDavid Quigley }
2830d47be3dfSDavid Quigley 
2831a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2832a518b0a5SVivek Goyal 					  struct qstr *name,
2833a518b0a5SVivek Goyal 					  const struct cred *old,
2834a518b0a5SVivek Goyal 					  struct cred *new)
2835a518b0a5SVivek Goyal {
2836a518b0a5SVivek Goyal 	u32 newsid;
2837a518b0a5SVivek Goyal 	int rc;
2838a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2839a518b0a5SVivek Goyal 
2840a518b0a5SVivek Goyal 	rc = selinux_determine_inode_label(old->security,
2841a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
2842a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
2843a518b0a5SVivek Goyal 					   &newsid);
2844a518b0a5SVivek Goyal 	if (rc)
2845a518b0a5SVivek Goyal 		return rc;
2846a518b0a5SVivek Goyal 
2847a518b0a5SVivek Goyal 	tsec = new->security;
2848a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
2849a518b0a5SVivek Goyal 	return 0;
2850a518b0a5SVivek Goyal }
2851a518b0a5SVivek Goyal 
28525e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
28539548906bSTetsuo Handa 				       const struct qstr *qstr,
28549548906bSTetsuo Handa 				       const char **name,
28552a7dba39SEric Paris 				       void **value, size_t *len)
28565e41ff9eSStephen Smalley {
28575fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
28585e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2859c0d4f464SCorentin LABBE 	u32 newsid, clen;
28605e41ff9eSStephen Smalley 	int rc;
28619548906bSTetsuo Handa 	char *context;
28625e41ff9eSStephen Smalley 
28635e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
28645e41ff9eSStephen Smalley 
28655e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2866275bb41eSDavid Howells 
2867c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2868c3c188b2SDavid Howells 		dir, qstr,
28695e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
2870c3c188b2SDavid Howells 		&newsid);
2871c3c188b2SDavid Howells 	if (rc)
28725e41ff9eSStephen Smalley 		return rc;
28735e41ff9eSStephen Smalley 
2874296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
28750d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2876296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2877296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2878296fddf7SEric Paris 		isec->sid = newsid;
28796f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
2880296fddf7SEric Paris 	}
28815e41ff9eSStephen Smalley 
2882aa8e712cSStephen Smalley 	if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
288325a74f3bSStephen Smalley 		return -EOPNOTSUPP;
288425a74f3bSStephen Smalley 
28859548906bSTetsuo Handa 	if (name)
28869548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
28875e41ff9eSStephen Smalley 
2888570bc1c2SStephen Smalley 	if (value && len) {
2889aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
2890aa8e712cSStephen Smalley 						   &context, &clen);
28919548906bSTetsuo Handa 		if (rc)
28925e41ff9eSStephen Smalley 			return rc;
28935e41ff9eSStephen Smalley 		*value = context;
2894570bc1c2SStephen Smalley 		*len = clen;
2895570bc1c2SStephen Smalley 	}
28965e41ff9eSStephen Smalley 
28975e41ff9eSStephen Smalley 	return 0;
28985e41ff9eSStephen Smalley }
28995e41ff9eSStephen Smalley 
29004acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
29011da177e4SLinus Torvalds {
29021da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
29031da177e4SLinus Torvalds }
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29061da177e4SLinus Torvalds {
29071da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
29081da177e4SLinus Torvalds }
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
29111da177e4SLinus Torvalds {
29121da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
29131da177e4SLinus Torvalds }
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
29161da177e4SLinus Torvalds {
29171da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
29181da177e4SLinus Torvalds }
29191da177e4SLinus Torvalds 
292018bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
29211da177e4SLinus Torvalds {
29221da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
29261da177e4SLinus Torvalds {
29271da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29301a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
29311da177e4SLinus Torvalds {
29321da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
29331da177e4SLinus Torvalds }
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
29361da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
29371da177e4SLinus Torvalds {
29381da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
29391da177e4SLinus Torvalds }
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
29421da177e4SLinus Torvalds {
294388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
294488e67f3bSDavid Howells 
29452875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
29461da177e4SLinus Torvalds }
29471da177e4SLinus Torvalds 
2948bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2949bda0be7aSNeilBrown 				     bool rcu)
29501da177e4SLinus Torvalds {
295188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2952bda0be7aSNeilBrown 	struct common_audit_data ad;
2953bda0be7aSNeilBrown 	struct inode_security_struct *isec;
2954bda0be7aSNeilBrown 	u32 sid;
29551da177e4SLinus Torvalds 
2956bda0be7aSNeilBrown 	validate_creds(cred);
2957bda0be7aSNeilBrown 
2958bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
2959bda0be7aSNeilBrown 	ad.u.dentry = dentry;
2960bda0be7aSNeilBrown 	sid = cred_sid(cred);
29615d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
29625d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
29635d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
2964bda0be7aSNeilBrown 
29656b6bc620SStephen Smalley 	return avc_has_perm_flags(&selinux_state,
29666b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
2967bda0be7aSNeilBrown 				  rcu ? MAY_NOT_BLOCK : 0);
29681da177e4SLinus Torvalds }
29691da177e4SLinus Torvalds 
2970d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2971d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2972626b9740SStephen Smalley 					   int result,
2973d4cf970dSEric Paris 					   unsigned flags)
2974d4cf970dSEric Paris {
2975d4cf970dSEric Paris 	struct common_audit_data ad;
2976d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2977d4cf970dSEric Paris 	int rc;
2978d4cf970dSEric Paris 
297950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2980d4cf970dSEric Paris 	ad.u.inode = inode;
2981d4cf970dSEric Paris 
29826b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
29836b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
2984626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
2985d4cf970dSEric Paris 	if (rc)
2986d4cf970dSEric Paris 		return rc;
2987d4cf970dSEric Paris 	return 0;
2988d4cf970dSEric Paris }
2989d4cf970dSEric Paris 
2990e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
29911da177e4SLinus Torvalds {
299288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2993b782e0a6SEric Paris 	u32 perms;
2994b782e0a6SEric Paris 	bool from_access;
2995cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
29962e334057SEric Paris 	struct inode_security_struct *isec;
29972e334057SEric Paris 	u32 sid;
29982e334057SEric Paris 	struct av_decision avd;
29992e334057SEric Paris 	int rc, rc2;
30002e334057SEric Paris 	u32 audited, denied;
30011da177e4SLinus Torvalds 
3002b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3003d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3004d09ca739SEric Paris 
30051da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3006b782e0a6SEric Paris 	if (!mask)
30071da177e4SLinus Torvalds 		return 0;
30081da177e4SLinus Torvalds 
30092e334057SEric Paris 	validate_creds(cred);
3010b782e0a6SEric Paris 
30112e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
30122e334057SEric Paris 		return 0;
3013b782e0a6SEric Paris 
3014b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3015b782e0a6SEric Paris 
30162e334057SEric Paris 	sid = cred_sid(cred);
30175d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
30185d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
30195d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
30202e334057SEric Paris 
30216b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
30226b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, perms, 0, &avd);
30232e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
30242e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
30252e334057SEric Paris 				     &denied);
30262e334057SEric Paris 	if (likely(!audited))
30272e334057SEric Paris 		return rc;
30282e334057SEric Paris 
3029626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
30302e334057SEric Paris 	if (rc2)
30312e334057SEric Paris 		return rc2;
30322e334057SEric Paris 	return rc;
30331da177e4SLinus Torvalds }
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
30361da177e4SLinus Torvalds {
303788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3038ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3039bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
304095dbf739SEric Paris 	__u32 av = FILE__WRITE;
30411da177e4SLinus Torvalds 
3042bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3043bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3044bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3045bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3046bc6a6008SAmerigo Wang 		if (!ia_valid)
30471da177e4SLinus Torvalds 			return 0;
3048bc6a6008SAmerigo Wang 	}
30491da177e4SLinus Torvalds 
3050bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3051bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
30522875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
30531da177e4SLinus Torvalds 
3054aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3055ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3056ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3057ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
305895dbf739SEric Paris 		av |= FILE__OPEN;
305995dbf739SEric Paris 
306095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
30633f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
30641da177e4SLinus Torvalds {
30653f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
3068db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3069db59000aSStephen Smalley {
3070db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3071db59000aSStephen Smalley 	int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3072db59000aSStephen Smalley 
3073db59000aSStephen Smalley 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3074db59000aSStephen Smalley 		return false;
3075db59000aSStephen Smalley 	if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3076db59000aSStephen Smalley 		return false;
3077db59000aSStephen Smalley 	return true;
3078db59000aSStephen Smalley }
3079db59000aSStephen Smalley 
30808f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
30818f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
30821da177e4SLinus Torvalds {
3083c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
308420cdef8dSPaul Moore 	struct inode_security_struct *isec;
30851da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
30862bf49690SThomas Liu 	struct common_audit_data ad;
3087275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
30881da177e4SLinus Torvalds 	int rc = 0;
30891da177e4SLinus Torvalds 
30906b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
30916b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
30926b240306SEric W. Biederman 		if (rc)
30936b240306SEric W. Biederman 			return rc;
30946b240306SEric W. Biederman 
30956b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
30966b240306SEric W. Biederman 		   ordinary setattr permission. */
30976b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
30986b240306SEric W. Biederman 	}
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
310112f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
31021da177e4SLinus Torvalds 		return -EOPNOTSUPP;
31031da177e4SLinus Torvalds 
31042e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
31051da177e4SLinus Torvalds 		return -EPERM;
31061da177e4SLinus Torvalds 
310750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3108a269434dSEric Paris 	ad.u.dentry = dentry;
31091da177e4SLinus Torvalds 
311020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
31116b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
31126b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
31131da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
31141da177e4SLinus Torvalds 	if (rc)
31151da177e4SLinus Torvalds 		return rc;
31161da177e4SLinus Torvalds 
3117aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3118aa8e712cSStephen Smalley 				     GFP_KERNEL);
311912b29f34SStephen Smalley 	if (rc == -EINVAL) {
3120db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3121d6ea83ecSEric Paris 			struct audit_buffer *ab;
3122d6ea83ecSEric Paris 			size_t audit_size;
3123d6ea83ecSEric Paris 
3124d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3125d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3126e3fea3f7SAl Viro 			if (value) {
3127add24372SColin Ian King 				const char *str = value;
3128add24372SColin Ian King 
3129d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3130d6ea83ecSEric Paris 					audit_size = size - 1;
3131d6ea83ecSEric Paris 				else
3132d6ea83ecSEric Paris 					audit_size = size;
3133e3fea3f7SAl Viro 			} else {
3134e3fea3f7SAl Viro 				audit_size = 0;
3135e3fea3f7SAl Viro 			}
3136cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3137cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3138d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3139d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3140d6ea83ecSEric Paris 			audit_log_end(ab);
3141d6ea83ecSEric Paris 
314212b29f34SStephen Smalley 			return rc;
3143d6ea83ecSEric Paris 		}
3144aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3145aa8e712cSStephen Smalley 						   size, &newsid);
314612b29f34SStephen Smalley 	}
31471da177e4SLinus Torvalds 	if (rc)
31481da177e4SLinus Torvalds 		return rc;
31491da177e4SLinus Torvalds 
31506b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
31516b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
31521da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
31531da177e4SLinus Torvalds 	if (rc)
31541da177e4SLinus Torvalds 		return rc;
31551da177e4SLinus Torvalds 
3156aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3157aa8e712cSStephen Smalley 					  sid, isec->sclass);
31581da177e4SLinus Torvalds 	if (rc)
31591da177e4SLinus Torvalds 		return rc;
31601da177e4SLinus Torvalds 
31616b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
31626b6bc620SStephen Smalley 			    newsid,
31631da177e4SLinus Torvalds 			    sbsec->sid,
31641da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
31651da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
31661da177e4SLinus Torvalds 			    &ad);
31671da177e4SLinus Torvalds }
31681da177e4SLinus Torvalds 
31698f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
31708f0cfa52SDavid Howells 					const void *value, size_t size,
31718f0cfa52SDavid Howells 					int flags)
31721da177e4SLinus Torvalds {
3173c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
317420cdef8dSPaul Moore 	struct inode_security_struct *isec;
31751da177e4SLinus Torvalds 	u32 newsid;
31761da177e4SLinus Torvalds 	int rc;
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
31791da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
31801da177e4SLinus Torvalds 		return;
31811da177e4SLinus Torvalds 	}
31821da177e4SLinus Torvalds 
3183aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3184aa8e712cSStephen Smalley 					   &newsid);
31851da177e4SLinus Torvalds 	if (rc) {
3186c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
318712b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
318812b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
31891da177e4SLinus Torvalds 		return;
31901da177e4SLinus Torvalds 	}
31911da177e4SLinus Torvalds 
319220cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
31939287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3194aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
31951da177e4SLinus Torvalds 	isec->sid = newsid;
31966f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
31979287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3198aa9c2669SDavid Quigley 
31991da177e4SLinus Torvalds 	return;
32001da177e4SLinus Torvalds }
32011da177e4SLinus Torvalds 
32028f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
32031da177e4SLinus Torvalds {
320488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
320588e67f3bSDavid Howells 
32062875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
32071da177e4SLinus Torvalds }
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
32101da177e4SLinus Torvalds {
321188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
321288e67f3bSDavid Howells 
32132875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
32141da177e4SLinus Torvalds }
32151da177e4SLinus Torvalds 
32168f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
32171da177e4SLinus Torvalds {
32186b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32196b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
32206b240306SEric W. Biederman 		if (rc)
32216b240306SEric W. Biederman 			return rc;
32226b240306SEric W. Biederman 
32236b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
32246b240306SEric W. Biederman 		   ordinary setattr permission. */
32256b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32266b240306SEric W. Biederman 	}
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
32291da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
32301da177e4SLinus Torvalds 	return -EACCES;
32311da177e4SLinus Torvalds }
32321da177e4SLinus Torvalds 
3233d381d8a9SJames Morris /*
3234abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3235d381d8a9SJames Morris  *
3236d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3237d381d8a9SJames Morris  */
3238ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
32391da177e4SLinus Torvalds {
324042492594SDavid P. Quigley 	u32 size;
324142492594SDavid P. Quigley 	int error;
324242492594SDavid P. Quigley 	char *context = NULL;
324320cdef8dSPaul Moore 	struct inode_security_struct *isec;
32441da177e4SLinus Torvalds 
32458c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
32468c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
32471da177e4SLinus Torvalds 
3248abc69bb6SStephen Smalley 	/*
3249abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3250abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3251abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3252abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3253abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3254abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3255abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3256abc69bb6SStephen Smalley 	 */
325720cdef8dSPaul Moore 	isec = inode_security(inode);
3258db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3259aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3260aa8e712cSStephen Smalley 						      isec->sid, &context,
3261abc69bb6SStephen Smalley 						      &size);
3262abc69bb6SStephen Smalley 	else
3263aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3264aa8e712cSStephen Smalley 						&context, &size);
326542492594SDavid P. Quigley 	if (error)
326642492594SDavid P. Quigley 		return error;
326742492594SDavid P. Quigley 	error = size;
326842492594SDavid P. Quigley 	if (alloc) {
326942492594SDavid P. Quigley 		*buffer = context;
327042492594SDavid P. Quigley 		goto out_nofree;
327142492594SDavid P. Quigley 	}
327242492594SDavid P. Quigley 	kfree(context);
327342492594SDavid P. Quigley out_nofree:
327442492594SDavid P. Quigley 	return error;
32751da177e4SLinus Torvalds }
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
32781da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
32791da177e4SLinus Torvalds {
32802c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
32811da177e4SLinus Torvalds 	u32 newsid;
32821da177e4SLinus Torvalds 	int rc;
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
32851da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 	if (!value || !size)
32881da177e4SLinus Torvalds 		return -EACCES;
32891da177e4SLinus Torvalds 
3290aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3291aa8e712cSStephen Smalley 				     GFP_KERNEL);
32921da177e4SLinus Torvalds 	if (rc)
32931da177e4SLinus Torvalds 		return rc;
32941da177e4SLinus Torvalds 
32959287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3296aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
32971da177e4SLinus Torvalds 	isec->sid = newsid;
32986f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
32999287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
33001da177e4SLinus Torvalds 	return 0;
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
33041da177e4SLinus Torvalds {
33051da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
33061da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
33071da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
33081da177e4SLinus Torvalds 	return len;
33091da177e4SLinus Torvalds }
33101da177e4SLinus Torvalds 
3311d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3312713a04aeSAhmed S. Darwish {
3313e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3314713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3315713a04aeSAhmed S. Darwish }
3316713a04aeSAhmed S. Darwish 
331756909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
331856909eb3SVivek Goyal {
331956909eb3SVivek Goyal 	u32 sid;
332056909eb3SVivek Goyal 	struct task_security_struct *tsec;
332156909eb3SVivek Goyal 	struct cred *new_creds = *new;
332256909eb3SVivek Goyal 
332356909eb3SVivek Goyal 	if (new_creds == NULL) {
332456909eb3SVivek Goyal 		new_creds = prepare_creds();
332556909eb3SVivek Goyal 		if (!new_creds)
332656909eb3SVivek Goyal 			return -ENOMEM;
332756909eb3SVivek Goyal 	}
332856909eb3SVivek Goyal 
332956909eb3SVivek Goyal 	tsec = new_creds->security;
333056909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
333156909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
333256909eb3SVivek Goyal 	tsec->create_sid = sid;
333356909eb3SVivek Goyal 	*new = new_creds;
333456909eb3SVivek Goyal 	return 0;
333556909eb3SVivek Goyal }
333656909eb3SVivek Goyal 
333719472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
333819472b69SVivek Goyal {
333919472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
334019472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
334119472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
334219472b69SVivek Goyal 	 */
334319472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
334419472b69SVivek Goyal 		return 1; /* Discard */
334519472b69SVivek Goyal 	/*
334619472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
334719472b69SVivek Goyal 	 * by selinux.
334819472b69SVivek Goyal 	 */
334919472b69SVivek Goyal 	return -EOPNOTSUPP;
335019472b69SVivek Goyal }
335119472b69SVivek Goyal 
33521da177e4SLinus Torvalds /* file security operations */
33531da177e4SLinus Torvalds 
3354788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
33551da177e4SLinus Torvalds {
335688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3357496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
33601da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
33611da177e4SLinus Torvalds 		mask |= MAY_APPEND;
33621da177e4SLinus Torvalds 
3363389fb800SPaul Moore 	return file_has_perm(cred, file,
33641da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
33651da177e4SLinus Torvalds }
33661da177e4SLinus Torvalds 
3367788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3368788e7dd4SYuichi Nakamura {
3369496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
337020dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
3371b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
337220dda18bSStephen Smalley 	u32 sid = current_sid();
337320dda18bSStephen Smalley 
3374389fb800SPaul Moore 	if (!mask)
3375788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3376788e7dd4SYuichi Nakamura 		return 0;
3377788e7dd4SYuichi Nakamura 
3378b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
337920dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
33806b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
338183d49856SEric Paris 		/* No change since file_open check. */
338220dda18bSStephen Smalley 		return 0;
338320dda18bSStephen Smalley 
3384788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3385788e7dd4SYuichi Nakamura }
3386788e7dd4SYuichi Nakamura 
33871da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
33881da177e4SLinus Torvalds {
33891da177e4SLinus Torvalds 	return file_alloc_security(file);
33901da177e4SLinus Torvalds }
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
33931da177e4SLinus Torvalds {
33941da177e4SLinus Torvalds 	file_free_security(file);
33951da177e4SLinus Torvalds }
33961da177e4SLinus Torvalds 
3397fa1aa143SJeff Vander Stoep /*
3398fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3399fa1aa143SJeff Vander Stoep  * operation to an inode.
3400fa1aa143SJeff Vander Stoep  */
34011d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3402fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3403fa1aa143SJeff Vander Stoep {
3404fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3405fa1aa143SJeff Vander Stoep 	struct file_security_struct *fsec = file->f_security;
3406fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
340720cdef8dSPaul Moore 	struct inode_security_struct *isec;
3408fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3409fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3410fa1aa143SJeff Vander Stoep 	int rc;
3411fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3412fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3413fa1aa143SJeff Vander Stoep 
3414fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3415fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3416fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3417fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3418fa1aa143SJeff Vander Stoep 
3419fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
34206b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
34216b6bc620SStephen Smalley 				  ssid, fsec->sid,
3422fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3423fa1aa143SJeff Vander Stoep 				FD__USE,
3424fa1aa143SJeff Vander Stoep 				&ad);
3425fa1aa143SJeff Vander Stoep 		if (rc)
3426fa1aa143SJeff Vander Stoep 			goto out;
3427fa1aa143SJeff Vander Stoep 	}
3428fa1aa143SJeff Vander Stoep 
3429fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3430fa1aa143SJeff Vander Stoep 		return 0;
3431fa1aa143SJeff Vander Stoep 
343220cdef8dSPaul Moore 	isec = inode_security(inode);
34336b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
34346b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3435fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3436fa1aa143SJeff Vander Stoep out:
3437fa1aa143SJeff Vander Stoep 	return rc;
3438fa1aa143SJeff Vander Stoep }
3439fa1aa143SJeff Vander Stoep 
34401da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
34411da177e4SLinus Torvalds 			      unsigned long arg)
34421da177e4SLinus Torvalds {
344388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
34440b24dcb7SEric Paris 	int error = 0;
34451da177e4SLinus Torvalds 
34460b24dcb7SEric Paris 	switch (cmd) {
34470b24dcb7SEric Paris 	case FIONREAD:
34480b24dcb7SEric Paris 	/* fall through */
34490b24dcb7SEric Paris 	case FIBMAP:
34500b24dcb7SEric Paris 	/* fall through */
34510b24dcb7SEric Paris 	case FIGETBSZ:
34520b24dcb7SEric Paris 	/* fall through */
34532f99c369SAl Viro 	case FS_IOC_GETFLAGS:
34540b24dcb7SEric Paris 	/* fall through */
34552f99c369SAl Viro 	case FS_IOC_GETVERSION:
34560b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
34570b24dcb7SEric Paris 		break;
34581da177e4SLinus Torvalds 
34592f99c369SAl Viro 	case FS_IOC_SETFLAGS:
34600b24dcb7SEric Paris 	/* fall through */
34612f99c369SAl Viro 	case FS_IOC_SETVERSION:
34620b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
34630b24dcb7SEric Paris 		break;
34640b24dcb7SEric Paris 
34650b24dcb7SEric Paris 	/* sys_ioctl() checks */
34660b24dcb7SEric Paris 	case FIONBIO:
34670b24dcb7SEric Paris 	/* fall through */
34680b24dcb7SEric Paris 	case FIOASYNC:
34690b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
34700b24dcb7SEric Paris 		break;
34710b24dcb7SEric Paris 
34720b24dcb7SEric Paris 	case KDSKBENT:
34730b24dcb7SEric Paris 	case KDSKBSENT:
34746a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
34758e4ff6f2SStephen Smalley 					    SECURITY_CAP_AUDIT, true);
34760b24dcb7SEric Paris 		break;
34770b24dcb7SEric Paris 
34780b24dcb7SEric Paris 	/* default case assumes that the command will go
34790b24dcb7SEric Paris 	 * to the file's ioctl() function.
34800b24dcb7SEric Paris 	 */
34810b24dcb7SEric Paris 	default:
3482fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
34830b24dcb7SEric Paris 	}
34840b24dcb7SEric Paris 	return error;
34851da177e4SLinus Torvalds }
34861da177e4SLinus Torvalds 
3487fcaaade1SStephen Smalley static int default_noexec;
3488fcaaade1SStephen Smalley 
34891da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
34901da177e4SLinus Torvalds {
349188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3492be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3493d84f4f99SDavid Howells 	int rc = 0;
349488e67f3bSDavid Howells 
3495fcaaade1SStephen Smalley 	if (default_noexec &&
3496892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3497892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
34981da177e4SLinus Torvalds 		/*
34991da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
35001da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
35011da177e4SLinus Torvalds 		 * This has an additional check.
35021da177e4SLinus Torvalds 		 */
35036b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
35046b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3505be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
35061da177e4SLinus Torvalds 		if (rc)
3507d84f4f99SDavid Howells 			goto error;
35081da177e4SLinus Torvalds 	}
35091da177e4SLinus Torvalds 
35101da177e4SLinus Torvalds 	if (file) {
35111da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
35121da177e4SLinus Torvalds 		u32 av = FILE__READ;
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
35151da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
35161da177e4SLinus Torvalds 			av |= FILE__WRITE;
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
35191da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
35201da177e4SLinus Torvalds 
352188e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
35221da177e4SLinus Torvalds 	}
3523d84f4f99SDavid Howells 
3524d84f4f99SDavid Howells error:
3525d84f4f99SDavid Howells 	return rc;
35261da177e4SLinus Torvalds }
35271da177e4SLinus Torvalds 
3528e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
35291da177e4SLinus Torvalds {
3530b1d9e6b0SCasey Schaufler 	int rc = 0;
353198883bfdSPaul Moore 
353298883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
353398883bfdSPaul Moore 		u32 sid = current_sid();
35346b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
35356b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
353698883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
353798883bfdSPaul Moore 	}
353898883bfdSPaul Moore 
353998883bfdSPaul Moore 	return rc;
3540e5467859SAl Viro }
35411da177e4SLinus Torvalds 
3542e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3543e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3544e5467859SAl Viro {
35453ba4bf5fSStephen Smalley 	struct common_audit_data ad;
35463ba4bf5fSStephen Smalley 	int rc;
35473ba4bf5fSStephen Smalley 
35483ba4bf5fSStephen Smalley 	if (file) {
35493ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
35503ba4bf5fSStephen Smalley 		ad.u.file = file;
35513ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
35523ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
35533ba4bf5fSStephen Smalley 		if (rc)
35543ba4bf5fSStephen Smalley 			return rc;
35553ba4bf5fSStephen Smalley 	}
35563ba4bf5fSStephen Smalley 
3557aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
35581da177e4SLinus Torvalds 		prot = reqprot;
35591da177e4SLinus Torvalds 
35601da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
35611da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
35621da177e4SLinus Torvalds }
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
35651da177e4SLinus Torvalds 				 unsigned long reqprot,
35661da177e4SLinus Torvalds 				 unsigned long prot)
35671da177e4SLinus Torvalds {
356888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3569be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
35701da177e4SLinus Torvalds 
3571aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
35721da177e4SLinus Torvalds 		prot = reqprot;
35731da177e4SLinus Torvalds 
3574fcaaade1SStephen Smalley 	if (default_noexec &&
3575fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3576d541bbeeSJames Morris 		int rc = 0;
3577db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3578db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
35796b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
35806b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3581be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3582db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3583c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3584c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3585d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
35866b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
35876b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3588be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3589db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3590db4c9641SStephen Smalley 			/*
3591db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3592db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3593db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3594db4c9641SStephen Smalley 			 * modified content.  This typically should only
3595db4c9641SStephen Smalley 			 * occur for text relocations.
3596db4c9641SStephen Smalley 			 */
3597d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3598db4c9641SStephen Smalley 		}
35996b992197SLorenzo Hernandez García-Hierro 		if (rc)
36006b992197SLorenzo Hernandez García-Hierro 			return rc;
36016b992197SLorenzo Hernandez García-Hierro 	}
36021da177e4SLinus Torvalds 
36031da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
36041da177e4SLinus Torvalds }
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
36071da177e4SLinus Torvalds {
360888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
360988e67f3bSDavid Howells 
361088e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
36111da177e4SLinus Torvalds }
36121da177e4SLinus Torvalds 
36131da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
36141da177e4SLinus Torvalds 			      unsigned long arg)
36151da177e4SLinus Torvalds {
361688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
36171da177e4SLinus Torvalds 	int err = 0;
36181da177e4SLinus Torvalds 
36191da177e4SLinus Torvalds 	switch (cmd) {
36201da177e4SLinus Torvalds 	case F_SETFL:
36211da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
362288e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
36231da177e4SLinus Torvalds 			break;
36241da177e4SLinus Torvalds 		}
36251da177e4SLinus Torvalds 		/* fall through */
36261da177e4SLinus Torvalds 	case F_SETOWN:
36271da177e4SLinus Torvalds 	case F_SETSIG:
36281da177e4SLinus Torvalds 	case F_GETFL:
36291da177e4SLinus Torvalds 	case F_GETOWN:
36301da177e4SLinus Torvalds 	case F_GETSIG:
36311d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
36321da177e4SLinus Torvalds 		/* Just check FD__USE permission */
363388e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
36341da177e4SLinus Torvalds 		break;
36351da177e4SLinus Torvalds 	case F_GETLK:
36361da177e4SLinus Torvalds 	case F_SETLK:
36371da177e4SLinus Torvalds 	case F_SETLKW:
36380d3f7a2dSJeff Layton 	case F_OFD_GETLK:
36390d3f7a2dSJeff Layton 	case F_OFD_SETLK:
36400d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
36411da177e4SLinus Torvalds #if BITS_PER_LONG == 32
36421da177e4SLinus Torvalds 	case F_GETLK64:
36431da177e4SLinus Torvalds 	case F_SETLK64:
36441da177e4SLinus Torvalds 	case F_SETLKW64:
36451da177e4SLinus Torvalds #endif
364688e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
36471da177e4SLinus Torvalds 		break;
36481da177e4SLinus Torvalds 	}
36491da177e4SLinus Torvalds 
36501da177e4SLinus Torvalds 	return err;
36511da177e4SLinus Torvalds }
36521da177e4SLinus Torvalds 
3653e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
36541da177e4SLinus Torvalds {
36551da177e4SLinus Torvalds 	struct file_security_struct *fsec;
36561da177e4SLinus Torvalds 
36571da177e4SLinus Torvalds 	fsec = file->f_security;
3658275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
36591da177e4SLinus Torvalds }
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
36621da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
36631da177e4SLinus Torvalds {
36641da177e4SLinus Torvalds 	struct file *file;
366565c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
36661da177e4SLinus Torvalds 	u32 perm;
36671da177e4SLinus Torvalds 	struct file_security_struct *fsec;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3670b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 	fsec = file->f_security;
36731da177e4SLinus Torvalds 
36741da177e4SLinus Torvalds 	if (!signum)
36751da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
36761da177e4SLinus Torvalds 	else
36771da177e4SLinus Torvalds 		perm = signal_to_av(signum);
36781da177e4SLinus Torvalds 
36796b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
36806b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
36811da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
36821da177e4SLinus Torvalds }
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
36851da177e4SLinus Torvalds {
368688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
368788e67f3bSDavid Howells 
368888e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
36891da177e4SLinus Torvalds }
36901da177e4SLinus Torvalds 
369194817692SAl Viro static int selinux_file_open(struct file *file)
3692788e7dd4SYuichi Nakamura {
3693788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3694788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3695d84f4f99SDavid Howells 
3696788e7dd4SYuichi Nakamura 	fsec = file->f_security;
369783da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3698788e7dd4SYuichi Nakamura 	/*
3699788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3700788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3701788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3702788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3703788e7dd4SYuichi Nakamura 	 * struct as its SID.
3704788e7dd4SYuichi Nakamura 	 */
3705788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
37066b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3707788e7dd4SYuichi Nakamura 	/*
3708788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3709788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3710788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3711788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3712788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3713788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3714788e7dd4SYuichi Nakamura 	 */
371594817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3716788e7dd4SYuichi Nakamura }
3717788e7dd4SYuichi Nakamura 
37181da177e4SLinus Torvalds /* task security operations */
37191da177e4SLinus Torvalds 
3720a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3721a79be238STetsuo Handa 			      unsigned long clone_flags)
37221da177e4SLinus Torvalds {
3723be0554c9SStephen Smalley 	u32 sid = current_sid();
3724be0554c9SStephen Smalley 
37256b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
37266b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
37271da177e4SLinus Torvalds }
37281da177e4SLinus Torvalds 
3729f1752eecSDavid Howells /*
3730ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3731ee18d64cSDavid Howells  */
3732ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3733ee18d64cSDavid Howells {
3734ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3735ee18d64cSDavid Howells 
3736ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3737ee18d64cSDavid Howells 	if (!tsec)
3738ee18d64cSDavid Howells 		return -ENOMEM;
3739ee18d64cSDavid Howells 
3740ee18d64cSDavid Howells 	cred->security = tsec;
3741ee18d64cSDavid Howells 	return 0;
3742ee18d64cSDavid Howells }
3743ee18d64cSDavid Howells 
3744ee18d64cSDavid Howells /*
3745f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3746f1752eecSDavid Howells  */
3747f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
37481da177e4SLinus Torvalds {
3749f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3750e0e81739SDavid Howells 
37512edeaa34STetsuo Handa 	/*
37522edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
37532edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
37542edeaa34STetsuo Handa 	 */
37552edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3756e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3757f1752eecSDavid Howells 	kfree(tsec);
37581da177e4SLinus Torvalds }
37591da177e4SLinus Torvalds 
3760d84f4f99SDavid Howells /*
3761d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3762d84f4f99SDavid Howells  */
3763d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3764d84f4f99SDavid Howells 				gfp_t gfp)
3765d84f4f99SDavid Howells {
3766d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3767d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3768d84f4f99SDavid Howells 
3769d84f4f99SDavid Howells 	old_tsec = old->security;
3770d84f4f99SDavid Howells 
3771d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3772d84f4f99SDavid Howells 	if (!tsec)
3773d84f4f99SDavid Howells 		return -ENOMEM;
3774d84f4f99SDavid Howells 
3775d84f4f99SDavid Howells 	new->security = tsec;
3776d84f4f99SDavid Howells 	return 0;
3777d84f4f99SDavid Howells }
3778d84f4f99SDavid Howells 
3779d84f4f99SDavid Howells /*
3780ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3781ee18d64cSDavid Howells  */
3782ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3783ee18d64cSDavid Howells {
3784ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3785ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3786ee18d64cSDavid Howells 
3787ee18d64cSDavid Howells 	*tsec = *old_tsec;
3788ee18d64cSDavid Howells }
3789ee18d64cSDavid Howells 
37903ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
37913ec30113SMatthew Garrett {
37923ec30113SMatthew Garrett 	*secid = cred_sid(c);
37933ec30113SMatthew Garrett }
37943ec30113SMatthew Garrett 
3795ee18d64cSDavid Howells /*
37963a3b7ce9SDavid Howells  * set the security data for a kernel service
37973a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
37983a3b7ce9SDavid Howells  */
37993a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
38003a3b7ce9SDavid Howells {
38013a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
38023a3b7ce9SDavid Howells 	u32 sid = current_sid();
38033a3b7ce9SDavid Howells 	int ret;
38043a3b7ce9SDavid Howells 
38056b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
38066b6bc620SStephen Smalley 			   sid, secid,
38073a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
38083a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
38093a3b7ce9SDavid Howells 			   NULL);
38103a3b7ce9SDavid Howells 	if (ret == 0) {
38113a3b7ce9SDavid Howells 		tsec->sid = secid;
38123a3b7ce9SDavid Howells 		tsec->create_sid = 0;
38133a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
38143a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
38153a3b7ce9SDavid Howells 	}
38163a3b7ce9SDavid Howells 	return ret;
38173a3b7ce9SDavid Howells }
38183a3b7ce9SDavid Howells 
38193a3b7ce9SDavid Howells /*
38203a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
38213a3b7ce9SDavid Howells  * objective context of the specified inode
38223a3b7ce9SDavid Howells  */
38233a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
38243a3b7ce9SDavid Howells {
382583da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
38263a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
38273a3b7ce9SDavid Howells 	u32 sid = current_sid();
38283a3b7ce9SDavid Howells 	int ret;
38293a3b7ce9SDavid Howells 
38306b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
38316b6bc620SStephen Smalley 			   sid, isec->sid,
38323a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
38333a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
38343a3b7ce9SDavid Howells 			   NULL);
38353a3b7ce9SDavid Howells 
38363a3b7ce9SDavid Howells 	if (ret == 0)
38373a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3838ef57471aSDavid Howells 	return ret;
38393a3b7ce9SDavid Howells }
38403a3b7ce9SDavid Howells 
3841dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
384225354c4fSEric Paris {
3843dd8dbf2eSEric Paris 	struct common_audit_data ad;
3844dd8dbf2eSEric Paris 
384550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3846dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3847dd8dbf2eSEric Paris 
38486b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38496b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3850dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
385125354c4fSEric Paris }
385225354c4fSEric Paris 
385361d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
385461d612eaSJeff Vander Stoep {
385561d612eaSJeff Vander Stoep 	struct common_audit_data ad;
385661d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
385761d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
385861d612eaSJeff Vander Stoep 	u32 sid = current_sid();
385961d612eaSJeff Vander Stoep 	int rc;
386061d612eaSJeff Vander Stoep 
386161d612eaSJeff Vander Stoep 	/* init_module */
386261d612eaSJeff Vander Stoep 	if (file == NULL)
38636b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
38646b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
386561d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
386661d612eaSJeff Vander Stoep 
386761d612eaSJeff Vander Stoep 	/* finit_module */
386820cdef8dSPaul Moore 
386943af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
387043af5de7SVivek Goyal 	ad.u.file = file;
387161d612eaSJeff Vander Stoep 
387261d612eaSJeff Vander Stoep 	fsec = file->f_security;
387361d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
38746b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
38756b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
387661d612eaSJeff Vander Stoep 		if (rc)
387761d612eaSJeff Vander Stoep 			return rc;
387861d612eaSJeff Vander Stoep 	}
387961d612eaSJeff Vander Stoep 
388020cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
38816b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38826b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
388361d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
388461d612eaSJeff Vander Stoep }
388561d612eaSJeff Vander Stoep 
388661d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
388761d612eaSJeff Vander Stoep 				    enum kernel_read_file_id id)
388861d612eaSJeff Vander Stoep {
388961d612eaSJeff Vander Stoep 	int rc = 0;
389061d612eaSJeff Vander Stoep 
389161d612eaSJeff Vander Stoep 	switch (id) {
389261d612eaSJeff Vander Stoep 	case READING_MODULE:
389361d612eaSJeff Vander Stoep 		rc = selinux_kernel_module_from_file(file);
389461d612eaSJeff Vander Stoep 		break;
389561d612eaSJeff Vander Stoep 	default:
389661d612eaSJeff Vander Stoep 		break;
389761d612eaSJeff Vander Stoep 	}
389861d612eaSJeff Vander Stoep 
389961d612eaSJeff Vander Stoep 	return rc;
390061d612eaSJeff Vander Stoep }
390161d612eaSJeff Vander Stoep 
3902c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id)
3903c77b8cdfSMimi Zohar {
3904c77b8cdfSMimi Zohar 	int rc = 0;
3905c77b8cdfSMimi Zohar 
3906c77b8cdfSMimi Zohar 	switch (id) {
3907c77b8cdfSMimi Zohar 	case LOADING_MODULE:
3908c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
3909c77b8cdfSMimi Zohar 	default:
3910c77b8cdfSMimi Zohar 		break;
3911c77b8cdfSMimi Zohar 	}
3912c77b8cdfSMimi Zohar 
3913c77b8cdfSMimi Zohar 	return rc;
3914c77b8cdfSMimi Zohar }
3915c77b8cdfSMimi Zohar 
39161da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
39171da177e4SLinus Torvalds {
39186b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39196b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3920be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
39211da177e4SLinus Torvalds }
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
39241da177e4SLinus Torvalds {
39256b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39266b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3927be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
39281da177e4SLinus Torvalds }
39291da177e4SLinus Torvalds 
39301da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
39311da177e4SLinus Torvalds {
39326b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39336b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3934be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
39351da177e4SLinus Torvalds }
39361da177e4SLinus Torvalds 
3937f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3938f9008e4cSDavid Quigley {
3939275bb41eSDavid Howells 	*secid = task_sid(p);
3940f9008e4cSDavid Quigley }
3941f9008e4cSDavid Quigley 
39421da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
39431da177e4SLinus Torvalds {
39446b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39456b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3946be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
39471da177e4SLinus Torvalds }
39481da177e4SLinus Torvalds 
394903e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
395003e68060SJames Morris {
39516b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39526b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3953be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
395403e68060SJames Morris }
395503e68060SJames Morris 
3956a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3957a1836a42SDavid Quigley {
39586b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39596b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3960be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
3961a1836a42SDavid Quigley }
3962a1836a42SDavid Quigley 
39634298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3964791ec491SStephen Smalley 				unsigned int flags)
3965791ec491SStephen Smalley {
3966791ec491SStephen Smalley 	u32 av = 0;
3967791ec491SStephen Smalley 
396884e6885eSStephen Smalley 	if (!flags)
396984e6885eSStephen Smalley 		return 0;
3970791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
3971791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
3972791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
3973791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
39746b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39756b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
3976791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
3977791ec491SStephen Smalley }
3978791ec491SStephen Smalley 
39798fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
39808fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
39811da177e4SLinus Torvalds {
39828fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
39851da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
39861da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3987d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
39881da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
39896b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
39906b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
3991be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds 	return 0;
39941da177e4SLinus Torvalds }
39951da177e4SLinus Torvalds 
3996b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
39971da177e4SLinus Torvalds {
39986b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39996b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4000be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
40011da177e4SLinus Torvalds }
40021da177e4SLinus Torvalds 
40031da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
40041da177e4SLinus Torvalds {
40056b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40066b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4007be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
40081da177e4SLinus Torvalds }
40091da177e4SLinus Torvalds 
401035601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
401135601547SDavid Quigley {
40126b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40136b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4014be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
401535601547SDavid Quigley }
401635601547SDavid Quigley 
4017ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
40186b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
40191da177e4SLinus Torvalds {
40206b4f3d01SStephen Smalley 	u32 secid;
40211da177e4SLinus Torvalds 	u32 perm;
40221da177e4SLinus Torvalds 
40231da177e4SLinus Torvalds 	if (!sig)
40241da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
40251da177e4SLinus Torvalds 	else
40261da177e4SLinus Torvalds 		perm = signal_to_av(sig);
40276b4f3d01SStephen Smalley 	if (!cred)
4028be0554c9SStephen Smalley 		secid = current_sid();
40296b4f3d01SStephen Smalley 	else
40306b4f3d01SStephen Smalley 		secid = cred_sid(cred);
40316b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40326b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
40331da177e4SLinus Torvalds }
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
40361da177e4SLinus Torvalds 				  struct inode *inode)
40371da177e4SLinus Torvalds {
40381da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
4039275bb41eSDavid Howells 	u32 sid = task_sid(p);
40401da177e4SLinus Torvalds 
40419287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4042db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4043275bb41eSDavid Howells 	isec->sid = sid;
40446f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
40459287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
40461da177e4SLinus Torvalds }
40471da177e4SLinus Torvalds 
40481da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
404967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
40502bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
40511da177e4SLinus Torvalds {
40521da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
40531da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
40541da177e4SLinus Torvalds 
4055bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
40561da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
40571da177e4SLinus Torvalds 	if (ih == NULL)
40581da177e4SLinus Torvalds 		goto out;
40591da177e4SLinus Torvalds 
40601da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
40611da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
40621da177e4SLinus Torvalds 		goto out;
40631da177e4SLinus Torvalds 
406448c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
406548c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
40661da177e4SLinus Torvalds 	ret = 0;
40671da177e4SLinus Torvalds 
406867f83cbfSVenkat Yekkirala 	if (proto)
406967f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
407067f83cbfSVenkat Yekkirala 
40711da177e4SLinus Torvalds 	switch (ih->protocol) {
40721da177e4SLinus Torvalds 	case IPPROTO_TCP: {
40731da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
40741da177e4SLinus Torvalds 
40751da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
40761da177e4SLinus Torvalds 			break;
40771da177e4SLinus Torvalds 
40781da177e4SLinus Torvalds 		offset += ihlen;
40791da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
40801da177e4SLinus Torvalds 		if (th == NULL)
40811da177e4SLinus Torvalds 			break;
40821da177e4SLinus Torvalds 
408348c62af6SEric Paris 		ad->u.net->sport = th->source;
408448c62af6SEric Paris 		ad->u.net->dport = th->dest;
40851da177e4SLinus Torvalds 		break;
40861da177e4SLinus Torvalds 	}
40871da177e4SLinus Torvalds 
40881da177e4SLinus Torvalds 	case IPPROTO_UDP: {
40891da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
40901da177e4SLinus Torvalds 
40911da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
40921da177e4SLinus Torvalds 			break;
40931da177e4SLinus Torvalds 
40941da177e4SLinus Torvalds 		offset += ihlen;
40951da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
40961da177e4SLinus Torvalds 		if (uh == NULL)
40971da177e4SLinus Torvalds 			break;
40981da177e4SLinus Torvalds 
409948c62af6SEric Paris 		ad->u.net->sport = uh->source;
410048c62af6SEric Paris 		ad->u.net->dport = uh->dest;
41011da177e4SLinus Torvalds 		break;
41021da177e4SLinus Torvalds 	}
41031da177e4SLinus Torvalds 
41042ee92d46SJames Morris 	case IPPROTO_DCCP: {
41052ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
41062ee92d46SJames Morris 
41072ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
41082ee92d46SJames Morris 			break;
41092ee92d46SJames Morris 
41102ee92d46SJames Morris 		offset += ihlen;
41112ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
41122ee92d46SJames Morris 		if (dh == NULL)
41132ee92d46SJames Morris 			break;
41142ee92d46SJames Morris 
411548c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
411648c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
41172ee92d46SJames Morris 		break;
41182ee92d46SJames Morris 	}
41192ee92d46SJames Morris 
4120d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4121d452930fSRichard Haines 	case IPPROTO_SCTP: {
4122d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4123d452930fSRichard Haines 
4124d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4125d452930fSRichard Haines 			break;
4126d452930fSRichard Haines 
4127d452930fSRichard Haines 		offset += ihlen;
4128d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4129d452930fSRichard Haines 		if (sh == NULL)
4130d452930fSRichard Haines 			break;
4131d452930fSRichard Haines 
4132d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4133d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4134d452930fSRichard Haines 		break;
4135d452930fSRichard Haines 	}
4136d452930fSRichard Haines #endif
41371da177e4SLinus Torvalds 	default:
41381da177e4SLinus Torvalds 		break;
41391da177e4SLinus Torvalds 	}
41401da177e4SLinus Torvalds out:
41411da177e4SLinus Torvalds 	return ret;
41421da177e4SLinus Torvalds }
41431da177e4SLinus Torvalds 
41441a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
41451da177e4SLinus Torvalds 
41461da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
414767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
41482bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
41491da177e4SLinus Torvalds {
41501da177e4SLinus Torvalds 	u8 nexthdr;
41511da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
41521da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
415375f2811cSJesse Gross 	__be16 frag_off;
41541da177e4SLinus Torvalds 
4155bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
41561da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
41571da177e4SLinus Torvalds 	if (ip6 == NULL)
41581da177e4SLinus Torvalds 		goto out;
41591da177e4SLinus Torvalds 
416048c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
416148c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
41621da177e4SLinus Torvalds 	ret = 0;
41631da177e4SLinus Torvalds 
41641da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
41651da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
416675f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
41671da177e4SLinus Torvalds 	if (offset < 0)
41681da177e4SLinus Torvalds 		goto out;
41691da177e4SLinus Torvalds 
417067f83cbfSVenkat Yekkirala 	if (proto)
417167f83cbfSVenkat Yekkirala 		*proto = nexthdr;
417267f83cbfSVenkat Yekkirala 
41731da177e4SLinus Torvalds 	switch (nexthdr) {
41741da177e4SLinus Torvalds 	case IPPROTO_TCP: {
41751da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
41781da177e4SLinus Torvalds 		if (th == NULL)
41791da177e4SLinus Torvalds 			break;
41801da177e4SLinus Torvalds 
418148c62af6SEric Paris 		ad->u.net->sport = th->source;
418248c62af6SEric Paris 		ad->u.net->dport = th->dest;
41831da177e4SLinus Torvalds 		break;
41841da177e4SLinus Torvalds 	}
41851da177e4SLinus Torvalds 
41861da177e4SLinus Torvalds 	case IPPROTO_UDP: {
41871da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
41901da177e4SLinus Torvalds 		if (uh == NULL)
41911da177e4SLinus Torvalds 			break;
41921da177e4SLinus Torvalds 
419348c62af6SEric Paris 		ad->u.net->sport = uh->source;
419448c62af6SEric Paris 		ad->u.net->dport = uh->dest;
41951da177e4SLinus Torvalds 		break;
41961da177e4SLinus Torvalds 	}
41971da177e4SLinus Torvalds 
41982ee92d46SJames Morris 	case IPPROTO_DCCP: {
41992ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
42002ee92d46SJames Morris 
42012ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
42022ee92d46SJames Morris 		if (dh == NULL)
42032ee92d46SJames Morris 			break;
42042ee92d46SJames Morris 
420548c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
420648c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
42072ee92d46SJames Morris 		break;
42082ee92d46SJames Morris 	}
42092ee92d46SJames Morris 
4210d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4211d452930fSRichard Haines 	case IPPROTO_SCTP: {
4212d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4213d452930fSRichard Haines 
4214d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4215d452930fSRichard Haines 		if (sh == NULL)
4216d452930fSRichard Haines 			break;
4217d452930fSRichard Haines 
4218d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4219d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4220d452930fSRichard Haines 		break;
4221d452930fSRichard Haines 	}
4222d452930fSRichard Haines #endif
42231da177e4SLinus Torvalds 	/* includes fragments */
42241da177e4SLinus Torvalds 	default:
42251da177e4SLinus Torvalds 		break;
42261da177e4SLinus Torvalds 	}
42271da177e4SLinus Torvalds out:
42281da177e4SLinus Torvalds 	return ret;
42291da177e4SLinus Torvalds }
42301da177e4SLinus Torvalds 
42311da177e4SLinus Torvalds #endif /* IPV6 */
42321da177e4SLinus Torvalds 
42332bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4234cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
42351da177e4SLinus Torvalds {
4236cf9481e2SDavid Howells 	char *addrp;
4237cf9481e2SDavid Howells 	int ret;
42381da177e4SLinus Torvalds 
423948c62af6SEric Paris 	switch (ad->u.net->family) {
42401da177e4SLinus Torvalds 	case PF_INET:
424167f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4242cf9481e2SDavid Howells 		if (ret)
4243cf9481e2SDavid Howells 			goto parse_error;
424448c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
424548c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4246cf9481e2SDavid Howells 		goto okay;
42471da177e4SLinus Torvalds 
42481a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
42491da177e4SLinus Torvalds 	case PF_INET6:
425067f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4251cf9481e2SDavid Howells 		if (ret)
4252cf9481e2SDavid Howells 			goto parse_error;
425348c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
425448c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4255cf9481e2SDavid Howells 		goto okay;
42561da177e4SLinus Torvalds #endif	/* IPV6 */
42571da177e4SLinus Torvalds 	default:
4258cf9481e2SDavid Howells 		addrp = NULL;
4259cf9481e2SDavid Howells 		goto okay;
42601da177e4SLinus Torvalds 	}
42611da177e4SLinus Torvalds 
4262cf9481e2SDavid Howells parse_error:
4263c103a91eSpeter enderborg 	pr_warn(
426471f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
426571f1cb05SPaul Moore 	       " unable to parse packet\n");
42661da177e4SLinus Torvalds 	return ret;
4267cf9481e2SDavid Howells 
4268cf9481e2SDavid Howells okay:
4269cf9481e2SDavid Howells 	if (_addrp)
4270cf9481e2SDavid Howells 		*_addrp = addrp;
4271cf9481e2SDavid Howells 	return 0;
42721da177e4SLinus Torvalds }
42731da177e4SLinus Torvalds 
42744f6a993fSPaul Moore /**
4275220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
42764f6a993fSPaul Moore  * @skb: the packet
427775e22910SPaul Moore  * @family: protocol family
4278220deb96SPaul Moore  * @sid: the packet's peer label SID
42794f6a993fSPaul Moore  *
42804f6a993fSPaul Moore  * Description:
4281220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4282220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4283220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4284220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4285220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4286220deb96SPaul Moore  * peer labels.
42874f6a993fSPaul Moore  *
42884f6a993fSPaul Moore  */
4289220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
42904f6a993fSPaul Moore {
429171f1cb05SPaul Moore 	int err;
42924f6a993fSPaul Moore 	u32 xfrm_sid;
42934f6a993fSPaul Moore 	u32 nlbl_sid;
4294220deb96SPaul Moore 	u32 nlbl_type;
42954f6a993fSPaul Moore 
4296817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4297bed4d7efSPaul Moore 	if (unlikely(err))
4298bed4d7efSPaul Moore 		return -EACCES;
4299bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4300bed4d7efSPaul Moore 	if (unlikely(err))
4301bed4d7efSPaul Moore 		return -EACCES;
4302220deb96SPaul Moore 
4303aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4304aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
430571f1cb05SPaul Moore 	if (unlikely(err)) {
4306c103a91eSpeter enderborg 		pr_warn(
430771f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
430871f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4309220deb96SPaul Moore 		return -EACCES;
431071f1cb05SPaul Moore 	}
4311220deb96SPaul Moore 
4312220deb96SPaul Moore 	return 0;
43134f6a993fSPaul Moore }
43144f6a993fSPaul Moore 
4315446b8024SPaul Moore /**
4316446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4317446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4318446b8024SPaul Moore  * @skb_sid: the packet's SID
4319446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4320446b8024SPaul Moore  *
4321446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4322446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4323446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
4324446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4325446b8024SPaul Moore  *
4326446b8024SPaul Moore  */
4327446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4328446b8024SPaul Moore {
4329446b8024SPaul Moore 	int err = 0;
4330446b8024SPaul Moore 
4331446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4332aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4333aa8e712cSStephen Smalley 					    conn_sid);
4334446b8024SPaul Moore 	else
4335446b8024SPaul Moore 		*conn_sid = sk_sid;
4336446b8024SPaul Moore 
4337446b8024SPaul Moore 	return err;
4338446b8024SPaul Moore }
4339446b8024SPaul Moore 
43401da177e4SLinus Torvalds /* socket security operations */
4341d4f2d978SPaul Moore 
43422ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
43432ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4344d4f2d978SPaul Moore {
43452ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
43462ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
43472ad18bdfSHarry Ciao 		return 0;
43482ad18bdfSHarry Ciao 	}
43492ad18bdfSHarry Ciao 
4350aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4351aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4352d4f2d978SPaul Moore }
4353d4f2d978SPaul Moore 
4354be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
43551da177e4SLinus Torvalds {
4356253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
43572bf49690SThomas Liu 	struct common_audit_data ad;
435848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
43591da177e4SLinus Torvalds 
4360253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4361253bfae6SPaul Moore 		return 0;
43621da177e4SLinus Torvalds 
436350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
436448c62af6SEric Paris 	ad.u.net = &net;
436548c62af6SEric Paris 	ad.u.net->sk = sk;
43661da177e4SLinus Torvalds 
43676b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
43686b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4369be0554c9SStephen Smalley 			    &ad);
43701da177e4SLinus Torvalds }
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
43731da177e4SLinus Torvalds 				 int protocol, int kern)
43741da177e4SLinus Torvalds {
43755fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4376d4f2d978SPaul Moore 	u32 newsid;
4377275bb41eSDavid Howells 	u16 secclass;
43782ad18bdfSHarry Ciao 	int rc;
43791da177e4SLinus Torvalds 
43801da177e4SLinus Torvalds 	if (kern)
4381d4f2d978SPaul Moore 		return 0;
43821da177e4SLinus Torvalds 
4383275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
43842ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
43852ad18bdfSHarry Ciao 	if (rc)
43862ad18bdfSHarry Ciao 		return rc;
43872ad18bdfSHarry Ciao 
43886b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
43896b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
43901da177e4SLinus Torvalds }
43911da177e4SLinus Torvalds 
43927420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
43931da177e4SLinus Torvalds 				      int type, int protocol, int kern)
43941da177e4SLinus Torvalds {
43955fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
43965d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4397892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
43989287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
43999287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4400275bb41eSDavid Howells 	int err = 0;
4401275bb41eSDavid Howells 
44029287aed2SAndreas Gruenbacher 	if (!kern) {
44039287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
44042ad18bdfSHarry Ciao 		if (err)
44052ad18bdfSHarry Ciao 			return err;
44062ad18bdfSHarry Ciao 	}
4407275bb41eSDavid Howells 
44089287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
44099287aed2SAndreas Gruenbacher 	isec->sid = sid;
44106f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
44111da177e4SLinus Torvalds 
4412892c141eSVenkat Yekkirala 	if (sock->sk) {
4413892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
44149287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
44159287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4416d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4417d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4418d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4419d452930fSRichard Haines 
4420389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4421892c141eSVenkat Yekkirala 	}
4422892c141eSVenkat Yekkirala 
44237420ed23SVenkat Yekkirala 	return err;
44241da177e4SLinus Torvalds }
44251da177e4SLinus Torvalds 
44260b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
44270b811db2SDavid Herrmann 				     struct socket *sockb)
44280b811db2SDavid Herrmann {
44290b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
44300b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
44310b811db2SDavid Herrmann 
44320b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
44330b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
44340b811db2SDavid Herrmann 
44350b811db2SDavid Herrmann 	return 0;
44360b811db2SDavid Herrmann }
44370b811db2SDavid Herrmann 
44381da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
44391da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
44401da177e4SLinus Torvalds    permission check between the socket and the port number. */
44411da177e4SLinus Torvalds 
44421da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
44431da177e4SLinus Torvalds {
4444253bfae6SPaul Moore 	struct sock *sk = sock->sk;
44450f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
44461da177e4SLinus Torvalds 	u16 family;
44471da177e4SLinus Torvalds 	int err;
44481da177e4SLinus Torvalds 
4449be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
44501da177e4SLinus Torvalds 	if (err)
44511da177e4SLinus Torvalds 		goto out;
44521da177e4SLinus Torvalds 
4453d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4454253bfae6SPaul Moore 	family = sk->sk_family;
44551da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
44561da177e4SLinus Torvalds 		char *addrp;
44572bf49690SThomas Liu 		struct common_audit_data ad;
445848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
44591da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
44601da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
44610f8db8ccSAlexey Kodanev 		u16 family_sa = address->sa_family;
44621da177e4SLinus Torvalds 		unsigned short snum;
4463e399f982SJames Morris 		u32 sid, node_perm;
44641da177e4SLinus Torvalds 
4465d452930fSRichard Haines 		/*
4466d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4467d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4468d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4469d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4470d452930fSRichard Haines 		 */
44710f8db8ccSAlexey Kodanev 		switch (family_sa) {
44720f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
447368741a8aSRichard Haines 		case AF_INET:
447468741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
447568741a8aSRichard Haines 				return -EINVAL;
44761da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
44770f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
44780f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
44790f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
44800f8db8ccSAlexey Kodanev 				 */
44810f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
44820f8db8ccSAlexey Kodanev 					goto err_af;
44830f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
44840f8db8ccSAlexey Kodanev 			}
44851da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
44861da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
448768741a8aSRichard Haines 			break;
448868741a8aSRichard Haines 		case AF_INET6:
448968741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
449068741a8aSRichard Haines 				return -EINVAL;
44911da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
44921da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
44931da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
449468741a8aSRichard Haines 			break;
449568741a8aSRichard Haines 		default:
44960f8db8ccSAlexey Kodanev 			goto err_af;
44971da177e4SLinus Torvalds 		}
44981da177e4SLinus Torvalds 
449988b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
450088b7d370SAlexey Kodanev 		ad.u.net = &net;
450188b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
450288b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
450388b7d370SAlexey Kodanev 
4504227b60f5SStephen Hemminger 		if (snum) {
4505227b60f5SStephen Hemminger 			int low, high;
4506227b60f5SStephen Hemminger 
45070bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4508227b60f5SStephen Hemminger 
45094548b683SKrister Johansen 			if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
45104548b683SKrister Johansen 			    snum > high) {
45113e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
45123e112172SPaul Moore 						      snum, &sid);
45131da177e4SLinus Torvalds 				if (err)
45141da177e4SLinus Torvalds 					goto out;
45156b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
45166b6bc620SStephen Smalley 						   sksec->sid, sid,
4517253bfae6SPaul Moore 						   sksec->sclass,
45181da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
45191da177e4SLinus Torvalds 				if (err)
45201da177e4SLinus Torvalds 					goto out;
45211da177e4SLinus Torvalds 			}
4522227b60f5SStephen Hemminger 		}
45231da177e4SLinus Torvalds 
4524253bfae6SPaul Moore 		switch (sksec->sclass) {
452513402580SJames Morris 		case SECCLASS_TCP_SOCKET:
45261da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
45271da177e4SLinus Torvalds 			break;
45281da177e4SLinus Torvalds 
452913402580SJames Morris 		case SECCLASS_UDP_SOCKET:
45301da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
45311da177e4SLinus Torvalds 			break;
45321da177e4SLinus Torvalds 
45332ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
45342ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
45352ee92d46SJames Morris 			break;
45362ee92d46SJames Morris 
4537d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4538d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4539d452930fSRichard Haines 			break;
4540d452930fSRichard Haines 
45411da177e4SLinus Torvalds 		default:
45421da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
45431da177e4SLinus Torvalds 			break;
45441da177e4SLinus Torvalds 		}
45451da177e4SLinus Torvalds 
454688b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
45471da177e4SLinus Torvalds 		if (err)
45481da177e4SLinus Torvalds 			goto out;
45491da177e4SLinus Torvalds 
45500f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
455148c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
45521da177e4SLinus Torvalds 		else
455348c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
45541da177e4SLinus Torvalds 
45556b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
45566b6bc620SStephen Smalley 				   sksec->sid, sid,
4557253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
45581da177e4SLinus Torvalds 		if (err)
45591da177e4SLinus Torvalds 			goto out;
45601da177e4SLinus Torvalds 	}
45611da177e4SLinus Torvalds out:
45621da177e4SLinus Torvalds 	return err;
45630f8db8ccSAlexey Kodanev err_af:
45640f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
45650f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
45660f8db8ccSAlexey Kodanev 		return -EINVAL;
45670f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
45681da177e4SLinus Torvalds }
45691da177e4SLinus Torvalds 
4570d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
45715fb94e9cSMauro Carvalho Chehab  * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
4572d452930fSRichard Haines  */
4573d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4574d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
45751da177e4SLinus Torvalds {
4576014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4577253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45781da177e4SLinus Torvalds 	int err;
45791da177e4SLinus Torvalds 
4580be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
45811da177e4SLinus Torvalds 	if (err)
45821da177e4SLinus Torvalds 		return err;
45831da177e4SLinus Torvalds 
45841da177e4SLinus Torvalds 	/*
4585d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4586d452930fSRichard Haines 	 * for the port.
45871da177e4SLinus Torvalds 	 */
4588253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4589d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4590d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
45912bf49690SThomas Liu 		struct common_audit_data ad;
459248c62af6SEric Paris 		struct lsm_network_audit net = {0,};
45931da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
45941da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
45951da177e4SLinus Torvalds 		unsigned short snum;
45962ee92d46SJames Morris 		u32 sid, perm;
45971da177e4SLinus Torvalds 
4598d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4599d452930fSRichard Haines 		 * that validates multiple connect 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:
46051da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4606911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
46071da177e4SLinus Torvalds 				return -EINVAL;
46081da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
460968741a8aSRichard Haines 			break;
461068741a8aSRichard Haines 		case AF_INET6:
46111da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4612911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
46131da177e4SLinus Torvalds 				return -EINVAL;
46141da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
461568741a8aSRichard Haines 			break;
461668741a8aSRichard Haines 		default:
461768741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
461868741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
461968741a8aSRichard Haines 			 */
462068741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
462168741a8aSRichard Haines 				return -EINVAL;
462268741a8aSRichard Haines 			else
462368741a8aSRichard Haines 				return -EAFNOSUPPORT;
46241da177e4SLinus Torvalds 		}
46251da177e4SLinus Torvalds 
46263e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
46271da177e4SLinus Torvalds 		if (err)
4628d452930fSRichard Haines 			return err;
46291da177e4SLinus Torvalds 
4630d452930fSRichard Haines 		switch (sksec->sclass) {
4631d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4632d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4633d452930fSRichard Haines 			break;
4634d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4635d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4636d452930fSRichard Haines 			break;
4637d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4638d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4639d452930fSRichard Haines 			break;
4640d452930fSRichard Haines 		}
46412ee92d46SJames Morris 
464250c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
464348c62af6SEric Paris 		ad.u.net = &net;
464448c62af6SEric Paris 		ad.u.net->dport = htons(snum);
464588b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
46466b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
46476b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
46481da177e4SLinus Torvalds 		if (err)
4649d452930fSRichard Haines 			return err;
46501da177e4SLinus Torvalds 	}
46511da177e4SLinus Torvalds 
4652d452930fSRichard Haines 	return 0;
4653d452930fSRichard Haines }
4654014ab19aSPaul Moore 
4655d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4656d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4657d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4658d452930fSRichard Haines {
4659d452930fSRichard Haines 	int err;
4660d452930fSRichard Haines 	struct sock *sk = sock->sk;
4661d452930fSRichard Haines 
4662d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4663d452930fSRichard Haines 	if (err)
46641da177e4SLinus Torvalds 		return err;
4665d452930fSRichard Haines 
4666d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
46671da177e4SLinus Torvalds }
46681da177e4SLinus Torvalds 
46691da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
46701da177e4SLinus Torvalds {
4671be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
46721da177e4SLinus Torvalds }
46731da177e4SLinus Torvalds 
46741da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
46751da177e4SLinus Torvalds {
46761da177e4SLinus Torvalds 	int err;
46771da177e4SLinus Torvalds 	struct inode_security_struct *isec;
46781da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
46799287aed2SAndreas Gruenbacher 	u16 sclass;
46809287aed2SAndreas Gruenbacher 	u32 sid;
46811da177e4SLinus Torvalds 
4682be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
46831da177e4SLinus Torvalds 	if (err)
46841da177e4SLinus Torvalds 		return err;
46851da177e4SLinus Torvalds 
46865d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
46879287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
46889287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
46899287aed2SAndreas Gruenbacher 	sid = isec->sid;
46909287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
46919287aed2SAndreas Gruenbacher 
46929287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
46939287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
46949287aed2SAndreas Gruenbacher 	newisec->sid = sid;
46956f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
46961da177e4SLinus Torvalds 
46971da177e4SLinus Torvalds 	return 0;
46981da177e4SLinus Torvalds }
46991da177e4SLinus Torvalds 
47001da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
47011da177e4SLinus Torvalds 				  int size)
47021da177e4SLinus Torvalds {
4703be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
47041da177e4SLinus Torvalds }
47051da177e4SLinus Torvalds 
47061da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
47071da177e4SLinus Torvalds 				  int size, int flags)
47081da177e4SLinus Torvalds {
4709be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
47101da177e4SLinus Torvalds }
47111da177e4SLinus Torvalds 
47121da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
47131da177e4SLinus Torvalds {
4714be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
47151da177e4SLinus Torvalds }
47161da177e4SLinus Torvalds 
47171da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
47181da177e4SLinus Torvalds {
4719be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
47201da177e4SLinus Torvalds }
47211da177e4SLinus Torvalds 
47221da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
47231da177e4SLinus Torvalds {
4724f8687afeSPaul Moore 	int err;
4725f8687afeSPaul Moore 
4726be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4727f8687afeSPaul Moore 	if (err)
4728f8687afeSPaul Moore 		return err;
4729f8687afeSPaul Moore 
4730f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
47311da177e4SLinus Torvalds }
47321da177e4SLinus Torvalds 
47331da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
47341da177e4SLinus Torvalds 				     int optname)
47351da177e4SLinus Torvalds {
4736be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
47371da177e4SLinus Torvalds }
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
47401da177e4SLinus Torvalds {
4741be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
47421da177e4SLinus Torvalds }
47431da177e4SLinus Torvalds 
47443610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
47453610cda5SDavid S. Miller 					      struct sock *other,
47461da177e4SLinus Torvalds 					      struct sock *newsk)
47471da177e4SLinus Torvalds {
47483610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
47493610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
47504d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
47512bf49690SThomas Liu 	struct common_audit_data ad;
475248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
47531da177e4SLinus Torvalds 	int err;
47541da177e4SLinus Torvalds 
475550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
475648c62af6SEric Paris 	ad.u.net = &net;
475748c62af6SEric Paris 	ad.u.net->sk = other;
47581da177e4SLinus Torvalds 
47596b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
47606b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
47614d1e2451SPaul Moore 			   sksec_other->sclass,
47621da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
47631da177e4SLinus Torvalds 	if (err)
47641da177e4SLinus Torvalds 		return err;
47651da177e4SLinus Torvalds 
47661da177e4SLinus Torvalds 	/* server child socket */
47674d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4768aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4769aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
47704d1e2451SPaul Moore 	if (err)
47714237c75cSVenkat Yekkirala 		return err;
47724d1e2451SPaul Moore 
47734d1e2451SPaul Moore 	/* connecting socket */
47744d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
47754d1e2451SPaul Moore 
47764d1e2451SPaul Moore 	return 0;
47771da177e4SLinus Torvalds }
47781da177e4SLinus Torvalds 
47791da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
47801da177e4SLinus Torvalds 					struct socket *other)
47811da177e4SLinus Torvalds {
4782253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4783253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
47842bf49690SThomas Liu 	struct common_audit_data ad;
478548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
47861da177e4SLinus Torvalds 
478750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
478848c62af6SEric Paris 	ad.u.net = &net;
478948c62af6SEric Paris 	ad.u.net->sk = other->sk;
47901da177e4SLinus Torvalds 
47916b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
47926b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4793253bfae6SPaul Moore 			    &ad);
47941da177e4SLinus Torvalds }
47951da177e4SLinus Torvalds 
4796cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4797cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
47982bf49690SThomas Liu 				    struct common_audit_data *ad)
4799effad8dfSPaul Moore {
4800effad8dfSPaul Moore 	int err;
4801effad8dfSPaul Moore 	u32 if_sid;
4802effad8dfSPaul Moore 	u32 node_sid;
4803effad8dfSPaul Moore 
4804cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4805effad8dfSPaul Moore 	if (err)
4806effad8dfSPaul Moore 		return err;
48076b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
48086b6bc620SStephen Smalley 			   peer_sid, if_sid,
4809effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4810effad8dfSPaul Moore 	if (err)
4811effad8dfSPaul Moore 		return err;
4812effad8dfSPaul Moore 
4813effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4814effad8dfSPaul Moore 	if (err)
4815effad8dfSPaul Moore 		return err;
48166b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
48176b6bc620SStephen Smalley 			    peer_sid, node_sid,
4818effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4819effad8dfSPaul Moore }
4820effad8dfSPaul Moore 
4821220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4822d8395c87SPaul Moore 				       u16 family)
4823220deb96SPaul Moore {
4824277d342fSPaul Moore 	int err = 0;
4825220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4826220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
48272bf49690SThomas Liu 	struct common_audit_data ad;
482848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4829d8395c87SPaul Moore 	char *addrp;
4830d8395c87SPaul Moore 
483150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
483248c62af6SEric Paris 	ad.u.net = &net;
483348c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
483448c62af6SEric Paris 	ad.u.net->family = family;
4835d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4836d8395c87SPaul Moore 	if (err)
4837d8395c87SPaul Moore 		return err;
4838220deb96SPaul Moore 
483958bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
48406b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48416b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4842d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4843220deb96SPaul Moore 		if (err)
4844220deb96SPaul Moore 			return err;
484558bfbb51SPaul Moore 	}
4846220deb96SPaul Moore 
4847d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4848220deb96SPaul Moore 	if (err)
4849220deb96SPaul Moore 		return err;
4850d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4851220deb96SPaul Moore 
48524e5ab4cbSJames Morris 	return err;
48534e5ab4cbSJames Morris }
4854d28d1e08STrent Jaeger 
48554e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
48564e5ab4cbSJames Morris {
4857220deb96SPaul Moore 	int err;
48584237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4859220deb96SPaul Moore 	u16 family = sk->sk_family;
4860220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
48612bf49690SThomas Liu 	struct common_audit_data ad;
486248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4863220deb96SPaul Moore 	char *addrp;
4864d8395c87SPaul Moore 	u8 secmark_active;
4865d8395c87SPaul Moore 	u8 peerlbl_active;
48664e5ab4cbSJames Morris 
48674e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4868220deb96SPaul Moore 		return 0;
48694e5ab4cbSJames Morris 
48704e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
487187fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
48724e5ab4cbSJames Morris 		family = PF_INET;
48734e5ab4cbSJames Morris 
4874d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4875d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4876d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4877d8395c87SPaul Moore 	 * as fast and as clean as possible. */
4878aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
4879d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4880d8395c87SPaul Moore 
4881d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
48822be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4883d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4884d8395c87SPaul Moore 		return 0;
4885d8395c87SPaul Moore 
488650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
488748c62af6SEric Paris 	ad.u.net = &net;
488848c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
488948c62af6SEric Paris 	ad.u.net->family = family;
4890224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
48914e5ab4cbSJames Morris 	if (err)
4892220deb96SPaul Moore 		return err;
48934e5ab4cbSJames Morris 
4894d8395c87SPaul Moore 	if (peerlbl_active) {
4895d621d35eSPaul Moore 		u32 peer_sid;
4896220deb96SPaul Moore 
4897220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4898220deb96SPaul Moore 		if (err)
4899220deb96SPaul Moore 			return err;
4900cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4901cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4902dfaebe98SPaul Moore 		if (err) {
4903a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
4904effad8dfSPaul Moore 			return err;
4905dfaebe98SPaul Moore 		}
49066b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
49076b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
4908d621d35eSPaul Moore 				   PEER__RECV, &ad);
490946d01d63SChad Hanson 		if (err) {
4910a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
491146d01d63SChad Hanson 			return err;
491246d01d63SChad Hanson 		}
4913d621d35eSPaul Moore 	}
4914d621d35eSPaul Moore 
4915d8395c87SPaul Moore 	if (secmark_active) {
49166b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
49176b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4918effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4919effad8dfSPaul Moore 		if (err)
4920effad8dfSPaul Moore 			return err;
4921effad8dfSPaul Moore 	}
4922effad8dfSPaul Moore 
4923d621d35eSPaul Moore 	return err;
49241da177e4SLinus Torvalds }
49251da177e4SLinus Torvalds 
49262c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
49271da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
49281da177e4SLinus Torvalds {
49291da177e4SLinus Torvalds 	int err = 0;
49301da177e4SLinus Torvalds 	char *scontext;
49311da177e4SLinus Torvalds 	u32 scontext_len;
4932253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
49333de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
49341da177e4SLinus Torvalds 
4935253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4936d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
4937d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
4938dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4939253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4940253bfae6SPaul Moore 		return -ENOPROTOOPT;
49411da177e4SLinus Torvalds 
4942aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4943aa8e712cSStephen Smalley 				      &scontext_len);
49441da177e4SLinus Torvalds 	if (err)
4945253bfae6SPaul Moore 		return err;
49461da177e4SLinus Torvalds 
49471da177e4SLinus Torvalds 	if (scontext_len > len) {
49481da177e4SLinus Torvalds 		err = -ERANGE;
49491da177e4SLinus Torvalds 		goto out_len;
49501da177e4SLinus Torvalds 	}
49511da177e4SLinus Torvalds 
49521da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
49531da177e4SLinus Torvalds 		err = -EFAULT;
49541da177e4SLinus Torvalds 
49551da177e4SLinus Torvalds out_len:
49561da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
49571da177e4SLinus Torvalds 		err = -EFAULT;
49581da177e4SLinus Torvalds 	kfree(scontext);
49591da177e4SLinus Torvalds 	return err;
49601da177e4SLinus Torvalds }
49611da177e4SLinus Torvalds 
4962dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
49632c7946a7SCatherine Zhang {
4964dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
496575e22910SPaul Moore 	u16 family;
4966899134f2SPaul Moore 	struct inode_security_struct *isec;
4967877ce7c1SCatherine Zhang 
4968aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4969aa862900SPaul Moore 		family = PF_INET;
4970aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4971aa862900SPaul Moore 		family = PF_INET6;
4972aa862900SPaul Moore 	else if (sock)
497375e22910SPaul Moore 		family = sock->sk->sk_family;
497475e22910SPaul Moore 	else
497575e22910SPaul Moore 		goto out;
497675e22910SPaul Moore 
4977899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
4978899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
4979899134f2SPaul Moore 		peer_secid = isec->sid;
4980899134f2SPaul Moore 	} else if (skb)
4981220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
49822c7946a7SCatherine Zhang 
498375e22910SPaul Moore out:
4984dc49c1f9SCatherine Zhang 	*secid = peer_secid;
498575e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
498675e22910SPaul Moore 		return -EINVAL;
498775e22910SPaul Moore 	return 0;
49882c7946a7SCatherine Zhang }
49892c7946a7SCatherine Zhang 
49907d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
49911da177e4SLinus Torvalds {
499284914b7eSPaul Moore 	struct sk_security_struct *sksec;
499384914b7eSPaul Moore 
499484914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
499584914b7eSPaul Moore 	if (!sksec)
499684914b7eSPaul Moore 		return -ENOMEM;
499784914b7eSPaul Moore 
499884914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
499984914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
50005dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
500184914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
500284914b7eSPaul Moore 	sk->sk_security = sksec;
500384914b7eSPaul Moore 
500484914b7eSPaul Moore 	return 0;
50051da177e4SLinus Torvalds }
50061da177e4SLinus Torvalds 
50071da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
50081da177e4SLinus Torvalds {
500984914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
501084914b7eSPaul Moore 
501184914b7eSPaul Moore 	sk->sk_security = NULL;
501284914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
501384914b7eSPaul Moore 	kfree(sksec);
50141da177e4SLinus Torvalds }
50151da177e4SLinus Torvalds 
5016892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5017892c141eSVenkat Yekkirala {
5018dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5019dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5020892c141eSVenkat Yekkirala 
5021dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5022dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5023dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
502499f59ed0SPaul Moore 
5025dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5026892c141eSVenkat Yekkirala }
5027892c141eSVenkat Yekkirala 
5028beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5029d28d1e08STrent Jaeger {
5030d28d1e08STrent Jaeger 	if (!sk)
5031beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5032892c141eSVenkat Yekkirala 	else {
5033892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5034d28d1e08STrent Jaeger 
5035beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5036892c141eSVenkat Yekkirala 	}
5037d28d1e08STrent Jaeger }
5038d28d1e08STrent Jaeger 
50399a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
50404237c75cSVenkat Yekkirala {
50415d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
50425d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
50434237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
50444237c75cSVenkat Yekkirala 
50452873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
50462873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
50474237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5048220deb96SPaul Moore 	sksec->sclass = isec->sclass;
50494237c75cSVenkat Yekkirala }
50504237c75cSVenkat Yekkirala 
5051d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5052d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5053d452930fSRichard Haines  * already present).
5054d452930fSRichard Haines  */
5055d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5056d452930fSRichard Haines 				      struct sk_buff *skb)
5057d452930fSRichard Haines {
5058d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5059d452930fSRichard Haines 	struct common_audit_data ad;
5060d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5061d452930fSRichard Haines 	u8 peerlbl_active;
5062d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5063d452930fSRichard Haines 	u32 conn_sid;
5064d452930fSRichard Haines 	int err = 0;
5065d452930fSRichard Haines 
5066aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5067d452930fSRichard Haines 		return 0;
5068d452930fSRichard Haines 
5069d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5070d452930fSRichard Haines 
5071d452930fSRichard Haines 	if (peerlbl_active) {
5072d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5073d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5074d452930fSRichard Haines 		 */
5075d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5076d452930fSRichard Haines 					      &peer_sid);
5077d452930fSRichard Haines 		if (err)
5078d452930fSRichard Haines 			return err;
5079d452930fSRichard Haines 
5080d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5081d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5082d452930fSRichard Haines 	}
5083d452930fSRichard Haines 
5084d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5085d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5086d452930fSRichard Haines 
5087d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5088d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5089d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5090d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5091d452930fSRichard Haines 		 */
5092d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5093d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5094d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5095d452930fSRichard Haines 		 * consistency among the peer SIDs.
5096d452930fSRichard Haines 		 */
5097d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5098d452930fSRichard Haines 		ad.u.net = &net;
5099d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
51006b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
51016b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5102d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5103d452930fSRichard Haines 		if (err)
5104d452930fSRichard Haines 			return err;
5105d452930fSRichard Haines 	}
5106d452930fSRichard Haines 
5107d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5108d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5109d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5110d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5111d452930fSRichard Haines 	 * plug this into the new socket.
5112d452930fSRichard Haines 	 */
5113d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5114d452930fSRichard Haines 	if (err)
5115d452930fSRichard Haines 		return err;
5116d452930fSRichard Haines 
5117d452930fSRichard Haines 	ep->secid = conn_sid;
5118d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5119d452930fSRichard Haines 
5120d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5121d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5122d452930fSRichard Haines }
5123d452930fSRichard Haines 
5124d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5125d452930fSRichard Haines  * based on their @optname.
5126d452930fSRichard Haines  */
5127d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5128d452930fSRichard Haines 				     struct sockaddr *address,
5129d452930fSRichard Haines 				     int addrlen)
5130d452930fSRichard Haines {
5131d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5132d452930fSRichard Haines 	void *addr_buf;
5133d452930fSRichard Haines 	struct sockaddr *addr;
5134d452930fSRichard Haines 	struct socket *sock;
5135d452930fSRichard Haines 
5136aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5137d452930fSRichard Haines 		return 0;
5138d452930fSRichard Haines 
5139d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5140d452930fSRichard Haines 	sock = sk->sk_socket;
5141d452930fSRichard Haines 	addr_buf = address;
5142d452930fSRichard Haines 
5143d452930fSRichard Haines 	while (walk_size < addrlen) {
5144d452930fSRichard Haines 		addr = addr_buf;
5145d452930fSRichard Haines 		switch (addr->sa_family) {
51464152dc91SAlexey Kodanev 		case AF_UNSPEC:
5147d452930fSRichard Haines 		case AF_INET:
5148d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5149d452930fSRichard Haines 			break;
5150d452930fSRichard Haines 		case AF_INET6:
5151d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5152d452930fSRichard Haines 			break;
5153d452930fSRichard Haines 		default:
51544152dc91SAlexey Kodanev 			return -EINVAL;
5155d452930fSRichard Haines 		}
5156d452930fSRichard Haines 
5157d452930fSRichard Haines 		err = -EINVAL;
5158d452930fSRichard Haines 		switch (optname) {
5159d452930fSRichard Haines 		/* Bind checks */
5160d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5161d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5162d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5163d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5164d452930fSRichard Haines 			break;
5165d452930fSRichard Haines 		/* Connect checks */
5166d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5167d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5168d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5169d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5170d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5171d452930fSRichard Haines 			if (err)
5172d452930fSRichard Haines 				return err;
5173d452930fSRichard Haines 
5174d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5175d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5176d452930fSRichard Haines 			 * therefore selinux_netlbl_socket_connect_locked() is
5177d452930fSRichard Haines 			 * is called here. The situations handled are:
5178d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5179d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5180d452930fSRichard Haines 			 * primary address is selected.
5181d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5182d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5183d452930fSRichard Haines 			 * selinux_socket_connect().
5184d452930fSRichard Haines 			 */
5185d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5186d452930fSRichard Haines 			break;
5187d452930fSRichard Haines 		}
5188d452930fSRichard Haines 
5189d452930fSRichard Haines 		if (err)
5190d452930fSRichard Haines 			return err;
5191d452930fSRichard Haines 
5192d452930fSRichard Haines 		addr_buf += len;
5193d452930fSRichard Haines 		walk_size += len;
5194d452930fSRichard Haines 	}
5195d452930fSRichard Haines 
5196d452930fSRichard Haines 	return 0;
5197d452930fSRichard Haines }
5198d452930fSRichard Haines 
5199d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5200d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5201d452930fSRichard Haines 				  struct sock *newsk)
5202d452930fSRichard Haines {
5203d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5204d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5205d452930fSRichard Haines 
5206d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5207d452930fSRichard Haines 	 * the non-sctp clone version.
5208d452930fSRichard Haines 	 */
5209aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5210d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5211d452930fSRichard Haines 
5212d452930fSRichard Haines 	newsksec->sid = ep->secid;
5213d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5214d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5215d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5216d452930fSRichard Haines }
5217d452930fSRichard Haines 
52189a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
52194237c75cSVenkat Yekkirala 				     struct request_sock *req)
52204237c75cSVenkat Yekkirala {
52214237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52224237c75cSVenkat Yekkirala 	int err;
52230b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5224446b8024SPaul Moore 	u32 connsid;
52254237c75cSVenkat Yekkirala 	u32 peersid;
52264237c75cSVenkat Yekkirala 
5227aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5228220deb96SPaul Moore 	if (err)
5229220deb96SPaul Moore 		return err;
5230446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
52314237c75cSVenkat Yekkirala 	if (err)
52324237c75cSVenkat Yekkirala 		return err;
5233446b8024SPaul Moore 	req->secid = connsid;
52346b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5235389fb800SPaul Moore 
5236389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
52374237c75cSVenkat Yekkirala }
52384237c75cSVenkat Yekkirala 
52399a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
52409a673e56SAdrian Bunk 				   const struct request_sock *req)
52414237c75cSVenkat Yekkirala {
52424237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
52434237c75cSVenkat Yekkirala 
52444237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
52456b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
52464237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
52474237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
52484237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
52494237c75cSVenkat Yekkirala 	   time it will have been created and available. */
525099f59ed0SPaul Moore 
52519f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
52529f2ad665SPaul Moore 	 * thread with access to newsksec */
5253389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
52544237c75cSVenkat Yekkirala }
52554237c75cSVenkat Yekkirala 
5256014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
52576b877699SVenkat Yekkirala {
5258aa862900SPaul Moore 	u16 family = sk->sk_family;
52596b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52606b877699SVenkat Yekkirala 
5261aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5262aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5263aa862900SPaul Moore 		family = PF_INET;
5264aa862900SPaul Moore 
5265aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
52666b877699SVenkat Yekkirala }
52676b877699SVenkat Yekkirala 
52682606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
52692606fd1fSEric Paris {
52702606fd1fSEric Paris 	const struct task_security_struct *__tsec;
52712606fd1fSEric Paris 	u32 tsid;
52722606fd1fSEric Paris 
52732606fd1fSEric Paris 	__tsec = current_security();
52742606fd1fSEric Paris 	tsid = __tsec->sid;
52752606fd1fSEric Paris 
52766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
52776b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
52786b6bc620SStephen Smalley 			    NULL);
52792606fd1fSEric Paris }
52802606fd1fSEric Paris 
52812606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
52822606fd1fSEric Paris {
52832606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
52842606fd1fSEric Paris }
52852606fd1fSEric Paris 
52862606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
52872606fd1fSEric Paris {
52882606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
52892606fd1fSEric Paris }
52902606fd1fSEric Paris 
52919a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
52929a673e56SAdrian Bunk 				      struct flowi *fl)
52934237c75cSVenkat Yekkirala {
52941d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
52954237c75cSVenkat Yekkirala }
52964237c75cSVenkat Yekkirala 
52975dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
52985dbbaf2dSPaul Moore {
52995dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
53005dbbaf2dSPaul Moore 
53015dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
53025dbbaf2dSPaul Moore 	if (!tunsec)
53035dbbaf2dSPaul Moore 		return -ENOMEM;
53045dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
53055dbbaf2dSPaul Moore 
53065dbbaf2dSPaul Moore 	*security = tunsec;
53075dbbaf2dSPaul Moore 	return 0;
53085dbbaf2dSPaul Moore }
53095dbbaf2dSPaul Moore 
53105dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
53115dbbaf2dSPaul Moore {
53125dbbaf2dSPaul Moore 	kfree(security);
53135dbbaf2dSPaul Moore }
53145dbbaf2dSPaul Moore 
5315ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5316ed6d76e4SPaul Moore {
5317ed6d76e4SPaul Moore 	u32 sid = current_sid();
5318ed6d76e4SPaul Moore 
5319ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5320ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5321ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5322ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5323ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5324ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5325ed6d76e4SPaul Moore 
53266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
53276b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5328ed6d76e4SPaul Moore 			    NULL);
5329ed6d76e4SPaul Moore }
5330ed6d76e4SPaul Moore 
53315dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5332ed6d76e4SPaul Moore {
53335dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
53345dbbaf2dSPaul Moore 
53356b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
53366b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
53375dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
53385dbbaf2dSPaul Moore }
53395dbbaf2dSPaul Moore 
53405dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
53415dbbaf2dSPaul Moore {
53425dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5343ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5344ed6d76e4SPaul Moore 
5345ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5346ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5347ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5348ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5349ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5350ed6d76e4SPaul Moore 	 * protocols were being used */
5351ed6d76e4SPaul Moore 
53525dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5353ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
53545dbbaf2dSPaul Moore 
53555dbbaf2dSPaul Moore 	return 0;
5356ed6d76e4SPaul Moore }
5357ed6d76e4SPaul Moore 
53585dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5359ed6d76e4SPaul Moore {
53605dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5361ed6d76e4SPaul Moore 	u32 sid = current_sid();
5362ed6d76e4SPaul Moore 	int err;
5363ed6d76e4SPaul Moore 
53646b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
53656b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5366ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5367ed6d76e4SPaul Moore 	if (err)
5368ed6d76e4SPaul Moore 		return err;
53696b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
53706b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5371ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5372ed6d76e4SPaul Moore 	if (err)
5373ed6d76e4SPaul Moore 		return err;
53745dbbaf2dSPaul Moore 	tunsec->sid = sid;
5375ed6d76e4SPaul Moore 
5376ed6d76e4SPaul Moore 	return 0;
5377ed6d76e4SPaul Moore }
5378ed6d76e4SPaul Moore 
53791da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
53801da177e4SLinus Torvalds {
53811da177e4SLinus Torvalds 	int err = 0;
53821da177e4SLinus Torvalds 	u32 perm;
53831da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5384253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
53851da177e4SLinus Torvalds 
538677954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
53871da177e4SLinus Torvalds 		err = -EINVAL;
53881da177e4SLinus Torvalds 		goto out;
53891da177e4SLinus Torvalds 	}
5390b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
53911da177e4SLinus Torvalds 
5392253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
53931da177e4SLinus Torvalds 	if (err) {
53941da177e4SLinus Torvalds 		if (err == -EINVAL) {
539576319946SVladis Dronov 			pr_warn_ratelimited("SELinux: unrecognized netlink"
539676319946SVladis Dronov 			       " message: protocol=%hu nlmsg_type=%hu sclass=%s"
539776319946SVladis Dronov 			       " pig=%d comm=%s\n",
5398cded3fffSMarek Milkovic 			       sk->sk_protocol, nlh->nlmsg_type,
539976319946SVladis Dronov 			       secclass_map[sksec->sclass - 1].name,
540076319946SVladis Dronov 			       task_pid_nr(current), current->comm);
5401e5a5ca96SPaul Moore 			if (!enforcing_enabled(&selinux_state) ||
5402aa8e712cSStephen Smalley 			    security_get_allow_unknown(&selinux_state))
54031da177e4SLinus Torvalds 				err = 0;
54041da177e4SLinus Torvalds 		}
54051da177e4SLinus Torvalds 
54061da177e4SLinus Torvalds 		/* Ignore */
54071da177e4SLinus Torvalds 		if (err == -ENOENT)
54081da177e4SLinus Torvalds 			err = 0;
54091da177e4SLinus Torvalds 		goto out;
54101da177e4SLinus Torvalds 	}
54111da177e4SLinus Torvalds 
5412be0554c9SStephen Smalley 	err = sock_has_perm(sk, perm);
54131da177e4SLinus Torvalds out:
54141da177e4SLinus Torvalds 	return err;
54151da177e4SLinus Torvalds }
54161da177e4SLinus Torvalds 
54171da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
54181da177e4SLinus Torvalds 
5419cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5420cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5421effad8dfSPaul Moore 				       u16 family)
54221da177e4SLinus Torvalds {
5423dfaebe98SPaul Moore 	int err;
5424effad8dfSPaul Moore 	char *addrp;
5425effad8dfSPaul Moore 	u32 peer_sid;
54262bf49690SThomas Liu 	struct common_audit_data ad;
542748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5428effad8dfSPaul Moore 	u8 secmark_active;
5429948bf85cSPaul Moore 	u8 netlbl_active;
5430effad8dfSPaul Moore 	u8 peerlbl_active;
54314237c75cSVenkat Yekkirala 
5432aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5433effad8dfSPaul Moore 		return NF_ACCEPT;
54344237c75cSVenkat Yekkirala 
5435effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5436948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
54372be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5438effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5439effad8dfSPaul Moore 		return NF_ACCEPT;
54404237c75cSVenkat Yekkirala 
5441d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5442d8395c87SPaul Moore 		return NF_DROP;
5443d8395c87SPaul Moore 
544450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
544548c62af6SEric Paris 	ad.u.net = &net;
5446cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
544748c62af6SEric Paris 	ad.u.net->family = family;
5448effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5449effad8dfSPaul Moore 		return NF_DROP;
54501da177e4SLinus Torvalds 
5451dfaebe98SPaul Moore 	if (peerlbl_active) {
5452cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5453cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5454dfaebe98SPaul Moore 		if (err) {
5455a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5456effad8dfSPaul Moore 			return NF_DROP;
5457dfaebe98SPaul Moore 		}
5458dfaebe98SPaul Moore 	}
5459effad8dfSPaul Moore 
5460effad8dfSPaul Moore 	if (secmark_active)
54616b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
54626b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5463effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5464effad8dfSPaul Moore 			return NF_DROP;
5465effad8dfSPaul Moore 
5466948bf85cSPaul Moore 	if (netlbl_active)
5467948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5468948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5469948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5470948bf85cSPaul Moore 		 * protection */
5471948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5472948bf85cSPaul Moore 			return NF_DROP;
5473948bf85cSPaul Moore 
5474effad8dfSPaul Moore 	return NF_ACCEPT;
5475effad8dfSPaul Moore }
5476effad8dfSPaul Moore 
547706198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5478effad8dfSPaul Moore 					 struct sk_buff *skb,
5479238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5480effad8dfSPaul Moore {
5481238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5482effad8dfSPaul Moore }
5483effad8dfSPaul Moore 
54841a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
548506198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5486effad8dfSPaul Moore 					 struct sk_buff *skb,
5487238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5488effad8dfSPaul Moore {
5489238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5490effad8dfSPaul Moore }
5491effad8dfSPaul Moore #endif	/* IPV6 */
5492effad8dfSPaul Moore 
5493948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5494948bf85cSPaul Moore 				      u16 family)
5495948bf85cSPaul Moore {
549647180068SPaul Moore 	struct sock *sk;
5497948bf85cSPaul Moore 	u32 sid;
5498948bf85cSPaul Moore 
5499948bf85cSPaul Moore 	if (!netlbl_enabled())
5500948bf85cSPaul Moore 		return NF_ACCEPT;
5501948bf85cSPaul Moore 
5502948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5503948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5504948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
550547180068SPaul Moore 	sk = skb->sk;
550647180068SPaul Moore 	if (sk) {
550747180068SPaul Moore 		struct sk_security_struct *sksec;
550847180068SPaul Moore 
5509e446f9dfSEric Dumazet 		if (sk_listener(sk))
551047180068SPaul Moore 			/* if the socket is the listening state then this
551147180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
551247180068SPaul Moore 			 * be labeled based on the connection/request_sock and
551347180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
551447180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
551547180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
551647180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
551747180068SPaul Moore 			 * as any IP option based labeling should be copied
551847180068SPaul Moore 			 * from the initial connection request (in the IP
551947180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
552047180068SPaul Moore 			 * security label in the packet itself this is the
552147180068SPaul Moore 			 * best we can do. */
552247180068SPaul Moore 			return NF_ACCEPT;
552347180068SPaul Moore 
552447180068SPaul Moore 		/* standard practice, label using the parent socket */
552547180068SPaul Moore 		sksec = sk->sk_security;
5526948bf85cSPaul Moore 		sid = sksec->sid;
5527948bf85cSPaul Moore 	} else
5528948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5529948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5530948bf85cSPaul Moore 		return NF_DROP;
5531948bf85cSPaul Moore 
5532948bf85cSPaul Moore 	return NF_ACCEPT;
5533948bf85cSPaul Moore }
5534948bf85cSPaul Moore 
553506198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5536948bf85cSPaul Moore 					struct sk_buff *skb,
5537238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5538948bf85cSPaul Moore {
5539948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5540948bf85cSPaul Moore }
5541948bf85cSPaul Moore 
55421a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
55432917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
55442917f57bSHuw Davies 					struct sk_buff *skb,
55452917f57bSHuw Davies 					const struct nf_hook_state *state)
55462917f57bSHuw Davies {
55472917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
55482917f57bSHuw Davies }
55492917f57bSHuw Davies #endif	/* IPV6 */
55502917f57bSHuw Davies 
5551effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5552effad8dfSPaul Moore 						int ifindex,
5553d8395c87SPaul Moore 						u16 family)
55544e5ab4cbSJames Morris {
555554abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
55564237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
55572bf49690SThomas Liu 	struct common_audit_data ad;
555848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5559d8395c87SPaul Moore 	char *addrp;
5560d8395c87SPaul Moore 	u8 proto;
55614e5ab4cbSJames Morris 
5562effad8dfSPaul Moore 	if (sk == NULL)
5563effad8dfSPaul Moore 		return NF_ACCEPT;
55644237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
55654e5ab4cbSJames Morris 
556650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
556748c62af6SEric Paris 	ad.u.net = &net;
556848c62af6SEric Paris 	ad.u.net->netif = ifindex;
556948c62af6SEric Paris 	ad.u.net->family = family;
5570d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5571d8395c87SPaul Moore 		return NF_DROP;
5572d8395c87SPaul Moore 
557358bfbb51SPaul Moore 	if (selinux_secmark_enabled())
55746b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
55756b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5576d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
55772fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
55781da177e4SLinus Torvalds 
5579d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
55802fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5581effad8dfSPaul Moore 
5582effad8dfSPaul Moore 	return NF_ACCEPT;
5583effad8dfSPaul Moore }
5584effad8dfSPaul Moore 
5585cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5586cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5587effad8dfSPaul Moore 					 u16 family)
5588effad8dfSPaul Moore {
5589effad8dfSPaul Moore 	u32 secmark_perm;
5590effad8dfSPaul Moore 	u32 peer_sid;
5591cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5592effad8dfSPaul Moore 	struct sock *sk;
55932bf49690SThomas Liu 	struct common_audit_data ad;
559448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5595effad8dfSPaul Moore 	char *addrp;
5596effad8dfSPaul Moore 	u8 secmark_active;
5597effad8dfSPaul Moore 	u8 peerlbl_active;
5598effad8dfSPaul Moore 
5599effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5600effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5601effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5602effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5603aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5604d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5605c0828e50SPaul Moore 
5606effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
56072be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5608effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5609effad8dfSPaul Moore 		return NF_ACCEPT;
5610effad8dfSPaul Moore 
561154abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5612c0828e50SPaul Moore 
5613effad8dfSPaul Moore #ifdef CONFIG_XFRM
5614effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5615effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5616effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5617effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5618effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5619c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5620c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5621c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5622c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5623c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5624c0828e50SPaul Moore 	 *       connection. */
5625c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5626e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5627effad8dfSPaul Moore 		return NF_ACCEPT;
5628effad8dfSPaul Moore #endif
5629effad8dfSPaul Moore 
5630d8395c87SPaul Moore 	if (sk == NULL) {
5631446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5632446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5633446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5634446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
56354a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5636d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5637d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
563804f6d70fSEric Paris 				return NF_DROP;
56394a7ab3dcSSteffen Klassert 		} else {
56404a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5641d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
56424a7ab3dcSSteffen Klassert 		}
5643e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5644446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5645446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5646446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5647446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5648446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5649446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5650446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5651446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5652446b8024SPaul Moore 		 * for similar problems. */
5653446b8024SPaul Moore 		u32 skb_sid;
5654e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5655e446f9dfSEric Dumazet 
5656e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5657446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5658446b8024SPaul Moore 			return NF_DROP;
5659c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5660c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5661c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5662c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5663c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5664c0828e50SPaul Moore 		 * pass the packet. */
5665c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5666c0828e50SPaul Moore 			switch (family) {
5667c0828e50SPaul Moore 			case PF_INET:
5668c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5669c0828e50SPaul Moore 					return NF_ACCEPT;
5670c0828e50SPaul Moore 				break;
5671c0828e50SPaul Moore 			case PF_INET6:
5672c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5673c0828e50SPaul Moore 					return NF_ACCEPT;
5674a7a91a19SPaul Moore 				break;
5675c0828e50SPaul Moore 			default:
5676c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5677c0828e50SPaul Moore 			}
5678c0828e50SPaul Moore 		}
5679446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5680446b8024SPaul Moore 			return NF_DROP;
5681446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5682d8395c87SPaul Moore 	} else {
5683446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5684446b8024SPaul Moore 		 * associated socket. */
5685effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5686effad8dfSPaul Moore 		peer_sid = sksec->sid;
5687effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5688effad8dfSPaul Moore 	}
5689effad8dfSPaul Moore 
569050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
569148c62af6SEric Paris 	ad.u.net = &net;
569248c62af6SEric Paris 	ad.u.net->netif = ifindex;
569348c62af6SEric Paris 	ad.u.net->family = family;
5694d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
569504f6d70fSEric Paris 		return NF_DROP;
5696d8395c87SPaul Moore 
5697effad8dfSPaul Moore 	if (secmark_active)
56986b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56996b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5700effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
57011f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5702effad8dfSPaul Moore 
5703effad8dfSPaul Moore 	if (peerlbl_active) {
5704effad8dfSPaul Moore 		u32 if_sid;
5705effad8dfSPaul Moore 		u32 node_sid;
5706effad8dfSPaul Moore 
5707cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
570804f6d70fSEric Paris 			return NF_DROP;
57096b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57106b6bc620SStephen Smalley 				 peer_sid, if_sid,
5711effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
57121f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5713effad8dfSPaul Moore 
5714effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
571504f6d70fSEric Paris 			return NF_DROP;
57166b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57176b6bc620SStephen Smalley 				 peer_sid, node_sid,
5718effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
57191f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5720effad8dfSPaul Moore 	}
5721effad8dfSPaul Moore 
5722effad8dfSPaul Moore 	return NF_ACCEPT;
5723effad8dfSPaul Moore }
5724effad8dfSPaul Moore 
572506198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5726a224be76SDavid S. Miller 					   struct sk_buff *skb,
5727238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
57281da177e4SLinus Torvalds {
5729238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
57301da177e4SLinus Torvalds }
57311da177e4SLinus Torvalds 
57321a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
573306198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5734a224be76SDavid S. Miller 					   struct sk_buff *skb,
5735238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
57361da177e4SLinus Torvalds {
5737238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
57381da177e4SLinus Torvalds }
57391da177e4SLinus Torvalds #endif	/* IPV6 */
57401da177e4SLinus Torvalds 
57411da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
57421da177e4SLinus Torvalds 
57431da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
57441da177e4SLinus Torvalds {
5745941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
57461da177e4SLinus Torvalds }
57471da177e4SLinus Torvalds 
5748be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm,
57491da177e4SLinus Torvalds 			      u16 sclass)
57501da177e4SLinus Torvalds {
57511da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
57521da177e4SLinus Torvalds 
575389d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
57541da177e4SLinus Torvalds 	if (!isec)
57551da177e4SLinus Torvalds 		return -ENOMEM;
57561da177e4SLinus Torvalds 
57571da177e4SLinus Torvalds 	isec->sclass = sclass;
5758be0554c9SStephen Smalley 	isec->sid = current_sid();
57591da177e4SLinus Torvalds 	perm->security = isec;
57601da177e4SLinus Torvalds 
57611da177e4SLinus Torvalds 	return 0;
57621da177e4SLinus Torvalds }
57631da177e4SLinus Torvalds 
57641da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
57651da177e4SLinus Torvalds {
57661da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
57671da177e4SLinus Torvalds 	perm->security = NULL;
57681da177e4SLinus Torvalds 	kfree(isec);
57691da177e4SLinus Torvalds }
57701da177e4SLinus Torvalds 
57711da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
57721da177e4SLinus Torvalds {
57731da177e4SLinus Torvalds 	struct msg_security_struct *msec;
57741da177e4SLinus Torvalds 
577589d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
57761da177e4SLinus Torvalds 	if (!msec)
57771da177e4SLinus Torvalds 		return -ENOMEM;
57781da177e4SLinus Torvalds 
57791da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
57801da177e4SLinus Torvalds 	msg->security = msec;
57811da177e4SLinus Torvalds 
57821da177e4SLinus Torvalds 	return 0;
57831da177e4SLinus Torvalds }
57841da177e4SLinus Torvalds 
57851da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
57861da177e4SLinus Torvalds {
57871da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
57881da177e4SLinus Torvalds 
57891da177e4SLinus Torvalds 	msg->security = NULL;
57901da177e4SLinus Torvalds 	kfree(msec);
57911da177e4SLinus Torvalds }
57921da177e4SLinus Torvalds 
57931da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
57946af963f1SStephen Smalley 			u32 perms)
57951da177e4SLinus Torvalds {
57961da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
57972bf49690SThomas Liu 	struct common_audit_data ad;
5798275bb41eSDavid Howells 	u32 sid = current_sid();
57991da177e4SLinus Torvalds 
58001da177e4SLinus Torvalds 	isec = ipc_perms->security;
58011da177e4SLinus Torvalds 
580250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
58031da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
58041da177e4SLinus Torvalds 
58056b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
58066b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
58071da177e4SLinus Torvalds }
58081da177e4SLinus Torvalds 
58091da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
58101da177e4SLinus Torvalds {
58111da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
58121da177e4SLinus Torvalds }
58131da177e4SLinus Torvalds 
58141da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
58151da177e4SLinus Torvalds {
58161da177e4SLinus Torvalds 	msg_msg_free_security(msg);
58171da177e4SLinus Torvalds }
58181da177e4SLinus Torvalds 
58191da177e4SLinus Torvalds /* message queue security operations */
5820d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
58211da177e4SLinus Torvalds {
58221da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58232bf49690SThomas Liu 	struct common_audit_data ad;
5824275bb41eSDavid Howells 	u32 sid = current_sid();
58251da177e4SLinus Torvalds 	int rc;
58261da177e4SLinus Torvalds 
5827d8c6e854SEric W. Biederman 	rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
58281da177e4SLinus Torvalds 	if (rc)
58291da177e4SLinus Torvalds 		return rc;
58301da177e4SLinus Torvalds 
5831d8c6e854SEric W. Biederman 	isec = msq->security;
58321da177e4SLinus Torvalds 
583350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5834d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
58351da177e4SLinus Torvalds 
58366b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
58376b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
58381da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
58391da177e4SLinus Torvalds 	if (rc) {
5840d8c6e854SEric W. Biederman 		ipc_free_security(msq);
58411da177e4SLinus Torvalds 		return rc;
58421da177e4SLinus Torvalds 	}
58431da177e4SLinus Torvalds 	return 0;
58441da177e4SLinus Torvalds }
58451da177e4SLinus Torvalds 
5846d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
58471da177e4SLinus Torvalds {
5848d8c6e854SEric W. Biederman 	ipc_free_security(msq);
58491da177e4SLinus Torvalds }
58501da177e4SLinus Torvalds 
5851d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
58521da177e4SLinus Torvalds {
58531da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58542bf49690SThomas Liu 	struct common_audit_data ad;
5855275bb41eSDavid Howells 	u32 sid = current_sid();
58561da177e4SLinus Torvalds 
5857d8c6e854SEric W. Biederman 	isec = msq->security;
58581da177e4SLinus Torvalds 
585950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5860d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
58611da177e4SLinus Torvalds 
58626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
58636b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
58641da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
58651da177e4SLinus Torvalds }
58661da177e4SLinus Torvalds 
5867d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
58681da177e4SLinus Torvalds {
58691da177e4SLinus Torvalds 	int err;
58701da177e4SLinus Torvalds 	int perms;
58711da177e4SLinus Torvalds 
58721da177e4SLinus Torvalds 	switch (cmd) {
58731da177e4SLinus Torvalds 	case IPC_INFO:
58741da177e4SLinus Torvalds 	case MSG_INFO:
58751da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
58766b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
58776b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
5878be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
58791da177e4SLinus Torvalds 	case IPC_STAT:
58801da177e4SLinus Torvalds 	case MSG_STAT:
588123c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
58821da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
58831da177e4SLinus Torvalds 		break;
58841da177e4SLinus Torvalds 	case IPC_SET:
58851da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
58861da177e4SLinus Torvalds 		break;
58871da177e4SLinus Torvalds 	case IPC_RMID:
58881da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
58891da177e4SLinus Torvalds 		break;
58901da177e4SLinus Torvalds 	default:
58911da177e4SLinus Torvalds 		return 0;
58921da177e4SLinus Torvalds 	}
58931da177e4SLinus Torvalds 
5894d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
58951da177e4SLinus Torvalds 	return err;
58961da177e4SLinus Torvalds }
58971da177e4SLinus Torvalds 
5898d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
58991da177e4SLinus Torvalds {
59001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59011da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59022bf49690SThomas Liu 	struct common_audit_data ad;
5903275bb41eSDavid Howells 	u32 sid = current_sid();
59041da177e4SLinus Torvalds 	int rc;
59051da177e4SLinus Torvalds 
5906d8c6e854SEric W. Biederman 	isec = msq->security;
59071da177e4SLinus Torvalds 	msec = msg->security;
59081da177e4SLinus Torvalds 
59091da177e4SLinus Torvalds 	/*
59101da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
59111da177e4SLinus Torvalds 	 */
59121da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
59131da177e4SLinus Torvalds 		/*
59141da177e4SLinus Torvalds 		 * Compute new sid based on current process and
59151da177e4SLinus Torvalds 		 * message queue this message will be stored in
59161da177e4SLinus Torvalds 		 */
5917aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
5918aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
59191da177e4SLinus Torvalds 		if (rc)
59201da177e4SLinus Torvalds 			return rc;
59211da177e4SLinus Torvalds 	}
59221da177e4SLinus Torvalds 
592350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5924d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59251da177e4SLinus Torvalds 
59261da177e4SLinus Torvalds 	/* Can this process write to the queue? */
59276b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59286b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
59291da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
59301da177e4SLinus Torvalds 	if (!rc)
59311da177e4SLinus Torvalds 		/* Can this process send the message */
59326b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59336b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
5934275bb41eSDavid Howells 				  MSG__SEND, &ad);
59351da177e4SLinus Torvalds 	if (!rc)
59361da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
59376b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59386b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
5939275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
59401da177e4SLinus Torvalds 
59411da177e4SLinus Torvalds 	return rc;
59421da177e4SLinus Torvalds }
59431da177e4SLinus Torvalds 
5944d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
59451da177e4SLinus Torvalds 				    struct task_struct *target,
59461da177e4SLinus Torvalds 				    long type, int mode)
59471da177e4SLinus Torvalds {
59481da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59491da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59502bf49690SThomas Liu 	struct common_audit_data ad;
5951275bb41eSDavid Howells 	u32 sid = task_sid(target);
59521da177e4SLinus Torvalds 	int rc;
59531da177e4SLinus Torvalds 
5954d8c6e854SEric W. Biederman 	isec = msq->security;
59551da177e4SLinus Torvalds 	msec = msg->security;
59561da177e4SLinus Torvalds 
595750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5958d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59591da177e4SLinus Torvalds 
59606b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59616b6bc620SStephen Smalley 			  sid, isec->sid,
59621da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
59631da177e4SLinus Torvalds 	if (!rc)
59646b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59656b6bc620SStephen Smalley 				  sid, msec->sid,
59661da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
59671da177e4SLinus Torvalds 	return rc;
59681da177e4SLinus Torvalds }
59691da177e4SLinus Torvalds 
59701da177e4SLinus Torvalds /* Shared Memory security operations */
59717191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
59721da177e4SLinus Torvalds {
59731da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59742bf49690SThomas Liu 	struct common_audit_data ad;
5975275bb41eSDavid Howells 	u32 sid = current_sid();
59761da177e4SLinus Torvalds 	int rc;
59771da177e4SLinus Torvalds 
59787191adffSEric W. Biederman 	rc = ipc_alloc_security(shp, SECCLASS_SHM);
59791da177e4SLinus Torvalds 	if (rc)
59801da177e4SLinus Torvalds 		return rc;
59811da177e4SLinus Torvalds 
59827191adffSEric W. Biederman 	isec = shp->security;
59831da177e4SLinus Torvalds 
598450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59857191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
59861da177e4SLinus Torvalds 
59876b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59886b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
59891da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
59901da177e4SLinus Torvalds 	if (rc) {
59917191adffSEric W. Biederman 		ipc_free_security(shp);
59921da177e4SLinus Torvalds 		return rc;
59931da177e4SLinus Torvalds 	}
59941da177e4SLinus Torvalds 	return 0;
59951da177e4SLinus Torvalds }
59961da177e4SLinus Torvalds 
59977191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp)
59981da177e4SLinus Torvalds {
59997191adffSEric W. Biederman 	ipc_free_security(shp);
60001da177e4SLinus Torvalds }
60011da177e4SLinus Torvalds 
60027191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
60031da177e4SLinus Torvalds {
60041da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60052bf49690SThomas Liu 	struct common_audit_data ad;
6006275bb41eSDavid Howells 	u32 sid = current_sid();
60071da177e4SLinus Torvalds 
60087191adffSEric W. Biederman 	isec = shp->security;
60091da177e4SLinus Torvalds 
601050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
60117191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
60121da177e4SLinus Torvalds 
60136b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60146b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
60151da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
60161da177e4SLinus Torvalds }
60171da177e4SLinus Torvalds 
60181da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
60197191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
60201da177e4SLinus Torvalds {
60211da177e4SLinus Torvalds 	int perms;
60221da177e4SLinus Torvalds 	int err;
60231da177e4SLinus Torvalds 
60241da177e4SLinus Torvalds 	switch (cmd) {
60251da177e4SLinus Torvalds 	case IPC_INFO:
60261da177e4SLinus Torvalds 	case SHM_INFO:
60271da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60286b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60296b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6030be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60311da177e4SLinus Torvalds 	case IPC_STAT:
60321da177e4SLinus Torvalds 	case SHM_STAT:
6033c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
60341da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
60351da177e4SLinus Torvalds 		break;
60361da177e4SLinus Torvalds 	case IPC_SET:
60371da177e4SLinus Torvalds 		perms = SHM__SETATTR;
60381da177e4SLinus Torvalds 		break;
60391da177e4SLinus Torvalds 	case SHM_LOCK:
60401da177e4SLinus Torvalds 	case SHM_UNLOCK:
60411da177e4SLinus Torvalds 		perms = SHM__LOCK;
60421da177e4SLinus Torvalds 		break;
60431da177e4SLinus Torvalds 	case IPC_RMID:
60441da177e4SLinus Torvalds 		perms = SHM__DESTROY;
60451da177e4SLinus Torvalds 		break;
60461da177e4SLinus Torvalds 	default:
60471da177e4SLinus Torvalds 		return 0;
60481da177e4SLinus Torvalds 	}
60491da177e4SLinus Torvalds 
60507191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
60511da177e4SLinus Torvalds 	return err;
60521da177e4SLinus Torvalds }
60531da177e4SLinus Torvalds 
60547191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
60551da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
60561da177e4SLinus Torvalds {
60571da177e4SLinus Torvalds 	u32 perms;
60581da177e4SLinus Torvalds 
60591da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
60601da177e4SLinus Torvalds 		perms = SHM__READ;
60611da177e4SLinus Torvalds 	else
60621da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
60631da177e4SLinus Torvalds 
60647191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
60651da177e4SLinus Torvalds }
60661da177e4SLinus Torvalds 
60671da177e4SLinus Torvalds /* Semaphore security operations */
6068aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
60691da177e4SLinus Torvalds {
60701da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60712bf49690SThomas Liu 	struct common_audit_data ad;
6072275bb41eSDavid Howells 	u32 sid = current_sid();
60731da177e4SLinus Torvalds 	int rc;
60741da177e4SLinus Torvalds 
6075aefad959SEric W. Biederman 	rc = ipc_alloc_security(sma, SECCLASS_SEM);
60761da177e4SLinus Torvalds 	if (rc)
60771da177e4SLinus Torvalds 		return rc;
60781da177e4SLinus Torvalds 
6079aefad959SEric W. Biederman 	isec = sma->security;
60801da177e4SLinus Torvalds 
608150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6082aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
60831da177e4SLinus Torvalds 
60846b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60856b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
60861da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
60871da177e4SLinus Torvalds 	if (rc) {
6088aefad959SEric W. Biederman 		ipc_free_security(sma);
60891da177e4SLinus Torvalds 		return rc;
60901da177e4SLinus Torvalds 	}
60911da177e4SLinus Torvalds 	return 0;
60921da177e4SLinus Torvalds }
60931da177e4SLinus Torvalds 
6094aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma)
60951da177e4SLinus Torvalds {
6096aefad959SEric W. Biederman 	ipc_free_security(sma);
60971da177e4SLinus Torvalds }
60981da177e4SLinus Torvalds 
6099aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
61001da177e4SLinus Torvalds {
61011da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61022bf49690SThomas Liu 	struct common_audit_data ad;
6103275bb41eSDavid Howells 	u32 sid = current_sid();
61041da177e4SLinus Torvalds 
6105aefad959SEric W. Biederman 	isec = sma->security;
61061da177e4SLinus Torvalds 
610750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6108aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
61091da177e4SLinus Torvalds 
61106b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
61116b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
61121da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
61131da177e4SLinus Torvalds }
61141da177e4SLinus Torvalds 
61151da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6116aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
61171da177e4SLinus Torvalds {
61181da177e4SLinus Torvalds 	int err;
61191da177e4SLinus Torvalds 	u32 perms;
61201da177e4SLinus Torvalds 
61211da177e4SLinus Torvalds 	switch (cmd) {
61221da177e4SLinus Torvalds 	case IPC_INFO:
61231da177e4SLinus Torvalds 	case SEM_INFO:
61241da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
61256b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
61266b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6127be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
61281da177e4SLinus Torvalds 	case GETPID:
61291da177e4SLinus Torvalds 	case GETNCNT:
61301da177e4SLinus Torvalds 	case GETZCNT:
61311da177e4SLinus Torvalds 		perms = SEM__GETATTR;
61321da177e4SLinus Torvalds 		break;
61331da177e4SLinus Torvalds 	case GETVAL:
61341da177e4SLinus Torvalds 	case GETALL:
61351da177e4SLinus Torvalds 		perms = SEM__READ;
61361da177e4SLinus Torvalds 		break;
61371da177e4SLinus Torvalds 	case SETVAL:
61381da177e4SLinus Torvalds 	case SETALL:
61391da177e4SLinus Torvalds 		perms = SEM__WRITE;
61401da177e4SLinus Torvalds 		break;
61411da177e4SLinus Torvalds 	case IPC_RMID:
61421da177e4SLinus Torvalds 		perms = SEM__DESTROY;
61431da177e4SLinus Torvalds 		break;
61441da177e4SLinus Torvalds 	case IPC_SET:
61451da177e4SLinus Torvalds 		perms = SEM__SETATTR;
61461da177e4SLinus Torvalds 		break;
61471da177e4SLinus Torvalds 	case IPC_STAT:
61481da177e4SLinus Torvalds 	case SEM_STAT:
6149a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
61501da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
61511da177e4SLinus Torvalds 		break;
61521da177e4SLinus Torvalds 	default:
61531da177e4SLinus Torvalds 		return 0;
61541da177e4SLinus Torvalds 	}
61551da177e4SLinus Torvalds 
6156aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
61571da177e4SLinus Torvalds 	return err;
61581da177e4SLinus Torvalds }
61591da177e4SLinus Torvalds 
6160aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
61611da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
61621da177e4SLinus Torvalds {
61631da177e4SLinus Torvalds 	u32 perms;
61641da177e4SLinus Torvalds 
61651da177e4SLinus Torvalds 	if (alter)
61661da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
61671da177e4SLinus Torvalds 	else
61681da177e4SLinus Torvalds 		perms = SEM__READ;
61691da177e4SLinus Torvalds 
6170aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
61711da177e4SLinus Torvalds }
61721da177e4SLinus Torvalds 
61731da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
61741da177e4SLinus Torvalds {
61751da177e4SLinus Torvalds 	u32 av = 0;
61761da177e4SLinus Torvalds 
61771da177e4SLinus Torvalds 	av = 0;
61781da177e4SLinus Torvalds 	if (flag & S_IRUGO)
61791da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
61801da177e4SLinus Torvalds 	if (flag & S_IWUGO)
61811da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
61821da177e4SLinus Torvalds 
61831da177e4SLinus Torvalds 	if (av == 0)
61841da177e4SLinus Torvalds 		return 0;
61851da177e4SLinus Torvalds 
61866af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
61871da177e4SLinus Torvalds }
61881da177e4SLinus Torvalds 
6189713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6190713a04aeSAhmed S. Darwish {
6191713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
6192713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6193713a04aeSAhmed S. Darwish }
6194713a04aeSAhmed S. Darwish 
61951da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
61961da177e4SLinus Torvalds {
61971da177e4SLinus Torvalds 	if (inode)
61981da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
61991da177e4SLinus Torvalds }
62001da177e4SLinus Torvalds 
62011da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
620204ff9708SAl Viro 			       char *name, char **value)
62031da177e4SLinus Torvalds {
6204275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
62058c8570fbSDustin Kirkland 	u32 sid;
62061da177e4SLinus Torvalds 	int error;
620704ff9708SAl Viro 	unsigned len;
62081da177e4SLinus Torvalds 
6209275bb41eSDavid Howells 	rcu_read_lock();
6210275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
62111da177e4SLinus Torvalds 
6212be0554c9SStephen Smalley 	if (current != p) {
62136b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62146b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6215be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6216be0554c9SStephen Smalley 		if (error)
6217be0554c9SStephen Smalley 			goto bad;
6218be0554c9SStephen Smalley 	}
6219be0554c9SStephen Smalley 
62201da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6221275bb41eSDavid Howells 		sid = __tsec->sid;
62221da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6223275bb41eSDavid Howells 		sid = __tsec->osid;
62241da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6225275bb41eSDavid Howells 		sid = __tsec->exec_sid;
62261da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6227275bb41eSDavid Howells 		sid = __tsec->create_sid;
62284eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6229275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
623042c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6231275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6232be0554c9SStephen Smalley 	else {
6233be0554c9SStephen Smalley 		error = -EINVAL;
6234be0554c9SStephen Smalley 		goto bad;
6235be0554c9SStephen Smalley 	}
6236275bb41eSDavid Howells 	rcu_read_unlock();
62371da177e4SLinus Torvalds 
62381da177e4SLinus Torvalds 	if (!sid)
62391da177e4SLinus Torvalds 		return 0;
62401da177e4SLinus Torvalds 
6241aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
624204ff9708SAl Viro 	if (error)
624304ff9708SAl Viro 		return error;
624404ff9708SAl Viro 	return len;
6245275bb41eSDavid Howells 
6246be0554c9SStephen Smalley bad:
6247275bb41eSDavid Howells 	rcu_read_unlock();
6248be0554c9SStephen Smalley 	return error;
62491da177e4SLinus Torvalds }
62501da177e4SLinus Torvalds 
6251b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
62521da177e4SLinus Torvalds {
62531da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6254d84f4f99SDavid Howells 	struct cred *new;
6255be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
62561da177e4SLinus Torvalds 	int error;
62571da177e4SLinus Torvalds 	char *str = value;
62581da177e4SLinus Torvalds 
62591da177e4SLinus Torvalds 	/*
62601da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
62611da177e4SLinus Torvalds 	 */
62621da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
62636b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62646b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6265be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
62661da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
62676b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62686b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6269be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
62704eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
62716b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62726b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6273be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
627442c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
62756b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62766b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6277be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
62781da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
62796b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62806b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6281be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
62821da177e4SLinus Torvalds 	else
62831da177e4SLinus Torvalds 		error = -EINVAL;
62841da177e4SLinus Torvalds 	if (error)
62851da177e4SLinus Torvalds 		return error;
62861da177e4SLinus Torvalds 
62871da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6288a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
62891da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
62901da177e4SLinus Torvalds 			str[size-1] = 0;
62911da177e4SLinus Torvalds 			size--;
62921da177e4SLinus Torvalds 		}
6293aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6294aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
629512b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6296db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6297d6ea83ecSEric Paris 				struct audit_buffer *ab;
6298d6ea83ecSEric Paris 				size_t audit_size;
6299d6ea83ecSEric Paris 
6300d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6301d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6302d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6303d6ea83ecSEric Paris 					audit_size = size - 1;
6304d6ea83ecSEric Paris 				else
6305d6ea83ecSEric Paris 					audit_size = size;
6306cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6307cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6308cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6309d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6310d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6311d6ea83ecSEric Paris 				audit_log_end(ab);
6312d6ea83ecSEric Paris 
631312b29f34SStephen Smalley 				return error;
6314d6ea83ecSEric Paris 			}
6315aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6316aa8e712cSStephen Smalley 						      &selinux_state,
6317aa8e712cSStephen Smalley 						      value, size, &sid);
631812b29f34SStephen Smalley 		}
63191da177e4SLinus Torvalds 		if (error)
63201da177e4SLinus Torvalds 			return error;
63211da177e4SLinus Torvalds 	}
63221da177e4SLinus Torvalds 
6323d84f4f99SDavid Howells 	new = prepare_creds();
6324d84f4f99SDavid Howells 	if (!new)
6325d84f4f99SDavid Howells 		return -ENOMEM;
6326d84f4f99SDavid Howells 
63271da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
63281da177e4SLinus Torvalds 	   performed during the actual operation (execve,
63291da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6330d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
63311da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
63321da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
6333d84f4f99SDavid Howells 	tsec = new->security;
6334d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
63351da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6336d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
63371da177e4SLinus Torvalds 		tsec->create_sid = sid;
6338d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
63396b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63406b6bc620SStephen Smalley 				     mysid, sid, SECCLASS_KEY, KEY__CREATE,
6341be0554c9SStephen Smalley 				     NULL);
63424eb582cfSMichael LeMay 		if (error)
6343d84f4f99SDavid Howells 			goto abort_change;
63444eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6345d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
634642c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6347d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6348d84f4f99SDavid Howells 		error = -EINVAL;
63491da177e4SLinus Torvalds 		if (sid == 0)
6350d84f4f99SDavid Howells 			goto abort_change;
6351d9250deaSKaiGai Kohei 
6352d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6353d84f4f99SDavid Howells 		error = -EPERM;
63545bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6355aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6356aa8e712cSStephen Smalley 							    tsec->sid, sid);
6357d84f4f99SDavid Howells 			if (error)
6358d84f4f99SDavid Howells 				goto abort_change;
63591da177e4SLinus Torvalds 		}
63601da177e4SLinus Torvalds 
63611da177e4SLinus Torvalds 		/* Check permissions for the transition. */
63626b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63636b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
63641da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
63651da177e4SLinus Torvalds 		if (error)
6366d84f4f99SDavid Howells 			goto abort_change;
63671da177e4SLinus Torvalds 
63681da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
63691da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6370be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
63710c6181cbSPaul Moore 		if (ptsid != 0) {
63726b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
63736b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6374d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6375d84f4f99SDavid Howells 			if (error)
6376d84f4f99SDavid Howells 				goto abort_change;
6377d84f4f99SDavid Howells 		}
6378d84f4f99SDavid Howells 
6379d84f4f99SDavid Howells 		tsec->sid = sid;
6380d84f4f99SDavid Howells 	} else {
6381d84f4f99SDavid Howells 		error = -EINVAL;
6382d84f4f99SDavid Howells 		goto abort_change;
6383d84f4f99SDavid Howells 	}
6384d84f4f99SDavid Howells 
6385d84f4f99SDavid Howells 	commit_creds(new);
63861da177e4SLinus Torvalds 	return size;
6387d84f4f99SDavid Howells 
6388d84f4f99SDavid Howells abort_change:
6389d84f4f99SDavid Howells 	abort_creds(new);
6390d84f4f99SDavid Howells 	return error;
63911da177e4SLinus Torvalds }
63921da177e4SLinus Torvalds 
6393746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6394746df9b5SDavid Quigley {
6395746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6396746df9b5SDavid Quigley }
6397746df9b5SDavid Quigley 
6398dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6399dc49c1f9SCatherine Zhang {
6400aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6401aa8e712cSStephen Smalley 				       secdata, seclen);
6402dc49c1f9SCatherine Zhang }
6403dc49c1f9SCatherine Zhang 
64047bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
640563cb3449SDavid Howells {
6406aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6407aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
640863cb3449SDavid Howells }
640963cb3449SDavid Howells 
6410dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6411dc49c1f9SCatherine Zhang {
6412dc49c1f9SCatherine Zhang 	kfree(secdata);
6413dc49c1f9SCatherine Zhang }
6414dc49c1f9SCatherine Zhang 
64156f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
64166f3be9f5SAndreas Gruenbacher {
64176f3be9f5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
64186f3be9f5SAndreas Gruenbacher 
64199287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
64206f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
64219287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
64226f3be9f5SAndreas Gruenbacher }
64236f3be9f5SAndreas Gruenbacher 
64241ee65e37SDavid P. Quigley /*
64251ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
64261ee65e37SDavid P. Quigley  */
64271ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
64281ee65e37SDavid P. Quigley {
64291ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
64301ee65e37SDavid P. Quigley }
64311ee65e37SDavid P. Quigley 
64321ee65e37SDavid P. Quigley /*
64331ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
64341ee65e37SDavid P. Quigley  */
64351ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
64361ee65e37SDavid P. Quigley {
64371ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
64381ee65e37SDavid P. Quigley }
64391ee65e37SDavid P. Quigley 
64401ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
64411ee65e37SDavid P. Quigley {
64421ee65e37SDavid P. Quigley 	int len = 0;
64431ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
64441ee65e37SDavid P. Quigley 						ctx, true);
64451ee65e37SDavid P. Quigley 	if (len < 0)
64461ee65e37SDavid P. Quigley 		return len;
64471ee65e37SDavid P. Quigley 	*ctxlen = len;
64481ee65e37SDavid P. Quigley 	return 0;
64491ee65e37SDavid P. Quigley }
6450d720024eSMichael LeMay #ifdef CONFIG_KEYS
6451d720024eSMichael LeMay 
6452d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
64537e047ef5SDavid Howells 			     unsigned long flags)
6454d720024eSMichael LeMay {
6455d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6456d720024eSMichael LeMay 	struct key_security_struct *ksec;
6457d720024eSMichael LeMay 
6458d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6459d720024eSMichael LeMay 	if (!ksec)
6460d720024eSMichael LeMay 		return -ENOMEM;
6461d720024eSMichael LeMay 
6462d84f4f99SDavid Howells 	tsec = cred->security;
6463d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6464d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
64654eb582cfSMichael LeMay 	else
6466d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6467d720024eSMichael LeMay 
6468275bb41eSDavid Howells 	k->security = ksec;
6469d720024eSMichael LeMay 	return 0;
6470d720024eSMichael LeMay }
6471d720024eSMichael LeMay 
6472d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6473d720024eSMichael LeMay {
6474d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6475d720024eSMichael LeMay 
6476d720024eSMichael LeMay 	k->security = NULL;
6477d720024eSMichael LeMay 	kfree(ksec);
6478d720024eSMichael LeMay }
6479d720024eSMichael LeMay 
6480d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6481d84f4f99SDavid Howells 				  const struct cred *cred,
6482f5895943SDavid Howells 				  unsigned perm)
6483d720024eSMichael LeMay {
6484d720024eSMichael LeMay 	struct key *key;
6485d720024eSMichael LeMay 	struct key_security_struct *ksec;
6486275bb41eSDavid Howells 	u32 sid;
6487d720024eSMichael LeMay 
6488d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
6489d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
6490d720024eSMichael LeMay 	   appear to be created. */
6491d720024eSMichael LeMay 	if (perm == 0)
6492d720024eSMichael LeMay 		return 0;
6493d720024eSMichael LeMay 
6494d84f4f99SDavid Howells 	sid = cred_sid(cred);
6495275bb41eSDavid Howells 
6496275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6497275bb41eSDavid Howells 	ksec = key->security;
6498275bb41eSDavid Howells 
64996b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65006b6bc620SStephen Smalley 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6501d720024eSMichael LeMay }
6502d720024eSMichael LeMay 
650370a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
650470a5bb72SDavid Howells {
650570a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
650670a5bb72SDavid Howells 	char *context = NULL;
650770a5bb72SDavid Howells 	unsigned len;
650870a5bb72SDavid Howells 	int rc;
650970a5bb72SDavid Howells 
6510aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6511aa8e712cSStephen Smalley 				     &context, &len);
651270a5bb72SDavid Howells 	if (!rc)
651370a5bb72SDavid Howells 		rc = len;
651470a5bb72SDavid Howells 	*_buffer = context;
651570a5bb72SDavid Howells 	return rc;
651670a5bb72SDavid Howells }
65173a976fa6SDaniel Jurgens #endif
651870a5bb72SDavid Howells 
65193a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6520cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6521cfc4d882SDaniel Jurgens {
6522cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6523cfc4d882SDaniel Jurgens 	int err;
6524cfc4d882SDaniel Jurgens 	u32 sid = 0;
6525cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6526cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6527cfc4d882SDaniel Jurgens 
6528409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6529cfc4d882SDaniel Jurgens 	if (err)
6530cfc4d882SDaniel Jurgens 		return err;
6531cfc4d882SDaniel Jurgens 
6532cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6533cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6534cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6535cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
65366b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65376b6bc620SStephen Smalley 			    sec->sid, sid,
6538cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6539cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6540cfc4d882SDaniel Jurgens }
6541cfc4d882SDaniel Jurgens 
6542ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6543ab861dfcSDaniel Jurgens 					    u8 port_num)
6544ab861dfcSDaniel Jurgens {
6545ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6546ab861dfcSDaniel Jurgens 	int err;
6547ab861dfcSDaniel Jurgens 	u32 sid = 0;
6548ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6549ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6550ab861dfcSDaniel Jurgens 
6551aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6552aa8e712cSStephen Smalley 				      &sid);
6553ab861dfcSDaniel Jurgens 
6554ab861dfcSDaniel Jurgens 	if (err)
6555ab861dfcSDaniel Jurgens 		return err;
6556ab861dfcSDaniel Jurgens 
6557ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6558ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6559ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6560ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
65616b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65626b6bc620SStephen Smalley 			    sec->sid, sid,
6563ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6564ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6565ab861dfcSDaniel Jurgens }
6566ab861dfcSDaniel Jurgens 
65673a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
65683a976fa6SDaniel Jurgens {
65693a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
65703a976fa6SDaniel Jurgens 
65713a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
65723a976fa6SDaniel Jurgens 	if (!sec)
65733a976fa6SDaniel Jurgens 		return -ENOMEM;
65743a976fa6SDaniel Jurgens 	sec->sid = current_sid();
65753a976fa6SDaniel Jurgens 
65763a976fa6SDaniel Jurgens 	*ib_sec = sec;
65773a976fa6SDaniel Jurgens 	return 0;
65783a976fa6SDaniel Jurgens }
65793a976fa6SDaniel Jurgens 
65803a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
65813a976fa6SDaniel Jurgens {
65823a976fa6SDaniel Jurgens 	kfree(ib_sec);
65833a976fa6SDaniel Jurgens }
6584d720024eSMichael LeMay #endif
6585d720024eSMichael LeMay 
6586ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6587ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6588ec27c356SChenbo Feng 				     unsigned int size)
6589ec27c356SChenbo Feng {
6590ec27c356SChenbo Feng 	u32 sid = current_sid();
6591ec27c356SChenbo Feng 	int ret;
6592ec27c356SChenbo Feng 
6593ec27c356SChenbo Feng 	switch (cmd) {
6594ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
65956b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
65966b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6597ec27c356SChenbo Feng 				   NULL);
6598ec27c356SChenbo Feng 		break;
6599ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
66006b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66016b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6602ec27c356SChenbo Feng 				   NULL);
6603ec27c356SChenbo Feng 		break;
6604ec27c356SChenbo Feng 	default:
6605ec27c356SChenbo Feng 		ret = 0;
6606ec27c356SChenbo Feng 		break;
6607ec27c356SChenbo Feng 	}
6608ec27c356SChenbo Feng 
6609ec27c356SChenbo Feng 	return ret;
6610ec27c356SChenbo Feng }
6611ec27c356SChenbo Feng 
6612ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6613ec27c356SChenbo Feng {
6614ec27c356SChenbo Feng 	u32 av = 0;
6615ec27c356SChenbo Feng 
6616ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6617ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6618ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6619ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6620ec27c356SChenbo Feng 	return av;
6621ec27c356SChenbo Feng }
6622ec27c356SChenbo Feng 
6623f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6624f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6625f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6626f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6627f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6628f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6629f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6630f66e448cSChenbo Feng  */
6631f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6632f66e448cSChenbo Feng {
6633f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6634f66e448cSChenbo Feng 	struct bpf_prog *prog;
6635f66e448cSChenbo Feng 	struct bpf_map *map;
6636f66e448cSChenbo Feng 	int ret;
6637f66e448cSChenbo Feng 
6638f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6639f66e448cSChenbo Feng 		map = file->private_data;
6640f66e448cSChenbo Feng 		bpfsec = map->security;
66416b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66426b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6643f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6644f66e448cSChenbo Feng 		if (ret)
6645f66e448cSChenbo Feng 			return ret;
6646f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6647f66e448cSChenbo Feng 		prog = file->private_data;
6648f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
66496b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66506b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6651f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6652f66e448cSChenbo Feng 		if (ret)
6653f66e448cSChenbo Feng 			return ret;
6654f66e448cSChenbo Feng 	}
6655f66e448cSChenbo Feng 	return 0;
6656f66e448cSChenbo Feng }
6657f66e448cSChenbo Feng 
6658ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6659ec27c356SChenbo Feng {
6660ec27c356SChenbo Feng 	u32 sid = current_sid();
6661ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6662ec27c356SChenbo Feng 
6663ec27c356SChenbo Feng 	bpfsec = map->security;
66646b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66656b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6666ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6667ec27c356SChenbo Feng }
6668ec27c356SChenbo Feng 
6669ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6670ec27c356SChenbo Feng {
6671ec27c356SChenbo Feng 	u32 sid = current_sid();
6672ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6673ec27c356SChenbo Feng 
6674ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
66756b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66766b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6677ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6678ec27c356SChenbo Feng }
6679ec27c356SChenbo Feng 
6680ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6681ec27c356SChenbo Feng {
6682ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6683ec27c356SChenbo Feng 
6684ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6685ec27c356SChenbo Feng 	if (!bpfsec)
6686ec27c356SChenbo Feng 		return -ENOMEM;
6687ec27c356SChenbo Feng 
6688ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6689ec27c356SChenbo Feng 	map->security = bpfsec;
6690ec27c356SChenbo Feng 
6691ec27c356SChenbo Feng 	return 0;
6692ec27c356SChenbo Feng }
6693ec27c356SChenbo Feng 
6694ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6695ec27c356SChenbo Feng {
6696ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6697ec27c356SChenbo Feng 
6698ec27c356SChenbo Feng 	map->security = NULL;
6699ec27c356SChenbo Feng 	kfree(bpfsec);
6700ec27c356SChenbo Feng }
6701ec27c356SChenbo Feng 
6702ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6703ec27c356SChenbo Feng {
6704ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6705ec27c356SChenbo Feng 
6706ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6707ec27c356SChenbo Feng 	if (!bpfsec)
6708ec27c356SChenbo Feng 		return -ENOMEM;
6709ec27c356SChenbo Feng 
6710ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6711ec27c356SChenbo Feng 	aux->security = bpfsec;
6712ec27c356SChenbo Feng 
6713ec27c356SChenbo Feng 	return 0;
6714ec27c356SChenbo Feng }
6715ec27c356SChenbo Feng 
6716ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6717ec27c356SChenbo Feng {
6718ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6719ec27c356SChenbo Feng 
6720ec27c356SChenbo Feng 	aux->security = NULL;
6721ec27c356SChenbo Feng 	kfree(bpfsec);
6722ec27c356SChenbo Feng }
6723ec27c356SChenbo Feng #endif
6724ec27c356SChenbo Feng 
6725ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6726e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6727e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6728e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6729e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6730076c54c5SAhmed S. Darwish 
6731e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6732e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6733e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
6734e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
6735e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
6736e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
6737e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
6738e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
6739e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
674079af7307SStephen Smalley 
6741e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
67421da177e4SLinus Torvalds 
6743e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6744e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6745e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
67461da177e4SLinus Torvalds 
6747e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6748e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
67495b400239SAl Viro 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
6750204cc0ccSAl Viro 	LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6751e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6752e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6753e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6754e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6755e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
6756e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
6757e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6758e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6759e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
67601da177e4SLinus Torvalds 
6761e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6762a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6763e0007529SEric Paris 
6764e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6765e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6766e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6767e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
6768e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
6769e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6770e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6771e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6772e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6773e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6774e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6775e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6776e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6777e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6778e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6779e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6780e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6781e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6782e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6783e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6784e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6785e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6786e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6787e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6788e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
678956909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
679019472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
67911da177e4SLinus Torvalds 
6792e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
6793e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6794e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6795e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6796e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6797e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6798e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6799e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
6800e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6801e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6802e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6803e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
68041da177e4SLinus Torvalds 
6805e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
68061da177e4SLinus Torvalds 
6807a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6808e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6809e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, selinux_cred_free),
6810e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6811e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
68123ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6813e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6814e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6815e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6816c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
681761d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6818e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6819e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6820e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6821e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6822e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6823e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6824e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6825791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6826e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6827e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6828e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6829e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6830e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
6831e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6832788e7dd4SYuichi Nakamura 
6833e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6834e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
68351da177e4SLinus Torvalds 
6836e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6837e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
68381da177e4SLinus Torvalds 
6839e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_alloc_security,
6840e20b043aSCasey Schaufler 			selinux_msg_queue_alloc_security),
6841e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6842e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6843e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6844e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6845e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
68461da177e4SLinus Torvalds 
6847e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6848e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6849e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6850e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6851e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
68521da177e4SLinus Torvalds 
6853e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6854e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6855e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6856e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6857e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
68581da177e4SLinus Torvalds 
6859e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
68601da177e4SLinus Torvalds 
6861e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6862e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
68631da177e4SLinus Torvalds 
6864e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6865e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6866e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6867e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
68686f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6869e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6870e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6871e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
68721da177e4SLinus Torvalds 
6873e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6874e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6875dc49c1f9SCatherine Zhang 
6876e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
6877e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
68780b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
6879e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6880e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6881e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6882e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6883e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6884e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6885e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6886e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6887e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6888e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6889e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6890e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6891e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
6892e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
6893e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6894e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6895e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6896e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6897e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6898e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6899d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6900d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6901d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
6902e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6903e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6904e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6906e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6907e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6908e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6912e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6913e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6914e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
69153a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6916cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
6917ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
6918ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
69193a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
69203a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
69213a976fa6SDaniel Jurgens #endif
6922d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
6923e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6924e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6925e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6926e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6927e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6928e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6929e20b043aSCasey Schaufler 			selinux_xfrm_state_alloc_acquire),
6930e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6931e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6932e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6933e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6934e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
6935e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
69361da177e4SLinus Torvalds #endif
6937d720024eSMichael LeMay 
6938d720024eSMichael LeMay #ifdef CONFIG_KEYS
6939e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6940e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
6941e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
6942e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6943d720024eSMichael LeMay #endif
69449d57a7f9SAhmed S. Darwish 
69459d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
6946e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6947e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6948e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6949e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
69509d57a7f9SAhmed S. Darwish #endif
6951ec27c356SChenbo Feng 
6952ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6953ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
6954ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6955ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6956ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6957ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6958ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6959ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6960ec27c356SChenbo Feng #endif
69611da177e4SLinus Torvalds };
69621da177e4SLinus Torvalds 
69631da177e4SLinus Torvalds static __init int selinux_init(void)
69641da177e4SLinus Torvalds {
6965b1d9e6b0SCasey Schaufler 	if (!security_module_enable("selinux")) {
6966076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
6967076c54c5SAhmed S. Darwish 		return 0;
6968076c54c5SAhmed S. Darwish 	}
6969076c54c5SAhmed S. Darwish 
69701da177e4SLinus Torvalds 	if (!selinux_enabled) {
6971c103a91eSpeter enderborg 		pr_info("SELinux:  Disabled at boot.\n");
69721da177e4SLinus Torvalds 		return 0;
69731da177e4SLinus Torvalds 	}
69741da177e4SLinus Torvalds 
6975c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
69761da177e4SLinus Torvalds 
6977aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
6978e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
6979aa8e712cSStephen Smalley 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
6980aa8e712cSStephen Smalley 	selinux_ss_init(&selinux_state.ss);
69816b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
6982aa8e712cSStephen Smalley 
69831da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
6984d84f4f99SDavid Howells 	cred_init_security();
69851da177e4SLinus Torvalds 
6986fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6987fcaaade1SStephen Smalley 
69887cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
69897cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
699020c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
699163205654SSangwoo 	file_security_cache = kmem_cache_create("selinux_file_security",
699263205654SSangwoo 					    sizeof(struct file_security_struct),
699363205654SSangwoo 					    0, SLAB_PANIC, NULL);
69941da177e4SLinus Torvalds 	avc_init();
69951da177e4SLinus Torvalds 
6996aa8e712cSStephen Smalley 	avtab_cache_init();
6997aa8e712cSStephen Smalley 
6998aa8e712cSStephen Smalley 	ebitmap_cache_init();
6999aa8e712cSStephen Smalley 
7000aa8e712cSStephen Smalley 	hashtab_cache_init();
7001aa8e712cSStephen Smalley 
7002d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
70031da177e4SLinus Torvalds 
7004615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7005615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7006615e51fdSPaul Moore 
70078f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
70088f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
70098f408ab6SDaniel Jurgens 
7010aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7011c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
7012828dfe1dSEric Paris 	else
7013c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
7014d720024eSMichael LeMay 
70151da177e4SLinus Torvalds 	return 0;
70161da177e4SLinus Torvalds }
70171da177e4SLinus Torvalds 
7018e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7019e8c26255SAl Viro {
7020204cc0ccSAl Viro 	selinux_set_mnt_opts(sb, NULL, 0, NULL);
7021e8c26255SAl Viro }
7022e8c26255SAl Viro 
70231da177e4SLinus Torvalds void selinux_complete_init(void)
70241da177e4SLinus Torvalds {
7025c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
70261da177e4SLinus Torvalds 
70271da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7028c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
7029e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
70301da177e4SLinus Torvalds }
70311da177e4SLinus Torvalds 
70321da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
70331da177e4SLinus Torvalds    all processes and objects when they are created. */
70343d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
703507aed2f2SKees Cook 	.name = "selinux",
70363d6e5f6dSKees Cook 	.init = selinux_init,
70373d6e5f6dSKees Cook };
70381da177e4SLinus Torvalds 
7039c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
70401da177e4SLinus Torvalds 
7041591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7042effad8dfSPaul Moore 	{
7043effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
70442597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
70456e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
70461da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7047effad8dfSPaul Moore 	},
7048effad8dfSPaul Moore 	{
7049effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
70502597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7051effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7052effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7053948bf85cSPaul Moore 	},
7054948bf85cSPaul Moore 	{
7055948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
70562597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7057948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7058948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
705925db6beaSJiri Pirko 	},
70601a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7061effad8dfSPaul Moore 	{
7062effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
70632597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
70646e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
70651da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7066effad8dfSPaul Moore 	},
7067effad8dfSPaul Moore 	{
7068effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
70692597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7070effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7071effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
707225db6beaSJiri Pirko 	},
70732917f57bSHuw Davies 	{
70742917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
70752917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
70762917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
70772917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
70782917f57bSHuw Davies 	},
70791da177e4SLinus Torvalds #endif	/* IPV6 */
708025db6beaSJiri Pirko };
70811da177e4SLinus Torvalds 
70828e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
70838e71bf75SFlorian Westphal {
70848e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
70858e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
70868e71bf75SFlorian Westphal }
70878e71bf75SFlorian Westphal 
70888e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
70898e71bf75SFlorian Westphal {
70908e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
70918e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
70928e71bf75SFlorian Westphal }
70938e71bf75SFlorian Westphal 
70948e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
70958e71bf75SFlorian Westphal 	.init = selinux_nf_register,
70968e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
70978e71bf75SFlorian Westphal };
70988e71bf75SFlorian Westphal 
70991da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
71001da177e4SLinus Torvalds {
710125db6beaSJiri Pirko 	int err;
71021da177e4SLinus Torvalds 
71031da177e4SLinus Torvalds 	if (!selinux_enabled)
710425db6beaSJiri Pirko 		return 0;
71051da177e4SLinus Torvalds 
7106c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
71071da177e4SLinus Torvalds 
71088e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
71091da177e4SLinus Torvalds 	if (err)
71108e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
71111da177e4SLinus Torvalds 
711225db6beaSJiri Pirko 	return 0;
71131da177e4SLinus Torvalds }
71141da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
71151da177e4SLinus Torvalds 
71161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
71171da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
71181da177e4SLinus Torvalds {
7119c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
71201da177e4SLinus Torvalds 
71218e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
71221da177e4SLinus Torvalds }
71231da177e4SLinus Torvalds #endif
71241da177e4SLinus Torvalds 
7125c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
71261da177e4SLinus Torvalds 
71271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
71281da177e4SLinus Torvalds #define selinux_nf_ip_exit()
71291da177e4SLinus Torvalds #endif
71301da177e4SLinus Torvalds 
7131c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
71321da177e4SLinus Torvalds 
71331da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7134aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
71351da177e4SLinus Torvalds {
7136aa8e712cSStephen Smalley 	if (state->initialized) {
71371da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
71381da177e4SLinus Torvalds 		return -EINVAL;
71391da177e4SLinus Torvalds 	}
71401da177e4SLinus Torvalds 
7141aa8e712cSStephen Smalley 	if (state->disabled) {
71421da177e4SLinus Torvalds 		/* Only do this once. */
71431da177e4SLinus Torvalds 		return -EINVAL;
71441da177e4SLinus Torvalds 	}
71451da177e4SLinus Torvalds 
7146aa8e712cSStephen Smalley 	state->disabled = 1;
7147aa8e712cSStephen Smalley 
7148c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
71491da177e4SLinus Torvalds 
715030d55280SStephen Smalley 	selinux_enabled = 0;
71511da177e4SLinus Torvalds 
7152b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
71531da177e4SLinus Torvalds 
7154af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7155af8ff049SEric Paris 	avc_disable();
7156af8ff049SEric Paris 
71571da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
71581da177e4SLinus Torvalds 	selinux_nf_ip_exit();
71591da177e4SLinus Torvalds 
71601da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
71611da177e4SLinus Torvalds 	exit_sel_fs();
71621da177e4SLinus Torvalds 
71631da177e4SLinus Torvalds 	return 0;
71641da177e4SLinus Torvalds }
71651da177e4SLinus Torvalds #endif
7166