xref: /openbmc/linux/security/selinux/hooks.c (revision e262e32d6bde0f77fb0c95d977482fc872c51996)
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>
91*e262e32dSDavid 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 
4361da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds enum {
44231e87930SEric Paris 	Opt_error = -1,
4431da177e4SLinus Torvalds 	Opt_context = 1,
4441da177e4SLinus Torvalds 	Opt_fscontext = 2,
445c9180a57SEric Paris 	Opt_defcontext = 3,
446c9180a57SEric Paris 	Opt_rootcontext = 4,
44711689d47SDavid P. Quigley 	Opt_labelsupport = 5,
448d355987fSEric Paris 	Opt_nextmntopt = 6,
4491da177e4SLinus Torvalds };
4501da177e4SLinus Torvalds 
451d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
452d355987fSEric Paris 
453a447c093SSteven Whitehouse static const match_table_t tokens = {
454832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
455832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
456832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
457832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
45811689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
45931e87930SEric Paris 	{Opt_error, NULL},
4601da177e4SLinus Torvalds };
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4631da177e4SLinus Torvalds 
464c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
465c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
466275bb41eSDavid Howells 			const struct cred *cred)
467c312feb2SEric Paris {
468275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
469c312feb2SEric Paris 	int rc;
470c312feb2SEric Paris 
4716b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4726b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
473c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
474c312feb2SEric Paris 	if (rc)
475c312feb2SEric Paris 		return rc;
476c312feb2SEric Paris 
4776b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4786b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
479c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
480c312feb2SEric Paris 	return rc;
481c312feb2SEric Paris }
482c312feb2SEric Paris 
4830808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
4840808925eSEric Paris 			struct superblock_security_struct *sbsec,
485275bb41eSDavid Howells 			const struct cred *cred)
4860808925eSEric Paris {
487275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
4880808925eSEric Paris 	int rc;
4896b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4906b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4910808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
4920808925eSEric Paris 	if (rc)
4930808925eSEric Paris 		return rc;
4940808925eSEric Paris 
4956b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
4966b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
4970808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4980808925eSEric Paris 	return rc;
4990808925eSEric Paris }
5000808925eSEric Paris 
501b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
502b43e725dSEric Paris {
503b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
504b43e725dSEric Paris 
505d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
506b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
507d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
5089fc2b4b4SJ. Bruce Fields 		sbsec->behavior == SECURITY_FS_USE_NATIVE ||
509d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
510d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
511d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
512d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
513a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
5142651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
515aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
5162651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
5172651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
518b43e725dSEric Paris }
519b43e725dSEric Paris 
520c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5231da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
524c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5251da177e4SLinus Torvalds 	int rc = 0;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
5281da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
5291da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
5301da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
5311da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
5321da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
5335d6c3191SAndreas Gruenbacher 		if (!(root_inode->i_opflags & IOP_XATTR)) {
534c103a91eSpeter enderborg 			pr_warn("SELinux: (dev %s, type %s) has no "
53529b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
5361da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5371da177e4SLinus Torvalds 			goto out;
5381da177e4SLinus Torvalds 		}
5395d6c3191SAndreas Gruenbacher 
5405d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
5411da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5421da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
543c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
54429b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
54529b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5461da177e4SLinus Torvalds 			else
547c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
54829b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
54929b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
5501da177e4SLinus Torvalds 			goto out;
5511da177e4SLinus Torvalds 		}
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 
554eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5550b4d3452SScott Mayhew 
5560b4d3452SScott Mayhew 	/*
5570b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5580b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5590b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5600b4d3452SScott Mayhew 	 */
561b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
56212f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5630b4d3452SScott Mayhew 	else
5640b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
565ddd29ec6SDavid P. Quigley 
5661da177e4SLinus Torvalds 	/* Initialize the root inode. */
567c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5701da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5711da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5721da177e4SLinus Torvalds 	   populates itself. */
5731da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5741da177e4SLinus Torvalds next_inode:
5751da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
5761da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5771da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
5781da177e4SLinus Torvalds 					   struct inode_security_struct, list);
5791da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
580923190d3SStephen Smalley 		list_del_init(&isec->list);
5811da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5821da177e4SLinus Torvalds 		inode = igrab(inode);
5831da177e4SLinus Torvalds 		if (inode) {
5841da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
5851da177e4SLinus Torvalds 				inode_doinit(inode);
5861da177e4SLinus Torvalds 			iput(inode);
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5891da177e4SLinus Torvalds 		goto next_inode;
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
5921da177e4SLinus Torvalds out:
593c9180a57SEric Paris 	return rc;
594c9180a57SEric Paris }
595c9180a57SEric Paris 
596c9180a57SEric Paris /*
597c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
598c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
599c9180a57SEric Paris  * mount options, or whatever.
600c9180a57SEric Paris  */
601c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
602e0007529SEric Paris 				struct security_mnt_opts *opts)
603c9180a57SEric Paris {
604c9180a57SEric Paris 	int rc = 0, i;
605c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
606c9180a57SEric Paris 	char *context = NULL;
607c9180a57SEric Paris 	u32 len;
608c9180a57SEric Paris 	char tmp;
609c9180a57SEric Paris 
610e0007529SEric Paris 	security_init_mnt_opts(opts);
611c9180a57SEric Paris 
6120d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
613c9180a57SEric Paris 		return -EINVAL;
614c9180a57SEric Paris 
615aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
616c9180a57SEric Paris 		return -EINVAL;
617c9180a57SEric Paris 
618af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
619af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
620af8e50ccSEric Paris 
6210d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
622c9180a57SEric Paris 	/* count the number of mount options for this sb */
623af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
624c9180a57SEric Paris 		if (tmp & 0x01)
625e0007529SEric Paris 			opts->num_mnt_opts++;
626c9180a57SEric Paris 		tmp >>= 1;
627c9180a57SEric Paris 	}
62811689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
6290b4bdb35SEric Paris 	if (sbsec->flags & SBLABEL_MNT)
63011689d47SDavid P. Quigley 		opts->num_mnt_opts++;
631c9180a57SEric Paris 
632e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
633e0007529SEric Paris 	if (!opts->mnt_opts) {
634c9180a57SEric Paris 		rc = -ENOMEM;
635c9180a57SEric Paris 		goto out_free;
636c9180a57SEric Paris 	}
637c9180a57SEric Paris 
638e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
639e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
640c9180a57SEric Paris 		rc = -ENOMEM;
641c9180a57SEric Paris 		goto out_free;
642c9180a57SEric Paris 	}
643c9180a57SEric Paris 
644c9180a57SEric Paris 	i = 0;
645c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
646aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, sbsec->sid,
647aa8e712cSStephen Smalley 					     &context, &len);
648c9180a57SEric Paris 		if (rc)
649c9180a57SEric Paris 			goto out_free;
650e0007529SEric Paris 		opts->mnt_opts[i] = context;
651e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
652c9180a57SEric Paris 	}
653c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
654aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state,
655aa8e712cSStephen Smalley 					     sbsec->mntpoint_sid,
656aa8e712cSStephen Smalley 					     &context, &len);
657c9180a57SEric Paris 		if (rc)
658c9180a57SEric Paris 			goto out_free;
659e0007529SEric Paris 		opts->mnt_opts[i] = context;
660e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
661c9180a57SEric Paris 	}
662c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
663aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, sbsec->def_sid,
664aa8e712cSStephen Smalley 					     &context, &len);
665c9180a57SEric Paris 		if (rc)
666c9180a57SEric Paris 			goto out_free;
667e0007529SEric Paris 		opts->mnt_opts[i] = context;
668e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
669c9180a57SEric Paris 	}
670c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
67183da53c5SAndreas Gruenbacher 		struct dentry *root = sbsec->sb->s_root;
67283da53c5SAndreas Gruenbacher 		struct inode_security_struct *isec = backing_inode_security(root);
673c9180a57SEric Paris 
674aa8e712cSStephen Smalley 		rc = security_sid_to_context(&selinux_state, isec->sid,
675aa8e712cSStephen Smalley 					     &context, &len);
676c9180a57SEric Paris 		if (rc)
677c9180a57SEric Paris 			goto out_free;
678e0007529SEric Paris 		opts->mnt_opts[i] = context;
679e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
680c9180a57SEric Paris 	}
68112f348b9SEric Paris 	if (sbsec->flags & SBLABEL_MNT) {
68211689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
68312f348b9SEric Paris 		opts->mnt_opts_flags[i++] = SBLABEL_MNT;
68411689d47SDavid P. Quigley 	}
685c9180a57SEric Paris 
686e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
687c9180a57SEric Paris 
688c9180a57SEric Paris 	return 0;
689c9180a57SEric Paris 
690c9180a57SEric Paris out_free:
691e0007529SEric Paris 	security_free_mnt_opts(opts);
692c9180a57SEric Paris 	return rc;
693c9180a57SEric Paris }
694c9180a57SEric Paris 
695c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
696c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
697c9180a57SEric Paris {
6980d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
6990d90a7ecSDavid P. Quigley 
700c9180a57SEric Paris 	/* check if the old mount command had the same options */
7010d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
702c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
703c9180a57SEric Paris 		    (old_sid != new_sid))
704c9180a57SEric Paris 			return 1;
705c9180a57SEric Paris 
706c9180a57SEric Paris 	/* check if we were passed the same options twice,
707c9180a57SEric Paris 	 * aka someone passed context=a,context=b
708c9180a57SEric Paris 	 */
7090d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
7100d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
711c9180a57SEric Paris 			return 1;
712c9180a57SEric Paris 	return 0;
713c9180a57SEric Paris }
714e0007529SEric Paris 
715c9180a57SEric Paris /*
716c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
717c9180a57SEric Paris  * labeling information.
718c9180a57SEric Paris  */
719e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
720649f6e77SDavid Quigley 				struct security_mnt_opts *opts,
721649f6e77SDavid Quigley 				unsigned long kern_flags,
722649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
723c9180a57SEric Paris {
724275bb41eSDavid Howells 	const struct cred *cred = current_cred();
725c9180a57SEric Paris 	int rc = 0, i;
726c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
72729b1deb2SLinus Torvalds 	const char *name = sb->s_type->name;
72883da53c5SAndreas Gruenbacher 	struct dentry *root = sbsec->sb->s_root;
7292c97165bSPaul Moore 	struct inode_security_struct *root_isec;
730c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
731c9180a57SEric Paris 	u32 defcontext_sid = 0;
732e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
733e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
734e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
735c9180a57SEric Paris 
736c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
737c9180a57SEric Paris 
738aa8e712cSStephen Smalley 	if (!selinux_state.initialized) {
739c9180a57SEric Paris 		if (!num_opts) {
740c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
741c9180a57SEric Paris 			   after the initial policy is loaded and the security
742c9180a57SEric Paris 			   server is ready to handle calls. */
743c9180a57SEric Paris 			goto out;
744c9180a57SEric Paris 		}
745c9180a57SEric Paris 		rc = -EINVAL;
746c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
747744ba35eSEric Paris 			"before the security server is initialized\n");
748c9180a57SEric Paris 		goto out;
749c9180a57SEric Paris 	}
750649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
751649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
752649f6e77SDavid Quigley 		 * place the results is not allowed */
753649f6e77SDavid Quigley 		rc = -EINVAL;
754649f6e77SDavid Quigley 		goto out;
755649f6e77SDavid Quigley 	}
756c9180a57SEric Paris 
757c9180a57SEric Paris 	/*
758e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
759e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
760e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
761e0007529SEric Paris 	 * we need to skip the double mount verification.
762e0007529SEric Paris 	 *
763e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
764e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
765e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
766e0007529SEric Paris 	 * will be used for both mounts)
767e0007529SEric Paris 	 */
7680d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
769e0007529SEric Paris 	    && (num_opts == 0))
770e0007529SEric Paris 		goto out;
771e0007529SEric Paris 
7722c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
7732c97165bSPaul Moore 
774e0007529SEric Paris 	/*
775c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
776c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
777c9180a57SEric Paris 	 * than once with different security options.
778c9180a57SEric Paris 	 */
779c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
780c9180a57SEric Paris 		u32 sid;
78111689d47SDavid P. Quigley 
78212f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
78311689d47SDavid P. Quigley 			continue;
784aa8e712cSStephen Smalley 		rc = security_context_str_to_sid(&selinux_state,
785aa8e712cSStephen Smalley 						 mount_options[i], &sid,
786aa8e712cSStephen Smalley 						 GFP_KERNEL);
787c9180a57SEric Paris 		if (rc) {
788c103a91eSpeter enderborg 			pr_warn("SELinux: security_context_str_to_sid"
78929b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
79029b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, name, rc);
791c9180a57SEric Paris 			goto out;
792c9180a57SEric Paris 		}
793c9180a57SEric Paris 		switch (flags[i]) {
794c9180a57SEric Paris 		case FSCONTEXT_MNT:
795c9180a57SEric Paris 			fscontext_sid = sid;
796c9180a57SEric Paris 
797c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
798c9180a57SEric Paris 					fscontext_sid))
799c9180a57SEric Paris 				goto out_double_mount;
800c9180a57SEric Paris 
801c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
802c9180a57SEric Paris 			break;
803c9180a57SEric Paris 		case CONTEXT_MNT:
804c9180a57SEric Paris 			context_sid = sid;
805c9180a57SEric Paris 
806c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
807c9180a57SEric Paris 					context_sid))
808c9180a57SEric Paris 				goto out_double_mount;
809c9180a57SEric Paris 
810c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
811c9180a57SEric Paris 			break;
812c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
813c9180a57SEric Paris 			rootcontext_sid = sid;
814c9180a57SEric Paris 
815c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
816c9180a57SEric Paris 					rootcontext_sid))
817c9180a57SEric Paris 				goto out_double_mount;
818c9180a57SEric Paris 
819c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
820c9180a57SEric Paris 
821c9180a57SEric Paris 			break;
822c9180a57SEric Paris 		case DEFCONTEXT_MNT:
823c9180a57SEric Paris 			defcontext_sid = sid;
824c9180a57SEric Paris 
825c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
826c9180a57SEric Paris 					defcontext_sid))
827c9180a57SEric Paris 				goto out_double_mount;
828c9180a57SEric Paris 
829c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
830c9180a57SEric Paris 
831c9180a57SEric Paris 			break;
832c9180a57SEric Paris 		default:
833c9180a57SEric Paris 			rc = -EINVAL;
834c9180a57SEric Paris 			goto out;
835c9180a57SEric Paris 		}
836c9180a57SEric Paris 	}
837c9180a57SEric Paris 
8380d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
839c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
8400d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
841c9180a57SEric Paris 			goto out_double_mount;
842c9180a57SEric Paris 		rc = 0;
843c9180a57SEric Paris 		goto out;
844c9180a57SEric Paris 	}
845c9180a57SEric Paris 
846089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
847134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
848134509d5SStephen Smalley 
8498e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
8506a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
8518e014720SStephen Smalley 	    !strcmp(sb->s_type->name, "sysfs") ||
852901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "pstore") ||
853901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
854901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
855134509d5SStephen Smalley 		sbsec->flags |= SE_SBGENFS;
856c9180a57SEric Paris 
857eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
858eb9ae686SDavid Quigley 		/*
859eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
860eb9ae686SDavid Quigley 		 * filesystem type.
861eb9ae686SDavid Quigley 		 */
862aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
863c9180a57SEric Paris 		if (rc) {
864c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
865089be43eSJames Morris 					__func__, sb->s_type->name, rc);
866c9180a57SEric Paris 			goto out;
867c9180a57SEric Paris 		}
868eb9ae686SDavid Quigley 	}
869aad82892SSeth Forshee 
870aad82892SSeth Forshee 	/*
87101593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
87201593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
87301593d32SStephen Smalley 	 * line and security labels must be ignored.
874aad82892SSeth Forshee 	 */
87501593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
87601593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
87701593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
87801593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
879aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
880aad82892SSeth Forshee 		    defcontext_sid) {
881aad82892SSeth Forshee 			rc = -EACCES;
882aad82892SSeth Forshee 			goto out;
883aad82892SSeth Forshee 		}
884aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
885aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
886aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
887aa8e712cSStephen Smalley 						     current_sid(),
888aa8e712cSStephen Smalley 						     current_sid(),
889aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
890aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
891aad82892SSeth Forshee 			if (rc)
892aad82892SSeth Forshee 				goto out;
893aad82892SSeth Forshee 		}
894aad82892SSeth Forshee 		goto out_set_opts;
895aad82892SSeth Forshee 	}
896aad82892SSeth Forshee 
897c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
898c9180a57SEric Paris 	if (fscontext_sid) {
899275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
900c9180a57SEric Paris 		if (rc)
901c9180a57SEric Paris 			goto out;
902c9180a57SEric Paris 
903c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
904c9180a57SEric Paris 	}
905c9180a57SEric Paris 
906c9180a57SEric Paris 	/*
907c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
908c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
909c9180a57SEric Paris 	 * the superblock context if not already set.
910c9180a57SEric Paris 	 */
911eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
912eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
913eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
914eb9ae686SDavid Quigley 	}
915eb9ae686SDavid Quigley 
916c9180a57SEric Paris 	if (context_sid) {
917c9180a57SEric Paris 		if (!fscontext_sid) {
918275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
919275bb41eSDavid Howells 							  cred);
920c9180a57SEric Paris 			if (rc)
921c9180a57SEric Paris 				goto out;
922c9180a57SEric Paris 			sbsec->sid = context_sid;
923c9180a57SEric Paris 		} else {
924275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
925275bb41eSDavid Howells 							     cred);
926c9180a57SEric Paris 			if (rc)
927c9180a57SEric Paris 				goto out;
928c9180a57SEric Paris 		}
929c9180a57SEric Paris 		if (!rootcontext_sid)
930c9180a57SEric Paris 			rootcontext_sid = context_sid;
931c9180a57SEric Paris 
932c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
933c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
934c9180a57SEric Paris 	}
935c9180a57SEric Paris 
936c9180a57SEric Paris 	if (rootcontext_sid) {
937275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
938275bb41eSDavid Howells 						     cred);
939c9180a57SEric Paris 		if (rc)
940c9180a57SEric Paris 			goto out;
941c9180a57SEric Paris 
942c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
9436f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
944c9180a57SEric Paris 	}
945c9180a57SEric Paris 
946c9180a57SEric Paris 	if (defcontext_sid) {
947eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
948eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
949c9180a57SEric Paris 			rc = -EINVAL;
950c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
951c9180a57SEric Paris 			       "invalid for this filesystem type\n");
952c9180a57SEric Paris 			goto out;
953c9180a57SEric Paris 		}
954c9180a57SEric Paris 
955c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
956c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
957275bb41eSDavid Howells 							     sbsec, cred);
958c9180a57SEric Paris 			if (rc)
959c9180a57SEric Paris 				goto out;
960c9180a57SEric Paris 		}
961c9180a57SEric Paris 
962c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
963c9180a57SEric Paris 	}
964c9180a57SEric Paris 
965aad82892SSeth Forshee out_set_opts:
966c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
967c9180a57SEric Paris out:
968bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
9691da177e4SLinus Torvalds 	return rc;
970c9180a57SEric Paris out_double_mount:
971c9180a57SEric Paris 	rc = -EINVAL;
972c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
97329b1deb2SLinus Torvalds 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
974c9180a57SEric Paris 	goto out;
975c9180a57SEric Paris }
976c9180a57SEric Paris 
977094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
978094f7b69SJeff Layton 				    const struct super_block *newsb)
979094f7b69SJeff Layton {
980094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
981094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
982094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
983094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
984094f7b69SJeff Layton 
985094f7b69SJeff Layton 	if (oldflags != newflags)
986094f7b69SJeff Layton 		goto mismatch;
987094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
988094f7b69SJeff Layton 		goto mismatch;
989094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
990094f7b69SJeff Layton 		goto mismatch;
991094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
992094f7b69SJeff Layton 		goto mismatch;
993094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
99483da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
99583da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
996094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
997094f7b69SJeff Layton 			goto mismatch;
998094f7b69SJeff Layton 	}
999094f7b69SJeff Layton 	return 0;
1000094f7b69SJeff Layton mismatch:
1001c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
1002094f7b69SJeff Layton 			    "different security settings for (dev %s, "
1003094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
1004094f7b69SJeff Layton 	return -EBUSY;
1005094f7b69SJeff Layton }
1006094f7b69SJeff Layton 
1007094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
10080b4d3452SScott Mayhew 					struct super_block *newsb,
10090b4d3452SScott Mayhew 					unsigned long kern_flags,
10100b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
1011c9180a57SEric Paris {
10120b4d3452SScott Mayhew 	int rc = 0;
1013c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
1014c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
1015c9180a57SEric Paris 
1016c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
1017c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
1018c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
1019c9180a57SEric Paris 
10200f5e6420SEric Paris 	/*
10210f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
1022e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
10230f5e6420SEric Paris 	 */
1024aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
1025094f7b69SJeff Layton 		return 0;
1026c9180a57SEric Paris 
10270b4d3452SScott Mayhew 	/*
10280b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
10290b4d3452SScott Mayhew 	 * place the results is not allowed.
10300b4d3452SScott Mayhew 	 */
10310b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
10320b4d3452SScott Mayhew 		return -EINVAL;
10330b4d3452SScott Mayhew 
1034c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
10350d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
1036c9180a57SEric Paris 
1037094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
10380d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
1039094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
10405a552617SEric Paris 
1041c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
1042c9180a57SEric Paris 
1043c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
1044c9180a57SEric Paris 
1045c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
1046c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
1047c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
1048c9180a57SEric Paris 
10490b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
10500b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
1051aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
10520b4d3452SScott Mayhew 		if (rc)
10530b4d3452SScott Mayhew 			goto out;
10540b4d3452SScott Mayhew 	}
10550b4d3452SScott Mayhew 
10560b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
10570b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
10580b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
10590b4d3452SScott Mayhew 	}
10600b4d3452SScott Mayhew 
1061c9180a57SEric Paris 	if (set_context) {
1062c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
1063c9180a57SEric Paris 
1064c9180a57SEric Paris 		if (!set_fscontext)
1065c9180a57SEric Paris 			newsbsec->sid = sid;
1066c9180a57SEric Paris 		if (!set_rootcontext) {
106783da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1068c9180a57SEric Paris 			newisec->sid = sid;
1069c9180a57SEric Paris 		}
1070c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
1071c9180a57SEric Paris 	}
1072c9180a57SEric Paris 	if (set_rootcontext) {
107383da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
107483da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1075c9180a57SEric Paris 
1076c9180a57SEric Paris 		newisec->sid = oldisec->sid;
1077c9180a57SEric Paris 	}
1078c9180a57SEric Paris 
1079c9180a57SEric Paris 	sb_finish_set_opts(newsb);
10800b4d3452SScott Mayhew out:
1081c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
10820b4d3452SScott Mayhew 	return rc;
1083c9180a57SEric Paris }
1084c9180a57SEric Paris 
10852e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
10862e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
1087c9180a57SEric Paris {
1088e0007529SEric Paris 	char *p;
1089c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
1090c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
1091e0007529SEric Paris 	int rc, num_mnt_opts = 0;
1092c9180a57SEric Paris 
1093e0007529SEric Paris 	opts->num_mnt_opts = 0;
1094c9180a57SEric Paris 
1095c9180a57SEric Paris 	/* Standard string-based options. */
1096c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
1097c9180a57SEric Paris 		int token;
1098c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
1099c9180a57SEric Paris 
1100c9180a57SEric Paris 		if (!*p)
1101c9180a57SEric Paris 			continue;
1102c9180a57SEric Paris 
1103c9180a57SEric Paris 		token = match_token(p, tokens, args);
1104c9180a57SEric Paris 
1105c9180a57SEric Paris 		switch (token) {
1106c9180a57SEric Paris 		case Opt_context:
1107c9180a57SEric Paris 			if (context || defcontext) {
1108c9180a57SEric Paris 				rc = -EINVAL;
1109c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1110c9180a57SEric Paris 				goto out_err;
1111c9180a57SEric Paris 			}
1112c9180a57SEric Paris 			context = match_strdup(&args[0]);
1113c9180a57SEric Paris 			if (!context) {
1114c9180a57SEric Paris 				rc = -ENOMEM;
1115c9180a57SEric Paris 				goto out_err;
1116c9180a57SEric Paris 			}
1117c9180a57SEric Paris 			break;
1118c9180a57SEric Paris 
1119c9180a57SEric Paris 		case Opt_fscontext:
1120c9180a57SEric Paris 			if (fscontext) {
1121c9180a57SEric Paris 				rc = -EINVAL;
1122c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1123c9180a57SEric Paris 				goto out_err;
1124c9180a57SEric Paris 			}
1125c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
1126c9180a57SEric Paris 			if (!fscontext) {
1127c9180a57SEric Paris 				rc = -ENOMEM;
1128c9180a57SEric Paris 				goto out_err;
1129c9180a57SEric Paris 			}
1130c9180a57SEric Paris 			break;
1131c9180a57SEric Paris 
1132c9180a57SEric Paris 		case Opt_rootcontext:
1133c9180a57SEric Paris 			if (rootcontext) {
1134c9180a57SEric Paris 				rc = -EINVAL;
1135c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1136c9180a57SEric Paris 				goto out_err;
1137c9180a57SEric Paris 			}
1138c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
1139c9180a57SEric Paris 			if (!rootcontext) {
1140c9180a57SEric Paris 				rc = -ENOMEM;
1141c9180a57SEric Paris 				goto out_err;
1142c9180a57SEric Paris 			}
1143c9180a57SEric Paris 			break;
1144c9180a57SEric Paris 
1145c9180a57SEric Paris 		case Opt_defcontext:
1146c9180a57SEric Paris 			if (context || defcontext) {
1147c9180a57SEric Paris 				rc = -EINVAL;
1148c103a91eSpeter enderborg 				pr_warn(SEL_MOUNT_FAIL_MSG);
1149c9180a57SEric Paris 				goto out_err;
1150c9180a57SEric Paris 			}
1151c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
1152c9180a57SEric Paris 			if (!defcontext) {
1153c9180a57SEric Paris 				rc = -ENOMEM;
1154c9180a57SEric Paris 				goto out_err;
1155c9180a57SEric Paris 			}
1156c9180a57SEric Paris 			break;
115711689d47SDavid P. Quigley 		case Opt_labelsupport:
115811689d47SDavid P. Quigley 			break;
1159c9180a57SEric Paris 		default:
1160c9180a57SEric Paris 			rc = -EINVAL;
1161c103a91eSpeter enderborg 			pr_warn("SELinux:  unknown mount option\n");
1162c9180a57SEric Paris 			goto out_err;
1163c9180a57SEric Paris 
1164c9180a57SEric Paris 		}
1165c9180a57SEric Paris 	}
1166c9180a57SEric Paris 
1167e0007529SEric Paris 	rc = -ENOMEM;
11688931c3bdSTetsuo Handa 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
1169e0007529SEric Paris 	if (!opts->mnt_opts)
1170e0007529SEric Paris 		goto out_err;
1171e0007529SEric Paris 
11728931c3bdSTetsuo Handa 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
11738931c3bdSTetsuo Handa 				       GFP_KERNEL);
1174023f108dSPaul Moore 	if (!opts->mnt_opts_flags)
1175e0007529SEric Paris 		goto out_err;
1176c9180a57SEric Paris 
1177e0007529SEric Paris 	if (fscontext) {
1178e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
1179e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1180e0007529SEric Paris 	}
1181e0007529SEric Paris 	if (context) {
1182e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
1183e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1184e0007529SEric Paris 	}
1185e0007529SEric Paris 	if (rootcontext) {
1186e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
1187e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1188e0007529SEric Paris 	}
1189e0007529SEric Paris 	if (defcontext) {
1190e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
1191e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1192e0007529SEric Paris 	}
1193e0007529SEric Paris 
1194e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
1195e0007529SEric Paris 	return 0;
1196e0007529SEric Paris 
1197c9180a57SEric Paris out_err:
1198023f108dSPaul Moore 	security_free_mnt_opts(opts);
1199c9180a57SEric Paris 	kfree(context);
1200c9180a57SEric Paris 	kfree(defcontext);
1201c9180a57SEric Paris 	kfree(fscontext);
1202c9180a57SEric Paris 	kfree(rootcontext);
1203c9180a57SEric Paris 	return rc;
12041da177e4SLinus Torvalds }
1205e0007529SEric Paris /*
1206e0007529SEric Paris  * string mount options parsing and call set the sbsec
1207e0007529SEric Paris  */
1208e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
1209e0007529SEric Paris {
1210e0007529SEric Paris 	int rc = 0;
1211e0007529SEric Paris 	char *options = data;
1212e0007529SEric Paris 	struct security_mnt_opts opts;
1213e0007529SEric Paris 
1214e0007529SEric Paris 	security_init_mnt_opts(&opts);
1215e0007529SEric Paris 
1216e0007529SEric Paris 	if (!data)
1217e0007529SEric Paris 		goto out;
1218e0007529SEric Paris 
1219e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1220e0007529SEric Paris 
1221e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1222e0007529SEric Paris 	if (rc)
1223e0007529SEric Paris 		goto out_err;
1224e0007529SEric Paris 
1225e0007529SEric Paris out:
1226649f6e77SDavid Quigley 	rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1227e0007529SEric Paris 
1228e0007529SEric Paris out_err:
1229e0007529SEric Paris 	security_free_mnt_opts(&opts);
1230e0007529SEric Paris 	return rc;
1231e0007529SEric Paris }
12321da177e4SLinus Torvalds 
12333583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
12343583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
12352069f457SEric Paris {
12362069f457SEric Paris 	int i;
12372069f457SEric Paris 	char *prefix;
12382069f457SEric Paris 
12392069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
124011689d47SDavid P. Quigley 		char *has_comma;
124111689d47SDavid P. Quigley 
124211689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
124311689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
124411689d47SDavid P. Quigley 		else
124511689d47SDavid P. Quigley 			has_comma = NULL;
12462069f457SEric Paris 
12472069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
12482069f457SEric Paris 		case CONTEXT_MNT:
12492069f457SEric Paris 			prefix = CONTEXT_STR;
12502069f457SEric Paris 			break;
12512069f457SEric Paris 		case FSCONTEXT_MNT:
12522069f457SEric Paris 			prefix = FSCONTEXT_STR;
12532069f457SEric Paris 			break;
12542069f457SEric Paris 		case ROOTCONTEXT_MNT:
12552069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
12562069f457SEric Paris 			break;
12572069f457SEric Paris 		case DEFCONTEXT_MNT:
12582069f457SEric Paris 			prefix = DEFCONTEXT_STR;
12592069f457SEric Paris 			break;
126012f348b9SEric Paris 		case SBLABEL_MNT:
126111689d47SDavid P. Quigley 			seq_putc(m, ',');
126211689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
126311689d47SDavid P. Quigley 			continue;
12642069f457SEric Paris 		default:
12652069f457SEric Paris 			BUG();
1266a35c6c83SEric Paris 			return;
12672069f457SEric Paris 		};
12682069f457SEric Paris 		/* we need a comma before each option */
12692069f457SEric Paris 		seq_putc(m, ',');
12702069f457SEric Paris 		seq_puts(m, prefix);
12712069f457SEric Paris 		if (has_comma)
12722069f457SEric Paris 			seq_putc(m, '\"');
1273a068acf2SKees Cook 		seq_escape(m, opts->mnt_opts[i], "\"\n\\");
12742069f457SEric Paris 		if (has_comma)
12752069f457SEric Paris 			seq_putc(m, '\"');
12762069f457SEric Paris 	}
12772069f457SEric Paris }
12782069f457SEric Paris 
12792069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
12802069f457SEric Paris {
12812069f457SEric Paris 	struct security_mnt_opts opts;
12822069f457SEric Paris 	int rc;
12832069f457SEric Paris 
12842069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1285383795c2SEric Paris 	if (rc) {
1286383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1287383795c2SEric Paris 		if (rc == -EINVAL)
1288383795c2SEric Paris 			rc = 0;
12892069f457SEric Paris 		return rc;
1290383795c2SEric Paris 	}
12912069f457SEric Paris 
12922069f457SEric Paris 	selinux_write_opts(m, &opts);
12932069f457SEric Paris 
12942069f457SEric Paris 	security_free_mnt_opts(&opts);
12952069f457SEric Paris 
12962069f457SEric Paris 	return rc;
12972069f457SEric Paris }
12982069f457SEric Paris 
12991da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
13021da177e4SLinus Torvalds 	case S_IFSOCK:
13031da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
13041da177e4SLinus Torvalds 	case S_IFLNK:
13051da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
13061da177e4SLinus Torvalds 	case S_IFREG:
13071da177e4SLinus Torvalds 		return SECCLASS_FILE;
13081da177e4SLinus Torvalds 	case S_IFBLK:
13091da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
13101da177e4SLinus Torvalds 	case S_IFDIR:
13111da177e4SLinus Torvalds 		return SECCLASS_DIR;
13121da177e4SLinus Torvalds 	case S_IFCHR:
13131da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
13141da177e4SLinus Torvalds 	case S_IFIFO:
13151da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	return SECCLASS_FILE;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
132213402580SJames Morris static inline int default_protocol_stream(int protocol)
132313402580SJames Morris {
132413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
132513402580SJames Morris }
132613402580SJames Morris 
132713402580SJames Morris static inline int default_protocol_dgram(int protocol)
132813402580SJames Morris {
132913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
133013402580SJames Morris }
133113402580SJames Morris 
13321da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
13331da177e4SLinus Torvalds {
1334aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1335da69a530SStephen Smalley 
13361da177e4SLinus Torvalds 	switch (family) {
13371da177e4SLinus Torvalds 	case PF_UNIX:
13381da177e4SLinus Torvalds 		switch (type) {
13391da177e4SLinus Torvalds 		case SOCK_STREAM:
13401da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
13411da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
13421da177e4SLinus Torvalds 		case SOCK_DGRAM:
13432a764b52SLuis Ressel 		case SOCK_RAW:
13441da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
13451da177e4SLinus Torvalds 		}
13461da177e4SLinus Torvalds 		break;
13471da177e4SLinus Torvalds 	case PF_INET:
13481da177e4SLinus Torvalds 	case PF_INET6:
13491da177e4SLinus Torvalds 		switch (type) {
13501da177e4SLinus Torvalds 		case SOCK_STREAM:
1351da69a530SStephen Smalley 		case SOCK_SEQPACKET:
135213402580SJames Morris 			if (default_protocol_stream(protocol))
13531da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1354da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1355da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
135613402580SJames Morris 			else
135713402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
13581da177e4SLinus Torvalds 		case SOCK_DGRAM:
135913402580SJames Morris 			if (default_protocol_dgram(protocol))
13601da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1361ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1362ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1363da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
136413402580SJames Morris 			else
136513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
13662ee92d46SJames Morris 		case SOCK_DCCP:
13672ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
136813402580SJames Morris 		default:
13691da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 		break;
13721da177e4SLinus Torvalds 	case PF_NETLINK:
13731da177e4SLinus Torvalds 		switch (protocol) {
13741da177e4SLinus Torvalds 		case NETLINK_ROUTE:
13751da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
13767f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
13771da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
13781da177e4SLinus Torvalds 		case NETLINK_NFLOG:
13791da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
13801da177e4SLinus Torvalds 		case NETLINK_XFRM:
13811da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
13821da177e4SLinus Torvalds 		case NETLINK_SELINUX:
13831da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
13846c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
13856c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
13861da177e4SLinus Torvalds 		case NETLINK_AUDIT:
13871da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
13886c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
13896c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
13906c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
13916c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
13926c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
13936c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
13941da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
13951da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
13960c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
13970c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
13986c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
13996c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
14006c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
14016c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
14026c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
14036c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
14046c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
14056c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
14061da177e4SLinus Torvalds 		default:
14071da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
14081da177e4SLinus Torvalds 		}
14091da177e4SLinus Torvalds 	case PF_PACKET:
14101da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
14111da177e4SLinus Torvalds 	case PF_KEY:
14121da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
14133e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
14143e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
1417da69a530SStephen Smalley 	if (extsockclass) {
1418da69a530SStephen Smalley 		switch (family) {
1419da69a530SStephen Smalley 		case PF_AX25:
1420da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1421da69a530SStephen Smalley 		case PF_IPX:
1422da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1423da69a530SStephen Smalley 		case PF_NETROM:
1424da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1425da69a530SStephen Smalley 		case PF_ATMPVC:
1426da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1427da69a530SStephen Smalley 		case PF_X25:
1428da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1429da69a530SStephen Smalley 		case PF_ROSE:
1430da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1431da69a530SStephen Smalley 		case PF_DECnet:
1432da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1433da69a530SStephen Smalley 		case PF_ATMSVC:
1434da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1435da69a530SStephen Smalley 		case PF_RDS:
1436da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1437da69a530SStephen Smalley 		case PF_IRDA:
1438da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1439da69a530SStephen Smalley 		case PF_PPPOX:
1440da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1441da69a530SStephen Smalley 		case PF_LLC:
1442da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1443da69a530SStephen Smalley 		case PF_CAN:
1444da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1445da69a530SStephen Smalley 		case PF_TIPC:
1446da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1447da69a530SStephen Smalley 		case PF_BLUETOOTH:
1448da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1449da69a530SStephen Smalley 		case PF_IUCV:
1450da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1451da69a530SStephen Smalley 		case PF_RXRPC:
1452da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1453da69a530SStephen Smalley 		case PF_ISDN:
1454da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1455da69a530SStephen Smalley 		case PF_PHONET:
1456da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1457da69a530SStephen Smalley 		case PF_IEEE802154:
1458da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1459da69a530SStephen Smalley 		case PF_CAIF:
1460da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1461da69a530SStephen Smalley 		case PF_ALG:
1462da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1463da69a530SStephen Smalley 		case PF_NFC:
1464da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1465da69a530SStephen Smalley 		case PF_VSOCK:
1466da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1467da69a530SStephen Smalley 		case PF_KCM:
1468da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1469da69a530SStephen Smalley 		case PF_QIPCRTR:
1470da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
14713051bf36SLinus Torvalds 		case PF_SMC:
14723051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
147368e8b849SBjörn Töpel 		case PF_XDP:
147468e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
147568e8b849SBjörn Töpel #if PF_MAX > 45
1476da69a530SStephen Smalley #error New address family defined, please update this function.
1477da69a530SStephen Smalley #endif
1478da69a530SStephen Smalley 		}
1479da69a530SStephen Smalley 	}
1480da69a530SStephen Smalley 
14811da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
1484134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
14851da177e4SLinus Torvalds 				 u16 tclass,
1486134509d5SStephen Smalley 				 u16 flags,
14871da177e4SLinus Torvalds 				 u32 *sid)
14881da177e4SLinus Torvalds {
14898e6c9693SLucian Adrian Grijincu 	int rc;
1490fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
14918e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
14941da177e4SLinus Torvalds 	if (!buffer)
14951da177e4SLinus Torvalds 		return -ENOMEM;
14961da177e4SLinus Torvalds 
14978e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
14988e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
14998e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
15008e6c9693SLucian Adrian Grijincu 	else {
1501134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
15028e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
15038e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
15048e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
15058e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
15068e6c9693SLucian Adrian Grijincu 				path[1] = '/';
15078e6c9693SLucian Adrian Grijincu 				path++;
15081da177e4SLinus Torvalds 			}
1509134509d5SStephen Smalley 		}
1510aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1511aa8e712cSStephen Smalley 					path, tclass, sid);
15127bb185edSStephen Smalley 		if (rc == -ENOENT) {
15137bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
15147bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
15157bb185edSStephen Smalley 			rc = 0;
15167bb185edSStephen Smalley 		}
15178e6c9693SLucian Adrian Grijincu 	}
15181da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
15191da177e4SLinus Torvalds 	return rc;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
15231da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
15261da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
15279287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
15289287aed2SAndreas Gruenbacher 	u16 sclass;
15291da177e4SLinus Torvalds 	struct dentry *dentry;
15301da177e4SLinus Torvalds #define INITCONTEXTLEN 255
15311da177e4SLinus Torvalds 	char *context = NULL;
15321da177e4SLinus Torvalds 	unsigned len = 0;
15331da177e4SLinus Torvalds 	int rc = 0;
15341da177e4SLinus Torvalds 
15356f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
153613457d07SAndreas Gruenbacher 		return 0;
15371da177e4SLinus Torvalds 
15389287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15396f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
154023970741SEric Paris 		goto out_unlock;
15411da177e4SLinus Torvalds 
154213457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
154313457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
154413457d07SAndreas Gruenbacher 
15451da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
15460d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
15471da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
15481da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
15491da177e4SLinus Torvalds 		   server is ready to handle calls. */
15501da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
15511da177e4SLinus Torvalds 		if (list_empty(&isec->list))
15521da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
15531da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
155423970741SEric Paris 		goto out_unlock;
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds 
15579287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
15589287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
15599287aed2SAndreas Gruenbacher 	sid = isec->sid;
15609287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
15619287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15629287aed2SAndreas Gruenbacher 
15631da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1564eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1565eb9ae686SDavid Quigley 		break;
15661da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
15675d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
15689287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
15691da177e4SLinus Torvalds 			break;
15701da177e4SLinus Torvalds 		}
15711da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
15721da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
15731da177e4SLinus Torvalds 		if (opt_dentry) {
15741da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
15751da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
15761da177e4SLinus Torvalds 		} else {
1577b127125dSAl Viro 			/*
1578b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1579b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1580b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1581b127125dSAl Viro 			 * two, depending upon that...
1582b127125dSAl Viro 			 */
15831da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1584b127125dSAl Viro 			if (!dentry)
1585b127125dSAl Viro 				dentry = d_find_any_alias(inode);
15861da177e4SLinus Torvalds 		}
15871da177e4SLinus Torvalds 		if (!dentry) {
1588df7f54c0SEric Paris 			/*
1589df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1590df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1591df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1592df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1593df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1594df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1595df7f54c0SEric Paris 			 * be used again by userspace.
1596df7f54c0SEric Paris 			 */
15979287aed2SAndreas Gruenbacher 			goto out;
15981da177e4SLinus Torvalds 		}
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
16014cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
16021da177e4SLinus Torvalds 		if (!context) {
16031da177e4SLinus Torvalds 			rc = -ENOMEM;
16041da177e4SLinus Torvalds 			dput(dentry);
16059287aed2SAndreas Gruenbacher 			goto out;
16061da177e4SLinus Torvalds 		}
16074cb912f1SEric Paris 		context[len] = '\0';
16085d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
16091da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1610314dabb8SJames Morris 			kfree(context);
1611314dabb8SJames Morris 
16121da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
16135d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
16141da177e4SLinus Torvalds 			if (rc < 0) {
16151da177e4SLinus Torvalds 				dput(dentry);
16169287aed2SAndreas Gruenbacher 				goto out;
16171da177e4SLinus Torvalds 			}
16181da177e4SLinus Torvalds 			len = rc;
16194cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
16201da177e4SLinus Torvalds 			if (!context) {
16211da177e4SLinus Torvalds 				rc = -ENOMEM;
16221da177e4SLinus Torvalds 				dput(dentry);
16239287aed2SAndreas Gruenbacher 				goto out;
16241da177e4SLinus Torvalds 			}
16254cb912f1SEric Paris 			context[len] = '\0';
16265d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
16271da177e4SLinus Torvalds 		}
16281da177e4SLinus Torvalds 		dput(dentry);
16291da177e4SLinus Torvalds 		if (rc < 0) {
16301da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1631c103a91eSpeter enderborg 				pr_warn("SELinux: %s:  getxattr returned "
1632dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
16331da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
16341da177e4SLinus Torvalds 				kfree(context);
16359287aed2SAndreas Gruenbacher 				goto out;
16361da177e4SLinus Torvalds 			}
16371da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
16381da177e4SLinus Torvalds 			sid = sbsec->def_sid;
16391da177e4SLinus Torvalds 			rc = 0;
16401da177e4SLinus Torvalds 		} else {
1641aa8e712cSStephen Smalley 			rc = security_context_to_sid_default(&selinux_state,
1642aa8e712cSStephen Smalley 							     context, rc, &sid,
1643869ab514SStephen Smalley 							     sbsec->def_sid,
1644869ab514SStephen Smalley 							     GFP_NOFS);
16451da177e4SLinus Torvalds 			if (rc) {
16464ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
16474ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
16484ba0a8adSEric Paris 
16494ba0a8adSEric Paris 				if (rc == -EINVAL) {
16504ba0a8adSEric Paris 					if (printk_ratelimit())
1651c103a91eSpeter enderborg 						pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
16524ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
16534ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
16544ba0a8adSEric Paris 				} else {
1655c103a91eSpeter enderborg 					pr_warn("SELinux: %s:  context_to_sid(%s) "
16561da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
16574ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
16584ba0a8adSEric Paris 				}
16591da177e4SLinus Torvalds 				kfree(context);
16601da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
16611da177e4SLinus Torvalds 				rc = 0;
16621da177e4SLinus Torvalds 				break;
16631da177e4SLinus Torvalds 			}
16641da177e4SLinus Torvalds 		}
16651da177e4SLinus Torvalds 		kfree(context);
16661da177e4SLinus Torvalds 		break;
16671da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
16689287aed2SAndreas Gruenbacher 		sid = task_sid;
16691da177e4SLinus Torvalds 		break;
16701da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
16711da177e4SLinus Torvalds 		/* Default to the fs SID. */
16729287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1675aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1676aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
16771da177e4SLinus Torvalds 		if (rc)
16789287aed2SAndreas Gruenbacher 			goto out;
16791da177e4SLinus Torvalds 		break;
1680c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
16819287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1682c312feb2SEric Paris 		break;
16831da177e4SLinus Torvalds 	default:
1684c312feb2SEric Paris 		/* Default to the fs superblock SID. */
16859287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
16861da177e4SLinus Torvalds 
1687134509d5SStephen Smalley 		if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1688f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1689f64410ecSPaul Moore 			 * procfs inodes */
1690b127125dSAl Viro 			if (opt_dentry) {
1691f64410ecSPaul Moore 				/* Called from d_instantiate or
1692f64410ecSPaul Moore 				 * d_splice_alias. */
1693f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1694b127125dSAl Viro 			} else {
1695f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1696b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1697b127125dSAl Viro 				 * a connected one, so try that first.
1698b127125dSAl Viro 				 */
1699f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1700b127125dSAl Viro 				if (!dentry)
1701b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1702b127125dSAl Viro 			}
1703f64410ecSPaul Moore 			/*
1704f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1705f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1706f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1707f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1708f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1709f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1710f64410ecSPaul Moore 			 * could be used again by userspace.
1711f64410ecSPaul Moore 			 */
1712f64410ecSPaul Moore 			if (!dentry)
17139287aed2SAndreas Gruenbacher 				goto out;
17149287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1715134509d5SStephen Smalley 						   sbsec->flags, &sid);
1716f64410ecSPaul Moore 			dput(dentry);
17171da177e4SLinus Torvalds 			if (rc)
17189287aed2SAndreas Gruenbacher 				goto out;
17191da177e4SLinus Torvalds 		}
17201da177e4SLinus Torvalds 		break;
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 
17239287aed2SAndreas Gruenbacher out:
17249287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
17259287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
17269287aed2SAndreas Gruenbacher 		if (!sid || rc) {
17279287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
17289287aed2SAndreas Gruenbacher 			goto out_unlock;
17299287aed2SAndreas Gruenbacher 		}
17309287aed2SAndreas Gruenbacher 
17316f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
17329287aed2SAndreas Gruenbacher 		isec->sid = sid;
17339287aed2SAndreas Gruenbacher 	}
17341da177e4SLinus Torvalds 
173523970741SEric Paris out_unlock:
17369287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
17371da177e4SLinus Torvalds 	return rc;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
17411da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
17421da177e4SLinus Torvalds {
17431da177e4SLinus Torvalds 	u32 perm = 0;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	switch (sig) {
17461da177e4SLinus Torvalds 	case SIGCHLD:
17471da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
17481da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
17491da177e4SLinus Torvalds 		break;
17501da177e4SLinus Torvalds 	case SIGKILL:
17511da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
17521da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
17531da177e4SLinus Torvalds 		break;
17541da177e4SLinus Torvalds 	case SIGSTOP:
17551da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
17561da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
17571da177e4SLinus Torvalds 		break;
17581da177e4SLinus Torvalds 	default:
17591da177e4SLinus Torvalds 		/* All other signals. */
17601da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
17611da177e4SLinus Torvalds 		break;
17621da177e4SLinus Torvalds 	}
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	return perm;
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
1767b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1768b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1769b68e418cSStephen Smalley #endif
1770b68e418cSStephen Smalley 
17711da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
17726a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
17738e4ff6f2SStephen Smalley 			       int cap, int audit, bool initns)
17741da177e4SLinus Torvalds {
17752bf49690SThomas Liu 	struct common_audit_data ad;
177606112163SEric Paris 	struct av_decision avd;
1777b68e418cSStephen Smalley 	u16 sclass;
17783699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1779b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
178006112163SEric Paris 	int rc;
17811da177e4SLinus Torvalds 
178250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
17831da177e4SLinus Torvalds 	ad.u.cap = cap;
17841da177e4SLinus Torvalds 
1785b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1786b68e418cSStephen Smalley 	case 0:
17878e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1788b68e418cSStephen Smalley 		break;
1789b68e418cSStephen Smalley 	case 1:
17908e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1791b68e418cSStephen Smalley 		break;
1792b68e418cSStephen Smalley 	default:
1793c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1794b68e418cSStephen Smalley 		BUG();
1795a35c6c83SEric Paris 		return -EINVAL;
1796b68e418cSStephen Smalley 	}
179706112163SEric Paris 
17986b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
17996b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
18009ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
18016b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
18026b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
18039ade0cf4SEric Paris 		if (rc2)
18049ade0cf4SEric Paris 			return rc2;
18059ade0cf4SEric Paris 	}
180606112163SEric Paris 	return rc;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
18101da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
18111da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
181288e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
18131da177e4SLinus Torvalds 			  struct inode *inode,
18141da177e4SLinus Torvalds 			  u32 perms,
181519e49834SLinus Torvalds 			  struct common_audit_data *adp)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1818275bb41eSDavid Howells 	u32 sid;
18191da177e4SLinus Torvalds 
1820e0e81739SDavid Howells 	validate_creds(cred);
1821e0e81739SDavid Howells 
1822bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1823bbaca6c2SStephen Smalley 		return 0;
1824bbaca6c2SStephen Smalley 
182588e67f3bSDavid Howells 	sid = cred_sid(cred);
18261da177e4SLinus Torvalds 	isec = inode->i_security;
18271da177e4SLinus Torvalds 
18286b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18296b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
18331da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
18341da177e4SLinus Torvalds    pathname if needed. */
183588e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
18361da177e4SLinus Torvalds 				  struct dentry *dentry,
18371da177e4SLinus Torvalds 				  u32 av)
18381da177e4SLinus Torvalds {
1839c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
18402bf49690SThomas Liu 	struct common_audit_data ad;
184188e67f3bSDavid Howells 
184250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18432875fa00SEric Paris 	ad.u.dentry = dentry;
18445d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
184519e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
18462875fa00SEric Paris }
18472875fa00SEric Paris 
18482875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
18492875fa00SEric Paris    the path to help the auditing code to more easily generate the
18502875fa00SEric Paris    pathname if needed. */
18512875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
18523f7036a0SAl Viro 				const struct path *path,
18532875fa00SEric Paris 				u32 av)
18542875fa00SEric Paris {
1855c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
18562875fa00SEric Paris 	struct common_audit_data ad;
18572875fa00SEric Paris 
185850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
18592875fa00SEric Paris 	ad.u.path = *path;
18605d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
186119e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
186413f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
186513f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
186613f8e981SDavid Howells 				     struct file *file,
186713f8e981SDavid Howells 				     u32 av)
186813f8e981SDavid Howells {
186913f8e981SDavid Howells 	struct common_audit_data ad;
187013f8e981SDavid Howells 
187143af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
187243af5de7SVivek Goyal 	ad.u.file = file;
187319e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
187413f8e981SDavid Howells }
187513f8e981SDavid Howells 
1876f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1877f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1878f66e448cSChenbo Feng #endif
1879f66e448cSChenbo Feng 
18801da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
18811da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
18821da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
18831da177e4SLinus Torvalds    check a particular permission to the file.
18841da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
18851da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
18861da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
18871da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
188888e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
18891da177e4SLinus Torvalds 			 struct file *file,
18901da177e4SLinus Torvalds 			 u32 av)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1893496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
18942bf49690SThomas Liu 	struct common_audit_data ad;
189588e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
18961da177e4SLinus Torvalds 	int rc;
18971da177e4SLinus Torvalds 
189843af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
189943af5de7SVivek Goyal 	ad.u.file = file;
19001da177e4SLinus Torvalds 
1901275bb41eSDavid Howells 	if (sid != fsec->sid) {
19026b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19036b6bc620SStephen Smalley 				  sid, fsec->sid,
19041da177e4SLinus Torvalds 				  SECCLASS_FD,
19051da177e4SLinus Torvalds 				  FD__USE,
19061da177e4SLinus Torvalds 				  &ad);
19071da177e4SLinus Torvalds 		if (rc)
190888e67f3bSDavid Howells 			goto out;
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds 
1911f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1912f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1913f66e448cSChenbo Feng 	if (rc)
1914f66e448cSChenbo Feng 		return rc;
1915f66e448cSChenbo Feng #endif
1916f66e448cSChenbo Feng 
19171da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
191888e67f3bSDavid Howells 	rc = 0;
19191da177e4SLinus Torvalds 	if (av)
192019e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
19211da177e4SLinus Torvalds 
192288e67f3bSDavid Howells out:
192388e67f3bSDavid Howells 	return rc;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
1926c3c188b2SDavid Howells /*
1927c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1928c3c188b2SDavid Howells  */
1929c957f6dfSVivek Goyal static int
1930c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1931c957f6dfSVivek Goyal 				 struct inode *dir,
1932c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1933c3c188b2SDavid Howells 				 u32 *_new_isid)
1934c3c188b2SDavid Howells {
1935c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1936c3c188b2SDavid Howells 
1937c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1938c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1939c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1940c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1941c3c188b2SDavid Howells 		   tsec->create_sid) {
1942c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1943c3c188b2SDavid Howells 	} else {
194420cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1945aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1946aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1947c3c188b2SDavid Howells 					       name, _new_isid);
1948c3c188b2SDavid Howells 	}
1949c3c188b2SDavid Howells 
1950c3c188b2SDavid Howells 	return 0;
1951c3c188b2SDavid Howells }
1952c3c188b2SDavid Howells 
19531da177e4SLinus Torvalds /* Check whether a task can create a file. */
19541da177e4SLinus Torvalds static int may_create(struct inode *dir,
19551da177e4SLinus Torvalds 		      struct dentry *dentry,
19561da177e4SLinus Torvalds 		      u16 tclass)
19571da177e4SLinus Torvalds {
19585fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
19591da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
19601da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1961275bb41eSDavid Howells 	u32 sid, newsid;
19622bf49690SThomas Liu 	struct common_audit_data ad;
19631da177e4SLinus Torvalds 	int rc;
19641da177e4SLinus Torvalds 
196583da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
19661da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
19671da177e4SLinus Torvalds 
1968275bb41eSDavid Howells 	sid = tsec->sid;
1969275bb41eSDavid Howells 
197050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1971a269434dSEric Paris 	ad.u.dentry = dentry;
19721da177e4SLinus Torvalds 
19736b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19746b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
19751da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
19761da177e4SLinus Torvalds 			  &ad);
19771da177e4SLinus Torvalds 	if (rc)
19781da177e4SLinus Torvalds 		return rc;
19791da177e4SLinus Torvalds 
1980c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(), dir,
1981c957f6dfSVivek Goyal 					   &dentry->d_name, tclass, &newsid);
19821da177e4SLinus Torvalds 	if (rc)
19831da177e4SLinus Torvalds 		return rc;
19841da177e4SLinus Torvalds 
19856b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19866b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
19871da177e4SLinus Torvalds 	if (rc)
19881da177e4SLinus Torvalds 		return rc;
19891da177e4SLinus Torvalds 
19906b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19916b6bc620SStephen Smalley 			    newsid, sbsec->sid,
19921da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
19931da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds #define MAY_LINK	0
19971da177e4SLinus Torvalds #define MAY_UNLINK	1
19981da177e4SLinus Torvalds #define MAY_RMDIR	2
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
20011da177e4SLinus Torvalds static int may_link(struct inode *dir,
20021da177e4SLinus Torvalds 		    struct dentry *dentry,
20031da177e4SLinus Torvalds 		    int kind)
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds {
20061da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
20072bf49690SThomas Liu 	struct common_audit_data ad;
2008275bb41eSDavid Howells 	u32 sid = current_sid();
20091da177e4SLinus Torvalds 	u32 av;
20101da177e4SLinus Torvalds 	int rc;
20111da177e4SLinus Torvalds 
201283da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
201383da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
20141da177e4SLinus Torvalds 
201550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2016a269434dSEric Paris 	ad.u.dentry = dentry;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	av = DIR__SEARCH;
20191da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
20206b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20216b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
20221da177e4SLinus Torvalds 	if (rc)
20231da177e4SLinus Torvalds 		return rc;
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	switch (kind) {
20261da177e4SLinus Torvalds 	case MAY_LINK:
20271da177e4SLinus Torvalds 		av = FILE__LINK;
20281da177e4SLinus Torvalds 		break;
20291da177e4SLinus Torvalds 	case MAY_UNLINK:
20301da177e4SLinus Torvalds 		av = FILE__UNLINK;
20311da177e4SLinus Torvalds 		break;
20321da177e4SLinus Torvalds 	case MAY_RMDIR:
20331da177e4SLinus Torvalds 		av = DIR__RMDIR;
20341da177e4SLinus Torvalds 		break;
20351da177e4SLinus Torvalds 	default:
2036c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
2037744ba35eSEric Paris 			__func__, kind);
20381da177e4SLinus Torvalds 		return 0;
20391da177e4SLinus Torvalds 	}
20401da177e4SLinus Torvalds 
20416b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20426b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
20431da177e4SLinus Torvalds 	return rc;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
20471da177e4SLinus Torvalds 			     struct dentry *old_dentry,
20481da177e4SLinus Torvalds 			     struct inode *new_dir,
20491da177e4SLinus Torvalds 			     struct dentry *new_dentry)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
20522bf49690SThomas Liu 	struct common_audit_data ad;
2053275bb41eSDavid Howells 	u32 sid = current_sid();
20541da177e4SLinus Torvalds 	u32 av;
20551da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
20561da177e4SLinus Torvalds 	int rc;
20571da177e4SLinus Torvalds 
205883da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
205983da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
2060e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
206183da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
20621da177e4SLinus Torvalds 
206350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
20641da177e4SLinus Torvalds 
2065a269434dSEric Paris 	ad.u.dentry = old_dentry;
20666b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20676b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
20681da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
20691da177e4SLinus Torvalds 	if (rc)
20701da177e4SLinus Torvalds 		return rc;
20716b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20726b6bc620SStephen Smalley 			  sid, old_isec->sid,
20731da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
20741da177e4SLinus Torvalds 	if (rc)
20751da177e4SLinus Torvalds 		return rc;
20761da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
20776b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20786b6bc620SStephen Smalley 				  sid, old_isec->sid,
20791da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
20801da177e4SLinus Torvalds 		if (rc)
20811da177e4SLinus Torvalds 			return rc;
20821da177e4SLinus Torvalds 	}
20831da177e4SLinus Torvalds 
2084a269434dSEric Paris 	ad.u.dentry = new_dentry;
20851da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
20862c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
20871da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
20886b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
20896b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
20901da177e4SLinus Torvalds 	if (rc)
20911da177e4SLinus Torvalds 		return rc;
20922c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
209383da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
2094e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
20956b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20966b6bc620SStephen Smalley 				  sid, new_isec->sid,
20971da177e4SLinus Torvalds 				  new_isec->sclass,
20981da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
20991da177e4SLinus Torvalds 		if (rc)
21001da177e4SLinus Torvalds 			return rc;
21011da177e4SLinus Torvalds 	}
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	return 0;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
210788e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
21081da177e4SLinus Torvalds 			       struct super_block *sb,
21091da177e4SLinus Torvalds 			       u32 perms,
21102bf49690SThomas Liu 			       struct common_audit_data *ad)
21111da177e4SLinus Torvalds {
21121da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
211388e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	sbsec = sb->s_security;
21166b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21176b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
21181da177e4SLinus Torvalds }
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
21211da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds 	u32 av = 0;
21241da177e4SLinus Torvalds 
2125dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
21261da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
21271da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
21281da177e4SLinus Torvalds 		if (mask & MAY_READ)
21291da177e4SLinus Torvalds 			av |= FILE__READ;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
21321da177e4SLinus Torvalds 			av |= FILE__APPEND;
21331da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
21341da177e4SLinus Torvalds 			av |= FILE__WRITE;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	} else {
21371da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
21381da177e4SLinus Torvalds 			av |= DIR__SEARCH;
21391da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
21401da177e4SLinus Torvalds 			av |= DIR__WRITE;
21411da177e4SLinus Torvalds 		if (mask & MAY_READ)
21421da177e4SLinus Torvalds 			av |= DIR__READ;
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	return av;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
21491da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
21501da177e4SLinus Torvalds {
21511da177e4SLinus Torvalds 	u32 av = 0;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
21541da177e4SLinus Torvalds 		av |= FILE__READ;
21551da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
21561da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
21571da177e4SLinus Torvalds 			av |= FILE__APPEND;
21581da177e4SLinus Torvalds 		else
21591da177e4SLinus Torvalds 			av |= FILE__WRITE;
21601da177e4SLinus Torvalds 	}
21610794c66dSStephen Smalley 	if (!av) {
21620794c66dSStephen Smalley 		/*
21630794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
21640794c66dSStephen Smalley 		 */
21650794c66dSStephen Smalley 		av = FILE__IOCTL;
21660794c66dSStephen Smalley 	}
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	return av;
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
21718b6a5a37SEric Paris /*
21728b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
21738b6a5a37SEric Paris  * open permission.
21748b6a5a37SEric Paris  */
21758b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
21768b6a5a37SEric Paris {
21778b6a5a37SEric Paris 	u32 av = file_to_av(file);
2178ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
21798b6a5a37SEric Paris 
2180aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
2181aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
21828b6a5a37SEric Paris 		av |= FILE__OPEN;
218349b7b8deSEric Paris 
21848b6a5a37SEric Paris 	return av;
21858b6a5a37SEric Paris }
21868b6a5a37SEric Paris 
21871da177e4SLinus Torvalds /* Hook functions begin here. */
21881da177e4SLinus Torvalds 
218979af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
219079af7307SStephen Smalley {
219179af7307SStephen Smalley 	u32 mysid = current_sid();
219279af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
219379af7307SStephen Smalley 
21946b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21956b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
219679af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
219779af7307SStephen Smalley }
219879af7307SStephen Smalley 
219979af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
220079af7307SStephen Smalley 				      struct task_struct *to)
220179af7307SStephen Smalley {
220279af7307SStephen Smalley 	u32 mysid = current_sid();
220379af7307SStephen Smalley 	u32 fromsid = task_sid(from);
220479af7307SStephen Smalley 	u32 tosid = task_sid(to);
220579af7307SStephen Smalley 	int rc;
220679af7307SStephen Smalley 
220779af7307SStephen Smalley 	if (mysid != fromsid) {
22086b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22096b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
221079af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
221179af7307SStephen Smalley 		if (rc)
221279af7307SStephen Smalley 			return rc;
221379af7307SStephen Smalley 	}
221479af7307SStephen Smalley 
22156b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22166b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
221779af7307SStephen Smalley 			    NULL);
221879af7307SStephen Smalley }
221979af7307SStephen Smalley 
222079af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
222179af7307SStephen Smalley 					  struct task_struct *to)
222279af7307SStephen Smalley {
222379af7307SStephen Smalley 	u32 fromsid = task_sid(from);
222479af7307SStephen Smalley 	u32 tosid = task_sid(to);
222579af7307SStephen Smalley 
22266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22276b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
222879af7307SStephen Smalley 			    NULL);
222979af7307SStephen Smalley }
223079af7307SStephen Smalley 
223179af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
223279af7307SStephen Smalley 					struct task_struct *to,
223379af7307SStephen Smalley 					struct file *file)
223479af7307SStephen Smalley {
223579af7307SStephen Smalley 	u32 sid = task_sid(to);
223679af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
223783da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
223820cdef8dSPaul Moore 	struct inode_security_struct *isec;
223979af7307SStephen Smalley 	struct common_audit_data ad;
224079af7307SStephen Smalley 	int rc;
224179af7307SStephen Smalley 
224279af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
224379af7307SStephen Smalley 	ad.u.path = file->f_path;
224479af7307SStephen Smalley 
224579af7307SStephen Smalley 	if (sid != fsec->sid) {
22466b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22476b6bc620SStephen Smalley 				  sid, fsec->sid,
224879af7307SStephen Smalley 				  SECCLASS_FD,
224979af7307SStephen Smalley 				  FD__USE,
225079af7307SStephen Smalley 				  &ad);
225179af7307SStephen Smalley 		if (rc)
225279af7307SStephen Smalley 			return rc;
225379af7307SStephen Smalley 	}
225479af7307SStephen Smalley 
2255f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2256f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2257f66e448cSChenbo Feng 	if (rc)
2258f66e448cSChenbo Feng 		return rc;
2259f66e448cSChenbo Feng #endif
2260f66e448cSChenbo Feng 
226183da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
226279af7307SStephen Smalley 		return 0;
226379af7307SStephen Smalley 
226420cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
22656b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22666b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
226779af7307SStephen Smalley 			    &ad);
226879af7307SStephen Smalley }
226979af7307SStephen Smalley 
22709e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2271006ebb40SStephen Smalley 				     unsigned int mode)
22721da177e4SLinus Torvalds {
2273275bb41eSDavid Howells 	u32 sid = current_sid();
2274275bb41eSDavid Howells 	u32 csid = task_sid(child);
2275006ebb40SStephen Smalley 
2276be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
22776b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22786b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2279be0554c9SStephen Smalley 
22806b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22816b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
22825cd9c58fSDavid Howells }
22835cd9c58fSDavid Howells 
22845cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
22855cd9c58fSDavid Howells {
22866b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22876b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2288be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
22921da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
22931da177e4SLinus Torvalds {
22946b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22956b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2296be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
22971da177e4SLinus Torvalds }
22981da177e4SLinus Torvalds 
2299d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2300d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
230115a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
230215a2460eSDavid Howells 			  const kernel_cap_t *permitted)
23031da177e4SLinus Torvalds {
23046b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
23056b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2306be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
23095626d3e8SJames Morris /*
23105626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
23115626d3e8SJames Morris  * which was removed).
23125626d3e8SJames Morris  *
23135626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
23145626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
23155626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
23165626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
23175626d3e8SJames Morris  */
23185626d3e8SJames Morris 
23196a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
23206a9de491SEric Paris 			   int cap, int audit)
23211da177e4SLinus Torvalds {
23228e4ff6f2SStephen Smalley 	return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
23231da177e4SLinus Torvalds }
23241da177e4SLinus Torvalds 
23251da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
23261da177e4SLinus Torvalds {
232788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
23281da177e4SLinus Torvalds 	int rc = 0;
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds 	if (!sb)
23311da177e4SLinus Torvalds 		return 0;
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	switch (cmds) {
23341da177e4SLinus Torvalds 	case Q_SYNC:
23351da177e4SLinus Torvalds 	case Q_QUOTAON:
23361da177e4SLinus Torvalds 	case Q_QUOTAOFF:
23371da177e4SLinus Torvalds 	case Q_SETINFO:
23381da177e4SLinus Torvalds 	case Q_SETQUOTA:
233988e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
23401da177e4SLinus Torvalds 		break;
23411da177e4SLinus Torvalds 	case Q_GETFMT:
23421da177e4SLinus Torvalds 	case Q_GETINFO:
23431da177e4SLinus Torvalds 	case Q_GETQUOTA:
234488e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
23451da177e4SLinus Torvalds 		break;
23461da177e4SLinus Torvalds 	default:
23471da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
23481da177e4SLinus Torvalds 		break;
23491da177e4SLinus Torvalds 	}
23501da177e4SLinus Torvalds 	return rc;
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
23541da177e4SLinus Torvalds {
235588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
235688e67f3bSDavid Howells 
23572875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
236012b3052cSEric Paris static int selinux_syslog(int type)
23611da177e4SLinus Torvalds {
23621da177e4SLinus Torvalds 	switch (type) {
2363d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2364d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
23656b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
23666b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2367be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2368d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2369d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2370d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2371d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
23726b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
23736b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2374be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2375be0554c9SStephen Smalley 				    NULL);
23761da177e4SLinus Torvalds 	}
2377be0554c9SStephen Smalley 	/* All other syslog types */
23786b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
23796b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2380be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds /*
23841da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
23851da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
23861da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
23871da177e4SLinus Torvalds  *
23881da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
23891da177e4SLinus Torvalds  * processes that allocate mappings.
23901da177e4SLinus Torvalds  */
239134b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
23921da177e4SLinus Torvalds {
23931da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
23941da177e4SLinus Torvalds 
2395b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
23968e4ff6f2SStephen Smalley 				 SECURITY_CAP_NOAUDIT, true);
23971da177e4SLinus Torvalds 	if (rc == 0)
23981da177e4SLinus Torvalds 		cap_sys_admin = 1;
23991da177e4SLinus Torvalds 
2400b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds /* binprm security operations */
24041da177e4SLinus Torvalds 
2405be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
24060c6181cbSPaul Moore {
24070c6181cbSPaul Moore 	u32 sid = 0;
24080c6181cbSPaul Moore 	struct task_struct *tracer;
24090c6181cbSPaul Moore 
24100c6181cbSPaul Moore 	rcu_read_lock();
2411be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
24120c6181cbSPaul Moore 	if (tracer)
24130c6181cbSPaul Moore 		sid = task_sid(tracer);
24140c6181cbSPaul Moore 	rcu_read_unlock();
24150c6181cbSPaul Moore 
24160c6181cbSPaul Moore 	return sid;
24170c6181cbSPaul Moore }
24180c6181cbSPaul Moore 
24197b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
24207b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
24217b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
24227b0d0b40SStephen Smalley {
24237b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2424380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
24257b0d0b40SStephen Smalley 	int rc;
2426af63f419SStephen Smalley 	u32 av;
24277b0d0b40SStephen Smalley 
24287b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
24297b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
24307b0d0b40SStephen Smalley 
24317b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
24327b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
24337b0d0b40SStephen Smalley 
24347b0d0b40SStephen Smalley 	/*
2435af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2436af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2437af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2438af63f419SStephen Smalley 	 * the old and new contexts.
2439af63f419SStephen Smalley 	 */
2440aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2441af63f419SStephen Smalley 		av = 0;
2442af63f419SStephen Smalley 		if (nnp)
2443af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2444af63f419SStephen Smalley 		if (nosuid)
2445af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
24466b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
24476b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2448af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2449af63f419SStephen Smalley 		if (!rc)
2450af63f419SStephen Smalley 			return 0;
2451af63f419SStephen Smalley 	}
2452af63f419SStephen Smalley 
2453af63f419SStephen Smalley 	/*
2454af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2455af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2456af63f419SStephen Smalley 	 * of the permissions of the current SID.
24577b0d0b40SStephen Smalley 	 */
2458aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2459aa8e712cSStephen Smalley 					 new_tsec->sid);
2460af63f419SStephen Smalley 	if (!rc)
2461af63f419SStephen Smalley 		return 0;
2462af63f419SStephen Smalley 
24637b0d0b40SStephen Smalley 	/*
24647b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
24657b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
24667b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
24677b0d0b40SStephen Smalley 	 */
24687b0d0b40SStephen Smalley 	if (nnp)
24697b0d0b40SStephen Smalley 		return -EPERM;
24707b0d0b40SStephen Smalley 	return -EACCES;
24717b0d0b40SStephen Smalley }
24727b0d0b40SStephen Smalley 
2473a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
24741da177e4SLinus Torvalds {
2475a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2476a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24771da177e4SLinus Torvalds 	struct inode_security_struct *isec;
24782bf49690SThomas Liu 	struct common_audit_data ad;
2479496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
24801da177e4SLinus Torvalds 	int rc;
24811da177e4SLinus Torvalds 
2482a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2483a6f76f23SDavid Howells 	 * the script interpreter */
2484ddb4a144SKees Cook 	if (bprm->called_set_creds)
24851da177e4SLinus Torvalds 		return 0;
24861da177e4SLinus Torvalds 
2487a6f76f23SDavid Howells 	old_tsec = current_security();
2488a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
248983da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 	/* Default to the current task SID. */
2492a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2493a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
24941da177e4SLinus Torvalds 
249528eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2496a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2497a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2498a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
24991da177e4SLinus Torvalds 
2500a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2501a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
25021da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2503a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2504259e5e6cSAndy Lutomirski 
25057b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
25067b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
25077b0d0b40SStephen Smalley 		if (rc)
25087b0d0b40SStephen Smalley 			return rc;
25091da177e4SLinus Torvalds 	} else {
25101da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2511aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2512aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2513652bb9b0SEric Paris 					     &new_tsec->sid);
25141da177e4SLinus Torvalds 		if (rc)
25151da177e4SLinus Torvalds 			return rc;
25167b0d0b40SStephen Smalley 
25177b0d0b40SStephen Smalley 		/*
25187b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
25197b0d0b40SStephen Smalley 		 * transition.
25207b0d0b40SStephen Smalley 		 */
25217b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
25227b0d0b40SStephen Smalley 		if (rc)
25237b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
25241da177e4SLinus Torvalds 	}
25251da177e4SLinus Torvalds 
252643af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
252743af5de7SVivek Goyal 	ad.u.file = bprm->file;
25281da177e4SLinus Torvalds 
2529a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
25306b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25316b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
25321da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
25331da177e4SLinus Torvalds 		if (rc)
25341da177e4SLinus Torvalds 			return rc;
25351da177e4SLinus Torvalds 	} else {
25361da177e4SLinus Torvalds 		/* Check permissions for the transition. */
25376b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25386b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
25391da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
25401da177e4SLinus Torvalds 		if (rc)
25411da177e4SLinus Torvalds 			return rc;
25421da177e4SLinus Torvalds 
25436b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25446b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
25451da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
25461da177e4SLinus Torvalds 		if (rc)
25471da177e4SLinus Torvalds 			return rc;
25481da177e4SLinus Torvalds 
2549a6f76f23SDavid Howells 		/* Check for shared state */
2550a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
25516b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
25526b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2553a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2554a6f76f23SDavid Howells 					  NULL);
2555a6f76f23SDavid Howells 			if (rc)
2556a6f76f23SDavid Howells 				return -EPERM;
25571da177e4SLinus Torvalds 		}
25581da177e4SLinus Torvalds 
2559a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2560a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
25619227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2562be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2563a6f76f23SDavid Howells 			if (ptsid != 0) {
25646b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
25656b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2566a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2567a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2568a6f76f23SDavid Howells 				if (rc)
2569a6f76f23SDavid Howells 					return -EPERM;
2570a6f76f23SDavid Howells 			}
2571a6f76f23SDavid Howells 		}
2572a6f76f23SDavid Howells 
2573a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2574a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2575a6f76f23SDavid Howells 
25761da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
25771da177e4SLinus Torvalds 		   the noatsecure permission is granted between
25781da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
25796b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
25806b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
258162874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
258262874c3aSKees Cook 				  NULL);
258362874c3aSKees Cook 		bprm->secureexec |= !!rc;
25841da177e4SLinus Torvalds 	}
25851da177e4SLinus Torvalds 
258662874c3aSKees Cook 	return 0;
25871da177e4SLinus Torvalds }
25881da177e4SLinus Torvalds 
2589c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2590c3c073f8SAl Viro {
2591c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2592c3c073f8SAl Viro }
2593c3c073f8SAl Viro 
25941da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2595745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2596745ca247SDavid Howells 					    struct files_struct *files)
25971da177e4SLinus Torvalds {
25981da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2599b20c8122SStephen Smalley 	struct tty_struct *tty;
260024ec839cSPeter Zijlstra 	int drop_tty = 0;
2601c3c073f8SAl Viro 	unsigned n;
26021da177e4SLinus Torvalds 
260324ec839cSPeter Zijlstra 	tty = get_current_tty();
26041da177e4SLinus Torvalds 	if (tty) {
26054a510969SPeter Hurley 		spin_lock(&tty->files_lock);
260637dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2607d996b62aSNick Piggin 			struct tty_file_private *file_priv;
260837dd0bd0SEric Paris 
26091da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
261013f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
261113f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
261213f8e981SDavid Howells 			   open file may belong to another process and we are
261313f8e981SDavid Howells 			   only interested in the inode-based check here. */
2614d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2615d996b62aSNick Piggin 						struct tty_file_private, list);
2616d996b62aSNick Piggin 			file = file_priv->file;
261713f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
261824ec839cSPeter Zijlstra 				drop_tty = 1;
26191da177e4SLinus Torvalds 		}
26204a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2621452a00d2SAlan Cox 		tty_kref_put(tty);
26221da177e4SLinus Torvalds 	}
262398a27ba4SEric W. Biederman 	/* Reset controlling tty. */
262498a27ba4SEric W. Biederman 	if (drop_tty)
262598a27ba4SEric W. Biederman 		no_tty();
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2628c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2629c3c073f8SAl Viro 	if (!n) /* none found? */
2630c3c073f8SAl Viro 		return;
26311da177e4SLinus Torvalds 
2632c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
263345525b26SAl Viro 	if (IS_ERR(devnull))
263445525b26SAl Viro 		devnull = NULL;
2635c3c073f8SAl Viro 	/* replace all the matching ones with this */
2636c3c073f8SAl Viro 	do {
263745525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2638c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
263945525b26SAl Viro 	if (devnull)
2640c3c073f8SAl Viro 		fput(devnull);
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds /*
2644a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
26451da177e4SLinus Torvalds  */
2646a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
26471da177e4SLinus Torvalds {
2648a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
26491da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
26501da177e4SLinus Torvalds 	int rc, i;
26511da177e4SLinus Torvalds 
2652a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2653a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
26541da177e4SLinus Torvalds 		return;
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2657a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
26581da177e4SLinus Torvalds 
2659a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2660a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2661a6f76f23SDavid Howells 
2662a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2663a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2664a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2665a6f76f23SDavid Howells 	 *
2666a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2667a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2668a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2669a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2670a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2671a6f76f23SDavid Howells 	 */
26726b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
26736b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2674a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2675a6f76f23SDavid Howells 	if (rc) {
2676eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2677eb2d55a3SOleg Nesterov 		task_lock(current);
2678a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2679a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2680a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2681a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2682a6f76f23SDavid Howells 		}
2683eb2d55a3SOleg Nesterov 		task_unlock(current);
2684baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2685eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2686a6f76f23SDavid Howells 	}
2687a6f76f23SDavid Howells }
2688a6f76f23SDavid Howells 
2689a6f76f23SDavid Howells /*
2690a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2691a6f76f23SDavid Howells  * due to exec
2692a6f76f23SDavid Howells  */
2693a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2694a6f76f23SDavid Howells {
2695a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2696a6f76f23SDavid Howells 	struct itimerval itimer;
2697a6f76f23SDavid Howells 	u32 osid, sid;
2698a6f76f23SDavid Howells 	int rc, i;
2699a6f76f23SDavid Howells 
2700a6f76f23SDavid Howells 	osid = tsec->osid;
2701a6f76f23SDavid Howells 	sid = tsec->sid;
2702a6f76f23SDavid Howells 
2703a6f76f23SDavid Howells 	if (sid == osid)
2704a6f76f23SDavid Howells 		return;
2705a6f76f23SDavid Howells 
2706a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2707a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2708a6f76f23SDavid Howells 	 * flush and unblock signals.
2709a6f76f23SDavid Howells 	 *
2710a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2711a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2712a6f76f23SDavid Howells 	 */
27136b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
27146b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
27151da177e4SLinus Torvalds 	if (rc) {
2716baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
27171da177e4SLinus Torvalds 			memset(&itimer, 0, sizeof itimer);
27181da177e4SLinus Torvalds 			for (i = 0; i < 3; i++)
27191da177e4SLinus Torvalds 				do_setitimer(i, &itimer, NULL);
2720baa73d9eSNicolas Pitre 		}
27211da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
27229e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
27239e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
27249e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
27251da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
27261da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
27279e7c8f8cSOleg Nesterov 			recalc_sigpending();
27283bcac026SDavid Howells 		}
27291da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds 
2732a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2733a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2734ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
27350b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2736ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds /* superblock security operations */
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
27421da177e4SLinus Torvalds {
27431da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
27471da177e4SLinus Torvalds {
27481da177e4SLinus Torvalds 	superblock_free_security(sb);
27491da177e4SLinus Torvalds }
27501da177e4SLinus Torvalds 
27511da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
27521da177e4SLinus Torvalds {
27531da177e4SLinus Torvalds 	if (plen > olen)
27541da177e4SLinus Torvalds 		return 0;
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
27601da177e4SLinus Torvalds {
2761832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2762832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2763832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
276411689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
276511689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
27691da177e4SLinus Torvalds {
27701da177e4SLinus Torvalds 	if (!*first) {
27711da177e4SLinus Torvalds 		**to = ',';
27721da177e4SLinus Torvalds 		*to += 1;
27733528a953SCory Olmo 	} else
27741da177e4SLinus Torvalds 		*first = 0;
27751da177e4SLinus Torvalds 	memcpy(*to, from, len);
27761da177e4SLinus Torvalds 	*to += len;
27771da177e4SLinus Torvalds }
27781da177e4SLinus Torvalds 
27793528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
27803528a953SCory Olmo 				       int len)
27813528a953SCory Olmo {
27823528a953SCory Olmo 	int current_size = 0;
27833528a953SCory Olmo 
27843528a953SCory Olmo 	if (!*first) {
27853528a953SCory Olmo 		**to = '|';
27863528a953SCory Olmo 		*to += 1;
2787828dfe1dSEric Paris 	} else
27883528a953SCory Olmo 		*first = 0;
27893528a953SCory Olmo 
27903528a953SCory Olmo 	while (current_size < len) {
27913528a953SCory Olmo 		if (*from != '"') {
27923528a953SCory Olmo 			**to = *from;
27933528a953SCory Olmo 			*to += 1;
27943528a953SCory Olmo 		}
27953528a953SCory Olmo 		from += 1;
27963528a953SCory Olmo 		current_size += 1;
27973528a953SCory Olmo 	}
27983528a953SCory Olmo }
27993528a953SCory Olmo 
2800e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
28011da177e4SLinus Torvalds {
28021da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
28031da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
28041da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
28053528a953SCory Olmo 	int open_quote = 0;
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds 	in_curr = orig;
28081da177e4SLinus Torvalds 	sec_curr = copy;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
28111da177e4SLinus Torvalds 	if (!nosec) {
28121da177e4SLinus Torvalds 		rc = -ENOMEM;
28131da177e4SLinus Torvalds 		goto out;
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	nosec_save = nosec;
28171da177e4SLinus Torvalds 	fnosec = fsec = 1;
28181da177e4SLinus Torvalds 	in_save = in_end = orig;
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	do {
28213528a953SCory Olmo 		if (*in_end == '"')
28223528a953SCory Olmo 			open_quote = !open_quote;
28233528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
28243528a953SCory Olmo 				*in_end == '\0') {
28251da177e4SLinus Torvalds 			int len = in_end - in_curr;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
28283528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
28291da177e4SLinus Torvalds 			else
28301da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 			in_curr = in_end + 1;
28331da177e4SLinus Torvalds 		}
28341da177e4SLinus Torvalds 	} while (*in_end++);
28351da177e4SLinus Torvalds 
28366931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2837da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
28381da177e4SLinus Torvalds out:
28391da177e4SLinus Torvalds 	return rc;
28401da177e4SLinus Torvalds }
28411da177e4SLinus Torvalds 
2842026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2843026eb167SEric Paris {
2844026eb167SEric Paris 	int rc, i, *flags;
2845026eb167SEric Paris 	struct security_mnt_opts opts;
2846026eb167SEric Paris 	char *secdata, **mount_options;
2847026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2848026eb167SEric Paris 
2849026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2850026eb167SEric Paris 		return 0;
2851026eb167SEric Paris 
2852026eb167SEric Paris 	if (!data)
2853026eb167SEric Paris 		return 0;
2854026eb167SEric Paris 
2855026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2856026eb167SEric Paris 		return 0;
2857026eb167SEric Paris 
2858026eb167SEric Paris 	security_init_mnt_opts(&opts);
2859026eb167SEric Paris 	secdata = alloc_secdata();
2860026eb167SEric Paris 	if (!secdata)
2861026eb167SEric Paris 		return -ENOMEM;
2862026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2863026eb167SEric Paris 	if (rc)
2864026eb167SEric Paris 		goto out_free_secdata;
2865026eb167SEric Paris 
2866026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2867026eb167SEric Paris 	if (rc)
2868026eb167SEric Paris 		goto out_free_secdata;
2869026eb167SEric Paris 
2870026eb167SEric Paris 	mount_options = opts.mnt_opts;
2871026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2872026eb167SEric Paris 
2873026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2874026eb167SEric Paris 		u32 sid;
2875026eb167SEric Paris 
287612f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
2877026eb167SEric Paris 			continue;
2878aa8e712cSStephen Smalley 		rc = security_context_str_to_sid(&selinux_state,
2879aa8e712cSStephen Smalley 						 mount_options[i], &sid,
2880aa8e712cSStephen Smalley 						 GFP_KERNEL);
2881026eb167SEric Paris 		if (rc) {
2882c103a91eSpeter enderborg 			pr_warn("SELinux: security_context_str_to_sid"
288329b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
288429b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2885026eb167SEric Paris 			goto out_free_opts;
2886026eb167SEric Paris 		}
2887026eb167SEric Paris 		rc = -EINVAL;
2888026eb167SEric Paris 		switch (flags[i]) {
2889026eb167SEric Paris 		case FSCONTEXT_MNT:
2890026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2891026eb167SEric Paris 				goto out_bad_option;
2892026eb167SEric Paris 			break;
2893026eb167SEric Paris 		case CONTEXT_MNT:
2894026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2895026eb167SEric Paris 				goto out_bad_option;
2896026eb167SEric Paris 			break;
2897026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2898026eb167SEric Paris 			struct inode_security_struct *root_isec;
289983da53c5SAndreas Gruenbacher 			root_isec = backing_inode_security(sb->s_root);
2900026eb167SEric Paris 
2901026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2902026eb167SEric Paris 				goto out_bad_option;
2903026eb167SEric Paris 			break;
2904026eb167SEric Paris 		}
2905026eb167SEric Paris 		case DEFCONTEXT_MNT:
2906026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2907026eb167SEric Paris 				goto out_bad_option;
2908026eb167SEric Paris 			break;
2909026eb167SEric Paris 		default:
2910026eb167SEric Paris 			goto out_free_opts;
2911026eb167SEric Paris 		}
2912026eb167SEric Paris 	}
2913026eb167SEric Paris 
2914026eb167SEric Paris 	rc = 0;
2915026eb167SEric Paris out_free_opts:
2916026eb167SEric Paris 	security_free_mnt_opts(&opts);
2917026eb167SEric Paris out_free_secdata:
2918026eb167SEric Paris 	free_secdata(secdata);
2919026eb167SEric Paris 	return rc;
2920026eb167SEric Paris out_bad_option:
2921c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
292229b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
292329b1deb2SLinus Torvalds 	       sb->s_type->name);
2924026eb167SEric Paris 	goto out_free_opts;
2925026eb167SEric Paris }
2926026eb167SEric Paris 
292712204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
29281da177e4SLinus Torvalds {
292988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29302bf49690SThomas Liu 	struct common_audit_data ad;
29311da177e4SLinus Torvalds 	int rc;
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
29341da177e4SLinus Torvalds 	if (rc)
29351da177e4SLinus Torvalds 		return rc;
29361da177e4SLinus Torvalds 
293774192246SJames Morris 	/* Allow all mounts performed by the kernel */
293874192246SJames Morris 	if (flags & MS_KERNMOUNT)
293974192246SJames Morris 		return 0;
294074192246SJames Morris 
294150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2942a269434dSEric Paris 	ad.u.dentry = sb->s_root;
294388e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
29441da177e4SLinus Torvalds }
29451da177e4SLinus Torvalds 
2946726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
29471da177e4SLinus Torvalds {
294888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29492bf49690SThomas Liu 	struct common_audit_data ad;
29501da177e4SLinus Torvalds 
295150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2952a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
295388e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
29541da177e4SLinus Torvalds }
29551da177e4SLinus Torvalds 
2956808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
29578a04c43bSAl Viro 			 const struct path *path,
2958808d4e3cSAl Viro 			 const char *type,
29591da177e4SLinus Torvalds 			 unsigned long flags,
29601da177e4SLinus Torvalds 			 void *data)
29611da177e4SLinus Torvalds {
296288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2965d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
29661da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
29671da177e4SLinus Torvalds 	else
29682875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
29721da177e4SLinus Torvalds {
297388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29741da177e4SLinus Torvalds 
297588e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
29761da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds /* inode security operations */
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
29821da177e4SLinus Torvalds {
29831da177e4SLinus Torvalds 	return inode_alloc_security(inode);
29841da177e4SLinus Torvalds }
29851da177e4SLinus Torvalds 
29861da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
29871da177e4SLinus Torvalds {
29881da177e4SLinus Torvalds 	inode_free_security(inode);
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
2991d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
29924f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2993d47be3dfSDavid Quigley 					u32 *ctxlen)
2994d47be3dfSDavid Quigley {
2995d47be3dfSDavid Quigley 	u32 newsid;
2996d47be3dfSDavid Quigley 	int rc;
2997d47be3dfSDavid Quigley 
2998c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2999c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
3000d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
3001d47be3dfSDavid Quigley 					   &newsid);
3002c3c188b2SDavid Howells 	if (rc)
3003d47be3dfSDavid Quigley 		return rc;
3004d47be3dfSDavid Quigley 
3005aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
3006aa8e712cSStephen Smalley 				       ctxlen);
3007d47be3dfSDavid Quigley }
3008d47be3dfSDavid Quigley 
3009a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
3010a518b0a5SVivek Goyal 					  struct qstr *name,
3011a518b0a5SVivek Goyal 					  const struct cred *old,
3012a518b0a5SVivek Goyal 					  struct cred *new)
3013a518b0a5SVivek Goyal {
3014a518b0a5SVivek Goyal 	u32 newsid;
3015a518b0a5SVivek Goyal 	int rc;
3016a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
3017a518b0a5SVivek Goyal 
3018a518b0a5SVivek Goyal 	rc = selinux_determine_inode_label(old->security,
3019a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
3020a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
3021a518b0a5SVivek Goyal 					   &newsid);
3022a518b0a5SVivek Goyal 	if (rc)
3023a518b0a5SVivek Goyal 		return rc;
3024a518b0a5SVivek Goyal 
3025a518b0a5SVivek Goyal 	tsec = new->security;
3026a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
3027a518b0a5SVivek Goyal 	return 0;
3028a518b0a5SVivek Goyal }
3029a518b0a5SVivek Goyal 
30305e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
30319548906bSTetsuo Handa 				       const struct qstr *qstr,
30329548906bSTetsuo Handa 				       const char **name,
30332a7dba39SEric Paris 				       void **value, size_t *len)
30345e41ff9eSStephen Smalley {
30355fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
30365e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
3037c0d4f464SCorentin LABBE 	u32 newsid, clen;
30385e41ff9eSStephen Smalley 	int rc;
30399548906bSTetsuo Handa 	char *context;
30405e41ff9eSStephen Smalley 
30415e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
30425e41ff9eSStephen Smalley 
30435e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
3044275bb41eSDavid Howells 
3045c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
3046c3c188b2SDavid Howells 		dir, qstr,
30475e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
3048c3c188b2SDavid Howells 		&newsid);
3049c3c188b2SDavid Howells 	if (rc)
30505e41ff9eSStephen Smalley 		return rc;
30515e41ff9eSStephen Smalley 
3052296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
30530d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
3054296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
3055296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
3056296fddf7SEric Paris 		isec->sid = newsid;
30576f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
3058296fddf7SEric Paris 	}
30595e41ff9eSStephen Smalley 
3060aa8e712cSStephen Smalley 	if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
306125a74f3bSStephen Smalley 		return -EOPNOTSUPP;
306225a74f3bSStephen Smalley 
30639548906bSTetsuo Handa 	if (name)
30649548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
30655e41ff9eSStephen Smalley 
3066570bc1c2SStephen Smalley 	if (value && len) {
3067aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
3068aa8e712cSStephen Smalley 						   &context, &clen);
30699548906bSTetsuo Handa 		if (rc)
30705e41ff9eSStephen Smalley 			return rc;
30715e41ff9eSStephen Smalley 		*value = context;
3072570bc1c2SStephen Smalley 		*len = clen;
3073570bc1c2SStephen Smalley 	}
30745e41ff9eSStephen Smalley 
30755e41ff9eSStephen Smalley 	return 0;
30765e41ff9eSStephen Smalley }
30775e41ff9eSStephen Smalley 
30784acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
30791da177e4SLinus Torvalds {
30801da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
30811da177e4SLinus Torvalds }
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
30861da177e4SLinus Torvalds }
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
30891da177e4SLinus Torvalds {
30901da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
30911da177e4SLinus Torvalds }
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
30941da177e4SLinus Torvalds {
30951da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
309818bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
30991da177e4SLinus Torvalds {
31001da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
31011da177e4SLinus Torvalds }
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
31041da177e4SLinus Torvalds {
31051da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
31081a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
31091da177e4SLinus Torvalds {
31101da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
31141da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
31151da177e4SLinus Torvalds {
31161da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
31171da177e4SLinus Torvalds }
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
31201da177e4SLinus Torvalds {
312188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
312288e67f3bSDavid Howells 
31232875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds 
3126bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3127bda0be7aSNeilBrown 				     bool rcu)
31281da177e4SLinus Torvalds {
312988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3130bda0be7aSNeilBrown 	struct common_audit_data ad;
3131bda0be7aSNeilBrown 	struct inode_security_struct *isec;
3132bda0be7aSNeilBrown 	u32 sid;
31331da177e4SLinus Torvalds 
3134bda0be7aSNeilBrown 	validate_creds(cred);
3135bda0be7aSNeilBrown 
3136bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
3137bda0be7aSNeilBrown 	ad.u.dentry = dentry;
3138bda0be7aSNeilBrown 	sid = cred_sid(cred);
31395d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
31405d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31415d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
3142bda0be7aSNeilBrown 
31436b6bc620SStephen Smalley 	return avc_has_perm_flags(&selinux_state,
31446b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
3145bda0be7aSNeilBrown 				  rcu ? MAY_NOT_BLOCK : 0);
31461da177e4SLinus Torvalds }
31471da177e4SLinus Torvalds 
3148d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
3149d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
3150626b9740SStephen Smalley 					   int result,
3151d4cf970dSEric Paris 					   unsigned flags)
3152d4cf970dSEric Paris {
3153d4cf970dSEric Paris 	struct common_audit_data ad;
3154d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
3155d4cf970dSEric Paris 	int rc;
3156d4cf970dSEric Paris 
315750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
3158d4cf970dSEric Paris 	ad.u.inode = inode;
3159d4cf970dSEric Paris 
31606b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
31616b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
3162626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
3163d4cf970dSEric Paris 	if (rc)
3164d4cf970dSEric Paris 		return rc;
3165d4cf970dSEric Paris 	return 0;
3166d4cf970dSEric Paris }
3167d4cf970dSEric Paris 
3168e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
31691da177e4SLinus Torvalds {
317088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3171b782e0a6SEric Paris 	u32 perms;
3172b782e0a6SEric Paris 	bool from_access;
3173cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
31742e334057SEric Paris 	struct inode_security_struct *isec;
31752e334057SEric Paris 	u32 sid;
31762e334057SEric Paris 	struct av_decision avd;
31772e334057SEric Paris 	int rc, rc2;
31782e334057SEric Paris 	u32 audited, denied;
31791da177e4SLinus Torvalds 
3180b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
3181d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3182d09ca739SEric Paris 
31831da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
3184b782e0a6SEric Paris 	if (!mask)
31851da177e4SLinus Torvalds 		return 0;
31861da177e4SLinus Torvalds 
31872e334057SEric Paris 	validate_creds(cred);
3188b782e0a6SEric Paris 
31892e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
31902e334057SEric Paris 		return 0;
3191b782e0a6SEric Paris 
3192b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
3193b782e0a6SEric Paris 
31942e334057SEric Paris 	sid = cred_sid(cred);
31955d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
31965d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
31975d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
31982e334057SEric Paris 
31996b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
32006b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, perms, 0, &avd);
32012e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
32022e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
32032e334057SEric Paris 				     &denied);
32042e334057SEric Paris 	if (likely(!audited))
32052e334057SEric Paris 		return rc;
32062e334057SEric Paris 
3207626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
32082e334057SEric Paris 	if (rc2)
32092e334057SEric Paris 		return rc2;
32102e334057SEric Paris 	return rc;
32111da177e4SLinus Torvalds }
32121da177e4SLinus Torvalds 
32131da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
32141da177e4SLinus Torvalds {
321588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3216ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
3217bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
321895dbf739SEric Paris 	__u32 av = FILE__WRITE;
32191da177e4SLinus Torvalds 
3220bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3221bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3222bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3223bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3224bc6a6008SAmerigo Wang 		if (!ia_valid)
32251da177e4SLinus Torvalds 			return 0;
3226bc6a6008SAmerigo Wang 	}
32271da177e4SLinus Torvalds 
3228bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3229bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
32302875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
32311da177e4SLinus Torvalds 
3232aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3233ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3234ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3235ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
323695dbf739SEric Paris 		av |= FILE__OPEN;
323795dbf739SEric Paris 
323895dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
32413f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
32421da177e4SLinus Torvalds {
32433f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds 
3246db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3247db59000aSStephen Smalley {
3248db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3249db59000aSStephen Smalley 	int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3250db59000aSStephen Smalley 
3251db59000aSStephen Smalley 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3252db59000aSStephen Smalley 		return false;
3253db59000aSStephen Smalley 	if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3254db59000aSStephen Smalley 		return false;
3255db59000aSStephen Smalley 	return true;
3256db59000aSStephen Smalley }
3257db59000aSStephen Smalley 
32588f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
32598f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
32601da177e4SLinus Torvalds {
3261c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
326220cdef8dSPaul Moore 	struct inode_security_struct *isec;
32631da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
32642bf49690SThomas Liu 	struct common_audit_data ad;
3265275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
32661da177e4SLinus Torvalds 	int rc = 0;
32671da177e4SLinus Torvalds 
32686b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
32696b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
32706b240306SEric W. Biederman 		if (rc)
32716b240306SEric W. Biederman 			return rc;
32726b240306SEric W. Biederman 
32736b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
32746b240306SEric W. Biederman 		   ordinary setattr permission. */
32756b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32766b240306SEric W. Biederman 	}
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
327912f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
32801da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32811da177e4SLinus Torvalds 
32822e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
32831da177e4SLinus Torvalds 		return -EPERM;
32841da177e4SLinus Torvalds 
328550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3286a269434dSEric Paris 	ad.u.dentry = dentry;
32871da177e4SLinus Torvalds 
328820cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
32896b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
32906b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
32911da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
32921da177e4SLinus Torvalds 	if (rc)
32931da177e4SLinus Torvalds 		return rc;
32941da177e4SLinus Torvalds 
3295aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3296aa8e712cSStephen Smalley 				     GFP_KERNEL);
329712b29f34SStephen Smalley 	if (rc == -EINVAL) {
3298db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3299d6ea83ecSEric Paris 			struct audit_buffer *ab;
3300d6ea83ecSEric Paris 			size_t audit_size;
3301d6ea83ecSEric Paris 
3302d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3303d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3304e3fea3f7SAl Viro 			if (value) {
3305add24372SColin Ian King 				const char *str = value;
3306add24372SColin Ian King 
3307d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3308d6ea83ecSEric Paris 					audit_size = size - 1;
3309d6ea83ecSEric Paris 				else
3310d6ea83ecSEric Paris 					audit_size = size;
3311e3fea3f7SAl Viro 			} else {
3312e3fea3f7SAl Viro 				audit_size = 0;
3313e3fea3f7SAl Viro 			}
3314cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3315cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3316d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3317d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3318d6ea83ecSEric Paris 			audit_log_end(ab);
3319d6ea83ecSEric Paris 
332012b29f34SStephen Smalley 			return rc;
3321d6ea83ecSEric Paris 		}
3322aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3323aa8e712cSStephen Smalley 						   size, &newsid);
332412b29f34SStephen Smalley 	}
33251da177e4SLinus Torvalds 	if (rc)
33261da177e4SLinus Torvalds 		return rc;
33271da177e4SLinus Torvalds 
33286b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
33296b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
33301da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
33311da177e4SLinus Torvalds 	if (rc)
33321da177e4SLinus Torvalds 		return rc;
33331da177e4SLinus Torvalds 
3334aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3335aa8e712cSStephen Smalley 					  sid, isec->sclass);
33361da177e4SLinus Torvalds 	if (rc)
33371da177e4SLinus Torvalds 		return rc;
33381da177e4SLinus Torvalds 
33396b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
33406b6bc620SStephen Smalley 			    newsid,
33411da177e4SLinus Torvalds 			    sbsec->sid,
33421da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
33431da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
33441da177e4SLinus Torvalds 			    &ad);
33451da177e4SLinus Torvalds }
33461da177e4SLinus Torvalds 
33478f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
33488f0cfa52SDavid Howells 					const void *value, size_t size,
33498f0cfa52SDavid Howells 					int flags)
33501da177e4SLinus Torvalds {
3351c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
335220cdef8dSPaul Moore 	struct inode_security_struct *isec;
33531da177e4SLinus Torvalds 	u32 newsid;
33541da177e4SLinus Torvalds 	int rc;
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33571da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
33581da177e4SLinus Torvalds 		return;
33591da177e4SLinus Torvalds 	}
33601da177e4SLinus Torvalds 
3361aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3362aa8e712cSStephen Smalley 					   &newsid);
33631da177e4SLinus Torvalds 	if (rc) {
3364c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
336512b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
336612b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
33671da177e4SLinus Torvalds 		return;
33681da177e4SLinus Torvalds 	}
33691da177e4SLinus Torvalds 
337020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
33719287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3372aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
33731da177e4SLinus Torvalds 	isec->sid = newsid;
33746f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
33759287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3376aa9c2669SDavid Quigley 
33771da177e4SLinus Torvalds 	return;
33781da177e4SLinus Torvalds }
33791da177e4SLinus Torvalds 
33808f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
33811da177e4SLinus Torvalds {
338288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
338388e67f3bSDavid Howells 
33842875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33851da177e4SLinus Torvalds }
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
33881da177e4SLinus Torvalds {
338988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
339088e67f3bSDavid Howells 
33912875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
33921da177e4SLinus Torvalds }
33931da177e4SLinus Torvalds 
33948f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
33951da177e4SLinus Torvalds {
33966b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
33976b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
33986b240306SEric W. Biederman 		if (rc)
33996b240306SEric W. Biederman 			return rc;
34006b240306SEric W. Biederman 
34016b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
34026b240306SEric W. Biederman 		   ordinary setattr permission. */
34036b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
34046b240306SEric W. Biederman 	}
34051da177e4SLinus Torvalds 
34061da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
34071da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
34081da177e4SLinus Torvalds 	return -EACCES;
34091da177e4SLinus Torvalds }
34101da177e4SLinus Torvalds 
3411d381d8a9SJames Morris /*
3412abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3413d381d8a9SJames Morris  *
3414d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3415d381d8a9SJames Morris  */
3416ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
34171da177e4SLinus Torvalds {
341842492594SDavid P. Quigley 	u32 size;
341942492594SDavid P. Quigley 	int error;
342042492594SDavid P. Quigley 	char *context = NULL;
342120cdef8dSPaul Moore 	struct inode_security_struct *isec;
34221da177e4SLinus Torvalds 
34238c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34248c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
34251da177e4SLinus Torvalds 
3426abc69bb6SStephen Smalley 	/*
3427abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3428abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3429abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3430abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3431abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3432abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3433abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3434abc69bb6SStephen Smalley 	 */
343520cdef8dSPaul Moore 	isec = inode_security(inode);
3436db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3437aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3438aa8e712cSStephen Smalley 						      isec->sid, &context,
3439abc69bb6SStephen Smalley 						      &size);
3440abc69bb6SStephen Smalley 	else
3441aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3442aa8e712cSStephen Smalley 						&context, &size);
344342492594SDavid P. Quigley 	if (error)
344442492594SDavid P. Quigley 		return error;
344542492594SDavid P. Quigley 	error = size;
344642492594SDavid P. Quigley 	if (alloc) {
344742492594SDavid P. Quigley 		*buffer = context;
344842492594SDavid P. Quigley 		goto out_nofree;
344942492594SDavid P. Quigley 	}
345042492594SDavid P. Quigley 	kfree(context);
345142492594SDavid P. Quigley out_nofree:
345242492594SDavid P. Quigley 	return error;
34531da177e4SLinus Torvalds }
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
34561da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
34571da177e4SLinus Torvalds {
34582c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
34591da177e4SLinus Torvalds 	u32 newsid;
34601da177e4SLinus Torvalds 	int rc;
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
34631da177e4SLinus Torvalds 		return -EOPNOTSUPP;
34641da177e4SLinus Torvalds 
34651da177e4SLinus Torvalds 	if (!value || !size)
34661da177e4SLinus Torvalds 		return -EACCES;
34671da177e4SLinus Torvalds 
3468aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3469aa8e712cSStephen Smalley 				     GFP_KERNEL);
34701da177e4SLinus Torvalds 	if (rc)
34711da177e4SLinus Torvalds 		return rc;
34721da177e4SLinus Torvalds 
34739287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3474aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
34751da177e4SLinus Torvalds 	isec->sid = newsid;
34766f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
34779287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
34781da177e4SLinus Torvalds 	return 0;
34791da177e4SLinus Torvalds }
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
34821da177e4SLinus Torvalds {
34831da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
34841da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
34851da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
34861da177e4SLinus Torvalds 	return len;
34871da177e4SLinus Torvalds }
34881da177e4SLinus Torvalds 
3489d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3490713a04aeSAhmed S. Darwish {
3491e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3492713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3493713a04aeSAhmed S. Darwish }
3494713a04aeSAhmed S. Darwish 
349556909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
349656909eb3SVivek Goyal {
349756909eb3SVivek Goyal 	u32 sid;
349856909eb3SVivek Goyal 	struct task_security_struct *tsec;
349956909eb3SVivek Goyal 	struct cred *new_creds = *new;
350056909eb3SVivek Goyal 
350156909eb3SVivek Goyal 	if (new_creds == NULL) {
350256909eb3SVivek Goyal 		new_creds = prepare_creds();
350356909eb3SVivek Goyal 		if (!new_creds)
350456909eb3SVivek Goyal 			return -ENOMEM;
350556909eb3SVivek Goyal 	}
350656909eb3SVivek Goyal 
350756909eb3SVivek Goyal 	tsec = new_creds->security;
350856909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
350956909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
351056909eb3SVivek Goyal 	tsec->create_sid = sid;
351156909eb3SVivek Goyal 	*new = new_creds;
351256909eb3SVivek Goyal 	return 0;
351356909eb3SVivek Goyal }
351456909eb3SVivek Goyal 
351519472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
351619472b69SVivek Goyal {
351719472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
351819472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
351919472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
352019472b69SVivek Goyal 	 */
352119472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
352219472b69SVivek Goyal 		return 1; /* Discard */
352319472b69SVivek Goyal 	/*
352419472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
352519472b69SVivek Goyal 	 * by selinux.
352619472b69SVivek Goyal 	 */
352719472b69SVivek Goyal 	return -EOPNOTSUPP;
352819472b69SVivek Goyal }
352919472b69SVivek Goyal 
35301da177e4SLinus Torvalds /* file security operations */
35311da177e4SLinus Torvalds 
3532788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
35331da177e4SLinus Torvalds {
353488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3535496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
35381da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
35391da177e4SLinus Torvalds 		mask |= MAY_APPEND;
35401da177e4SLinus Torvalds 
3541389fb800SPaul Moore 	return file_has_perm(cred, file,
35421da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
35431da177e4SLinus Torvalds }
35441da177e4SLinus Torvalds 
3545788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3546788e7dd4SYuichi Nakamura {
3547496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
354820dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
3549b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
355020dda18bSStephen Smalley 	u32 sid = current_sid();
355120dda18bSStephen Smalley 
3552389fb800SPaul Moore 	if (!mask)
3553788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3554788e7dd4SYuichi Nakamura 		return 0;
3555788e7dd4SYuichi Nakamura 
3556b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
355720dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
35586b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
355983d49856SEric Paris 		/* No change since file_open check. */
356020dda18bSStephen Smalley 		return 0;
356120dda18bSStephen Smalley 
3562788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3563788e7dd4SYuichi Nakamura }
3564788e7dd4SYuichi Nakamura 
35651da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
35661da177e4SLinus Torvalds {
35671da177e4SLinus Torvalds 	return file_alloc_security(file);
35681da177e4SLinus Torvalds }
35691da177e4SLinus Torvalds 
35701da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
35711da177e4SLinus Torvalds {
35721da177e4SLinus Torvalds 	file_free_security(file);
35731da177e4SLinus Torvalds }
35741da177e4SLinus Torvalds 
3575fa1aa143SJeff Vander Stoep /*
3576fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3577fa1aa143SJeff Vander Stoep  * operation to an inode.
3578fa1aa143SJeff Vander Stoep  */
35791d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3580fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3581fa1aa143SJeff Vander Stoep {
3582fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3583fa1aa143SJeff Vander Stoep 	struct file_security_struct *fsec = file->f_security;
3584fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
358520cdef8dSPaul Moore 	struct inode_security_struct *isec;
3586fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3587fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3588fa1aa143SJeff Vander Stoep 	int rc;
3589fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3590fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3591fa1aa143SJeff Vander Stoep 
3592fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3593fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3594fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3595fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3596fa1aa143SJeff Vander Stoep 
3597fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
35986b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
35996b6bc620SStephen Smalley 				  ssid, fsec->sid,
3600fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3601fa1aa143SJeff Vander Stoep 				FD__USE,
3602fa1aa143SJeff Vander Stoep 				&ad);
3603fa1aa143SJeff Vander Stoep 		if (rc)
3604fa1aa143SJeff Vander Stoep 			goto out;
3605fa1aa143SJeff Vander Stoep 	}
3606fa1aa143SJeff Vander Stoep 
3607fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3608fa1aa143SJeff Vander Stoep 		return 0;
3609fa1aa143SJeff Vander Stoep 
361020cdef8dSPaul Moore 	isec = inode_security(inode);
36116b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
36126b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3613fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3614fa1aa143SJeff Vander Stoep out:
3615fa1aa143SJeff Vander Stoep 	return rc;
3616fa1aa143SJeff Vander Stoep }
3617fa1aa143SJeff Vander Stoep 
36181da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
36191da177e4SLinus Torvalds 			      unsigned long arg)
36201da177e4SLinus Torvalds {
362188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
36220b24dcb7SEric Paris 	int error = 0;
36231da177e4SLinus Torvalds 
36240b24dcb7SEric Paris 	switch (cmd) {
36250b24dcb7SEric Paris 	case FIONREAD:
36260b24dcb7SEric Paris 	/* fall through */
36270b24dcb7SEric Paris 	case FIBMAP:
36280b24dcb7SEric Paris 	/* fall through */
36290b24dcb7SEric Paris 	case FIGETBSZ:
36300b24dcb7SEric Paris 	/* fall through */
36312f99c369SAl Viro 	case FS_IOC_GETFLAGS:
36320b24dcb7SEric Paris 	/* fall through */
36332f99c369SAl Viro 	case FS_IOC_GETVERSION:
36340b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
36350b24dcb7SEric Paris 		break;
36361da177e4SLinus Torvalds 
36372f99c369SAl Viro 	case FS_IOC_SETFLAGS:
36380b24dcb7SEric Paris 	/* fall through */
36392f99c369SAl Viro 	case FS_IOC_SETVERSION:
36400b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
36410b24dcb7SEric Paris 		break;
36420b24dcb7SEric Paris 
36430b24dcb7SEric Paris 	/* sys_ioctl() checks */
36440b24dcb7SEric Paris 	case FIONBIO:
36450b24dcb7SEric Paris 	/* fall through */
36460b24dcb7SEric Paris 	case FIOASYNC:
36470b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
36480b24dcb7SEric Paris 		break;
36490b24dcb7SEric Paris 
36500b24dcb7SEric Paris 	case KDSKBENT:
36510b24dcb7SEric Paris 	case KDSKBSENT:
36526a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
36538e4ff6f2SStephen Smalley 					    SECURITY_CAP_AUDIT, true);
36540b24dcb7SEric Paris 		break;
36550b24dcb7SEric Paris 
36560b24dcb7SEric Paris 	/* default case assumes that the command will go
36570b24dcb7SEric Paris 	 * to the file's ioctl() function.
36580b24dcb7SEric Paris 	 */
36590b24dcb7SEric Paris 	default:
3660fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
36610b24dcb7SEric Paris 	}
36620b24dcb7SEric Paris 	return error;
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
3665fcaaade1SStephen Smalley static int default_noexec;
3666fcaaade1SStephen Smalley 
36671da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
36681da177e4SLinus Torvalds {
366988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3670be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3671d84f4f99SDavid Howells 	int rc = 0;
367288e67f3bSDavid Howells 
3673fcaaade1SStephen Smalley 	if (default_noexec &&
3674892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3675892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
36761da177e4SLinus Torvalds 		/*
36771da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
36781da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
36791da177e4SLinus Torvalds 		 * This has an additional check.
36801da177e4SLinus Torvalds 		 */
36816b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
36826b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3683be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
36841da177e4SLinus Torvalds 		if (rc)
3685d84f4f99SDavid Howells 			goto error;
36861da177e4SLinus Torvalds 	}
36871da177e4SLinus Torvalds 
36881da177e4SLinus Torvalds 	if (file) {
36891da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
36901da177e4SLinus Torvalds 		u32 av = FILE__READ;
36911da177e4SLinus Torvalds 
36921da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
36931da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
36941da177e4SLinus Torvalds 			av |= FILE__WRITE;
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
36971da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
36981da177e4SLinus Torvalds 
369988e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
37001da177e4SLinus Torvalds 	}
3701d84f4f99SDavid Howells 
3702d84f4f99SDavid Howells error:
3703d84f4f99SDavid Howells 	return rc;
37041da177e4SLinus Torvalds }
37051da177e4SLinus Torvalds 
3706e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
37071da177e4SLinus Torvalds {
3708b1d9e6b0SCasey Schaufler 	int rc = 0;
370998883bfdSPaul Moore 
371098883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
371198883bfdSPaul Moore 		u32 sid = current_sid();
37126b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
37136b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
371498883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
371598883bfdSPaul Moore 	}
371698883bfdSPaul Moore 
371798883bfdSPaul Moore 	return rc;
3718e5467859SAl Viro }
37191da177e4SLinus Torvalds 
3720e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3721e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3722e5467859SAl Viro {
37233ba4bf5fSStephen Smalley 	struct common_audit_data ad;
37243ba4bf5fSStephen Smalley 	int rc;
37253ba4bf5fSStephen Smalley 
37263ba4bf5fSStephen Smalley 	if (file) {
37273ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
37283ba4bf5fSStephen Smalley 		ad.u.file = file;
37293ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
37303ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
37313ba4bf5fSStephen Smalley 		if (rc)
37323ba4bf5fSStephen Smalley 			return rc;
37333ba4bf5fSStephen Smalley 	}
37343ba4bf5fSStephen Smalley 
3735aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37361da177e4SLinus Torvalds 		prot = reqprot;
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
37391da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
37401da177e4SLinus Torvalds }
37411da177e4SLinus Torvalds 
37421da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
37431da177e4SLinus Torvalds 				 unsigned long reqprot,
37441da177e4SLinus Torvalds 				 unsigned long prot)
37451da177e4SLinus Torvalds {
374688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3747be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
37481da177e4SLinus Torvalds 
3749aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
37501da177e4SLinus Torvalds 		prot = reqprot;
37511da177e4SLinus Torvalds 
3752fcaaade1SStephen Smalley 	if (default_noexec &&
3753fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3754d541bbeeSJames Morris 		int rc = 0;
3755db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3756db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
37576b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37586b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3759be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3760db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3761c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3762c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3763d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
37646b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
37656b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3766be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3767db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3768db4c9641SStephen Smalley 			/*
3769db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3770db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3771db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3772db4c9641SStephen Smalley 			 * modified content.  This typically should only
3773db4c9641SStephen Smalley 			 * occur for text relocations.
3774db4c9641SStephen Smalley 			 */
3775d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3776db4c9641SStephen Smalley 		}
37776b992197SLorenzo Hernandez García-Hierro 		if (rc)
37786b992197SLorenzo Hernandez García-Hierro 			return rc;
37796b992197SLorenzo Hernandez García-Hierro 	}
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
37821da177e4SLinus Torvalds }
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
37851da177e4SLinus Torvalds {
378688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
378788e67f3bSDavid Howells 
378888e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
37891da177e4SLinus Torvalds }
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
37921da177e4SLinus Torvalds 			      unsigned long arg)
37931da177e4SLinus Torvalds {
379488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
37951da177e4SLinus Torvalds 	int err = 0;
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	switch (cmd) {
37981da177e4SLinus Torvalds 	case F_SETFL:
37991da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
380088e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
38011da177e4SLinus Torvalds 			break;
38021da177e4SLinus Torvalds 		}
38031da177e4SLinus Torvalds 		/* fall through */
38041da177e4SLinus Torvalds 	case F_SETOWN:
38051da177e4SLinus Torvalds 	case F_SETSIG:
38061da177e4SLinus Torvalds 	case F_GETFL:
38071da177e4SLinus Torvalds 	case F_GETOWN:
38081da177e4SLinus Torvalds 	case F_GETSIG:
38091d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
38101da177e4SLinus Torvalds 		/* Just check FD__USE permission */
381188e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
38121da177e4SLinus Torvalds 		break;
38131da177e4SLinus Torvalds 	case F_GETLK:
38141da177e4SLinus Torvalds 	case F_SETLK:
38151da177e4SLinus Torvalds 	case F_SETLKW:
38160d3f7a2dSJeff Layton 	case F_OFD_GETLK:
38170d3f7a2dSJeff Layton 	case F_OFD_SETLK:
38180d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
38191da177e4SLinus Torvalds #if BITS_PER_LONG == 32
38201da177e4SLinus Torvalds 	case F_GETLK64:
38211da177e4SLinus Torvalds 	case F_SETLK64:
38221da177e4SLinus Torvalds 	case F_SETLKW64:
38231da177e4SLinus Torvalds #endif
382488e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
38251da177e4SLinus Torvalds 		break;
38261da177e4SLinus Torvalds 	}
38271da177e4SLinus Torvalds 
38281da177e4SLinus Torvalds 	return err;
38291da177e4SLinus Torvalds }
38301da177e4SLinus Torvalds 
3831e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
38321da177e4SLinus Torvalds {
38331da177e4SLinus Torvalds 	struct file_security_struct *fsec;
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds 	fsec = file->f_security;
3836275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
38371da177e4SLinus Torvalds }
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
38401da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
38411da177e4SLinus Torvalds {
38421da177e4SLinus Torvalds 	struct file *file;
384365c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
38441da177e4SLinus Torvalds 	u32 perm;
38451da177e4SLinus Torvalds 	struct file_security_struct *fsec;
38461da177e4SLinus Torvalds 
38471da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3848b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
38491da177e4SLinus Torvalds 
38501da177e4SLinus Torvalds 	fsec = file->f_security;
38511da177e4SLinus Torvalds 
38521da177e4SLinus Torvalds 	if (!signum)
38531da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
38541da177e4SLinus Torvalds 	else
38551da177e4SLinus Torvalds 		perm = signal_to_av(signum);
38561da177e4SLinus Torvalds 
38576b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38586b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
38591da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
38601da177e4SLinus Torvalds }
38611da177e4SLinus Torvalds 
38621da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
38631da177e4SLinus Torvalds {
386488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
386588e67f3bSDavid Howells 
386688e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
38671da177e4SLinus Torvalds }
38681da177e4SLinus Torvalds 
386994817692SAl Viro static int selinux_file_open(struct file *file)
3870788e7dd4SYuichi Nakamura {
3871788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3872788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3873d84f4f99SDavid Howells 
3874788e7dd4SYuichi Nakamura 	fsec = file->f_security;
387583da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3876788e7dd4SYuichi Nakamura 	/*
3877788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3878788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3879788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3880788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3881788e7dd4SYuichi Nakamura 	 * struct as its SID.
3882788e7dd4SYuichi Nakamura 	 */
3883788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
38846b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3885788e7dd4SYuichi Nakamura 	/*
3886788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3887788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3888788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3889788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3890788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3891788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3892788e7dd4SYuichi Nakamura 	 */
389394817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3894788e7dd4SYuichi Nakamura }
3895788e7dd4SYuichi Nakamura 
38961da177e4SLinus Torvalds /* task security operations */
38971da177e4SLinus Torvalds 
3898a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3899a79be238STetsuo Handa 			      unsigned long clone_flags)
39001da177e4SLinus Torvalds {
3901be0554c9SStephen Smalley 	u32 sid = current_sid();
3902be0554c9SStephen Smalley 
39036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39046b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
39051da177e4SLinus Torvalds }
39061da177e4SLinus Torvalds 
3907f1752eecSDavid Howells /*
3908ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3909ee18d64cSDavid Howells  */
3910ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3911ee18d64cSDavid Howells {
3912ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3913ee18d64cSDavid Howells 
3914ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3915ee18d64cSDavid Howells 	if (!tsec)
3916ee18d64cSDavid Howells 		return -ENOMEM;
3917ee18d64cSDavid Howells 
3918ee18d64cSDavid Howells 	cred->security = tsec;
3919ee18d64cSDavid Howells 	return 0;
3920ee18d64cSDavid Howells }
3921ee18d64cSDavid Howells 
3922ee18d64cSDavid Howells /*
3923f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3924f1752eecSDavid Howells  */
3925f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
39261da177e4SLinus Torvalds {
3927f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3928e0e81739SDavid Howells 
39292edeaa34STetsuo Handa 	/*
39302edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
39312edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
39322edeaa34STetsuo Handa 	 */
39332edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3934e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3935f1752eecSDavid Howells 	kfree(tsec);
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds 
3938d84f4f99SDavid Howells /*
3939d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3940d84f4f99SDavid Howells  */
3941d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3942d84f4f99SDavid Howells 				gfp_t gfp)
3943d84f4f99SDavid Howells {
3944d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3945d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3946d84f4f99SDavid Howells 
3947d84f4f99SDavid Howells 	old_tsec = old->security;
3948d84f4f99SDavid Howells 
3949d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3950d84f4f99SDavid Howells 	if (!tsec)
3951d84f4f99SDavid Howells 		return -ENOMEM;
3952d84f4f99SDavid Howells 
3953d84f4f99SDavid Howells 	new->security = tsec;
3954d84f4f99SDavid Howells 	return 0;
3955d84f4f99SDavid Howells }
3956d84f4f99SDavid Howells 
3957d84f4f99SDavid Howells /*
3958ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3959ee18d64cSDavid Howells  */
3960ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3961ee18d64cSDavid Howells {
3962ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3963ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3964ee18d64cSDavid Howells 
3965ee18d64cSDavid Howells 	*tsec = *old_tsec;
3966ee18d64cSDavid Howells }
3967ee18d64cSDavid Howells 
39683ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
39693ec30113SMatthew Garrett {
39703ec30113SMatthew Garrett 	*secid = cred_sid(c);
39713ec30113SMatthew Garrett }
39723ec30113SMatthew Garrett 
3973ee18d64cSDavid Howells /*
39743a3b7ce9SDavid Howells  * set the security data for a kernel service
39753a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
39763a3b7ce9SDavid Howells  */
39773a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
39783a3b7ce9SDavid Howells {
39793a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
39803a3b7ce9SDavid Howells 	u32 sid = current_sid();
39813a3b7ce9SDavid Howells 	int ret;
39823a3b7ce9SDavid Howells 
39836b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
39846b6bc620SStephen Smalley 			   sid, secid,
39853a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
39863a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
39873a3b7ce9SDavid Howells 			   NULL);
39883a3b7ce9SDavid Howells 	if (ret == 0) {
39893a3b7ce9SDavid Howells 		tsec->sid = secid;
39903a3b7ce9SDavid Howells 		tsec->create_sid = 0;
39913a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
39923a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
39933a3b7ce9SDavid Howells 	}
39943a3b7ce9SDavid Howells 	return ret;
39953a3b7ce9SDavid Howells }
39963a3b7ce9SDavid Howells 
39973a3b7ce9SDavid Howells /*
39983a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
39993a3b7ce9SDavid Howells  * objective context of the specified inode
40003a3b7ce9SDavid Howells  */
40013a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
40023a3b7ce9SDavid Howells {
400383da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
40043a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
40053a3b7ce9SDavid Howells 	u32 sid = current_sid();
40063a3b7ce9SDavid Howells 	int ret;
40073a3b7ce9SDavid Howells 
40086b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
40096b6bc620SStephen Smalley 			   sid, isec->sid,
40103a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
40113a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
40123a3b7ce9SDavid Howells 			   NULL);
40133a3b7ce9SDavid Howells 
40143a3b7ce9SDavid Howells 	if (ret == 0)
40153a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
4016ef57471aSDavid Howells 	return ret;
40173a3b7ce9SDavid Howells }
40183a3b7ce9SDavid Howells 
4019dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
402025354c4fSEric Paris {
4021dd8dbf2eSEric Paris 	struct common_audit_data ad;
4022dd8dbf2eSEric Paris 
402350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
4024dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
4025dd8dbf2eSEric Paris 
40266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40276b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4028dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
402925354c4fSEric Paris }
403025354c4fSEric Paris 
403161d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
403261d612eaSJeff Vander Stoep {
403361d612eaSJeff Vander Stoep 	struct common_audit_data ad;
403461d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
403561d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
403661d612eaSJeff Vander Stoep 	u32 sid = current_sid();
403761d612eaSJeff Vander Stoep 	int rc;
403861d612eaSJeff Vander Stoep 
403961d612eaSJeff Vander Stoep 	/* init_module */
404061d612eaSJeff Vander Stoep 	if (file == NULL)
40416b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
40426b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
404361d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
404461d612eaSJeff Vander Stoep 
404561d612eaSJeff Vander Stoep 	/* finit_module */
404620cdef8dSPaul Moore 
404743af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
404843af5de7SVivek Goyal 	ad.u.file = file;
404961d612eaSJeff Vander Stoep 
405061d612eaSJeff Vander Stoep 	fsec = file->f_security;
405161d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
40526b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
40536b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
405461d612eaSJeff Vander Stoep 		if (rc)
405561d612eaSJeff Vander Stoep 			return rc;
405661d612eaSJeff Vander Stoep 	}
405761d612eaSJeff Vander Stoep 
405820cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
40596b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40606b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
406161d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
406261d612eaSJeff Vander Stoep }
406361d612eaSJeff Vander Stoep 
406461d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
406561d612eaSJeff Vander Stoep 				    enum kernel_read_file_id id)
406661d612eaSJeff Vander Stoep {
406761d612eaSJeff Vander Stoep 	int rc = 0;
406861d612eaSJeff Vander Stoep 
406961d612eaSJeff Vander Stoep 	switch (id) {
407061d612eaSJeff Vander Stoep 	case READING_MODULE:
407161d612eaSJeff Vander Stoep 		rc = selinux_kernel_module_from_file(file);
407261d612eaSJeff Vander Stoep 		break;
407361d612eaSJeff Vander Stoep 	default:
407461d612eaSJeff Vander Stoep 		break;
407561d612eaSJeff Vander Stoep 	}
407661d612eaSJeff Vander Stoep 
407761d612eaSJeff Vander Stoep 	return rc;
407861d612eaSJeff Vander Stoep }
407961d612eaSJeff Vander Stoep 
4080c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id)
4081c77b8cdfSMimi Zohar {
4082c77b8cdfSMimi Zohar 	int rc = 0;
4083c77b8cdfSMimi Zohar 
4084c77b8cdfSMimi Zohar 	switch (id) {
4085c77b8cdfSMimi Zohar 	case LOADING_MODULE:
4086c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
4087c77b8cdfSMimi Zohar 	default:
4088c77b8cdfSMimi Zohar 		break;
4089c77b8cdfSMimi Zohar 	}
4090c77b8cdfSMimi Zohar 
4091c77b8cdfSMimi Zohar 	return rc;
4092c77b8cdfSMimi Zohar }
4093c77b8cdfSMimi Zohar 
40941da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
40951da177e4SLinus Torvalds {
40966b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
40976b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4098be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
40991da177e4SLinus Torvalds }
41001da177e4SLinus Torvalds 
41011da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
41021da177e4SLinus Torvalds {
41036b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41046b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4105be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
41061da177e4SLinus Torvalds }
41071da177e4SLinus Torvalds 
41081da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
41091da177e4SLinus Torvalds {
41106b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41116b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4112be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
41131da177e4SLinus Torvalds }
41141da177e4SLinus Torvalds 
4115f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4116f9008e4cSDavid Quigley {
4117275bb41eSDavid Howells 	*secid = task_sid(p);
4118f9008e4cSDavid Quigley }
4119f9008e4cSDavid Quigley 
41201da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
41211da177e4SLinus Torvalds {
41226b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41236b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4124be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41251da177e4SLinus Torvalds }
41261da177e4SLinus Torvalds 
412703e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
412803e68060SJames Morris {
41296b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41306b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4131be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
413203e68060SJames Morris }
413303e68060SJames Morris 
4134a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
4135a1836a42SDavid Quigley {
41366b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41376b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4138be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
4139a1836a42SDavid Quigley }
4140a1836a42SDavid Quigley 
41414298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4142791ec491SStephen Smalley 				unsigned int flags)
4143791ec491SStephen Smalley {
4144791ec491SStephen Smalley 	u32 av = 0;
4145791ec491SStephen Smalley 
414684e6885eSStephen Smalley 	if (!flags)
414784e6885eSStephen Smalley 		return 0;
4148791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
4149791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
4150791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
4151791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
41526b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41536b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
4154791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
4155791ec491SStephen Smalley }
4156791ec491SStephen Smalley 
41578fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
41588fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
41591da177e4SLinus Torvalds {
41608fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
41611da177e4SLinus Torvalds 
41621da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
41631da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
41641da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
4165d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
41661da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
41676b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
41686b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
4169be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
41701da177e4SLinus Torvalds 
41711da177e4SLinus Torvalds 	return 0;
41721da177e4SLinus Torvalds }
41731da177e4SLinus Torvalds 
4174b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
41751da177e4SLinus Torvalds {
41766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41776b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4178be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
41791da177e4SLinus Torvalds }
41801da177e4SLinus Torvalds 
41811da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
41821da177e4SLinus Torvalds {
41836b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41846b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4185be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
41861da177e4SLinus Torvalds }
41871da177e4SLinus Torvalds 
418835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
418935601547SDavid Quigley {
41906b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
41916b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
4192be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
419335601547SDavid Quigley }
419435601547SDavid Quigley 
4195ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
41966b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
41971da177e4SLinus Torvalds {
41986b4f3d01SStephen Smalley 	u32 secid;
41991da177e4SLinus Torvalds 	u32 perm;
42001da177e4SLinus Torvalds 
42011da177e4SLinus Torvalds 	if (!sig)
42021da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
42031da177e4SLinus Torvalds 	else
42041da177e4SLinus Torvalds 		perm = signal_to_av(sig);
42056b4f3d01SStephen Smalley 	if (!cred)
4206be0554c9SStephen Smalley 		secid = current_sid();
42076b4f3d01SStephen Smalley 	else
42086b4f3d01SStephen Smalley 		secid = cred_sid(cred);
42096b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
42106b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
42111da177e4SLinus Torvalds }
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
42141da177e4SLinus Torvalds 				  struct inode *inode)
42151da177e4SLinus Torvalds {
42161da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
4217275bb41eSDavid Howells 	u32 sid = task_sid(p);
42181da177e4SLinus Torvalds 
42199287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4220db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4221275bb41eSDavid Howells 	isec->sid = sid;
42226f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
42239287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
42241da177e4SLinus Torvalds }
42251da177e4SLinus Torvalds 
42261da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
422767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
42282bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
42291da177e4SLinus Torvalds {
42301da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
42311da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
42321da177e4SLinus Torvalds 
4233bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
42341da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
42351da177e4SLinus Torvalds 	if (ih == NULL)
42361da177e4SLinus Torvalds 		goto out;
42371da177e4SLinus Torvalds 
42381da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
42391da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
42401da177e4SLinus Torvalds 		goto out;
42411da177e4SLinus Torvalds 
424248c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
424348c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
42441da177e4SLinus Torvalds 	ret = 0;
42451da177e4SLinus Torvalds 
424667f83cbfSVenkat Yekkirala 	if (proto)
424767f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
424867f83cbfSVenkat Yekkirala 
42491da177e4SLinus Torvalds 	switch (ih->protocol) {
42501da177e4SLinus Torvalds 	case IPPROTO_TCP: {
42511da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
42521da177e4SLinus Torvalds 
42531da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
42541da177e4SLinus Torvalds 			break;
42551da177e4SLinus Torvalds 
42561da177e4SLinus Torvalds 		offset += ihlen;
42571da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
42581da177e4SLinus Torvalds 		if (th == NULL)
42591da177e4SLinus Torvalds 			break;
42601da177e4SLinus Torvalds 
426148c62af6SEric Paris 		ad->u.net->sport = th->source;
426248c62af6SEric Paris 		ad->u.net->dport = th->dest;
42631da177e4SLinus Torvalds 		break;
42641da177e4SLinus Torvalds 	}
42651da177e4SLinus Torvalds 
42661da177e4SLinus Torvalds 	case IPPROTO_UDP: {
42671da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
42681da177e4SLinus Torvalds 
42691da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
42701da177e4SLinus Torvalds 			break;
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 		offset += ihlen;
42731da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
42741da177e4SLinus Torvalds 		if (uh == NULL)
42751da177e4SLinus Torvalds 			break;
42761da177e4SLinus Torvalds 
427748c62af6SEric Paris 		ad->u.net->sport = uh->source;
427848c62af6SEric Paris 		ad->u.net->dport = uh->dest;
42791da177e4SLinus Torvalds 		break;
42801da177e4SLinus Torvalds 	}
42811da177e4SLinus Torvalds 
42822ee92d46SJames Morris 	case IPPROTO_DCCP: {
42832ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
42842ee92d46SJames Morris 
42852ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
42862ee92d46SJames Morris 			break;
42872ee92d46SJames Morris 
42882ee92d46SJames Morris 		offset += ihlen;
42892ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
42902ee92d46SJames Morris 		if (dh == NULL)
42912ee92d46SJames Morris 			break;
42922ee92d46SJames Morris 
429348c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
429448c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
42952ee92d46SJames Morris 		break;
42962ee92d46SJames Morris 	}
42972ee92d46SJames Morris 
4298d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4299d452930fSRichard Haines 	case IPPROTO_SCTP: {
4300d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4301d452930fSRichard Haines 
4302d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4303d452930fSRichard Haines 			break;
4304d452930fSRichard Haines 
4305d452930fSRichard Haines 		offset += ihlen;
4306d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4307d452930fSRichard Haines 		if (sh == NULL)
4308d452930fSRichard Haines 			break;
4309d452930fSRichard Haines 
4310d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4311d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4312d452930fSRichard Haines 		break;
4313d452930fSRichard Haines 	}
4314d452930fSRichard Haines #endif
43151da177e4SLinus Torvalds 	default:
43161da177e4SLinus Torvalds 		break;
43171da177e4SLinus Torvalds 	}
43181da177e4SLinus Torvalds out:
43191da177e4SLinus Torvalds 	return ret;
43201da177e4SLinus Torvalds }
43211da177e4SLinus Torvalds 
43221a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
43231da177e4SLinus Torvalds 
43241da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
432567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
43262bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
43271da177e4SLinus Torvalds {
43281da177e4SLinus Torvalds 	u8 nexthdr;
43291da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
43301da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
433175f2811cSJesse Gross 	__be16 frag_off;
43321da177e4SLinus Torvalds 
4333bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
43341da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
43351da177e4SLinus Torvalds 	if (ip6 == NULL)
43361da177e4SLinus Torvalds 		goto out;
43371da177e4SLinus Torvalds 
433848c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
433948c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
43401da177e4SLinus Torvalds 	ret = 0;
43411da177e4SLinus Torvalds 
43421da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
43431da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
434475f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
43451da177e4SLinus Torvalds 	if (offset < 0)
43461da177e4SLinus Torvalds 		goto out;
43471da177e4SLinus Torvalds 
434867f83cbfSVenkat Yekkirala 	if (proto)
434967f83cbfSVenkat Yekkirala 		*proto = nexthdr;
435067f83cbfSVenkat Yekkirala 
43511da177e4SLinus Torvalds 	switch (nexthdr) {
43521da177e4SLinus Torvalds 	case IPPROTO_TCP: {
43531da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
43541da177e4SLinus Torvalds 
43551da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
43561da177e4SLinus Torvalds 		if (th == NULL)
43571da177e4SLinus Torvalds 			break;
43581da177e4SLinus Torvalds 
435948c62af6SEric Paris 		ad->u.net->sport = th->source;
436048c62af6SEric Paris 		ad->u.net->dport = th->dest;
43611da177e4SLinus Torvalds 		break;
43621da177e4SLinus Torvalds 	}
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds 	case IPPROTO_UDP: {
43651da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
43661da177e4SLinus Torvalds 
43671da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
43681da177e4SLinus Torvalds 		if (uh == NULL)
43691da177e4SLinus Torvalds 			break;
43701da177e4SLinus Torvalds 
437148c62af6SEric Paris 		ad->u.net->sport = uh->source;
437248c62af6SEric Paris 		ad->u.net->dport = uh->dest;
43731da177e4SLinus Torvalds 		break;
43741da177e4SLinus Torvalds 	}
43751da177e4SLinus Torvalds 
43762ee92d46SJames Morris 	case IPPROTO_DCCP: {
43772ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
43782ee92d46SJames Morris 
43792ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
43802ee92d46SJames Morris 		if (dh == NULL)
43812ee92d46SJames Morris 			break;
43822ee92d46SJames Morris 
438348c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
438448c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
43852ee92d46SJames Morris 		break;
43862ee92d46SJames Morris 	}
43872ee92d46SJames Morris 
4388d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4389d452930fSRichard Haines 	case IPPROTO_SCTP: {
4390d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4391d452930fSRichard Haines 
4392d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4393d452930fSRichard Haines 		if (sh == NULL)
4394d452930fSRichard Haines 			break;
4395d452930fSRichard Haines 
4396d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4397d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4398d452930fSRichard Haines 		break;
4399d452930fSRichard Haines 	}
4400d452930fSRichard Haines #endif
44011da177e4SLinus Torvalds 	/* includes fragments */
44021da177e4SLinus Torvalds 	default:
44031da177e4SLinus Torvalds 		break;
44041da177e4SLinus Torvalds 	}
44051da177e4SLinus Torvalds out:
44061da177e4SLinus Torvalds 	return ret;
44071da177e4SLinus Torvalds }
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds #endif /* IPV6 */
44101da177e4SLinus Torvalds 
44112bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4412cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
44131da177e4SLinus Torvalds {
4414cf9481e2SDavid Howells 	char *addrp;
4415cf9481e2SDavid Howells 	int ret;
44161da177e4SLinus Torvalds 
441748c62af6SEric Paris 	switch (ad->u.net->family) {
44181da177e4SLinus Torvalds 	case PF_INET:
441967f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4420cf9481e2SDavid Howells 		if (ret)
4421cf9481e2SDavid Howells 			goto parse_error;
442248c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
442348c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4424cf9481e2SDavid Howells 		goto okay;
44251da177e4SLinus Torvalds 
44261a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
44271da177e4SLinus Torvalds 	case PF_INET6:
442867f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4429cf9481e2SDavid Howells 		if (ret)
4430cf9481e2SDavid Howells 			goto parse_error;
443148c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
443248c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4433cf9481e2SDavid Howells 		goto okay;
44341da177e4SLinus Torvalds #endif	/* IPV6 */
44351da177e4SLinus Torvalds 	default:
4436cf9481e2SDavid Howells 		addrp = NULL;
4437cf9481e2SDavid Howells 		goto okay;
44381da177e4SLinus Torvalds 	}
44391da177e4SLinus Torvalds 
4440cf9481e2SDavid Howells parse_error:
4441c103a91eSpeter enderborg 	pr_warn(
444271f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
444371f1cb05SPaul Moore 	       " unable to parse packet\n");
44441da177e4SLinus Torvalds 	return ret;
4445cf9481e2SDavid Howells 
4446cf9481e2SDavid Howells okay:
4447cf9481e2SDavid Howells 	if (_addrp)
4448cf9481e2SDavid Howells 		*_addrp = addrp;
4449cf9481e2SDavid Howells 	return 0;
44501da177e4SLinus Torvalds }
44511da177e4SLinus Torvalds 
44524f6a993fSPaul Moore /**
4453220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
44544f6a993fSPaul Moore  * @skb: the packet
445575e22910SPaul Moore  * @family: protocol family
4456220deb96SPaul Moore  * @sid: the packet's peer label SID
44574f6a993fSPaul Moore  *
44584f6a993fSPaul Moore  * Description:
4459220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4460220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4461220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4462220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4463220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4464220deb96SPaul Moore  * peer labels.
44654f6a993fSPaul Moore  *
44664f6a993fSPaul Moore  */
4467220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
44684f6a993fSPaul Moore {
446971f1cb05SPaul Moore 	int err;
44704f6a993fSPaul Moore 	u32 xfrm_sid;
44714f6a993fSPaul Moore 	u32 nlbl_sid;
4472220deb96SPaul Moore 	u32 nlbl_type;
44734f6a993fSPaul Moore 
4474817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4475bed4d7efSPaul Moore 	if (unlikely(err))
4476bed4d7efSPaul Moore 		return -EACCES;
4477bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4478bed4d7efSPaul Moore 	if (unlikely(err))
4479bed4d7efSPaul Moore 		return -EACCES;
4480220deb96SPaul Moore 
4481aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4482aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
448371f1cb05SPaul Moore 	if (unlikely(err)) {
4484c103a91eSpeter enderborg 		pr_warn(
448571f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
448671f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4487220deb96SPaul Moore 		return -EACCES;
448871f1cb05SPaul Moore 	}
4489220deb96SPaul Moore 
4490220deb96SPaul Moore 	return 0;
44914f6a993fSPaul Moore }
44924f6a993fSPaul Moore 
4493446b8024SPaul Moore /**
4494446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4495446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4496446b8024SPaul Moore  * @skb_sid: the packet's SID
4497446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4498446b8024SPaul Moore  *
4499446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4500446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4501446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
4502446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4503446b8024SPaul Moore  *
4504446b8024SPaul Moore  */
4505446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4506446b8024SPaul Moore {
4507446b8024SPaul Moore 	int err = 0;
4508446b8024SPaul Moore 
4509446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4510aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4511aa8e712cSStephen Smalley 					    conn_sid);
4512446b8024SPaul Moore 	else
4513446b8024SPaul Moore 		*conn_sid = sk_sid;
4514446b8024SPaul Moore 
4515446b8024SPaul Moore 	return err;
4516446b8024SPaul Moore }
4517446b8024SPaul Moore 
45181da177e4SLinus Torvalds /* socket security operations */
4519d4f2d978SPaul Moore 
45202ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
45212ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4522d4f2d978SPaul Moore {
45232ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
45242ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
45252ad18bdfSHarry Ciao 		return 0;
45262ad18bdfSHarry Ciao 	}
45272ad18bdfSHarry Ciao 
4528aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4529aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4530d4f2d978SPaul Moore }
4531d4f2d978SPaul Moore 
4532be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
45331da177e4SLinus Torvalds {
4534253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45352bf49690SThomas Liu 	struct common_audit_data ad;
453648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
45371da177e4SLinus Torvalds 
4538253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4539253bfae6SPaul Moore 		return 0;
45401da177e4SLinus Torvalds 
454150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
454248c62af6SEric Paris 	ad.u.net = &net;
454348c62af6SEric Paris 	ad.u.net->sk = sk;
45441da177e4SLinus Torvalds 
45456b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
45466b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4547be0554c9SStephen Smalley 			    &ad);
45481da177e4SLinus Torvalds }
45491da177e4SLinus Torvalds 
45501da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
45511da177e4SLinus Torvalds 				 int protocol, int kern)
45521da177e4SLinus Torvalds {
45535fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4554d4f2d978SPaul Moore 	u32 newsid;
4555275bb41eSDavid Howells 	u16 secclass;
45562ad18bdfSHarry Ciao 	int rc;
45571da177e4SLinus Torvalds 
45581da177e4SLinus Torvalds 	if (kern)
4559d4f2d978SPaul Moore 		return 0;
45601da177e4SLinus Torvalds 
4561275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
45622ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
45632ad18bdfSHarry Ciao 	if (rc)
45642ad18bdfSHarry Ciao 		return rc;
45652ad18bdfSHarry Ciao 
45666b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
45676b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
45681da177e4SLinus Torvalds }
45691da177e4SLinus Torvalds 
45707420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
45711da177e4SLinus Torvalds 				      int type, int protocol, int kern)
45721da177e4SLinus Torvalds {
45735fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
45745d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4575892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
45769287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
45779287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4578275bb41eSDavid Howells 	int err = 0;
4579275bb41eSDavid Howells 
45809287aed2SAndreas Gruenbacher 	if (!kern) {
45819287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
45822ad18bdfSHarry Ciao 		if (err)
45832ad18bdfSHarry Ciao 			return err;
45842ad18bdfSHarry Ciao 	}
4585275bb41eSDavid Howells 
45869287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
45879287aed2SAndreas Gruenbacher 	isec->sid = sid;
45886f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
45891da177e4SLinus Torvalds 
4590892c141eSVenkat Yekkirala 	if (sock->sk) {
4591892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
45929287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
45939287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4594d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4595d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4596d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4597d452930fSRichard Haines 
4598389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4599892c141eSVenkat Yekkirala 	}
4600892c141eSVenkat Yekkirala 
46017420ed23SVenkat Yekkirala 	return err;
46021da177e4SLinus Torvalds }
46031da177e4SLinus Torvalds 
46040b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
46050b811db2SDavid Herrmann 				     struct socket *sockb)
46060b811db2SDavid Herrmann {
46070b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
46080b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
46090b811db2SDavid Herrmann 
46100b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
46110b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
46120b811db2SDavid Herrmann 
46130b811db2SDavid Herrmann 	return 0;
46140b811db2SDavid Herrmann }
46150b811db2SDavid Herrmann 
46161da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
46171da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
46181da177e4SLinus Torvalds    permission check between the socket and the port number. */
46191da177e4SLinus Torvalds 
46201da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
46211da177e4SLinus Torvalds {
4622253bfae6SPaul Moore 	struct sock *sk = sock->sk;
46230f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
46241da177e4SLinus Torvalds 	u16 family;
46251da177e4SLinus Torvalds 	int err;
46261da177e4SLinus Torvalds 
4627be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
46281da177e4SLinus Torvalds 	if (err)
46291da177e4SLinus Torvalds 		goto out;
46301da177e4SLinus Torvalds 
4631d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4632253bfae6SPaul Moore 	family = sk->sk_family;
46331da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
46341da177e4SLinus Torvalds 		char *addrp;
46352bf49690SThomas Liu 		struct common_audit_data ad;
463648c62af6SEric Paris 		struct lsm_network_audit net = {0,};
46371da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
46381da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
46390f8db8ccSAlexey Kodanev 		u16 family_sa = address->sa_family;
46401da177e4SLinus Torvalds 		unsigned short snum;
4641e399f982SJames Morris 		u32 sid, node_perm;
46421da177e4SLinus Torvalds 
4643d452930fSRichard Haines 		/*
4644d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4645d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4646d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4647d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4648d452930fSRichard Haines 		 */
46490f8db8ccSAlexey Kodanev 		switch (family_sa) {
46500f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
465168741a8aSRichard Haines 		case AF_INET:
465268741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
465368741a8aSRichard Haines 				return -EINVAL;
46541da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
46550f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
46560f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
46570f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
46580f8db8ccSAlexey Kodanev 				 */
46590f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
46600f8db8ccSAlexey Kodanev 					goto err_af;
46610f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
46620f8db8ccSAlexey Kodanev 			}
46631da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
46641da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
466568741a8aSRichard Haines 			break;
466668741a8aSRichard Haines 		case AF_INET6:
466768741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
466868741a8aSRichard Haines 				return -EINVAL;
46691da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
46701da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
46711da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
467268741a8aSRichard Haines 			break;
467368741a8aSRichard Haines 		default:
46740f8db8ccSAlexey Kodanev 			goto err_af;
46751da177e4SLinus Torvalds 		}
46761da177e4SLinus Torvalds 
467788b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
467888b7d370SAlexey Kodanev 		ad.u.net = &net;
467988b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
468088b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
468188b7d370SAlexey Kodanev 
4682227b60f5SStephen Hemminger 		if (snum) {
4683227b60f5SStephen Hemminger 			int low, high;
4684227b60f5SStephen Hemminger 
46850bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4686227b60f5SStephen Hemminger 
46874548b683SKrister Johansen 			if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
46884548b683SKrister Johansen 			    snum > high) {
46893e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
46903e112172SPaul Moore 						      snum, &sid);
46911da177e4SLinus Torvalds 				if (err)
46921da177e4SLinus Torvalds 					goto out;
46936b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
46946b6bc620SStephen Smalley 						   sksec->sid, sid,
4695253bfae6SPaul Moore 						   sksec->sclass,
46961da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
46971da177e4SLinus Torvalds 				if (err)
46981da177e4SLinus Torvalds 					goto out;
46991da177e4SLinus Torvalds 			}
4700227b60f5SStephen Hemminger 		}
47011da177e4SLinus Torvalds 
4702253bfae6SPaul Moore 		switch (sksec->sclass) {
470313402580SJames Morris 		case SECCLASS_TCP_SOCKET:
47041da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
47051da177e4SLinus Torvalds 			break;
47061da177e4SLinus Torvalds 
470713402580SJames Morris 		case SECCLASS_UDP_SOCKET:
47081da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
47091da177e4SLinus Torvalds 			break;
47101da177e4SLinus Torvalds 
47112ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
47122ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
47132ee92d46SJames Morris 			break;
47142ee92d46SJames Morris 
4715d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4716d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4717d452930fSRichard Haines 			break;
4718d452930fSRichard Haines 
47191da177e4SLinus Torvalds 		default:
47201da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
47211da177e4SLinus Torvalds 			break;
47221da177e4SLinus Torvalds 		}
47231da177e4SLinus Torvalds 
472488b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
47251da177e4SLinus Torvalds 		if (err)
47261da177e4SLinus Torvalds 			goto out;
47271da177e4SLinus Torvalds 
47280f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
472948c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
47301da177e4SLinus Torvalds 		else
473148c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
47321da177e4SLinus Torvalds 
47336b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
47346b6bc620SStephen Smalley 				   sksec->sid, sid,
4735253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
47361da177e4SLinus Torvalds 		if (err)
47371da177e4SLinus Torvalds 			goto out;
47381da177e4SLinus Torvalds 	}
47391da177e4SLinus Torvalds out:
47401da177e4SLinus Torvalds 	return err;
47410f8db8ccSAlexey Kodanev err_af:
47420f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
47430f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
47440f8db8ccSAlexey Kodanev 		return -EINVAL;
47450f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
47461da177e4SLinus Torvalds }
47471da177e4SLinus Torvalds 
4748d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
47495fb94e9cSMauro Carvalho Chehab  * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
4750d452930fSRichard Haines  */
4751d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4752d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
47531da177e4SLinus Torvalds {
4754014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4755253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
47561da177e4SLinus Torvalds 	int err;
47571da177e4SLinus Torvalds 
4758be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
47591da177e4SLinus Torvalds 	if (err)
47601da177e4SLinus Torvalds 		return err;
47611da177e4SLinus Torvalds 
47621da177e4SLinus Torvalds 	/*
4763d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4764d452930fSRichard Haines 	 * for the port.
47651da177e4SLinus Torvalds 	 */
4766253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4767d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4768d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
47692bf49690SThomas Liu 		struct common_audit_data ad;
477048c62af6SEric Paris 		struct lsm_network_audit net = {0,};
47711da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
47721da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
47731da177e4SLinus Torvalds 		unsigned short snum;
47742ee92d46SJames Morris 		u32 sid, perm;
47751da177e4SLinus Torvalds 
4776d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4777d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4778d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4779d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4780d452930fSRichard Haines 		 */
478168741a8aSRichard Haines 		switch (address->sa_family) {
478268741a8aSRichard Haines 		case AF_INET:
47831da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4784911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
47851da177e4SLinus Torvalds 				return -EINVAL;
47861da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
478768741a8aSRichard Haines 			break;
478868741a8aSRichard Haines 		case AF_INET6:
47891da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4790911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
47911da177e4SLinus Torvalds 				return -EINVAL;
47921da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
479368741a8aSRichard Haines 			break;
479468741a8aSRichard Haines 		default:
479568741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
479668741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
479768741a8aSRichard Haines 			 */
479868741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
479968741a8aSRichard Haines 				return -EINVAL;
480068741a8aSRichard Haines 			else
480168741a8aSRichard Haines 				return -EAFNOSUPPORT;
48021da177e4SLinus Torvalds 		}
48031da177e4SLinus Torvalds 
48043e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
48051da177e4SLinus Torvalds 		if (err)
4806d452930fSRichard Haines 			return err;
48071da177e4SLinus Torvalds 
4808d452930fSRichard Haines 		switch (sksec->sclass) {
4809d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4810d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4811d452930fSRichard Haines 			break;
4812d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4813d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4814d452930fSRichard Haines 			break;
4815d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4816d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4817d452930fSRichard Haines 			break;
4818d452930fSRichard Haines 		}
48192ee92d46SJames Morris 
482050c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
482148c62af6SEric Paris 		ad.u.net = &net;
482248c62af6SEric Paris 		ad.u.net->dport = htons(snum);
482388b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
48246b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48256b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
48261da177e4SLinus Torvalds 		if (err)
4827d452930fSRichard Haines 			return err;
48281da177e4SLinus Torvalds 	}
48291da177e4SLinus Torvalds 
4830d452930fSRichard Haines 	return 0;
4831d452930fSRichard Haines }
4832014ab19aSPaul Moore 
4833d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4834d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4835d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4836d452930fSRichard Haines {
4837d452930fSRichard Haines 	int err;
4838d452930fSRichard Haines 	struct sock *sk = sock->sk;
4839d452930fSRichard Haines 
4840d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4841d452930fSRichard Haines 	if (err)
48421da177e4SLinus Torvalds 		return err;
4843d452930fSRichard Haines 
4844d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
48451da177e4SLinus Torvalds }
48461da177e4SLinus Torvalds 
48471da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
48481da177e4SLinus Torvalds {
4849be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
48501da177e4SLinus Torvalds }
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
48531da177e4SLinus Torvalds {
48541da177e4SLinus Torvalds 	int err;
48551da177e4SLinus Torvalds 	struct inode_security_struct *isec;
48561da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
48579287aed2SAndreas Gruenbacher 	u16 sclass;
48589287aed2SAndreas Gruenbacher 	u32 sid;
48591da177e4SLinus Torvalds 
4860be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
48611da177e4SLinus Torvalds 	if (err)
48621da177e4SLinus Torvalds 		return err;
48631da177e4SLinus Torvalds 
48645d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
48659287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
48669287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
48679287aed2SAndreas Gruenbacher 	sid = isec->sid;
48689287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
48699287aed2SAndreas Gruenbacher 
48709287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
48719287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
48729287aed2SAndreas Gruenbacher 	newisec->sid = sid;
48736f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds 	return 0;
48761da177e4SLinus Torvalds }
48771da177e4SLinus Torvalds 
48781da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
48791da177e4SLinus Torvalds 				  int size)
48801da177e4SLinus Torvalds {
4881be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
48821da177e4SLinus Torvalds }
48831da177e4SLinus Torvalds 
48841da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
48851da177e4SLinus Torvalds 				  int size, int flags)
48861da177e4SLinus Torvalds {
4887be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
48881da177e4SLinus Torvalds }
48891da177e4SLinus Torvalds 
48901da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
48911da177e4SLinus Torvalds {
4892be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48931da177e4SLinus Torvalds }
48941da177e4SLinus Torvalds 
48951da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
48961da177e4SLinus Torvalds {
4897be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
48981da177e4SLinus Torvalds }
48991da177e4SLinus Torvalds 
49001da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
49011da177e4SLinus Torvalds {
4902f8687afeSPaul Moore 	int err;
4903f8687afeSPaul Moore 
4904be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4905f8687afeSPaul Moore 	if (err)
4906f8687afeSPaul Moore 		return err;
4907f8687afeSPaul Moore 
4908f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
49091da177e4SLinus Torvalds }
49101da177e4SLinus Torvalds 
49111da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
49121da177e4SLinus Torvalds 				     int optname)
49131da177e4SLinus Torvalds {
4914be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
49151da177e4SLinus Torvalds }
49161da177e4SLinus Torvalds 
49171da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
49181da177e4SLinus Torvalds {
4919be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
49201da177e4SLinus Torvalds }
49211da177e4SLinus Torvalds 
49223610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
49233610cda5SDavid S. Miller 					      struct sock *other,
49241da177e4SLinus Torvalds 					      struct sock *newsk)
49251da177e4SLinus Torvalds {
49263610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
49273610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
49284d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
49292bf49690SThomas Liu 	struct common_audit_data ad;
493048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49311da177e4SLinus Torvalds 	int err;
49321da177e4SLinus Torvalds 
493350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
493448c62af6SEric Paris 	ad.u.net = &net;
493548c62af6SEric Paris 	ad.u.net->sk = other;
49361da177e4SLinus Torvalds 
49376b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
49386b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
49394d1e2451SPaul Moore 			   sksec_other->sclass,
49401da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
49411da177e4SLinus Torvalds 	if (err)
49421da177e4SLinus Torvalds 		return err;
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds 	/* server child socket */
49454d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4946aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4947aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
49484d1e2451SPaul Moore 	if (err)
49494237c75cSVenkat Yekkirala 		return err;
49504d1e2451SPaul Moore 
49514d1e2451SPaul Moore 	/* connecting socket */
49524d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
49534d1e2451SPaul Moore 
49544d1e2451SPaul Moore 	return 0;
49551da177e4SLinus Torvalds }
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
49581da177e4SLinus Torvalds 					struct socket *other)
49591da177e4SLinus Torvalds {
4960253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4961253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
49622bf49690SThomas Liu 	struct common_audit_data ad;
496348c62af6SEric Paris 	struct lsm_network_audit net = {0,};
49641da177e4SLinus Torvalds 
496550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
496648c62af6SEric Paris 	ad.u.net = &net;
496748c62af6SEric Paris 	ad.u.net->sk = other->sk;
49681da177e4SLinus Torvalds 
49696b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49706b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4971253bfae6SPaul Moore 			    &ad);
49721da177e4SLinus Torvalds }
49731da177e4SLinus Torvalds 
4974cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4975cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
49762bf49690SThomas Liu 				    struct common_audit_data *ad)
4977effad8dfSPaul Moore {
4978effad8dfSPaul Moore 	int err;
4979effad8dfSPaul Moore 	u32 if_sid;
4980effad8dfSPaul Moore 	u32 node_sid;
4981effad8dfSPaul Moore 
4982cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4983effad8dfSPaul Moore 	if (err)
4984effad8dfSPaul Moore 		return err;
49856b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
49866b6bc620SStephen Smalley 			   peer_sid, if_sid,
4987effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4988effad8dfSPaul Moore 	if (err)
4989effad8dfSPaul Moore 		return err;
4990effad8dfSPaul Moore 
4991effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4992effad8dfSPaul Moore 	if (err)
4993effad8dfSPaul Moore 		return err;
49946b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
49956b6bc620SStephen Smalley 			    peer_sid, node_sid,
4996effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4997effad8dfSPaul Moore }
4998effad8dfSPaul Moore 
4999220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5000d8395c87SPaul Moore 				       u16 family)
5001220deb96SPaul Moore {
5002277d342fSPaul Moore 	int err = 0;
5003220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5004220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
50052bf49690SThomas Liu 	struct common_audit_data ad;
500648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5007d8395c87SPaul Moore 	char *addrp;
5008d8395c87SPaul Moore 
500950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
501048c62af6SEric Paris 	ad.u.net = &net;
501148c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
501248c62af6SEric Paris 	ad.u.net->family = family;
5013d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5014d8395c87SPaul Moore 	if (err)
5015d8395c87SPaul Moore 		return err;
5016220deb96SPaul Moore 
501758bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
50186b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50196b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5020d8395c87SPaul Moore 				   PACKET__RECV, &ad);
5021220deb96SPaul Moore 		if (err)
5022220deb96SPaul Moore 			return err;
502358bfbb51SPaul Moore 	}
5024220deb96SPaul Moore 
5025d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5026220deb96SPaul Moore 	if (err)
5027220deb96SPaul Moore 		return err;
5028d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5029220deb96SPaul Moore 
50304e5ab4cbSJames Morris 	return err;
50314e5ab4cbSJames Morris }
5032d28d1e08STrent Jaeger 
50334e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
50344e5ab4cbSJames Morris {
5035220deb96SPaul Moore 	int err;
50364237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
5037220deb96SPaul Moore 	u16 family = sk->sk_family;
5038220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
50392bf49690SThomas Liu 	struct common_audit_data ad;
504048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5041220deb96SPaul Moore 	char *addrp;
5042d8395c87SPaul Moore 	u8 secmark_active;
5043d8395c87SPaul Moore 	u8 peerlbl_active;
50444e5ab4cbSJames Morris 
50454e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
5046220deb96SPaul Moore 		return 0;
50474e5ab4cbSJames Morris 
50484e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
504987fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
50504e5ab4cbSJames Morris 		family = PF_INET;
50514e5ab4cbSJames Morris 
5052d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5053d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
5054d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5055d8395c87SPaul Moore 	 * as fast and as clean as possible. */
5056aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5057d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
5058d8395c87SPaul Moore 
5059d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
50602be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5061d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
5062d8395c87SPaul Moore 		return 0;
5063d8395c87SPaul Moore 
506450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
506548c62af6SEric Paris 	ad.u.net = &net;
506648c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
506748c62af6SEric Paris 	ad.u.net->family = family;
5068224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
50694e5ab4cbSJames Morris 	if (err)
5070220deb96SPaul Moore 		return err;
50714e5ab4cbSJames Morris 
5072d8395c87SPaul Moore 	if (peerlbl_active) {
5073d621d35eSPaul Moore 		u32 peer_sid;
5074220deb96SPaul Moore 
5075220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5076220deb96SPaul Moore 		if (err)
5077220deb96SPaul Moore 			return err;
5078cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5079cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5080dfaebe98SPaul Moore 		if (err) {
5081a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
5082effad8dfSPaul Moore 			return err;
5083dfaebe98SPaul Moore 		}
50846b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50856b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
5086d621d35eSPaul Moore 				   PEER__RECV, &ad);
508746d01d63SChad Hanson 		if (err) {
5088a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
508946d01d63SChad Hanson 			return err;
509046d01d63SChad Hanson 		}
5091d621d35eSPaul Moore 	}
5092d621d35eSPaul Moore 
5093d8395c87SPaul Moore 	if (secmark_active) {
50946b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50956b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
5096effad8dfSPaul Moore 				   PACKET__RECV, &ad);
5097effad8dfSPaul Moore 		if (err)
5098effad8dfSPaul Moore 			return err;
5099effad8dfSPaul Moore 	}
5100effad8dfSPaul Moore 
5101d621d35eSPaul Moore 	return err;
51021da177e4SLinus Torvalds }
51031da177e4SLinus Torvalds 
51042c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
51051da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
51061da177e4SLinus Torvalds {
51071da177e4SLinus Torvalds 	int err = 0;
51081da177e4SLinus Torvalds 	char *scontext;
51091da177e4SLinus Torvalds 	u32 scontext_len;
5110253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
51113de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
51121da177e4SLinus Torvalds 
5113253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5114d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
5115d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
5116dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
5117253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
5118253bfae6SPaul Moore 		return -ENOPROTOOPT;
51191da177e4SLinus Torvalds 
5120aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5121aa8e712cSStephen Smalley 				      &scontext_len);
51221da177e4SLinus Torvalds 	if (err)
5123253bfae6SPaul Moore 		return err;
51241da177e4SLinus Torvalds 
51251da177e4SLinus Torvalds 	if (scontext_len > len) {
51261da177e4SLinus Torvalds 		err = -ERANGE;
51271da177e4SLinus Torvalds 		goto out_len;
51281da177e4SLinus Torvalds 	}
51291da177e4SLinus Torvalds 
51301da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
51311da177e4SLinus Torvalds 		err = -EFAULT;
51321da177e4SLinus Torvalds 
51331da177e4SLinus Torvalds out_len:
51341da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
51351da177e4SLinus Torvalds 		err = -EFAULT;
51361da177e4SLinus Torvalds 	kfree(scontext);
51371da177e4SLinus Torvalds 	return err;
51381da177e4SLinus Torvalds }
51391da177e4SLinus Torvalds 
5140dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
51412c7946a7SCatherine Zhang {
5142dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
514375e22910SPaul Moore 	u16 family;
5144899134f2SPaul Moore 	struct inode_security_struct *isec;
5145877ce7c1SCatherine Zhang 
5146aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
5147aa862900SPaul Moore 		family = PF_INET;
5148aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
5149aa862900SPaul Moore 		family = PF_INET6;
5150aa862900SPaul Moore 	else if (sock)
515175e22910SPaul Moore 		family = sock->sk->sk_family;
515275e22910SPaul Moore 	else
515375e22910SPaul Moore 		goto out;
515475e22910SPaul Moore 
5155899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
5156899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
5157899134f2SPaul Moore 		peer_secid = isec->sid;
5158899134f2SPaul Moore 	} else if (skb)
5159220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
51602c7946a7SCatherine Zhang 
516175e22910SPaul Moore out:
5162dc49c1f9SCatherine Zhang 	*secid = peer_secid;
516375e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
516475e22910SPaul Moore 		return -EINVAL;
516575e22910SPaul Moore 	return 0;
51662c7946a7SCatherine Zhang }
51672c7946a7SCatherine Zhang 
51687d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
51691da177e4SLinus Torvalds {
517084914b7eSPaul Moore 	struct sk_security_struct *sksec;
517184914b7eSPaul Moore 
517284914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
517384914b7eSPaul Moore 	if (!sksec)
517484914b7eSPaul Moore 		return -ENOMEM;
517584914b7eSPaul Moore 
517684914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
517784914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
51785dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
517984914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
518084914b7eSPaul Moore 	sk->sk_security = sksec;
518184914b7eSPaul Moore 
518284914b7eSPaul Moore 	return 0;
51831da177e4SLinus Torvalds }
51841da177e4SLinus Torvalds 
51851da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
51861da177e4SLinus Torvalds {
518784914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
518884914b7eSPaul Moore 
518984914b7eSPaul Moore 	sk->sk_security = NULL;
519084914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
519184914b7eSPaul Moore 	kfree(sksec);
51921da177e4SLinus Torvalds }
51931da177e4SLinus Torvalds 
5194892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5195892c141eSVenkat Yekkirala {
5196dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
5197dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
5198892c141eSVenkat Yekkirala 
5199dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
5200dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
5201dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
520299f59ed0SPaul Moore 
5203dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
5204892c141eSVenkat Yekkirala }
5205892c141eSVenkat Yekkirala 
5206beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5207d28d1e08STrent Jaeger {
5208d28d1e08STrent Jaeger 	if (!sk)
5209beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
5210892c141eSVenkat Yekkirala 	else {
5211892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
5212d28d1e08STrent Jaeger 
5213beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
5214892c141eSVenkat Yekkirala 	}
5215d28d1e08STrent Jaeger }
5216d28d1e08STrent Jaeger 
52179a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
52184237c75cSVenkat Yekkirala {
52195d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
52205d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
52214237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52224237c75cSVenkat Yekkirala 
52232873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
52242873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
52254237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5226220deb96SPaul Moore 	sksec->sclass = isec->sclass;
52274237c75cSVenkat Yekkirala }
52284237c75cSVenkat Yekkirala 
5229d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5230d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5231d452930fSRichard Haines  * already present).
5232d452930fSRichard Haines  */
5233d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5234d452930fSRichard Haines 				      struct sk_buff *skb)
5235d452930fSRichard Haines {
5236d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5237d452930fSRichard Haines 	struct common_audit_data ad;
5238d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5239d452930fSRichard Haines 	u8 peerlbl_active;
5240d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5241d452930fSRichard Haines 	u32 conn_sid;
5242d452930fSRichard Haines 	int err = 0;
5243d452930fSRichard Haines 
5244aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5245d452930fSRichard Haines 		return 0;
5246d452930fSRichard Haines 
5247d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5248d452930fSRichard Haines 
5249d452930fSRichard Haines 	if (peerlbl_active) {
5250d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5251d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5252d452930fSRichard Haines 		 */
5253d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5254d452930fSRichard Haines 					      &peer_sid);
5255d452930fSRichard Haines 		if (err)
5256d452930fSRichard Haines 			return err;
5257d452930fSRichard Haines 
5258d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5259d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5260d452930fSRichard Haines 	}
5261d452930fSRichard Haines 
5262d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5263d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5264d452930fSRichard Haines 
5265d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5266d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5267d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5268d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5269d452930fSRichard Haines 		 */
5270d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5271d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5272d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5273d452930fSRichard Haines 		 * consistency among the peer SIDs.
5274d452930fSRichard Haines 		 */
5275d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5276d452930fSRichard Haines 		ad.u.net = &net;
5277d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
52786b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
52796b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5280d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5281d452930fSRichard Haines 		if (err)
5282d452930fSRichard Haines 			return err;
5283d452930fSRichard Haines 	}
5284d452930fSRichard Haines 
5285d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5286d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5287d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5288d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5289d452930fSRichard Haines 	 * plug this into the new socket.
5290d452930fSRichard Haines 	 */
5291d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5292d452930fSRichard Haines 	if (err)
5293d452930fSRichard Haines 		return err;
5294d452930fSRichard Haines 
5295d452930fSRichard Haines 	ep->secid = conn_sid;
5296d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5297d452930fSRichard Haines 
5298d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5299d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5300d452930fSRichard Haines }
5301d452930fSRichard Haines 
5302d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5303d452930fSRichard Haines  * based on their @optname.
5304d452930fSRichard Haines  */
5305d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5306d452930fSRichard Haines 				     struct sockaddr *address,
5307d452930fSRichard Haines 				     int addrlen)
5308d452930fSRichard Haines {
5309d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5310d452930fSRichard Haines 	void *addr_buf;
5311d452930fSRichard Haines 	struct sockaddr *addr;
5312d452930fSRichard Haines 	struct socket *sock;
5313d452930fSRichard Haines 
5314aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5315d452930fSRichard Haines 		return 0;
5316d452930fSRichard Haines 
5317d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5318d452930fSRichard Haines 	sock = sk->sk_socket;
5319d452930fSRichard Haines 	addr_buf = address;
5320d452930fSRichard Haines 
5321d452930fSRichard Haines 	while (walk_size < addrlen) {
5322d452930fSRichard Haines 		addr = addr_buf;
5323d452930fSRichard Haines 		switch (addr->sa_family) {
53244152dc91SAlexey Kodanev 		case AF_UNSPEC:
5325d452930fSRichard Haines 		case AF_INET:
5326d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5327d452930fSRichard Haines 			break;
5328d452930fSRichard Haines 		case AF_INET6:
5329d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5330d452930fSRichard Haines 			break;
5331d452930fSRichard Haines 		default:
53324152dc91SAlexey Kodanev 			return -EINVAL;
5333d452930fSRichard Haines 		}
5334d452930fSRichard Haines 
5335d452930fSRichard Haines 		err = -EINVAL;
5336d452930fSRichard Haines 		switch (optname) {
5337d452930fSRichard Haines 		/* Bind checks */
5338d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5339d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5340d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5341d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5342d452930fSRichard Haines 			break;
5343d452930fSRichard Haines 		/* Connect checks */
5344d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5345d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5346d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5347d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5348d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5349d452930fSRichard Haines 			if (err)
5350d452930fSRichard Haines 				return err;
5351d452930fSRichard Haines 
5352d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5353d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5354d452930fSRichard Haines 			 * therefore selinux_netlbl_socket_connect_locked() is
5355d452930fSRichard Haines 			 * is called here. The situations handled are:
5356d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5357d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5358d452930fSRichard Haines 			 * primary address is selected.
5359d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5360d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5361d452930fSRichard Haines 			 * selinux_socket_connect().
5362d452930fSRichard Haines 			 */
5363d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5364d452930fSRichard Haines 			break;
5365d452930fSRichard Haines 		}
5366d452930fSRichard Haines 
5367d452930fSRichard Haines 		if (err)
5368d452930fSRichard Haines 			return err;
5369d452930fSRichard Haines 
5370d452930fSRichard Haines 		addr_buf += len;
5371d452930fSRichard Haines 		walk_size += len;
5372d452930fSRichard Haines 	}
5373d452930fSRichard Haines 
5374d452930fSRichard Haines 	return 0;
5375d452930fSRichard Haines }
5376d452930fSRichard Haines 
5377d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5378d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5379d452930fSRichard Haines 				  struct sock *newsk)
5380d452930fSRichard Haines {
5381d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5382d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5383d452930fSRichard Haines 
5384d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5385d452930fSRichard Haines 	 * the non-sctp clone version.
5386d452930fSRichard Haines 	 */
5387aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5388d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5389d452930fSRichard Haines 
5390d452930fSRichard Haines 	newsksec->sid = ep->secid;
5391d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5392d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5393d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5394d452930fSRichard Haines }
5395d452930fSRichard Haines 
53969a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
53974237c75cSVenkat Yekkirala 				     struct request_sock *req)
53984237c75cSVenkat Yekkirala {
53994237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
54004237c75cSVenkat Yekkirala 	int err;
54010b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5402446b8024SPaul Moore 	u32 connsid;
54034237c75cSVenkat Yekkirala 	u32 peersid;
54044237c75cSVenkat Yekkirala 
5405aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5406220deb96SPaul Moore 	if (err)
5407220deb96SPaul Moore 		return err;
5408446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
54094237c75cSVenkat Yekkirala 	if (err)
54104237c75cSVenkat Yekkirala 		return err;
5411446b8024SPaul Moore 	req->secid = connsid;
54126b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5413389fb800SPaul Moore 
5414389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
54154237c75cSVenkat Yekkirala }
54164237c75cSVenkat Yekkirala 
54179a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
54189a673e56SAdrian Bunk 				   const struct request_sock *req)
54194237c75cSVenkat Yekkirala {
54204237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
54214237c75cSVenkat Yekkirala 
54224237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
54236b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
54244237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
54254237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
54264237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
54274237c75cSVenkat Yekkirala 	   time it will have been created and available. */
542899f59ed0SPaul Moore 
54299f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
54309f2ad665SPaul Moore 	 * thread with access to newsksec */
5431389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
54324237c75cSVenkat Yekkirala }
54334237c75cSVenkat Yekkirala 
5434014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
54356b877699SVenkat Yekkirala {
5436aa862900SPaul Moore 	u16 family = sk->sk_family;
54376b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
54386b877699SVenkat Yekkirala 
5439aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5440aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5441aa862900SPaul Moore 		family = PF_INET;
5442aa862900SPaul Moore 
5443aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
54446b877699SVenkat Yekkirala }
54456b877699SVenkat Yekkirala 
54462606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
54472606fd1fSEric Paris {
54482606fd1fSEric Paris 	const struct task_security_struct *__tsec;
54492606fd1fSEric Paris 	u32 tsid;
54502606fd1fSEric Paris 
54512606fd1fSEric Paris 	__tsec = current_security();
54522606fd1fSEric Paris 	tsid = __tsec->sid;
54532606fd1fSEric Paris 
54546b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
54556b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
54566b6bc620SStephen Smalley 			    NULL);
54572606fd1fSEric Paris }
54582606fd1fSEric Paris 
54592606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
54602606fd1fSEric Paris {
54612606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
54622606fd1fSEric Paris }
54632606fd1fSEric Paris 
54642606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
54652606fd1fSEric Paris {
54662606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
54672606fd1fSEric Paris }
54682606fd1fSEric Paris 
54699a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
54709a673e56SAdrian Bunk 				      struct flowi *fl)
54714237c75cSVenkat Yekkirala {
54721d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
54734237c75cSVenkat Yekkirala }
54744237c75cSVenkat Yekkirala 
54755dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
54765dbbaf2dSPaul Moore {
54775dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
54785dbbaf2dSPaul Moore 
54795dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
54805dbbaf2dSPaul Moore 	if (!tunsec)
54815dbbaf2dSPaul Moore 		return -ENOMEM;
54825dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
54835dbbaf2dSPaul Moore 
54845dbbaf2dSPaul Moore 	*security = tunsec;
54855dbbaf2dSPaul Moore 	return 0;
54865dbbaf2dSPaul Moore }
54875dbbaf2dSPaul Moore 
54885dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
54895dbbaf2dSPaul Moore {
54905dbbaf2dSPaul Moore 	kfree(security);
54915dbbaf2dSPaul Moore }
54925dbbaf2dSPaul Moore 
5493ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5494ed6d76e4SPaul Moore {
5495ed6d76e4SPaul Moore 	u32 sid = current_sid();
5496ed6d76e4SPaul Moore 
5497ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5498ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5499ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5500ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5501ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5502ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5503ed6d76e4SPaul Moore 
55046b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55056b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5506ed6d76e4SPaul Moore 			    NULL);
5507ed6d76e4SPaul Moore }
5508ed6d76e4SPaul Moore 
55095dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5510ed6d76e4SPaul Moore {
55115dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
55125dbbaf2dSPaul Moore 
55136b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
55146b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
55155dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
55165dbbaf2dSPaul Moore }
55175dbbaf2dSPaul Moore 
55185dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
55195dbbaf2dSPaul Moore {
55205dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5521ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5522ed6d76e4SPaul Moore 
5523ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5524ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5525ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5526ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5527ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5528ed6d76e4SPaul Moore 	 * protocols were being used */
5529ed6d76e4SPaul Moore 
55305dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5531ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
55325dbbaf2dSPaul Moore 
55335dbbaf2dSPaul Moore 	return 0;
5534ed6d76e4SPaul Moore }
5535ed6d76e4SPaul Moore 
55365dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5537ed6d76e4SPaul Moore {
55385dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5539ed6d76e4SPaul Moore 	u32 sid = current_sid();
5540ed6d76e4SPaul Moore 	int err;
5541ed6d76e4SPaul Moore 
55426b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
55436b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5544ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5545ed6d76e4SPaul Moore 	if (err)
5546ed6d76e4SPaul Moore 		return err;
55476b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
55486b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5549ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5550ed6d76e4SPaul Moore 	if (err)
5551ed6d76e4SPaul Moore 		return err;
55525dbbaf2dSPaul Moore 	tunsec->sid = sid;
5553ed6d76e4SPaul Moore 
5554ed6d76e4SPaul Moore 	return 0;
5555ed6d76e4SPaul Moore }
5556ed6d76e4SPaul Moore 
55571da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
55581da177e4SLinus Torvalds {
55591da177e4SLinus Torvalds 	int err = 0;
55601da177e4SLinus Torvalds 	u32 perm;
55611da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5562253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
55631da177e4SLinus Torvalds 
556477954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
55651da177e4SLinus Torvalds 		err = -EINVAL;
55661da177e4SLinus Torvalds 		goto out;
55671da177e4SLinus Torvalds 	}
5568b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
55691da177e4SLinus Torvalds 
5570253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
55711da177e4SLinus Torvalds 	if (err) {
55721da177e4SLinus Torvalds 		if (err == -EINVAL) {
557376319946SVladis Dronov 			pr_warn_ratelimited("SELinux: unrecognized netlink"
557476319946SVladis Dronov 			       " message: protocol=%hu nlmsg_type=%hu sclass=%s"
557576319946SVladis Dronov 			       " pig=%d comm=%s\n",
5576cded3fffSMarek Milkovic 			       sk->sk_protocol, nlh->nlmsg_type,
557776319946SVladis Dronov 			       secclass_map[sksec->sclass - 1].name,
557876319946SVladis Dronov 			       task_pid_nr(current), current->comm);
5579e5a5ca96SPaul Moore 			if (!enforcing_enabled(&selinux_state) ||
5580aa8e712cSStephen Smalley 			    security_get_allow_unknown(&selinux_state))
55811da177e4SLinus Torvalds 				err = 0;
55821da177e4SLinus Torvalds 		}
55831da177e4SLinus Torvalds 
55841da177e4SLinus Torvalds 		/* Ignore */
55851da177e4SLinus Torvalds 		if (err == -ENOENT)
55861da177e4SLinus Torvalds 			err = 0;
55871da177e4SLinus Torvalds 		goto out;
55881da177e4SLinus Torvalds 	}
55891da177e4SLinus Torvalds 
5590be0554c9SStephen Smalley 	err = sock_has_perm(sk, perm);
55911da177e4SLinus Torvalds out:
55921da177e4SLinus Torvalds 	return err;
55931da177e4SLinus Torvalds }
55941da177e4SLinus Torvalds 
55951da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
55961da177e4SLinus Torvalds 
5597cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5598cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5599effad8dfSPaul Moore 				       u16 family)
56001da177e4SLinus Torvalds {
5601dfaebe98SPaul Moore 	int err;
5602effad8dfSPaul Moore 	char *addrp;
5603effad8dfSPaul Moore 	u32 peer_sid;
56042bf49690SThomas Liu 	struct common_audit_data ad;
560548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5606effad8dfSPaul Moore 	u8 secmark_active;
5607948bf85cSPaul Moore 	u8 netlbl_active;
5608effad8dfSPaul Moore 	u8 peerlbl_active;
56094237c75cSVenkat Yekkirala 
5610aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5611effad8dfSPaul Moore 		return NF_ACCEPT;
56124237c75cSVenkat Yekkirala 
5613effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5614948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
56152be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5616effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5617effad8dfSPaul Moore 		return NF_ACCEPT;
56184237c75cSVenkat Yekkirala 
5619d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5620d8395c87SPaul Moore 		return NF_DROP;
5621d8395c87SPaul Moore 
562250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
562348c62af6SEric Paris 	ad.u.net = &net;
5624cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
562548c62af6SEric Paris 	ad.u.net->family = family;
5626effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5627effad8dfSPaul Moore 		return NF_DROP;
56281da177e4SLinus Torvalds 
5629dfaebe98SPaul Moore 	if (peerlbl_active) {
5630cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5631cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5632dfaebe98SPaul Moore 		if (err) {
5633a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5634effad8dfSPaul Moore 			return NF_DROP;
5635dfaebe98SPaul Moore 		}
5636dfaebe98SPaul Moore 	}
5637effad8dfSPaul Moore 
5638effad8dfSPaul Moore 	if (secmark_active)
56396b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56406b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5641effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5642effad8dfSPaul Moore 			return NF_DROP;
5643effad8dfSPaul Moore 
5644948bf85cSPaul Moore 	if (netlbl_active)
5645948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5646948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5647948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5648948bf85cSPaul Moore 		 * protection */
5649948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5650948bf85cSPaul Moore 			return NF_DROP;
5651948bf85cSPaul Moore 
5652effad8dfSPaul Moore 	return NF_ACCEPT;
5653effad8dfSPaul Moore }
5654effad8dfSPaul Moore 
565506198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5656effad8dfSPaul Moore 					 struct sk_buff *skb,
5657238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5658effad8dfSPaul Moore {
5659238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5660effad8dfSPaul Moore }
5661effad8dfSPaul Moore 
56621a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
566306198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5664effad8dfSPaul Moore 					 struct sk_buff *skb,
5665238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5666effad8dfSPaul Moore {
5667238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5668effad8dfSPaul Moore }
5669effad8dfSPaul Moore #endif	/* IPV6 */
5670effad8dfSPaul Moore 
5671948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5672948bf85cSPaul Moore 				      u16 family)
5673948bf85cSPaul Moore {
567447180068SPaul Moore 	struct sock *sk;
5675948bf85cSPaul Moore 	u32 sid;
5676948bf85cSPaul Moore 
5677948bf85cSPaul Moore 	if (!netlbl_enabled())
5678948bf85cSPaul Moore 		return NF_ACCEPT;
5679948bf85cSPaul Moore 
5680948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5681948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5682948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
568347180068SPaul Moore 	sk = skb->sk;
568447180068SPaul Moore 	if (sk) {
568547180068SPaul Moore 		struct sk_security_struct *sksec;
568647180068SPaul Moore 
5687e446f9dfSEric Dumazet 		if (sk_listener(sk))
568847180068SPaul Moore 			/* if the socket is the listening state then this
568947180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
569047180068SPaul Moore 			 * be labeled based on the connection/request_sock and
569147180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
569247180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
569347180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
569447180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
569547180068SPaul Moore 			 * as any IP option based labeling should be copied
569647180068SPaul Moore 			 * from the initial connection request (in the IP
569747180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
569847180068SPaul Moore 			 * security label in the packet itself this is the
569947180068SPaul Moore 			 * best we can do. */
570047180068SPaul Moore 			return NF_ACCEPT;
570147180068SPaul Moore 
570247180068SPaul Moore 		/* standard practice, label using the parent socket */
570347180068SPaul Moore 		sksec = sk->sk_security;
5704948bf85cSPaul Moore 		sid = sksec->sid;
5705948bf85cSPaul Moore 	} else
5706948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5707948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5708948bf85cSPaul Moore 		return NF_DROP;
5709948bf85cSPaul Moore 
5710948bf85cSPaul Moore 	return NF_ACCEPT;
5711948bf85cSPaul Moore }
5712948bf85cSPaul Moore 
571306198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5714948bf85cSPaul Moore 					struct sk_buff *skb,
5715238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5716948bf85cSPaul Moore {
5717948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5718948bf85cSPaul Moore }
5719948bf85cSPaul Moore 
57201a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
57212917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
57222917f57bSHuw Davies 					struct sk_buff *skb,
57232917f57bSHuw Davies 					const struct nf_hook_state *state)
57242917f57bSHuw Davies {
57252917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
57262917f57bSHuw Davies }
57272917f57bSHuw Davies #endif	/* IPV6 */
57282917f57bSHuw Davies 
5729effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5730effad8dfSPaul Moore 						int ifindex,
5731d8395c87SPaul Moore 						u16 family)
57324e5ab4cbSJames Morris {
573354abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
57344237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
57352bf49690SThomas Liu 	struct common_audit_data ad;
573648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5737d8395c87SPaul Moore 	char *addrp;
5738d8395c87SPaul Moore 	u8 proto;
57394e5ab4cbSJames Morris 
5740effad8dfSPaul Moore 	if (sk == NULL)
5741effad8dfSPaul Moore 		return NF_ACCEPT;
57424237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
57434e5ab4cbSJames Morris 
574450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
574548c62af6SEric Paris 	ad.u.net = &net;
574648c62af6SEric Paris 	ad.u.net->netif = ifindex;
574748c62af6SEric Paris 	ad.u.net->family = family;
5748d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5749d8395c87SPaul Moore 		return NF_DROP;
5750d8395c87SPaul Moore 
575158bfbb51SPaul Moore 	if (selinux_secmark_enabled())
57526b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
57536b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5754d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
57552fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
57561da177e4SLinus Torvalds 
5757d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
57582fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5759effad8dfSPaul Moore 
5760effad8dfSPaul Moore 	return NF_ACCEPT;
5761effad8dfSPaul Moore }
5762effad8dfSPaul Moore 
5763cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5764cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5765effad8dfSPaul Moore 					 u16 family)
5766effad8dfSPaul Moore {
5767effad8dfSPaul Moore 	u32 secmark_perm;
5768effad8dfSPaul Moore 	u32 peer_sid;
5769cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5770effad8dfSPaul Moore 	struct sock *sk;
57712bf49690SThomas Liu 	struct common_audit_data ad;
577248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5773effad8dfSPaul Moore 	char *addrp;
5774effad8dfSPaul Moore 	u8 secmark_active;
5775effad8dfSPaul Moore 	u8 peerlbl_active;
5776effad8dfSPaul Moore 
5777effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5778effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5779effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5780effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5781aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5782d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5783c0828e50SPaul Moore 
5784effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
57852be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5786effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5787effad8dfSPaul Moore 		return NF_ACCEPT;
5788effad8dfSPaul Moore 
578954abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5790c0828e50SPaul Moore 
5791effad8dfSPaul Moore #ifdef CONFIG_XFRM
5792effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5793effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5794effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5795effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5796effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5797c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5798c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5799c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5800c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5801c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5802c0828e50SPaul Moore 	 *       connection. */
5803c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5804e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5805effad8dfSPaul Moore 		return NF_ACCEPT;
5806effad8dfSPaul Moore #endif
5807effad8dfSPaul Moore 
5808d8395c87SPaul Moore 	if (sk == NULL) {
5809446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5810446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5811446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5812446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
58134a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5814d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5815d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
581604f6d70fSEric Paris 				return NF_DROP;
58174a7ab3dcSSteffen Klassert 		} else {
58184a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5819d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
58204a7ab3dcSSteffen Klassert 		}
5821e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5822446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5823446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5824446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5825446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5826446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5827446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5828446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5829446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5830446b8024SPaul Moore 		 * for similar problems. */
5831446b8024SPaul Moore 		u32 skb_sid;
5832e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5833e446f9dfSEric Dumazet 
5834e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5835446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5836446b8024SPaul Moore 			return NF_DROP;
5837c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5838c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5839c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5840c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5841c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5842c0828e50SPaul Moore 		 * pass the packet. */
5843c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5844c0828e50SPaul Moore 			switch (family) {
5845c0828e50SPaul Moore 			case PF_INET:
5846c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5847c0828e50SPaul Moore 					return NF_ACCEPT;
5848c0828e50SPaul Moore 				break;
5849c0828e50SPaul Moore 			case PF_INET6:
5850c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5851c0828e50SPaul Moore 					return NF_ACCEPT;
5852a7a91a19SPaul Moore 				break;
5853c0828e50SPaul Moore 			default:
5854c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5855c0828e50SPaul Moore 			}
5856c0828e50SPaul Moore 		}
5857446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5858446b8024SPaul Moore 			return NF_DROP;
5859446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5860d8395c87SPaul Moore 	} else {
5861446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5862446b8024SPaul Moore 		 * associated socket. */
5863effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5864effad8dfSPaul Moore 		peer_sid = sksec->sid;
5865effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5866effad8dfSPaul Moore 	}
5867effad8dfSPaul Moore 
586850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
586948c62af6SEric Paris 	ad.u.net = &net;
587048c62af6SEric Paris 	ad.u.net->netif = ifindex;
587148c62af6SEric Paris 	ad.u.net->family = family;
5872d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
587304f6d70fSEric Paris 		return NF_DROP;
5874d8395c87SPaul Moore 
5875effad8dfSPaul Moore 	if (secmark_active)
58766b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58776b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5878effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
58791f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5880effad8dfSPaul Moore 
5881effad8dfSPaul Moore 	if (peerlbl_active) {
5882effad8dfSPaul Moore 		u32 if_sid;
5883effad8dfSPaul Moore 		u32 node_sid;
5884effad8dfSPaul Moore 
5885cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
588604f6d70fSEric Paris 			return NF_DROP;
58876b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58886b6bc620SStephen Smalley 				 peer_sid, if_sid,
5889effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
58901f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5891effad8dfSPaul Moore 
5892effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
589304f6d70fSEric Paris 			return NF_DROP;
58946b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
58956b6bc620SStephen Smalley 				 peer_sid, node_sid,
5896effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
58971f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5898effad8dfSPaul Moore 	}
5899effad8dfSPaul Moore 
5900effad8dfSPaul Moore 	return NF_ACCEPT;
5901effad8dfSPaul Moore }
5902effad8dfSPaul Moore 
590306198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5904a224be76SDavid S. Miller 					   struct sk_buff *skb,
5905238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
59061da177e4SLinus Torvalds {
5907238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
59081da177e4SLinus Torvalds }
59091da177e4SLinus Torvalds 
59101a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
591106198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5912a224be76SDavid S. Miller 					   struct sk_buff *skb,
5913238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
59141da177e4SLinus Torvalds {
5915238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
59161da177e4SLinus Torvalds }
59171da177e4SLinus Torvalds #endif	/* IPV6 */
59181da177e4SLinus Torvalds 
59191da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
59201da177e4SLinus Torvalds 
59211da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
59221da177e4SLinus Torvalds {
5923941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
59241da177e4SLinus Torvalds }
59251da177e4SLinus Torvalds 
5926be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm,
59271da177e4SLinus Torvalds 			      u16 sclass)
59281da177e4SLinus Torvalds {
59291da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59301da177e4SLinus Torvalds 
593189d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
59321da177e4SLinus Torvalds 	if (!isec)
59331da177e4SLinus Torvalds 		return -ENOMEM;
59341da177e4SLinus Torvalds 
59351da177e4SLinus Torvalds 	isec->sclass = sclass;
5936be0554c9SStephen Smalley 	isec->sid = current_sid();
59371da177e4SLinus Torvalds 	perm->security = isec;
59381da177e4SLinus Torvalds 
59391da177e4SLinus Torvalds 	return 0;
59401da177e4SLinus Torvalds }
59411da177e4SLinus Torvalds 
59421da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
59431da177e4SLinus Torvalds {
59441da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
59451da177e4SLinus Torvalds 	perm->security = NULL;
59461da177e4SLinus Torvalds 	kfree(isec);
59471da177e4SLinus Torvalds }
59481da177e4SLinus Torvalds 
59491da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
59501da177e4SLinus Torvalds {
59511da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59521da177e4SLinus Torvalds 
595389d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
59541da177e4SLinus Torvalds 	if (!msec)
59551da177e4SLinus Torvalds 		return -ENOMEM;
59561da177e4SLinus Torvalds 
59571da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
59581da177e4SLinus Torvalds 	msg->security = msec;
59591da177e4SLinus Torvalds 
59601da177e4SLinus Torvalds 	return 0;
59611da177e4SLinus Torvalds }
59621da177e4SLinus Torvalds 
59631da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
59641da177e4SLinus Torvalds {
59651da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
59661da177e4SLinus Torvalds 
59671da177e4SLinus Torvalds 	msg->security = NULL;
59681da177e4SLinus Torvalds 	kfree(msec);
59691da177e4SLinus Torvalds }
59701da177e4SLinus Torvalds 
59711da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
59726af963f1SStephen Smalley 			u32 perms)
59731da177e4SLinus Torvalds {
59741da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59752bf49690SThomas Liu 	struct common_audit_data ad;
5976275bb41eSDavid Howells 	u32 sid = current_sid();
59771da177e4SLinus Torvalds 
59781da177e4SLinus Torvalds 	isec = ipc_perms->security;
59791da177e4SLinus Torvalds 
598050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59811da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
59821da177e4SLinus Torvalds 
59836b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59846b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
59851da177e4SLinus Torvalds }
59861da177e4SLinus Torvalds 
59871da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
59881da177e4SLinus Torvalds {
59891da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
59901da177e4SLinus Torvalds }
59911da177e4SLinus Torvalds 
59921da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
59931da177e4SLinus Torvalds {
59941da177e4SLinus Torvalds 	msg_msg_free_security(msg);
59951da177e4SLinus Torvalds }
59961da177e4SLinus Torvalds 
59971da177e4SLinus Torvalds /* message queue security operations */
5998d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
59991da177e4SLinus Torvalds {
60001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60012bf49690SThomas Liu 	struct common_audit_data ad;
6002275bb41eSDavid Howells 	u32 sid = current_sid();
60031da177e4SLinus Torvalds 	int rc;
60041da177e4SLinus Torvalds 
6005d8c6e854SEric W. Biederman 	rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
60061da177e4SLinus Torvalds 	if (rc)
60071da177e4SLinus Torvalds 		return rc;
60081da177e4SLinus Torvalds 
6009d8c6e854SEric W. Biederman 	isec = msq->security;
60101da177e4SLinus Torvalds 
601150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6012d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60131da177e4SLinus Torvalds 
60146b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60156b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
60161da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
60171da177e4SLinus Torvalds 	if (rc) {
6018d8c6e854SEric W. Biederman 		ipc_free_security(msq);
60191da177e4SLinus Torvalds 		return rc;
60201da177e4SLinus Torvalds 	}
60211da177e4SLinus Torvalds 	return 0;
60221da177e4SLinus Torvalds }
60231da177e4SLinus Torvalds 
6024d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
60251da177e4SLinus Torvalds {
6026d8c6e854SEric W. Biederman 	ipc_free_security(msq);
60271da177e4SLinus Torvalds }
60281da177e4SLinus Torvalds 
6029d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
60301da177e4SLinus Torvalds {
60311da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60322bf49690SThomas Liu 	struct common_audit_data ad;
6033275bb41eSDavid Howells 	u32 sid = current_sid();
60341da177e4SLinus Torvalds 
6035d8c6e854SEric W. Biederman 	isec = msq->security;
60361da177e4SLinus Torvalds 
603750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6038d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
60391da177e4SLinus Torvalds 
60406b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60416b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
60421da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
60431da177e4SLinus Torvalds }
60441da177e4SLinus Torvalds 
6045d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
60461da177e4SLinus Torvalds {
60471da177e4SLinus Torvalds 	int err;
60481da177e4SLinus Torvalds 	int perms;
60491da177e4SLinus Torvalds 
60501da177e4SLinus Torvalds 	switch (cmd) {
60511da177e4SLinus Torvalds 	case IPC_INFO:
60521da177e4SLinus Torvalds 	case MSG_INFO:
60531da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60546b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60556b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6056be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60571da177e4SLinus Torvalds 	case IPC_STAT:
60581da177e4SLinus Torvalds 	case MSG_STAT:
605923c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
60601da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
60611da177e4SLinus Torvalds 		break;
60621da177e4SLinus Torvalds 	case IPC_SET:
60631da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
60641da177e4SLinus Torvalds 		break;
60651da177e4SLinus Torvalds 	case IPC_RMID:
60661da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
60671da177e4SLinus Torvalds 		break;
60681da177e4SLinus Torvalds 	default:
60691da177e4SLinus Torvalds 		return 0;
60701da177e4SLinus Torvalds 	}
60711da177e4SLinus Torvalds 
6072d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
60731da177e4SLinus Torvalds 	return err;
60741da177e4SLinus Torvalds }
60751da177e4SLinus Torvalds 
6076d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
60771da177e4SLinus Torvalds {
60781da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60791da177e4SLinus Torvalds 	struct msg_security_struct *msec;
60802bf49690SThomas Liu 	struct common_audit_data ad;
6081275bb41eSDavid Howells 	u32 sid = current_sid();
60821da177e4SLinus Torvalds 	int rc;
60831da177e4SLinus Torvalds 
6084d8c6e854SEric W. Biederman 	isec = msq->security;
60851da177e4SLinus Torvalds 	msec = msg->security;
60861da177e4SLinus Torvalds 
60871da177e4SLinus Torvalds 	/*
60881da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
60891da177e4SLinus Torvalds 	 */
60901da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
60911da177e4SLinus Torvalds 		/*
60921da177e4SLinus Torvalds 		 * Compute new sid based on current process and
60931da177e4SLinus Torvalds 		 * message queue this message will be stored in
60941da177e4SLinus Torvalds 		 */
6095aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
6096aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
60971da177e4SLinus Torvalds 		if (rc)
60981da177e4SLinus Torvalds 			return rc;
60991da177e4SLinus Torvalds 	}
61001da177e4SLinus Torvalds 
610150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6102d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
61031da177e4SLinus Torvalds 
61041da177e4SLinus Torvalds 	/* Can this process write to the queue? */
61056b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61066b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
61071da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
61081da177e4SLinus Torvalds 	if (!rc)
61091da177e4SLinus Torvalds 		/* Can this process send the message */
61106b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61116b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
6112275bb41eSDavid Howells 				  MSG__SEND, &ad);
61131da177e4SLinus Torvalds 	if (!rc)
61141da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
61156b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61166b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
6117275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
61181da177e4SLinus Torvalds 
61191da177e4SLinus Torvalds 	return rc;
61201da177e4SLinus Torvalds }
61211da177e4SLinus Torvalds 
6122d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
61231da177e4SLinus Torvalds 				    struct task_struct *target,
61241da177e4SLinus Torvalds 				    long type, int mode)
61251da177e4SLinus Torvalds {
61261da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61271da177e4SLinus Torvalds 	struct msg_security_struct *msec;
61282bf49690SThomas Liu 	struct common_audit_data ad;
6129275bb41eSDavid Howells 	u32 sid = task_sid(target);
61301da177e4SLinus Torvalds 	int rc;
61311da177e4SLinus Torvalds 
6132d8c6e854SEric W. Biederman 	isec = msq->security;
61331da177e4SLinus Torvalds 	msec = msg->security;
61341da177e4SLinus Torvalds 
613550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6136d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
61371da177e4SLinus Torvalds 
61386b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61396b6bc620SStephen Smalley 			  sid, isec->sid,
61401da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
61411da177e4SLinus Torvalds 	if (!rc)
61426b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
61436b6bc620SStephen Smalley 				  sid, msec->sid,
61441da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
61451da177e4SLinus Torvalds 	return rc;
61461da177e4SLinus Torvalds }
61471da177e4SLinus Torvalds 
61481da177e4SLinus Torvalds /* Shared Memory security operations */
61497191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
61501da177e4SLinus Torvalds {
61511da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61522bf49690SThomas Liu 	struct common_audit_data ad;
6153275bb41eSDavid Howells 	u32 sid = current_sid();
61541da177e4SLinus Torvalds 	int rc;
61551da177e4SLinus Torvalds 
61567191adffSEric W. Biederman 	rc = ipc_alloc_security(shp, SECCLASS_SHM);
61571da177e4SLinus Torvalds 	if (rc)
61581da177e4SLinus Torvalds 		return rc;
61591da177e4SLinus Torvalds 
61607191adffSEric W. Biederman 	isec = shp->security;
61611da177e4SLinus Torvalds 
616250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61637191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61641da177e4SLinus Torvalds 
61656b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
61666b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
61671da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
61681da177e4SLinus Torvalds 	if (rc) {
61697191adffSEric W. Biederman 		ipc_free_security(shp);
61701da177e4SLinus Torvalds 		return rc;
61711da177e4SLinus Torvalds 	}
61721da177e4SLinus Torvalds 	return 0;
61731da177e4SLinus Torvalds }
61741da177e4SLinus Torvalds 
61757191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp)
61761da177e4SLinus Torvalds {
61777191adffSEric W. Biederman 	ipc_free_security(shp);
61781da177e4SLinus Torvalds }
61791da177e4SLinus Torvalds 
61807191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
61811da177e4SLinus Torvalds {
61821da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
61832bf49690SThomas Liu 	struct common_audit_data ad;
6184275bb41eSDavid Howells 	u32 sid = current_sid();
61851da177e4SLinus Torvalds 
61867191adffSEric W. Biederman 	isec = shp->security;
61871da177e4SLinus Torvalds 
618850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
61897191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
61901da177e4SLinus Torvalds 
61916b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
61926b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
61931da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
61941da177e4SLinus Torvalds }
61951da177e4SLinus Torvalds 
61961da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
61977191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
61981da177e4SLinus Torvalds {
61991da177e4SLinus Torvalds 	int perms;
62001da177e4SLinus Torvalds 	int err;
62011da177e4SLinus Torvalds 
62021da177e4SLinus Torvalds 	switch (cmd) {
62031da177e4SLinus Torvalds 	case IPC_INFO:
62041da177e4SLinus Torvalds 	case SHM_INFO:
62051da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
62066b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
62076b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6208be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
62091da177e4SLinus Torvalds 	case IPC_STAT:
62101da177e4SLinus Torvalds 	case SHM_STAT:
6211c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
62121da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
62131da177e4SLinus Torvalds 		break;
62141da177e4SLinus Torvalds 	case IPC_SET:
62151da177e4SLinus Torvalds 		perms = SHM__SETATTR;
62161da177e4SLinus Torvalds 		break;
62171da177e4SLinus Torvalds 	case SHM_LOCK:
62181da177e4SLinus Torvalds 	case SHM_UNLOCK:
62191da177e4SLinus Torvalds 		perms = SHM__LOCK;
62201da177e4SLinus Torvalds 		break;
62211da177e4SLinus Torvalds 	case IPC_RMID:
62221da177e4SLinus Torvalds 		perms = SHM__DESTROY;
62231da177e4SLinus Torvalds 		break;
62241da177e4SLinus Torvalds 	default:
62251da177e4SLinus Torvalds 		return 0;
62261da177e4SLinus Torvalds 	}
62271da177e4SLinus Torvalds 
62287191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
62291da177e4SLinus Torvalds 	return err;
62301da177e4SLinus Torvalds }
62311da177e4SLinus Torvalds 
62327191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
62331da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
62341da177e4SLinus Torvalds {
62351da177e4SLinus Torvalds 	u32 perms;
62361da177e4SLinus Torvalds 
62371da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
62381da177e4SLinus Torvalds 		perms = SHM__READ;
62391da177e4SLinus Torvalds 	else
62401da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
62411da177e4SLinus Torvalds 
62427191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
62431da177e4SLinus Torvalds }
62441da177e4SLinus Torvalds 
62451da177e4SLinus Torvalds /* Semaphore security operations */
6246aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
62471da177e4SLinus Torvalds {
62481da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62492bf49690SThomas Liu 	struct common_audit_data ad;
6250275bb41eSDavid Howells 	u32 sid = current_sid();
62511da177e4SLinus Torvalds 	int rc;
62521da177e4SLinus Torvalds 
6253aefad959SEric W. Biederman 	rc = ipc_alloc_security(sma, SECCLASS_SEM);
62541da177e4SLinus Torvalds 	if (rc)
62551da177e4SLinus Torvalds 		return rc;
62561da177e4SLinus Torvalds 
6257aefad959SEric W. Biederman 	isec = sma->security;
62581da177e4SLinus Torvalds 
625950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6260aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62611da177e4SLinus Torvalds 
62626b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
62636b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
62641da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
62651da177e4SLinus Torvalds 	if (rc) {
6266aefad959SEric W. Biederman 		ipc_free_security(sma);
62671da177e4SLinus Torvalds 		return rc;
62681da177e4SLinus Torvalds 	}
62691da177e4SLinus Torvalds 	return 0;
62701da177e4SLinus Torvalds }
62711da177e4SLinus Torvalds 
6272aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma)
62731da177e4SLinus Torvalds {
6274aefad959SEric W. Biederman 	ipc_free_security(sma);
62751da177e4SLinus Torvalds }
62761da177e4SLinus Torvalds 
6277aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
62781da177e4SLinus Torvalds {
62791da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
62802bf49690SThomas Liu 	struct common_audit_data ad;
6281275bb41eSDavid Howells 	u32 sid = current_sid();
62821da177e4SLinus Torvalds 
6283aefad959SEric W. Biederman 	isec = sma->security;
62841da177e4SLinus Torvalds 
628550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6286aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
62871da177e4SLinus Torvalds 
62886b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
62896b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
62901da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
62911da177e4SLinus Torvalds }
62921da177e4SLinus Torvalds 
62931da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6294aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
62951da177e4SLinus Torvalds {
62961da177e4SLinus Torvalds 	int err;
62971da177e4SLinus Torvalds 	u32 perms;
62981da177e4SLinus Torvalds 
62991da177e4SLinus Torvalds 	switch (cmd) {
63001da177e4SLinus Torvalds 	case IPC_INFO:
63011da177e4SLinus Torvalds 	case SEM_INFO:
63021da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
63036b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
63046b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6305be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
63061da177e4SLinus Torvalds 	case GETPID:
63071da177e4SLinus Torvalds 	case GETNCNT:
63081da177e4SLinus Torvalds 	case GETZCNT:
63091da177e4SLinus Torvalds 		perms = SEM__GETATTR;
63101da177e4SLinus Torvalds 		break;
63111da177e4SLinus Torvalds 	case GETVAL:
63121da177e4SLinus Torvalds 	case GETALL:
63131da177e4SLinus Torvalds 		perms = SEM__READ;
63141da177e4SLinus Torvalds 		break;
63151da177e4SLinus Torvalds 	case SETVAL:
63161da177e4SLinus Torvalds 	case SETALL:
63171da177e4SLinus Torvalds 		perms = SEM__WRITE;
63181da177e4SLinus Torvalds 		break;
63191da177e4SLinus Torvalds 	case IPC_RMID:
63201da177e4SLinus Torvalds 		perms = SEM__DESTROY;
63211da177e4SLinus Torvalds 		break;
63221da177e4SLinus Torvalds 	case IPC_SET:
63231da177e4SLinus Torvalds 		perms = SEM__SETATTR;
63241da177e4SLinus Torvalds 		break;
63251da177e4SLinus Torvalds 	case IPC_STAT:
63261da177e4SLinus Torvalds 	case SEM_STAT:
6327a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
63281da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
63291da177e4SLinus Torvalds 		break;
63301da177e4SLinus Torvalds 	default:
63311da177e4SLinus Torvalds 		return 0;
63321da177e4SLinus Torvalds 	}
63331da177e4SLinus Torvalds 
6334aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
63351da177e4SLinus Torvalds 	return err;
63361da177e4SLinus Torvalds }
63371da177e4SLinus Torvalds 
6338aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
63391da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
63401da177e4SLinus Torvalds {
63411da177e4SLinus Torvalds 	u32 perms;
63421da177e4SLinus Torvalds 
63431da177e4SLinus Torvalds 	if (alter)
63441da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
63451da177e4SLinus Torvalds 	else
63461da177e4SLinus Torvalds 		perms = SEM__READ;
63471da177e4SLinus Torvalds 
6348aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
63491da177e4SLinus Torvalds }
63501da177e4SLinus Torvalds 
63511da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
63521da177e4SLinus Torvalds {
63531da177e4SLinus Torvalds 	u32 av = 0;
63541da177e4SLinus Torvalds 
63551da177e4SLinus Torvalds 	av = 0;
63561da177e4SLinus Torvalds 	if (flag & S_IRUGO)
63571da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
63581da177e4SLinus Torvalds 	if (flag & S_IWUGO)
63591da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
63601da177e4SLinus Torvalds 
63611da177e4SLinus Torvalds 	if (av == 0)
63621da177e4SLinus Torvalds 		return 0;
63631da177e4SLinus Torvalds 
63646af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
63651da177e4SLinus Torvalds }
63661da177e4SLinus Torvalds 
6367713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6368713a04aeSAhmed S. Darwish {
6369713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
6370713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6371713a04aeSAhmed S. Darwish }
6372713a04aeSAhmed S. Darwish 
63731da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
63741da177e4SLinus Torvalds {
63751da177e4SLinus Torvalds 	if (inode)
63761da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
63771da177e4SLinus Torvalds }
63781da177e4SLinus Torvalds 
63791da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
638004ff9708SAl Viro 			       char *name, char **value)
63811da177e4SLinus Torvalds {
6382275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
63838c8570fbSDustin Kirkland 	u32 sid;
63841da177e4SLinus Torvalds 	int error;
638504ff9708SAl Viro 	unsigned len;
63861da177e4SLinus Torvalds 
6387275bb41eSDavid Howells 	rcu_read_lock();
6388275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
63891da177e4SLinus Torvalds 
6390be0554c9SStephen Smalley 	if (current != p) {
63916b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63926b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6393be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6394be0554c9SStephen Smalley 		if (error)
6395be0554c9SStephen Smalley 			goto bad;
6396be0554c9SStephen Smalley 	}
6397be0554c9SStephen Smalley 
63981da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6399275bb41eSDavid Howells 		sid = __tsec->sid;
64001da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6401275bb41eSDavid Howells 		sid = __tsec->osid;
64021da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6403275bb41eSDavid Howells 		sid = __tsec->exec_sid;
64041da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6405275bb41eSDavid Howells 		sid = __tsec->create_sid;
64064eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6407275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
640842c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6409275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6410be0554c9SStephen Smalley 	else {
6411be0554c9SStephen Smalley 		error = -EINVAL;
6412be0554c9SStephen Smalley 		goto bad;
6413be0554c9SStephen Smalley 	}
6414275bb41eSDavid Howells 	rcu_read_unlock();
64151da177e4SLinus Torvalds 
64161da177e4SLinus Torvalds 	if (!sid)
64171da177e4SLinus Torvalds 		return 0;
64181da177e4SLinus Torvalds 
6419aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
642004ff9708SAl Viro 	if (error)
642104ff9708SAl Viro 		return error;
642204ff9708SAl Viro 	return len;
6423275bb41eSDavid Howells 
6424be0554c9SStephen Smalley bad:
6425275bb41eSDavid Howells 	rcu_read_unlock();
6426be0554c9SStephen Smalley 	return error;
64271da177e4SLinus Torvalds }
64281da177e4SLinus Torvalds 
6429b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
64301da177e4SLinus Torvalds {
64311da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6432d84f4f99SDavid Howells 	struct cred *new;
6433be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
64341da177e4SLinus Torvalds 	int error;
64351da177e4SLinus Torvalds 	char *str = value;
64361da177e4SLinus Torvalds 
64371da177e4SLinus Torvalds 	/*
64381da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
64391da177e4SLinus Torvalds 	 */
64401da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
64416b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64426b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6443be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
64441da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
64456b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64466b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6447be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
64484eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
64496b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64506b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6451be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
645242c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
64536b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64546b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6455be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
64561da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
64576b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
64586b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6459be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
64601da177e4SLinus Torvalds 	else
64611da177e4SLinus Torvalds 		error = -EINVAL;
64621da177e4SLinus Torvalds 	if (error)
64631da177e4SLinus Torvalds 		return error;
64641da177e4SLinus Torvalds 
64651da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6466a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
64671da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
64681da177e4SLinus Torvalds 			str[size-1] = 0;
64691da177e4SLinus Torvalds 			size--;
64701da177e4SLinus Torvalds 		}
6471aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6472aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
647312b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6474db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6475d6ea83ecSEric Paris 				struct audit_buffer *ab;
6476d6ea83ecSEric Paris 				size_t audit_size;
6477d6ea83ecSEric Paris 
6478d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6479d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6480d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6481d6ea83ecSEric Paris 					audit_size = size - 1;
6482d6ea83ecSEric Paris 				else
6483d6ea83ecSEric Paris 					audit_size = size;
6484cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6485cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6486cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6487d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6488d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6489d6ea83ecSEric Paris 				audit_log_end(ab);
6490d6ea83ecSEric Paris 
649112b29f34SStephen Smalley 				return error;
6492d6ea83ecSEric Paris 			}
6493aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6494aa8e712cSStephen Smalley 						      &selinux_state,
6495aa8e712cSStephen Smalley 						      value, size, &sid);
649612b29f34SStephen Smalley 		}
64971da177e4SLinus Torvalds 		if (error)
64981da177e4SLinus Torvalds 			return error;
64991da177e4SLinus Torvalds 	}
65001da177e4SLinus Torvalds 
6501d84f4f99SDavid Howells 	new = prepare_creds();
6502d84f4f99SDavid Howells 	if (!new)
6503d84f4f99SDavid Howells 		return -ENOMEM;
6504d84f4f99SDavid Howells 
65051da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
65061da177e4SLinus Torvalds 	   performed during the actual operation (execve,
65071da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6508d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
65091da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
65101da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
6511d84f4f99SDavid Howells 	tsec = new->security;
6512d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
65131da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6514d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
65151da177e4SLinus Torvalds 		tsec->create_sid = sid;
6516d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
65176b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
65186b6bc620SStephen Smalley 				     mysid, sid, SECCLASS_KEY, KEY__CREATE,
6519be0554c9SStephen Smalley 				     NULL);
65204eb582cfSMichael LeMay 		if (error)
6521d84f4f99SDavid Howells 			goto abort_change;
65224eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6523d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
652442c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6525d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6526d84f4f99SDavid Howells 		error = -EINVAL;
65271da177e4SLinus Torvalds 		if (sid == 0)
6528d84f4f99SDavid Howells 			goto abort_change;
6529d9250deaSKaiGai Kohei 
6530d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6531d84f4f99SDavid Howells 		error = -EPERM;
65325bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6533aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6534aa8e712cSStephen Smalley 							    tsec->sid, sid);
6535d84f4f99SDavid Howells 			if (error)
6536d84f4f99SDavid Howells 				goto abort_change;
65371da177e4SLinus Torvalds 		}
65381da177e4SLinus Torvalds 
65391da177e4SLinus Torvalds 		/* Check permissions for the transition. */
65406b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
65416b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
65421da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
65431da177e4SLinus Torvalds 		if (error)
6544d84f4f99SDavid Howells 			goto abort_change;
65451da177e4SLinus Torvalds 
65461da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
65471da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6548be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
65490c6181cbSPaul Moore 		if (ptsid != 0) {
65506b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
65516b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6552d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6553d84f4f99SDavid Howells 			if (error)
6554d84f4f99SDavid Howells 				goto abort_change;
6555d84f4f99SDavid Howells 		}
6556d84f4f99SDavid Howells 
6557d84f4f99SDavid Howells 		tsec->sid = sid;
6558d84f4f99SDavid Howells 	} else {
6559d84f4f99SDavid Howells 		error = -EINVAL;
6560d84f4f99SDavid Howells 		goto abort_change;
6561d84f4f99SDavid Howells 	}
6562d84f4f99SDavid Howells 
6563d84f4f99SDavid Howells 	commit_creds(new);
65641da177e4SLinus Torvalds 	return size;
6565d84f4f99SDavid Howells 
6566d84f4f99SDavid Howells abort_change:
6567d84f4f99SDavid Howells 	abort_creds(new);
6568d84f4f99SDavid Howells 	return error;
65691da177e4SLinus Torvalds }
65701da177e4SLinus Torvalds 
6571746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6572746df9b5SDavid Quigley {
6573746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6574746df9b5SDavid Quigley }
6575746df9b5SDavid Quigley 
6576dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6577dc49c1f9SCatherine Zhang {
6578aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6579aa8e712cSStephen Smalley 				       secdata, seclen);
6580dc49c1f9SCatherine Zhang }
6581dc49c1f9SCatherine Zhang 
65827bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
658363cb3449SDavid Howells {
6584aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6585aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
658663cb3449SDavid Howells }
658763cb3449SDavid Howells 
6588dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6589dc49c1f9SCatherine Zhang {
6590dc49c1f9SCatherine Zhang 	kfree(secdata);
6591dc49c1f9SCatherine Zhang }
6592dc49c1f9SCatherine Zhang 
65936f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
65946f3be9f5SAndreas Gruenbacher {
65956f3be9f5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
65966f3be9f5SAndreas Gruenbacher 
65979287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
65986f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
65999287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
66006f3be9f5SAndreas Gruenbacher }
66016f3be9f5SAndreas Gruenbacher 
66021ee65e37SDavid P. Quigley /*
66031ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
66041ee65e37SDavid P. Quigley  */
66051ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
66061ee65e37SDavid P. Quigley {
66071ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
66081ee65e37SDavid P. Quigley }
66091ee65e37SDavid P. Quigley 
66101ee65e37SDavid P. Quigley /*
66111ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
66121ee65e37SDavid P. Quigley  */
66131ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
66141ee65e37SDavid P. Quigley {
66151ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
66161ee65e37SDavid P. Quigley }
66171ee65e37SDavid P. Quigley 
66181ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
66191ee65e37SDavid P. Quigley {
66201ee65e37SDavid P. Quigley 	int len = 0;
66211ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
66221ee65e37SDavid P. Quigley 						ctx, true);
66231ee65e37SDavid P. Quigley 	if (len < 0)
66241ee65e37SDavid P. Quigley 		return len;
66251ee65e37SDavid P. Quigley 	*ctxlen = len;
66261ee65e37SDavid P. Quigley 	return 0;
66271ee65e37SDavid P. Quigley }
6628d720024eSMichael LeMay #ifdef CONFIG_KEYS
6629d720024eSMichael LeMay 
6630d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
66317e047ef5SDavid Howells 			     unsigned long flags)
6632d720024eSMichael LeMay {
6633d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6634d720024eSMichael LeMay 	struct key_security_struct *ksec;
6635d720024eSMichael LeMay 
6636d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6637d720024eSMichael LeMay 	if (!ksec)
6638d720024eSMichael LeMay 		return -ENOMEM;
6639d720024eSMichael LeMay 
6640d84f4f99SDavid Howells 	tsec = cred->security;
6641d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6642d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
66434eb582cfSMichael LeMay 	else
6644d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6645d720024eSMichael LeMay 
6646275bb41eSDavid Howells 	k->security = ksec;
6647d720024eSMichael LeMay 	return 0;
6648d720024eSMichael LeMay }
6649d720024eSMichael LeMay 
6650d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6651d720024eSMichael LeMay {
6652d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6653d720024eSMichael LeMay 
6654d720024eSMichael LeMay 	k->security = NULL;
6655d720024eSMichael LeMay 	kfree(ksec);
6656d720024eSMichael LeMay }
6657d720024eSMichael LeMay 
6658d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6659d84f4f99SDavid Howells 				  const struct cred *cred,
6660f5895943SDavid Howells 				  unsigned perm)
6661d720024eSMichael LeMay {
6662d720024eSMichael LeMay 	struct key *key;
6663d720024eSMichael LeMay 	struct key_security_struct *ksec;
6664275bb41eSDavid Howells 	u32 sid;
6665d720024eSMichael LeMay 
6666d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
6667d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
6668d720024eSMichael LeMay 	   appear to be created. */
6669d720024eSMichael LeMay 	if (perm == 0)
6670d720024eSMichael LeMay 		return 0;
6671d720024eSMichael LeMay 
6672d84f4f99SDavid Howells 	sid = cred_sid(cred);
6673275bb41eSDavid Howells 
6674275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6675275bb41eSDavid Howells 	ksec = key->security;
6676275bb41eSDavid Howells 
66776b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66786b6bc620SStephen Smalley 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6679d720024eSMichael LeMay }
6680d720024eSMichael LeMay 
668170a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
668270a5bb72SDavid Howells {
668370a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
668470a5bb72SDavid Howells 	char *context = NULL;
668570a5bb72SDavid Howells 	unsigned len;
668670a5bb72SDavid Howells 	int rc;
668770a5bb72SDavid Howells 
6688aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6689aa8e712cSStephen Smalley 				     &context, &len);
669070a5bb72SDavid Howells 	if (!rc)
669170a5bb72SDavid Howells 		rc = len;
669270a5bb72SDavid Howells 	*_buffer = context;
669370a5bb72SDavid Howells 	return rc;
669470a5bb72SDavid Howells }
66953a976fa6SDaniel Jurgens #endif
669670a5bb72SDavid Howells 
66973a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6698cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6699cfc4d882SDaniel Jurgens {
6700cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6701cfc4d882SDaniel Jurgens 	int err;
6702cfc4d882SDaniel Jurgens 	u32 sid = 0;
6703cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6704cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6705cfc4d882SDaniel Jurgens 
6706409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6707cfc4d882SDaniel Jurgens 	if (err)
6708cfc4d882SDaniel Jurgens 		return err;
6709cfc4d882SDaniel Jurgens 
6710cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6711cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6712cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6713cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
67146b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67156b6bc620SStephen Smalley 			    sec->sid, sid,
6716cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6717cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6718cfc4d882SDaniel Jurgens }
6719cfc4d882SDaniel Jurgens 
6720ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6721ab861dfcSDaniel Jurgens 					    u8 port_num)
6722ab861dfcSDaniel Jurgens {
6723ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6724ab861dfcSDaniel Jurgens 	int err;
6725ab861dfcSDaniel Jurgens 	u32 sid = 0;
6726ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6727ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6728ab861dfcSDaniel Jurgens 
6729aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6730aa8e712cSStephen Smalley 				      &sid);
6731ab861dfcSDaniel Jurgens 
6732ab861dfcSDaniel Jurgens 	if (err)
6733ab861dfcSDaniel Jurgens 		return err;
6734ab861dfcSDaniel Jurgens 
6735ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6736ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6737ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6738ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
67396b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
67406b6bc620SStephen Smalley 			    sec->sid, sid,
6741ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6742ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6743ab861dfcSDaniel Jurgens }
6744ab861dfcSDaniel Jurgens 
67453a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
67463a976fa6SDaniel Jurgens {
67473a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
67483a976fa6SDaniel Jurgens 
67493a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
67503a976fa6SDaniel Jurgens 	if (!sec)
67513a976fa6SDaniel Jurgens 		return -ENOMEM;
67523a976fa6SDaniel Jurgens 	sec->sid = current_sid();
67533a976fa6SDaniel Jurgens 
67543a976fa6SDaniel Jurgens 	*ib_sec = sec;
67553a976fa6SDaniel Jurgens 	return 0;
67563a976fa6SDaniel Jurgens }
67573a976fa6SDaniel Jurgens 
67583a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
67593a976fa6SDaniel Jurgens {
67603a976fa6SDaniel Jurgens 	kfree(ib_sec);
67613a976fa6SDaniel Jurgens }
6762d720024eSMichael LeMay #endif
6763d720024eSMichael LeMay 
6764ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6765ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6766ec27c356SChenbo Feng 				     unsigned int size)
6767ec27c356SChenbo Feng {
6768ec27c356SChenbo Feng 	u32 sid = current_sid();
6769ec27c356SChenbo Feng 	int ret;
6770ec27c356SChenbo Feng 
6771ec27c356SChenbo Feng 	switch (cmd) {
6772ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
67736b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67746b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6775ec27c356SChenbo Feng 				   NULL);
6776ec27c356SChenbo Feng 		break;
6777ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
67786b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
67796b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6780ec27c356SChenbo Feng 				   NULL);
6781ec27c356SChenbo Feng 		break;
6782ec27c356SChenbo Feng 	default:
6783ec27c356SChenbo Feng 		ret = 0;
6784ec27c356SChenbo Feng 		break;
6785ec27c356SChenbo Feng 	}
6786ec27c356SChenbo Feng 
6787ec27c356SChenbo Feng 	return ret;
6788ec27c356SChenbo Feng }
6789ec27c356SChenbo Feng 
6790ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6791ec27c356SChenbo Feng {
6792ec27c356SChenbo Feng 	u32 av = 0;
6793ec27c356SChenbo Feng 
6794ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6795ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6796ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6797ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6798ec27c356SChenbo Feng 	return av;
6799ec27c356SChenbo Feng }
6800ec27c356SChenbo Feng 
6801f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6802f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6803f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6804f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6805f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6806f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6807f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6808f66e448cSChenbo Feng  */
6809f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6810f66e448cSChenbo Feng {
6811f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6812f66e448cSChenbo Feng 	struct bpf_prog *prog;
6813f66e448cSChenbo Feng 	struct bpf_map *map;
6814f66e448cSChenbo Feng 	int ret;
6815f66e448cSChenbo Feng 
6816f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6817f66e448cSChenbo Feng 		map = file->private_data;
6818f66e448cSChenbo Feng 		bpfsec = map->security;
68196b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68206b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6821f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6822f66e448cSChenbo Feng 		if (ret)
6823f66e448cSChenbo Feng 			return ret;
6824f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6825f66e448cSChenbo Feng 		prog = file->private_data;
6826f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
68276b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
68286b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6829f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6830f66e448cSChenbo Feng 		if (ret)
6831f66e448cSChenbo Feng 			return ret;
6832f66e448cSChenbo Feng 	}
6833f66e448cSChenbo Feng 	return 0;
6834f66e448cSChenbo Feng }
6835f66e448cSChenbo Feng 
6836ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6837ec27c356SChenbo Feng {
6838ec27c356SChenbo Feng 	u32 sid = current_sid();
6839ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6840ec27c356SChenbo Feng 
6841ec27c356SChenbo Feng 	bpfsec = map->security;
68426b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68436b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6844ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6845ec27c356SChenbo Feng }
6846ec27c356SChenbo Feng 
6847ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6848ec27c356SChenbo Feng {
6849ec27c356SChenbo Feng 	u32 sid = current_sid();
6850ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6851ec27c356SChenbo Feng 
6852ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
68536b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
68546b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6855ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6856ec27c356SChenbo Feng }
6857ec27c356SChenbo Feng 
6858ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6859ec27c356SChenbo Feng {
6860ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6861ec27c356SChenbo Feng 
6862ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6863ec27c356SChenbo Feng 	if (!bpfsec)
6864ec27c356SChenbo Feng 		return -ENOMEM;
6865ec27c356SChenbo Feng 
6866ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6867ec27c356SChenbo Feng 	map->security = bpfsec;
6868ec27c356SChenbo Feng 
6869ec27c356SChenbo Feng 	return 0;
6870ec27c356SChenbo Feng }
6871ec27c356SChenbo Feng 
6872ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6873ec27c356SChenbo Feng {
6874ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6875ec27c356SChenbo Feng 
6876ec27c356SChenbo Feng 	map->security = NULL;
6877ec27c356SChenbo Feng 	kfree(bpfsec);
6878ec27c356SChenbo Feng }
6879ec27c356SChenbo Feng 
6880ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6881ec27c356SChenbo Feng {
6882ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6883ec27c356SChenbo Feng 
6884ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6885ec27c356SChenbo Feng 	if (!bpfsec)
6886ec27c356SChenbo Feng 		return -ENOMEM;
6887ec27c356SChenbo Feng 
6888ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6889ec27c356SChenbo Feng 	aux->security = bpfsec;
6890ec27c356SChenbo Feng 
6891ec27c356SChenbo Feng 	return 0;
6892ec27c356SChenbo Feng }
6893ec27c356SChenbo Feng 
6894ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6895ec27c356SChenbo Feng {
6896ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6897ec27c356SChenbo Feng 
6898ec27c356SChenbo Feng 	aux->security = NULL;
6899ec27c356SChenbo Feng 	kfree(bpfsec);
6900ec27c356SChenbo Feng }
6901ec27c356SChenbo Feng #endif
6902ec27c356SChenbo Feng 
6903ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6904e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6906e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6907e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6908076c54c5SAhmed S. Darwish 
6909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
6912e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
6913e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
6914e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
6915e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
6916e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
6917e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
691879af7307SStephen Smalley 
6919e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
69201da177e4SLinus Torvalds 
6921e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6922e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6923e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
69241da177e4SLinus Torvalds 
6925e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6926e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6927e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6928e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6929e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6930e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6931e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6932e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
6933e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
6934e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6935e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6936e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
69371da177e4SLinus Torvalds 
6938e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6939a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6940e0007529SEric Paris 
6941e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6942e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6943e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6944e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
6945e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
6946e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6947e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6948e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6949e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6950e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6951e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6952e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6953e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6954e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6955e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6956e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6957e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6958e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6959e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6960e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6961e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6962e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6963e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6964e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6965e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
696656909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
696719472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
69681da177e4SLinus Torvalds 
6969e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
6970e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6971e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6972e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6973e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6974e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6975e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6976e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
6977e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6978e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6979e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6980e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
69811da177e4SLinus Torvalds 
6982e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
69831da177e4SLinus Torvalds 
6984a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6985e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6986e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, selinux_cred_free),
6987e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6988e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
69893ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6990e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6991e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6992e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6993c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
699461d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6995e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6996e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6997e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6998e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6999e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7000e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7001e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7002791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7003e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7004e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7005e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7006e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7007e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
7008e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7009788e7dd4SYuichi Nakamura 
7010e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7011e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
70121da177e4SLinus Torvalds 
7013e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7014e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
70151da177e4SLinus Torvalds 
7016e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_alloc_security,
7017e20b043aSCasey Schaufler 			selinux_msg_queue_alloc_security),
7018e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
7019e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7020e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7021e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7022e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
70231da177e4SLinus Torvalds 
7024e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7025e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
7026e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7027e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7028e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
70291da177e4SLinus Torvalds 
7030e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7031e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
7032e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7033e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7034e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
70351da177e4SLinus Torvalds 
7036e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
70371da177e4SLinus Torvalds 
7038e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7039e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
70401da177e4SLinus Torvalds 
7041e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7042e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7043e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7044e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
70456f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7046e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7047e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
7048e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
70491da177e4SLinus Torvalds 
7050e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7051e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7052dc49c1f9SCatherine Zhang 
7053e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
7054e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
70550b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7056e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7057e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7058e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7059e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7060e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7061e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7062e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7063e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7064e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7065e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7066e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7067e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7068e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
7069e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
7070e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7071e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7072e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7073e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7074e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7075e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7076d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7077d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7078d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7079e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7080e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7081e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7082e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7083e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7084e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7085e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7086e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7087e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7088e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7089e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7090e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7091e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
70923a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
7093cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7094ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
7095ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
70963a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
70973a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
70983a976fa6SDaniel Jurgens #endif
7099d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
7100e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7101e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7102e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7103e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7104e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7105e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7106e20b043aSCasey Schaufler 			selinux_xfrm_state_alloc_acquire),
7107e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7108e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7109e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7110e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7111e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
7112e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
71131da177e4SLinus Torvalds #endif
7114d720024eSMichael LeMay 
7115d720024eSMichael LeMay #ifdef CONFIG_KEYS
7116e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7117e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
7118e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
7119e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7120d720024eSMichael LeMay #endif
71219d57a7f9SAhmed S. Darwish 
71229d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
7123e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7124e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7125e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7126e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
71279d57a7f9SAhmed S. Darwish #endif
7128ec27c356SChenbo Feng 
7129ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
7130ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
7131ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7132ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7133ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7134ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7135ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7136ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7137ec27c356SChenbo Feng #endif
71381da177e4SLinus Torvalds };
71391da177e4SLinus Torvalds 
71401da177e4SLinus Torvalds static __init int selinux_init(void)
71411da177e4SLinus Torvalds {
7142b1d9e6b0SCasey Schaufler 	if (!security_module_enable("selinux")) {
7143076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
7144076c54c5SAhmed S. Darwish 		return 0;
7145076c54c5SAhmed S. Darwish 	}
7146076c54c5SAhmed S. Darwish 
71471da177e4SLinus Torvalds 	if (!selinux_enabled) {
7148c103a91eSpeter enderborg 		pr_info("SELinux:  Disabled at boot.\n");
71491da177e4SLinus Torvalds 		return 0;
71501da177e4SLinus Torvalds 	}
71511da177e4SLinus Torvalds 
7152c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
71531da177e4SLinus Torvalds 
7154aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
7155e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
7156aa8e712cSStephen Smalley 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
7157aa8e712cSStephen Smalley 	selinux_ss_init(&selinux_state.ss);
71586b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
7159aa8e712cSStephen Smalley 
71601da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
7161d84f4f99SDavid Howells 	cred_init_security();
71621da177e4SLinus Torvalds 
7163fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7164fcaaade1SStephen Smalley 
71657cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
71667cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
716720c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
716863205654SSangwoo 	file_security_cache = kmem_cache_create("selinux_file_security",
716963205654SSangwoo 					    sizeof(struct file_security_struct),
717063205654SSangwoo 					    0, SLAB_PANIC, NULL);
71711da177e4SLinus Torvalds 	avc_init();
71721da177e4SLinus Torvalds 
7173aa8e712cSStephen Smalley 	avtab_cache_init();
7174aa8e712cSStephen Smalley 
7175aa8e712cSStephen Smalley 	ebitmap_cache_init();
7176aa8e712cSStephen Smalley 
7177aa8e712cSStephen Smalley 	hashtab_cache_init();
7178aa8e712cSStephen Smalley 
7179d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
71801da177e4SLinus Torvalds 
7181615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7182615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
7183615e51fdSPaul Moore 
71848f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
71858f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
71868f408ab6SDaniel Jurgens 
7187aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
7188c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
7189828dfe1dSEric Paris 	else
7190c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
7191d720024eSMichael LeMay 
71921da177e4SLinus Torvalds 	return 0;
71931da177e4SLinus Torvalds }
71941da177e4SLinus Torvalds 
7195e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
7196e8c26255SAl Viro {
7197e8c26255SAl Viro 	superblock_doinit(sb, NULL);
7198e8c26255SAl Viro }
7199e8c26255SAl Viro 
72001da177e4SLinus Torvalds void selinux_complete_init(void)
72011da177e4SLinus Torvalds {
7202c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
72031da177e4SLinus Torvalds 
72041da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
7205c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
7206e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
72071da177e4SLinus Torvalds }
72081da177e4SLinus Torvalds 
72091da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
72101da177e4SLinus Torvalds    all processes and objects when they are created. */
72113d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
721207aed2f2SKees Cook 	.name = "selinux",
72133d6e5f6dSKees Cook 	.init = selinux_init,
72143d6e5f6dSKees Cook };
72151da177e4SLinus Torvalds 
7216c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
72171da177e4SLinus Torvalds 
7218591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
7219effad8dfSPaul Moore 	{
7220effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
72212597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
72226e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
72231da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7224effad8dfSPaul Moore 	},
7225effad8dfSPaul Moore 	{
7226effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
72272597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7228effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7229effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7230948bf85cSPaul Moore 	},
7231948bf85cSPaul Moore 	{
7232948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
72332597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7234948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7235948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
723625db6beaSJiri Pirko 	},
72371a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7238effad8dfSPaul Moore 	{
7239effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
72402597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
72416e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
72421da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7243effad8dfSPaul Moore 	},
7244effad8dfSPaul Moore 	{
7245effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
72462597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7247effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7248effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
724925db6beaSJiri Pirko 	},
72502917f57bSHuw Davies 	{
72512917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
72522917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
72532917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
72542917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
72552917f57bSHuw Davies 	},
72561da177e4SLinus Torvalds #endif	/* IPV6 */
725725db6beaSJiri Pirko };
72581da177e4SLinus Torvalds 
72598e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
72608e71bf75SFlorian Westphal {
72618e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
72628e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
72638e71bf75SFlorian Westphal }
72648e71bf75SFlorian Westphal 
72658e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
72668e71bf75SFlorian Westphal {
72678e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
72688e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
72698e71bf75SFlorian Westphal }
72708e71bf75SFlorian Westphal 
72718e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
72728e71bf75SFlorian Westphal 	.init = selinux_nf_register,
72738e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
72748e71bf75SFlorian Westphal };
72758e71bf75SFlorian Westphal 
72761da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
72771da177e4SLinus Torvalds {
727825db6beaSJiri Pirko 	int err;
72791da177e4SLinus Torvalds 
72801da177e4SLinus Torvalds 	if (!selinux_enabled)
728125db6beaSJiri Pirko 		return 0;
72821da177e4SLinus Torvalds 
7283c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
72841da177e4SLinus Torvalds 
72858e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
72861da177e4SLinus Torvalds 	if (err)
72878e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
72881da177e4SLinus Torvalds 
728925db6beaSJiri Pirko 	return 0;
72901da177e4SLinus Torvalds }
72911da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
72921da177e4SLinus Torvalds 
72931da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
72941da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
72951da177e4SLinus Torvalds {
7296c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
72971da177e4SLinus Torvalds 
72988e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
72991da177e4SLinus Torvalds }
73001da177e4SLinus Torvalds #endif
73011da177e4SLinus Torvalds 
7302c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
73031da177e4SLinus Torvalds 
73041da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
73051da177e4SLinus Torvalds #define selinux_nf_ip_exit()
73061da177e4SLinus Torvalds #endif
73071da177e4SLinus Torvalds 
7308c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
73091da177e4SLinus Torvalds 
73101da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7311aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
73121da177e4SLinus Torvalds {
7313aa8e712cSStephen Smalley 	if (state->initialized) {
73141da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
73151da177e4SLinus Torvalds 		return -EINVAL;
73161da177e4SLinus Torvalds 	}
73171da177e4SLinus Torvalds 
7318aa8e712cSStephen Smalley 	if (state->disabled) {
73191da177e4SLinus Torvalds 		/* Only do this once. */
73201da177e4SLinus Torvalds 		return -EINVAL;
73211da177e4SLinus Torvalds 	}
73221da177e4SLinus Torvalds 
7323aa8e712cSStephen Smalley 	state->disabled = 1;
7324aa8e712cSStephen Smalley 
7325c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
73261da177e4SLinus Torvalds 
732730d55280SStephen Smalley 	selinux_enabled = 0;
73281da177e4SLinus Torvalds 
7329b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
73301da177e4SLinus Torvalds 
7331af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7332af8ff049SEric Paris 	avc_disable();
7333af8ff049SEric Paris 
73341da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
73351da177e4SLinus Torvalds 	selinux_nf_ip_exit();
73361da177e4SLinus Torvalds 
73371da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
73381da177e4SLinus Torvalds 	exit_sel_fs();
73391da177e4SLinus Torvalds 
73401da177e4SLinus Torvalds 	return 0;
73411da177e4SLinus Torvalds }
73421da177e4SLinus Torvalds #endif
7343