xref: /openbmc/linux/security/selinux/hooks.c (revision be6ec88f41ba94746f830ba38cc4d08dd5ddbb08)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  NSA Security-Enhanced Linux (SELinux) security module
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This file contains the SELinux hook function implementations.
51da177e4SLinus Torvalds  *
67efbb60bSStephen Smalley  *  Authors:  Stephen Smalley, <sds@tycho.nsa.gov>
71da177e4SLinus Torvalds  *	      Chris Vance, <cvance@nai.com>
81da177e4SLinus Torvalds  *	      Wayne Salamon, <wsalamon@nai.com>
91da177e4SLinus Torvalds  *	      James Morris <jmorris@redhat.com>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
122069f457SEric Paris  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
132069f457SEric Paris  *					   Eric Paris <eparis@redhat.com>
141da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
151da177e4SLinus Torvalds  *			    <dgoeddel@trustedcs.com>
16ed6d76e4SPaul Moore  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
1782c21bfaSPaul Moore  *	Paul Moore <paul@paul-moore.com>
18788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19788e7dd4SYuichi Nakamura  *		       Yuichi Nakamura <ynakam@hitachisoft.jp>
203a976fa6SDaniel Jurgens  *  Copyright (C) 2016 Mellanox Technologies
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
231da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
241da177e4SLinus Torvalds  *	as published by the Free Software Foundation.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/init.h>
280b24dcb7SEric Paris #include <linux/kd.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
300d094efeSRoland McGrath #include <linux/tracehook.h>
311da177e4SLinus Torvalds #include <linux/errno.h>
323f07c014SIngo Molnar #include <linux/sched/signal.h>
3329930025SIngo Molnar #include <linux/sched/task.h>
343c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
351da177e4SLinus Torvalds #include <linux/xattr.h>
361da177e4SLinus Torvalds #include <linux/capability.h>
371da177e4SLinus Torvalds #include <linux/unistd.h>
381da177e4SLinus Torvalds #include <linux/mm.h>
391da177e4SLinus Torvalds #include <linux/mman.h>
401da177e4SLinus Torvalds #include <linux/slab.h>
411da177e4SLinus Torvalds #include <linux/pagemap.h>
420b24dcb7SEric Paris #include <linux/proc_fs.h>
431da177e4SLinus Torvalds #include <linux/swap.h>
441da177e4SLinus Torvalds #include <linux/spinlock.h>
451da177e4SLinus Torvalds #include <linux/syscalls.h>
462a7dba39SEric Paris #include <linux/dcache.h>
471da177e4SLinus Torvalds #include <linux/file.h>
489f3acc31SAl Viro #include <linux/fdtable.h>
491da177e4SLinus Torvalds #include <linux/namei.h>
501da177e4SLinus Torvalds #include <linux/mount.h>
511da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
521da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
531da177e4SLinus Torvalds #include <linux/tty.h>
541da177e4SLinus Torvalds #include <net/icmp.h>
55227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
561da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5747180068SPaul Moore #include <net/inet_connection_sock.h>
58220deb96SPaul Moore #include <net/net_namespace.h>
59d621d35eSPaul Moore #include <net/netlabel.h>
60f5269710SEric Paris #include <linux/uaccess.h>
611da177e4SLinus Torvalds #include <asm/ioctls.h>
6260063497SArun Sharma #include <linux/atomic.h>
631da177e4SLinus Torvalds #include <linux/bitops.h>
641da177e4SLinus Torvalds #include <linux/interrupt.h>
651da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6677954983SHong zhi guo #include <net/netlink.h>
671da177e4SLinus Torvalds #include <linux/tcp.h>
681da177e4SLinus Torvalds #include <linux/udp.h>
692ee92d46SJames Morris #include <linux/dccp.h>
70d452930fSRichard Haines #include <linux/sctp.h>
71d452930fSRichard Haines #include <net/sctp/structs.h>
721da177e4SLinus Torvalds #include <linux/quota.h>
731da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
741da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
751da177e4SLinus Torvalds #include <linux/parser.h>
761da177e4SLinus Torvalds #include <linux/nfs_mount.h>
771da177e4SLinus Torvalds #include <net/ipv6.h>
781da177e4SLinus Torvalds #include <linux/hugetlb.h>
791da177e4SLinus Torvalds #include <linux/personality.h>
801da177e4SLinus Torvalds #include <linux/audit.h>
816931dfc9SEric Paris #include <linux/string.h>
82877ce7c1SCatherine Zhang #include <linux/selinux.h>
8323970741SEric Paris #include <linux/mutex.h>
84f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8500234592SKees Cook #include <linux/syslog.h>
863486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8744fc7ea0SPaul Gortmaker #include <linux/export.h>
8840401530SAl Viro #include <linux/msg.h>
8940401530SAl Viro #include <linux/shm.h>
90ec27c356SChenbo Feng #include <linux/bpf.h>
91e262e32dSDavid Howells #include <uapi/linux/mount.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include "avc.h"
941da177e4SLinus Torvalds #include "objsec.h"
951da177e4SLinus Torvalds #include "netif.h"
96224dfbd8SPaul Moore #include "netnode.h"
973e112172SPaul Moore #include "netport.h"
98409dcf31SDaniel Jurgens #include "ibpkey.h"
99d28d1e08STrent Jaeger #include "xfrm.h"
100c60475bfSPaul Moore #include "netlabel.h"
1019d57a7f9SAhmed S. Darwish #include "audit.h"
1027b98a585SJames Morris #include "avc_ss.h"
1031da177e4SLinus Torvalds 
104aa8e712cSStephen Smalley struct selinux_state selinux_state;
105aa8e712cSStephen Smalley 
106d621d35eSPaul Moore /* SECMARK reference count */
10756a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
108d621d35eSPaul Moore 
1091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
110aa8e712cSStephen Smalley static int selinux_enforcing_boot;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1131da177e4SLinus Torvalds {
114f5269710SEric Paris 	unsigned long enforcing;
11529707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
116aa8e712cSStephen Smalley 		selinux_enforcing_boot = enforcing ? 1 : 0;
1171da177e4SLinus Torvalds 	return 1;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
120aa8e712cSStephen Smalley #else
121aa8e712cSStephen Smalley #define selinux_enforcing_boot 1
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 
124*be6ec88fSKees Cook int selinux_enabled __lsm_ro_after_init = 1;
1251da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1261da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1271da177e4SLinus Torvalds {
128f5269710SEric Paris 	unsigned long enabled;
12929707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
130f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1311da177e4SLinus Torvalds 	return 1;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
1341da177e4SLinus Torvalds #endif
1351da177e4SLinus Torvalds 
136aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot =
137aa8e712cSStephen Smalley 	CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
138aa8e712cSStephen Smalley 
139aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str)
140aa8e712cSStephen Smalley {
141aa8e712cSStephen Smalley 	unsigned long checkreqprot;
142aa8e712cSStephen Smalley 
143aa8e712cSStephen Smalley 	if (!kstrtoul(str, 0, &checkreqprot))
144aa8e712cSStephen Smalley 		selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
145aa8e712cSStephen Smalley 	return 1;
146aa8e712cSStephen Smalley }
147aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup);
148aa8e712cSStephen Smalley 
149e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
15063205654SSangwoo static struct kmem_cache *file_security_cache;
1517cae7e26SJames Morris 
152d621d35eSPaul Moore /**
153d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
154d621d35eSPaul Moore  *
155d621d35eSPaul Moore  * Description:
156d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
157d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
158d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1592be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1602be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
161d621d35eSPaul Moore  *
162d621d35eSPaul Moore  */
163d621d35eSPaul Moore static int selinux_secmark_enabled(void)
164d621d35eSPaul Moore {
165aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
166aa8e712cSStephen Smalley 		atomic_read(&selinux_secmark_refcount));
1672be4d74fSChris PeBenito }
1682be4d74fSChris PeBenito 
1692be4d74fSChris PeBenito /**
1702be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1712be4d74fSChris PeBenito  *
1722be4d74fSChris PeBenito  * Description:
1732be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1742be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1752be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1762be4d74fSChris PeBenito  * is always considered enabled.
1772be4d74fSChris PeBenito  *
1782be4d74fSChris PeBenito  */
1792be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1802be4d74fSChris PeBenito {
181aa8e712cSStephen Smalley 	return (selinux_policycap_alwaysnetwork() ||
182aa8e712cSStephen Smalley 		netlbl_enabled() || selinux_xfrm_enabled());
183d621d35eSPaul Moore }
184d621d35eSPaul Moore 
185615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
186615e51fdSPaul Moore {
187615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
188615e51fdSPaul Moore 		sel_netif_flush();
189615e51fdSPaul Moore 		sel_netnode_flush();
190615e51fdSPaul Moore 		sel_netport_flush();
191615e51fdSPaul Moore 		synchronize_net();
192615e51fdSPaul Moore 	}
193615e51fdSPaul Moore 	return 0;
194615e51fdSPaul Moore }
195615e51fdSPaul Moore 
1968f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event)
1978f408ab6SDaniel Jurgens {
198409dcf31SDaniel Jurgens 	if (event == AVC_CALLBACK_RESET) {
199409dcf31SDaniel Jurgens 		sel_ib_pkey_flush();
2008f408ab6SDaniel Jurgens 		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
201409dcf31SDaniel Jurgens 	}
2028f408ab6SDaniel Jurgens 
2038f408ab6SDaniel Jurgens 	return 0;
2048f408ab6SDaniel Jurgens }
2058f408ab6SDaniel Jurgens 
206d84f4f99SDavid Howells /*
207d84f4f99SDavid Howells  * initialise the security for the init task
208d84f4f99SDavid Howells  */
209d84f4f99SDavid Howells static void cred_init_security(void)
2101da177e4SLinus Torvalds {
2113b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
2121da177e4SLinus Torvalds 	struct task_security_struct *tsec;
2131da177e4SLinus Torvalds 
21489d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
2151da177e4SLinus Torvalds 	if (!tsec)
216d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
2171da177e4SLinus Torvalds 
218d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
219f1752eecSDavid Howells 	cred->security = tsec;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
222275bb41eSDavid Howells /*
22388e67f3bSDavid Howells  * get the security ID of a set of credentials
22488e67f3bSDavid Howells  */
22588e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
22688e67f3bSDavid Howells {
22788e67f3bSDavid Howells 	const struct task_security_struct *tsec;
22888e67f3bSDavid Howells 
22988e67f3bSDavid Howells 	tsec = cred->security;
23088e67f3bSDavid Howells 	return tsec->sid;
23188e67f3bSDavid Howells }
23288e67f3bSDavid Howells 
23388e67f3bSDavid Howells /*
2343b11a1deSDavid Howells  * get the objective security ID of a task
235275bb41eSDavid Howells  */
236275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
237275bb41eSDavid Howells {
238275bb41eSDavid Howells 	u32 sid;
239275bb41eSDavid Howells 
240275bb41eSDavid Howells 	rcu_read_lock();
24188e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
242275bb41eSDavid Howells 	rcu_read_unlock();
243275bb41eSDavid Howells 	return sid;
244275bb41eSDavid Howells }
245275bb41eSDavid Howells 
24688e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
24788e67f3bSDavid Howells 
2481da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct inode_security_struct *isec;
251275bb41eSDavid Howells 	u32 sid = current_sid();
2521da177e4SLinus Torvalds 
253a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2541da177e4SLinus Torvalds 	if (!isec)
2551da177e4SLinus Torvalds 		return -ENOMEM;
2561da177e4SLinus Torvalds 
2579287aed2SAndreas Gruenbacher 	spin_lock_init(&isec->lock);
2581da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2591da177e4SLinus Torvalds 	isec->inode = inode;
2601da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2611da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
262275bb41eSDavid Howells 	isec->task_sid = sid;
26342059112SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
2641da177e4SLinus Torvalds 	inode->i_security = isec;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	return 0;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2695d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2705d226df4SAndreas Gruenbacher 
2715d226df4SAndreas Gruenbacher /*
2725d226df4SAndreas Gruenbacher  * Try reloading inode security labels that have been marked as invalid.  The
2735d226df4SAndreas Gruenbacher  * @may_sleep parameter indicates when sleeping and thus reloading labels is
27442059112SAndreas Gruenbacher  * allowed; when set to false, returns -ECHILD when the label is
275e9193288SAl Viro  * invalid.  The @dentry parameter should be set to a dentry of the inode.
2765d226df4SAndreas Gruenbacher  */
2775d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode,
278e9193288SAl Viro 				       struct dentry *dentry,
2795d226df4SAndreas Gruenbacher 				       bool may_sleep)
2805d226df4SAndreas Gruenbacher {
2815d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
2825d226df4SAndreas Gruenbacher 
2835d226df4SAndreas Gruenbacher 	might_sleep_if(may_sleep);
2845d226df4SAndreas Gruenbacher 
285aa8e712cSStephen Smalley 	if (selinux_state.initialized &&
286aa8e712cSStephen Smalley 	    isec->initialized != LABEL_INITIALIZED) {
2875d226df4SAndreas Gruenbacher 		if (!may_sleep)
2885d226df4SAndreas Gruenbacher 			return -ECHILD;
2895d226df4SAndreas Gruenbacher 
2905d226df4SAndreas Gruenbacher 		/*
2915d226df4SAndreas Gruenbacher 		 * Try reloading the inode security label.  This will fail if
2925d226df4SAndreas Gruenbacher 		 * @opt_dentry is NULL and no dentry for this inode can be
2935d226df4SAndreas Gruenbacher 		 * found; in that case, continue using the old label.
2945d226df4SAndreas Gruenbacher 		 */
295e9193288SAl Viro 		inode_doinit_with_dentry(inode, dentry);
2965d226df4SAndreas Gruenbacher 	}
2975d226df4SAndreas Gruenbacher 	return 0;
2985d226df4SAndreas Gruenbacher }
2995d226df4SAndreas Gruenbacher 
3005d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
3015d226df4SAndreas Gruenbacher {
3025d226df4SAndreas Gruenbacher 	return inode->i_security;
3035d226df4SAndreas Gruenbacher }
3045d226df4SAndreas Gruenbacher 
3055d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
3065d226df4SAndreas Gruenbacher {
3075d226df4SAndreas Gruenbacher 	int error;
3085d226df4SAndreas Gruenbacher 
3095d226df4SAndreas Gruenbacher 	error = __inode_security_revalidate(inode, NULL, !rcu);
3105d226df4SAndreas Gruenbacher 	if (error)
3115d226df4SAndreas Gruenbacher 		return ERR_PTR(error);
3125d226df4SAndreas Gruenbacher 	return inode->i_security;
3135d226df4SAndreas Gruenbacher }
3145d226df4SAndreas Gruenbacher 
31583da53c5SAndreas Gruenbacher /*
31683da53c5SAndreas Gruenbacher  * Get the security label of an inode.
31783da53c5SAndreas Gruenbacher  */
31883da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode)
31983da53c5SAndreas Gruenbacher {
3205d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, NULL, true);
32183da53c5SAndreas Gruenbacher 	return inode->i_security;
32283da53c5SAndreas Gruenbacher }
32383da53c5SAndreas Gruenbacher 
3242c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
3252c97165bSPaul Moore {
3262c97165bSPaul Moore 	struct inode *inode = d_backing_inode(dentry);
3272c97165bSPaul Moore 
3282c97165bSPaul Moore 	return inode->i_security;
3292c97165bSPaul Moore }
3302c97165bSPaul Moore 
33183da53c5SAndreas Gruenbacher /*
33283da53c5SAndreas Gruenbacher  * Get the security label of a dentry's backing inode.
33383da53c5SAndreas Gruenbacher  */
33483da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
33583da53c5SAndreas Gruenbacher {
33683da53c5SAndreas Gruenbacher 	struct inode *inode = d_backing_inode(dentry);
33783da53c5SAndreas Gruenbacher 
3385d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
33983da53c5SAndreas Gruenbacher 	return inode->i_security;
34083da53c5SAndreas Gruenbacher }
34183da53c5SAndreas Gruenbacher 
3423dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head)
3433dc91d43SSteven Rostedt {
3443dc91d43SSteven Rostedt 	struct inode_security_struct *isec;
3453dc91d43SSteven Rostedt 
3463dc91d43SSteven Rostedt 	isec = container_of(head, struct inode_security_struct, rcu);
3473dc91d43SSteven Rostedt 	kmem_cache_free(sel_inode_cache, isec);
3483dc91d43SSteven Rostedt }
3493dc91d43SSteven Rostedt 
3501da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3531da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3541da177e4SLinus Torvalds 
3559629d04aSWaiman Long 	/*
3569629d04aSWaiman Long 	 * As not all inode security structures are in a list, we check for
3579629d04aSWaiman Long 	 * empty list outside of the lock to make sure that we won't waste
3589629d04aSWaiman Long 	 * time taking a lock doing nothing.
3599629d04aSWaiman Long 	 *
3609629d04aSWaiman Long 	 * The list_del_init() function can be safely called more than once.
3619629d04aSWaiman Long 	 * It should not be possible for this function to be called with
3629629d04aSWaiman Long 	 * concurrent list_add(), but for better safety against future changes
3639629d04aSWaiman Long 	 * in the code, we use list_empty_careful() here.
3649629d04aSWaiman Long 	 */
3659629d04aSWaiman Long 	if (!list_empty_careful(&isec->list)) {
3661da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
3671da177e4SLinus Torvalds 		list_del_init(&isec->list);
3681da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
3699629d04aSWaiman Long 	}
3701da177e4SLinus Torvalds 
3713dc91d43SSteven Rostedt 	/*
3723dc91d43SSteven Rostedt 	 * The inode may still be referenced in a path walk and
3733dc91d43SSteven Rostedt 	 * a call to selinux_inode_permission() can be made
3743dc91d43SSteven Rostedt 	 * after inode_free_security() is called. Ideally, the VFS
3753dc91d43SSteven Rostedt 	 * wouldn't do this, but fixing that is a much harder
3763dc91d43SSteven Rostedt 	 * job. For now, simply free the i_security via RCU, and
3773dc91d43SSteven Rostedt 	 * leave the current inode->i_security pointer intact.
3783dc91d43SSteven Rostedt 	 * The inode will be freed after the RCU grace period too.
3793dc91d43SSteven Rostedt 	 */
3803dc91d43SSteven Rostedt 	call_rcu(&isec->rcu, inode_free_rcu);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct file_security_struct *fsec;
386275bb41eSDavid Howells 	u32 sid = current_sid();
3871da177e4SLinus Torvalds 
38863205654SSangwoo 	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
3891da177e4SLinus Torvalds 	if (!fsec)
3901da177e4SLinus Torvalds 		return -ENOMEM;
3911da177e4SLinus Torvalds 
392275bb41eSDavid Howells 	fsec->sid = sid;
393275bb41eSDavid Howells 	fsec->fown_sid = sid;
3941da177e4SLinus Torvalds 	file->f_security = fsec;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds static void file_free_security(struct file *file)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
4021da177e4SLinus Torvalds 	file->f_security = NULL;
40363205654SSangwoo 	kmem_cache_free(file_security_cache, fsec);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
4091da177e4SLinus Torvalds 
41089d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
4111da177e4SLinus Torvalds 	if (!sbsec)
4121da177e4SLinus Torvalds 		return -ENOMEM;
4131da177e4SLinus Torvalds 
414bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
4151da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
4161da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
4171da177e4SLinus Torvalds 	sbsec->sb = sb;
4181da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
4191da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
420c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
4211da177e4SLinus Torvalds 	sb->s_security = sbsec;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	return 0;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4291da177e4SLinus Torvalds 	sb->s_security = NULL;
4301da177e4SLinus Torvalds 	kfree(sbsec);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433bd323655SAl Viro struct selinux_mnt_opts {
434bd323655SAl Viro 	const char *fscontext, *context, *rootcontext, *defcontext;
435bd323655SAl Viro };
436bd323655SAl Viro 
437204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts)
438204cc0ccSAl Viro {
439bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
440bd323655SAl Viro 	kfree(opts->fscontext);
441bd323655SAl Viro 	kfree(opts->context);
442bd323655SAl Viro 	kfree(opts->rootcontext);
443bd323655SAl Viro 	kfree(opts->defcontext);
444204cc0ccSAl Viro 	kfree(opts);
445204cc0ccSAl Viro }
446204cc0ccSAl Viro 
4471da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds enum {
45331e87930SEric Paris 	Opt_error = -1,
4541da177e4SLinus Torvalds 	Opt_context = 1,
4551da177e4SLinus Torvalds 	Opt_fscontext = 2,
456c9180a57SEric Paris 	Opt_defcontext = 3,
457c9180a57SEric Paris 	Opt_rootcontext = 4,
458da3d76abSAl Viro 	Opt_seclabel = 5,
4591da177e4SLinus Torvalds };
4601da177e4SLinus Torvalds 
461da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
462169d68efSAl Viro static struct {
463169d68efSAl Viro 	const char *name;
464169d68efSAl Viro 	int len;
465169d68efSAl Viro 	int opt;
466169d68efSAl Viro 	bool has_arg;
467169d68efSAl Viro } tokens[] = {
468da3d76abSAl Viro 	A(context, true),
469da3d76abSAl Viro 	A(fscontext, true),
470da3d76abSAl Viro 	A(defcontext, true),
471da3d76abSAl Viro 	A(rootcontext, true),
472da3d76abSAl Viro 	A(seclabel, false),
4731da177e4SLinus Torvalds };
474169d68efSAl Viro #undef A
475169d68efSAl Viro 
476169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg)
477169d68efSAl Viro {
478169d68efSAl Viro 	int i;
479169d68efSAl Viro 
480169d68efSAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
481169d68efSAl Viro 		size_t len = tokens[i].len;
482169d68efSAl Viro 		if (len > l || memcmp(s, tokens[i].name, len))
483169d68efSAl Viro 			continue;
484169d68efSAl Viro 		if (tokens[i].has_arg) {
485169d68efSAl Viro 			if (len == l || s[len] != '=')
486169d68efSAl Viro 				continue;
487169d68efSAl Viro 			*arg = s + len + 1;
488169d68efSAl Viro 		} else if (len != l)
489169d68efSAl Viro 			continue;
490169d68efSAl Viro 		return tokens[i].opt;
491169d68efSAl Viro 	}
492169d68efSAl Viro 	return Opt_error;
493169d68efSAl Viro }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
4961da177e4SLinus Torvalds 
497c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
498c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
499275bb41eSDavid Howells 			const struct cred *cred)
500c312feb2SEric Paris {
501275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
502c312feb2SEric Paris 	int rc;
503c312feb2SEric Paris 
5046b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5056b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
506c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
507c312feb2SEric Paris 	if (rc)
508c312feb2SEric Paris 		return rc;
509c312feb2SEric Paris 
5106b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5116b6bc620SStephen Smalley 			  tsec->sid, sid, SECCLASS_FILESYSTEM,
512c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
513c312feb2SEric Paris 	return rc;
514c312feb2SEric Paris }
515c312feb2SEric Paris 
5160808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
5170808925eSEric Paris 			struct superblock_security_struct *sbsec,
518275bb41eSDavid Howells 			const struct cred *cred)
5190808925eSEric Paris {
520275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
5210808925eSEric Paris 	int rc;
5226b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5236b6bc620SStephen Smalley 			  tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
5240808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
5250808925eSEric Paris 	if (rc)
5260808925eSEric Paris 		return rc;
5270808925eSEric Paris 
5286b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
5296b6bc620SStephen Smalley 			  sid, sbsec->sid, SECCLASS_FILESYSTEM,
5300808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
5310808925eSEric Paris 	return rc;
5320808925eSEric Paris }
5330808925eSEric Paris 
534b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
535b43e725dSEric Paris {
536b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
537b43e725dSEric Paris 
538d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
539b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
540d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
5419fc2b4b4SJ. Bruce Fields 		sbsec->behavior == SECURITY_FS_USE_NATIVE ||
542d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
543d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
544d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
545d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
546a2c7c6fbSYongqin Liu 		!strcmp(sb->s_type->name, "tracefs") ||
5472651225bSStephen Smalley 		!strcmp(sb->s_type->name, "rootfs") ||
548aa8e712cSStephen Smalley 		(selinux_policycap_cgroupseclabel() &&
5492651225bSStephen Smalley 		 (!strcmp(sb->s_type->name, "cgroup") ||
5502651225bSStephen Smalley 		  !strcmp(sb->s_type->name, "cgroup2")));
551b43e725dSEric Paris }
552b43e725dSEric Paris 
553c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5561da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
557c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
5581da177e4SLinus Torvalds 	int rc = 0;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
5611da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
5621da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
5631da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
5641da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
5651da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
5665d6c3191SAndreas Gruenbacher 		if (!(root_inode->i_opflags & IOP_XATTR)) {
567c103a91eSpeter enderborg 			pr_warn("SELinux: (dev %s, type %s) has no "
56829b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
5691da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5701da177e4SLinus Torvalds 			goto out;
5711da177e4SLinus Torvalds 		}
5725d6c3191SAndreas Gruenbacher 
5735d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
5741da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5751da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
576c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
57729b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
57829b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5791da177e4SLinus Torvalds 			else
580c103a91eSpeter enderborg 				pr_warn("SELinux: (dev %s, type "
58129b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
58229b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
5831da177e4SLinus Torvalds 			goto out;
5841da177e4SLinus Torvalds 		}
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
587eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
5880b4d3452SScott Mayhew 
5890b4d3452SScott Mayhew 	/*
5900b4d3452SScott Mayhew 	 * Explicitly set or clear SBLABEL_MNT.  It's not sufficient to simply
5910b4d3452SScott Mayhew 	 * leave the flag untouched because sb_clone_mnt_opts might be handing
5920b4d3452SScott Mayhew 	 * us a superblock that needs the flag to be cleared.
5930b4d3452SScott Mayhew 	 */
594b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
59512f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
5960b4d3452SScott Mayhew 	else
5970b4d3452SScott Mayhew 		sbsec->flags &= ~SBLABEL_MNT;
598ddd29ec6SDavid P. Quigley 
5991da177e4SLinus Torvalds 	/* Initialize the root inode. */
600c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
6031da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
6041da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
6051da177e4SLinus Torvalds 	   populates itself. */
6061da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
6078d64124aSAl Viro 	while (!list_empty(&sbsec->isec_head)) {
6081da177e4SLinus Torvalds 		struct inode_security_struct *isec =
6098d64124aSAl Viro 				list_first_entry(&sbsec->isec_head,
6101da177e4SLinus Torvalds 					   struct inode_security_struct, list);
6111da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
612923190d3SStephen Smalley 		list_del_init(&isec->list);
6131da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
6141da177e4SLinus Torvalds 		inode = igrab(inode);
6151da177e4SLinus Torvalds 		if (inode) {
6161da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
6171da177e4SLinus Torvalds 				inode_doinit(inode);
6181da177e4SLinus Torvalds 			iput(inode);
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
6231da177e4SLinus Torvalds out:
624c9180a57SEric Paris 	return rc;
625c9180a57SEric Paris }
626c9180a57SEric Paris 
627c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
628c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
629c9180a57SEric Paris {
6300d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
6310d90a7ecSDavid P. Quigley 
632c9180a57SEric Paris 	/* check if the old mount command had the same options */
6330d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
634c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
635c9180a57SEric Paris 		    (old_sid != new_sid))
636c9180a57SEric Paris 			return 1;
637c9180a57SEric Paris 
638c9180a57SEric Paris 	/* check if we were passed the same options twice,
639c9180a57SEric Paris 	 * aka someone passed context=a,context=b
640c9180a57SEric Paris 	 */
6410d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
6420d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
643c9180a57SEric Paris 			return 1;
644c9180a57SEric Paris 	return 0;
645c9180a57SEric Paris }
646e0007529SEric Paris 
647bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
648bd323655SAl Viro {
649bd323655SAl Viro 	int rc = security_context_str_to_sid(&selinux_state, s,
650bd323655SAl Viro 					     sid, GFP_KERNEL);
651bd323655SAl Viro 	if (rc)
652bd323655SAl Viro 		pr_warn("SELinux: security_context_str_to_sid"
653bd323655SAl Viro 		       "(%s) failed for (dev %s, type %s) errno=%d\n",
654bd323655SAl Viro 		       s, sb->s_id, sb->s_type->name, rc);
655bd323655SAl Viro 	return rc;
656bd323655SAl Viro }
657bd323655SAl Viro 
658c9180a57SEric Paris /*
659c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
660c9180a57SEric Paris  * labeling information.
661c9180a57SEric Paris  */
662e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
663204cc0ccSAl Viro 				void *mnt_opts,
664649f6e77SDavid Quigley 				unsigned long kern_flags,
665649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
666c9180a57SEric Paris {
667275bb41eSDavid Howells 	const struct cred *cred = current_cred();
668c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
66983da53c5SAndreas Gruenbacher 	struct dentry *root = sbsec->sb->s_root;
670bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
6712c97165bSPaul Moore 	struct inode_security_struct *root_isec;
672c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
673c9180a57SEric Paris 	u32 defcontext_sid = 0;
674bd323655SAl Viro 	int rc = 0;
675c9180a57SEric Paris 
676c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
677c9180a57SEric Paris 
678aa8e712cSStephen Smalley 	if (!selinux_state.initialized) {
679bd323655SAl Viro 		if (!opts) {
680c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
681c9180a57SEric Paris 			   after the initial policy is loaded and the security
682c9180a57SEric Paris 			   server is ready to handle calls. */
683c9180a57SEric Paris 			goto out;
684c9180a57SEric Paris 		}
685c9180a57SEric Paris 		rc = -EINVAL;
686c103a91eSpeter enderborg 		pr_warn("SELinux: Unable to set superblock options "
687744ba35eSEric Paris 			"before the security server is initialized\n");
688c9180a57SEric Paris 		goto out;
689c9180a57SEric Paris 	}
690649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
691649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
692649f6e77SDavid Quigley 		 * place the results is not allowed */
693649f6e77SDavid Quigley 		rc = -EINVAL;
694649f6e77SDavid Quigley 		goto out;
695649f6e77SDavid Quigley 	}
696c9180a57SEric Paris 
697c9180a57SEric Paris 	/*
698e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
699e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
700e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
701e0007529SEric Paris 	 * we need to skip the double mount verification.
702e0007529SEric Paris 	 *
703e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
704e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
705e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
706e0007529SEric Paris 	 * will be used for both mounts)
707e0007529SEric Paris 	 */
7080d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
709bd323655SAl Viro 	    && !opts)
710e0007529SEric Paris 		goto out;
711e0007529SEric Paris 
7122c97165bSPaul Moore 	root_isec = backing_inode_security_novalidate(root);
7132c97165bSPaul Moore 
714e0007529SEric Paris 	/*
715c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
716c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
717c9180a57SEric Paris 	 * than once with different security options.
718c9180a57SEric Paris 	 */
719bd323655SAl Viro 	if (opts) {
720bd323655SAl Viro 		if (opts->fscontext) {
721bd323655SAl Viro 			rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
722bd323655SAl Viro 			if (rc)
723c9180a57SEric Paris 				goto out;
724c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
725c9180a57SEric Paris 					fscontext_sid))
726c9180a57SEric Paris 				goto out_double_mount;
727c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
728bd323655SAl Viro 		}
729bd323655SAl Viro 		if (opts->context) {
730bd323655SAl Viro 			rc = parse_sid(sb, opts->context, &context_sid);
731bd323655SAl Viro 			if (rc)
732bd323655SAl Viro 				goto out;
733c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
734c9180a57SEric Paris 					context_sid))
735c9180a57SEric Paris 				goto out_double_mount;
736c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
737bd323655SAl Viro 		}
738bd323655SAl Viro 		if (opts->rootcontext) {
739bd323655SAl Viro 			rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
740bd323655SAl Viro 			if (rc)
741bd323655SAl Viro 				goto out;
742c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
743c9180a57SEric Paris 					rootcontext_sid))
744c9180a57SEric Paris 				goto out_double_mount;
745c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
746bd323655SAl Viro 		}
747bd323655SAl Viro 		if (opts->defcontext) {
748bd323655SAl Viro 			rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
749bd323655SAl Viro 			if (rc)
750bd323655SAl Viro 				goto out;
751c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
752c9180a57SEric Paris 					defcontext_sid))
753c9180a57SEric Paris 				goto out_double_mount;
754c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
755c9180a57SEric Paris 		}
756c9180a57SEric Paris 	}
757c9180a57SEric Paris 
7580d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
759c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
760bd323655SAl Viro 		if ((sbsec->flags & SE_MNTMASK) && !opts)
761c9180a57SEric Paris 			goto out_double_mount;
762c9180a57SEric Paris 		rc = 0;
763c9180a57SEric Paris 		goto out;
764c9180a57SEric Paris 	}
765c9180a57SEric Paris 
766089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
767134509d5SStephen Smalley 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
768134509d5SStephen Smalley 
7698e014720SStephen Smalley 	if (!strcmp(sb->s_type->name, "debugfs") ||
7706a391183SJeff Vander Stoep 	    !strcmp(sb->s_type->name, "tracefs") ||
7718e014720SStephen Smalley 	    !strcmp(sb->s_type->name, "sysfs") ||
772901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "pstore") ||
773901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup") ||
774901ef845SAntonio Murdaca 	    !strcmp(sb->s_type->name, "cgroup2"))
775134509d5SStephen Smalley 		sbsec->flags |= SE_SBGENFS;
776c9180a57SEric Paris 
777eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
778eb9ae686SDavid Quigley 		/*
779eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
780eb9ae686SDavid Quigley 		 * filesystem type.
781eb9ae686SDavid Quigley 		 */
782aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, sb);
783c9180a57SEric Paris 		if (rc) {
784c103a91eSpeter enderborg 			pr_warn("%s: security_fs_use(%s) returned %d\n",
785089be43eSJames Morris 					__func__, sb->s_type->name, rc);
786c9180a57SEric Paris 			goto out;
787c9180a57SEric Paris 		}
788eb9ae686SDavid Quigley 	}
789aad82892SSeth Forshee 
790aad82892SSeth Forshee 	/*
79101593d32SStephen Smalley 	 * If this is a user namespace mount and the filesystem type is not
79201593d32SStephen Smalley 	 * explicitly whitelisted, then no contexts are allowed on the command
79301593d32SStephen Smalley 	 * line and security labels must be ignored.
794aad82892SSeth Forshee 	 */
79501593d32SStephen Smalley 	if (sb->s_user_ns != &init_user_ns &&
79601593d32SStephen Smalley 	    strcmp(sb->s_type->name, "tmpfs") &&
79701593d32SStephen Smalley 	    strcmp(sb->s_type->name, "ramfs") &&
79801593d32SStephen Smalley 	    strcmp(sb->s_type->name, "devpts")) {
799aad82892SSeth Forshee 		if (context_sid || fscontext_sid || rootcontext_sid ||
800aad82892SSeth Forshee 		    defcontext_sid) {
801aad82892SSeth Forshee 			rc = -EACCES;
802aad82892SSeth Forshee 			goto out;
803aad82892SSeth Forshee 		}
804aad82892SSeth Forshee 		if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
805aad82892SSeth Forshee 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
806aa8e712cSStephen Smalley 			rc = security_transition_sid(&selinux_state,
807aa8e712cSStephen Smalley 						     current_sid(),
808aa8e712cSStephen Smalley 						     current_sid(),
809aad82892SSeth Forshee 						     SECCLASS_FILE, NULL,
810aad82892SSeth Forshee 						     &sbsec->mntpoint_sid);
811aad82892SSeth Forshee 			if (rc)
812aad82892SSeth Forshee 				goto out;
813aad82892SSeth Forshee 		}
814aad82892SSeth Forshee 		goto out_set_opts;
815aad82892SSeth Forshee 	}
816aad82892SSeth Forshee 
817c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
818c9180a57SEric Paris 	if (fscontext_sid) {
819275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
820c9180a57SEric Paris 		if (rc)
821c9180a57SEric Paris 			goto out;
822c9180a57SEric Paris 
823c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
824c9180a57SEric Paris 	}
825c9180a57SEric Paris 
826c9180a57SEric Paris 	/*
827c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
828c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
829c9180a57SEric Paris 	 * the superblock context if not already set.
830c9180a57SEric Paris 	 */
831eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
832eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
833eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
834eb9ae686SDavid Quigley 	}
835eb9ae686SDavid Quigley 
836c9180a57SEric Paris 	if (context_sid) {
837c9180a57SEric Paris 		if (!fscontext_sid) {
838275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
839275bb41eSDavid Howells 							  cred);
840c9180a57SEric Paris 			if (rc)
841c9180a57SEric Paris 				goto out;
842c9180a57SEric Paris 			sbsec->sid = context_sid;
843c9180a57SEric Paris 		} else {
844275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
845275bb41eSDavid Howells 							     cred);
846c9180a57SEric Paris 			if (rc)
847c9180a57SEric Paris 				goto out;
848c9180a57SEric Paris 		}
849c9180a57SEric Paris 		if (!rootcontext_sid)
850c9180a57SEric Paris 			rootcontext_sid = context_sid;
851c9180a57SEric Paris 
852c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
853c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
854c9180a57SEric Paris 	}
855c9180a57SEric Paris 
856c9180a57SEric Paris 	if (rootcontext_sid) {
857275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
858275bb41eSDavid Howells 						     cred);
859c9180a57SEric Paris 		if (rc)
860c9180a57SEric Paris 			goto out;
861c9180a57SEric Paris 
862c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
8636f3be9f5SAndreas Gruenbacher 		root_isec->initialized = LABEL_INITIALIZED;
864c9180a57SEric Paris 	}
865c9180a57SEric Paris 
866c9180a57SEric Paris 	if (defcontext_sid) {
867eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
868eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
869c9180a57SEric Paris 			rc = -EINVAL;
870c103a91eSpeter enderborg 			pr_warn("SELinux: defcontext option is "
871c9180a57SEric Paris 			       "invalid for this filesystem type\n");
872c9180a57SEric Paris 			goto out;
873c9180a57SEric Paris 		}
874c9180a57SEric Paris 
875c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
876c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
877275bb41eSDavid Howells 							     sbsec, cred);
878c9180a57SEric Paris 			if (rc)
879c9180a57SEric Paris 				goto out;
880c9180a57SEric Paris 		}
881c9180a57SEric Paris 
882c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
883c9180a57SEric Paris 	}
884c9180a57SEric Paris 
885aad82892SSeth Forshee out_set_opts:
886c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
887c9180a57SEric Paris out:
888bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8891da177e4SLinus Torvalds 	return rc;
890c9180a57SEric Paris out_double_mount:
891c9180a57SEric Paris 	rc = -EINVAL;
892c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, different "
893bd323655SAl Viro 	       "security settings for (dev %s, type %s)\n", sb->s_id,
894bd323655SAl Viro 	       sb->s_type->name);
895c9180a57SEric Paris 	goto out;
896c9180a57SEric Paris }
897c9180a57SEric Paris 
898094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
899094f7b69SJeff Layton 				    const struct super_block *newsb)
900094f7b69SJeff Layton {
901094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
902094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
903094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
904094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
905094f7b69SJeff Layton 
906094f7b69SJeff Layton 	if (oldflags != newflags)
907094f7b69SJeff Layton 		goto mismatch;
908094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
909094f7b69SJeff Layton 		goto mismatch;
910094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
911094f7b69SJeff Layton 		goto mismatch;
912094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
913094f7b69SJeff Layton 		goto mismatch;
914094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
91583da53c5SAndreas Gruenbacher 		struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
91683da53c5SAndreas Gruenbacher 		struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
917094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
918094f7b69SJeff Layton 			goto mismatch;
919094f7b69SJeff Layton 	}
920094f7b69SJeff Layton 	return 0;
921094f7b69SJeff Layton mismatch:
922c103a91eSpeter enderborg 	pr_warn("SELinux: mount invalid.  Same superblock, "
923094f7b69SJeff Layton 			    "different security settings for (dev %s, "
924094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
925094f7b69SJeff Layton 	return -EBUSY;
926094f7b69SJeff Layton }
927094f7b69SJeff Layton 
928094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
9290b4d3452SScott Mayhew 					struct super_block *newsb,
9300b4d3452SScott Mayhew 					unsigned long kern_flags,
9310b4d3452SScott Mayhew 					unsigned long *set_kern_flags)
932c9180a57SEric Paris {
9330b4d3452SScott Mayhew 	int rc = 0;
934c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
935c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
936c9180a57SEric Paris 
937c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
938c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
939c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
940c9180a57SEric Paris 
9410f5e6420SEric Paris 	/*
9420f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
943e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
9440f5e6420SEric Paris 	 */
945aa8e712cSStephen Smalley 	if (!selinux_state.initialized)
946094f7b69SJeff Layton 		return 0;
947c9180a57SEric Paris 
9480b4d3452SScott Mayhew 	/*
9490b4d3452SScott Mayhew 	 * Specifying internal flags without providing a place to
9500b4d3452SScott Mayhew 	 * place the results is not allowed.
9510b4d3452SScott Mayhew 	 */
9520b4d3452SScott Mayhew 	if (kern_flags && !set_kern_flags)
9530b4d3452SScott Mayhew 		return -EINVAL;
9540b4d3452SScott Mayhew 
955c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
9560d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
957c9180a57SEric Paris 
958094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
9590d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
960094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
9615a552617SEric Paris 
962c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
963c9180a57SEric Paris 
964c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
965c9180a57SEric Paris 
966c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
967c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
968c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
969c9180a57SEric Paris 
9700b4d3452SScott Mayhew 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
9710b4d3452SScott Mayhew 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
972aa8e712cSStephen Smalley 		rc = security_fs_use(&selinux_state, newsb);
9730b4d3452SScott Mayhew 		if (rc)
9740b4d3452SScott Mayhew 			goto out;
9750b4d3452SScott Mayhew 	}
9760b4d3452SScott Mayhew 
9770b4d3452SScott Mayhew 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
9780b4d3452SScott Mayhew 		newsbsec->behavior = SECURITY_FS_USE_NATIVE;
9790b4d3452SScott Mayhew 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
9800b4d3452SScott Mayhew 	}
9810b4d3452SScott Mayhew 
982c9180a57SEric Paris 	if (set_context) {
983c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
984c9180a57SEric Paris 
985c9180a57SEric Paris 		if (!set_fscontext)
986c9180a57SEric Paris 			newsbsec->sid = sid;
987c9180a57SEric Paris 		if (!set_rootcontext) {
98883da53c5SAndreas Gruenbacher 			struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
989c9180a57SEric Paris 			newisec->sid = sid;
990c9180a57SEric Paris 		}
991c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
992c9180a57SEric Paris 	}
993c9180a57SEric Paris 	if (set_rootcontext) {
99483da53c5SAndreas Gruenbacher 		const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
99583da53c5SAndreas Gruenbacher 		struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
996c9180a57SEric Paris 
997c9180a57SEric Paris 		newisec->sid = oldisec->sid;
998c9180a57SEric Paris 	}
999c9180a57SEric Paris 
1000c9180a57SEric Paris 	sb_finish_set_opts(newsb);
10010b4d3452SScott Mayhew out:
1002c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
10030b4d3452SScott Mayhew 	return rc;
1004c9180a57SEric Paris }
1005c9180a57SEric Paris 
1006ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts)
1007c9180a57SEric Paris {
1008ba641862SAl Viro 	struct selinux_mnt_opts *opts = *mnt_opts;
1009c9180a57SEric Paris 
1010da3d76abSAl Viro 	if (token == Opt_seclabel)	/* eaten and completely ignored */
1011e0007529SEric Paris 		return 0;
1012e0007529SEric Paris 
1013ba641862SAl Viro 	if (!opts) {
1014ba641862SAl Viro 		opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1015ba641862SAl Viro 		if (!opts)
1016ba641862SAl Viro 			return -ENOMEM;
1017ba641862SAl Viro 		*mnt_opts = opts;
1018ba641862SAl Viro 	}
1019ba641862SAl Viro 	if (!s)
1020ba641862SAl Viro 		return -ENOMEM;
1021ba641862SAl Viro 	switch (token) {
1022ba641862SAl Viro 	case Opt_context:
1023ba641862SAl Viro 		if (opts->context || opts->defcontext)
1024ba641862SAl Viro 			goto Einval;
1025ba641862SAl Viro 		opts->context = s;
1026ba641862SAl Viro 		break;
1027ba641862SAl Viro 	case Opt_fscontext:
1028ba641862SAl Viro 		if (opts->fscontext)
1029ba641862SAl Viro 			goto Einval;
1030ba641862SAl Viro 		opts->fscontext = s;
1031ba641862SAl Viro 		break;
1032ba641862SAl Viro 	case Opt_rootcontext:
1033ba641862SAl Viro 		if (opts->rootcontext)
1034ba641862SAl Viro 			goto Einval;
1035ba641862SAl Viro 		opts->rootcontext = s;
1036ba641862SAl Viro 		break;
1037ba641862SAl Viro 	case Opt_defcontext:
1038ba641862SAl Viro 		if (opts->context || opts->defcontext)
1039ba641862SAl Viro 			goto Einval;
1040ba641862SAl Viro 		opts->defcontext = s;
1041ba641862SAl Viro 		break;
1042ba641862SAl Viro 	}
1043ba641862SAl Viro 	return 0;
1044ba641862SAl Viro Einval:
1045ba641862SAl Viro 	pr_warn(SEL_MOUNT_FAIL_MSG);
1046ba641862SAl Viro 	return -EINVAL;
1047ba641862SAl Viro }
1048ba641862SAl Viro 
1049757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1050204cc0ccSAl Viro 			       void **mnt_opts)
1051c9180a57SEric Paris {
1052757cbe59SAl Viro 	int token = Opt_error;
1053757cbe59SAl Viro 	int rc, i;
1054c9180a57SEric Paris 
1055757cbe59SAl Viro 	for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1056757cbe59SAl Viro 		if (strcmp(option, tokens[i].name) == 0) {
1057757cbe59SAl Viro 			token = tokens[i].opt;
1058757cbe59SAl Viro 			break;
1059757cbe59SAl Viro 		}
1060169d68efSAl Viro 	}
1061169d68efSAl Viro 
1062757cbe59SAl Viro 	if (token == Opt_error)
1063757cbe59SAl Viro 		return -EINVAL;
1064c9180a57SEric Paris 
1065757cbe59SAl Viro 	if (token != Opt_seclabel)
1066757cbe59SAl Viro 		val = kmemdup_nul(val, len, GFP_KERNEL);
1067757cbe59SAl Viro 	rc = selinux_add_opt(token, val, mnt_opts);
1068757cbe59SAl Viro 	if (unlikely(rc)) {
1069757cbe59SAl Viro 		kfree(val);
1070757cbe59SAl Viro 		if (*mnt_opts) {
1071ba641862SAl Viro 			selinux_free_mnt_opts(*mnt_opts);
1072ba641862SAl Viro 			*mnt_opts = NULL;
1073757cbe59SAl Viro 		}
1074757cbe59SAl Viro 	}
1075c9180a57SEric Paris 	return rc;
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
1078e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid)
10792069f457SEric Paris {
1080e3489f89SAl Viro 	char *context = NULL;
1081e3489f89SAl Viro 	u32 len;
1082e3489f89SAl Viro 	int rc;
10832069f457SEric Paris 
1084e3489f89SAl Viro 	rc = security_sid_to_context(&selinux_state, sid,
1085e3489f89SAl Viro 					     &context, &len);
1086e3489f89SAl Viro 	if (!rc) {
1087e3489f89SAl Viro 		bool has_comma = context && strchr(context, ',');
108811689d47SDavid P. Quigley 
10892069f457SEric Paris 		if (has_comma)
10902069f457SEric Paris 			seq_putc(m, '\"');
1091e3489f89SAl Viro 		seq_escape(m, context, "\"\n\\");
10922069f457SEric Paris 		if (has_comma)
10932069f457SEric Paris 			seq_putc(m, '\"');
10942069f457SEric Paris 	}
10951da177e4SLinus Torvalds 	kfree(context);
10961da177e4SLinus Torvalds 	return rc;
10971da177e4SLinus Torvalds }
10982069f457SEric Paris 
10992069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
11002069f457SEric Paris {
1101e3489f89SAl Viro 	struct superblock_security_struct *sbsec = sb->s_security;
11022069f457SEric Paris 	int rc;
11032069f457SEric Paris 
1104e3489f89SAl Viro 	if (!(sbsec->flags & SE_SBINITIALIZED))
1105e3489f89SAl Viro 		return 0;
1106e3489f89SAl Viro 
1107e3489f89SAl Viro 	if (!selinux_state.initialized)
1108e3489f89SAl Viro 		return 0;
1109e3489f89SAl Viro 
1110e3489f89SAl Viro 	if (sbsec->flags & FSCONTEXT_MNT) {
1111e3489f89SAl Viro 		seq_putc(m, ',');
1112e3489f89SAl Viro 		seq_puts(m, FSCONTEXT_STR);
1113e3489f89SAl Viro 		rc = show_sid(m, sbsec->sid);
1114e3489f89SAl Viro 		if (rc)
11152069f457SEric Paris 			return rc;
1116383795c2SEric Paris 	}
1117e3489f89SAl Viro 	if (sbsec->flags & CONTEXT_MNT) {
1118e3489f89SAl Viro 		seq_putc(m, ',');
1119e3489f89SAl Viro 		seq_puts(m, CONTEXT_STR);
1120e3489f89SAl Viro 		rc = show_sid(m, sbsec->mntpoint_sid);
1121e3489f89SAl Viro 		if (rc)
11222069f457SEric Paris 			return rc;
11232069f457SEric Paris 	}
1124e3489f89SAl Viro 	if (sbsec->flags & DEFCONTEXT_MNT) {
1125e3489f89SAl Viro 		seq_putc(m, ',');
1126e3489f89SAl Viro 		seq_puts(m, DEFCONTEXT_STR);
1127e3489f89SAl Viro 		rc = show_sid(m, sbsec->def_sid);
1128e3489f89SAl Viro 		if (rc)
1129e3489f89SAl Viro 			return rc;
1130e3489f89SAl Viro 	}
1131e3489f89SAl Viro 	if (sbsec->flags & ROOTCONTEXT_MNT) {
1132e3489f89SAl Viro 		struct dentry *root = sbsec->sb->s_root;
1133e3489f89SAl Viro 		struct inode_security_struct *isec = backing_inode_security(root);
1134e3489f89SAl Viro 		seq_putc(m, ',');
1135e3489f89SAl Viro 		seq_puts(m, ROOTCONTEXT_STR);
1136e3489f89SAl Viro 		rc = show_sid(m, isec->sid);
1137e3489f89SAl Viro 		if (rc)
1138e3489f89SAl Viro 			return rc;
1139e3489f89SAl Viro 	}
1140e3489f89SAl Viro 	if (sbsec->flags & SBLABEL_MNT) {
1141e3489f89SAl Viro 		seq_putc(m, ',');
1142e3489f89SAl Viro 		seq_puts(m, LABELSUPP_STR);
1143e3489f89SAl Viro 	}
1144e3489f89SAl Viro 	return 0;
1145e3489f89SAl Viro }
11462069f457SEric Paris 
11471da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11501da177e4SLinus Torvalds 	case S_IFSOCK:
11511da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11521da177e4SLinus Torvalds 	case S_IFLNK:
11531da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11541da177e4SLinus Torvalds 	case S_IFREG:
11551da177e4SLinus Torvalds 		return SECCLASS_FILE;
11561da177e4SLinus Torvalds 	case S_IFBLK:
11571da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11581da177e4SLinus Torvalds 	case S_IFDIR:
11591da177e4SLinus Torvalds 		return SECCLASS_DIR;
11601da177e4SLinus Torvalds 	case S_IFCHR:
11611da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11621da177e4SLinus Torvalds 	case S_IFIFO:
11631da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	return SECCLASS_FILE;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
117013402580SJames Morris static inline int default_protocol_stream(int protocol)
117113402580SJames Morris {
117213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
117313402580SJames Morris }
117413402580SJames Morris 
117513402580SJames Morris static inline int default_protocol_dgram(int protocol)
117613402580SJames Morris {
117713402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
117813402580SJames Morris }
117913402580SJames Morris 
11801da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11811da177e4SLinus Torvalds {
1182aa8e712cSStephen Smalley 	int extsockclass = selinux_policycap_extsockclass();
1183da69a530SStephen Smalley 
11841da177e4SLinus Torvalds 	switch (family) {
11851da177e4SLinus Torvalds 	case PF_UNIX:
11861da177e4SLinus Torvalds 		switch (type) {
11871da177e4SLinus Torvalds 		case SOCK_STREAM:
11881da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11891da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11901da177e4SLinus Torvalds 		case SOCK_DGRAM:
11912a764b52SLuis Ressel 		case SOCK_RAW:
11921da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11931da177e4SLinus Torvalds 		}
11941da177e4SLinus Torvalds 		break;
11951da177e4SLinus Torvalds 	case PF_INET:
11961da177e4SLinus Torvalds 	case PF_INET6:
11971da177e4SLinus Torvalds 		switch (type) {
11981da177e4SLinus Torvalds 		case SOCK_STREAM:
1199da69a530SStephen Smalley 		case SOCK_SEQPACKET:
120013402580SJames Morris 			if (default_protocol_stream(protocol))
12011da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
1202da69a530SStephen Smalley 			else if (extsockclass && protocol == IPPROTO_SCTP)
1203da69a530SStephen Smalley 				return SECCLASS_SCTP_SOCKET;
120413402580SJames Morris 			else
120513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
12061da177e4SLinus Torvalds 		case SOCK_DGRAM:
120713402580SJames Morris 			if (default_protocol_dgram(protocol))
12081da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
1209ef37979aSStephen Smalley 			else if (extsockclass && (protocol == IPPROTO_ICMP ||
1210ef37979aSStephen Smalley 						  protocol == IPPROTO_ICMPV6))
1211da69a530SStephen Smalley 				return SECCLASS_ICMP_SOCKET;
121213402580SJames Morris 			else
121313402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
12142ee92d46SJames Morris 		case SOCK_DCCP:
12152ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
121613402580SJames Morris 		default:
12171da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
12181da177e4SLinus Torvalds 		}
12191da177e4SLinus Torvalds 		break;
12201da177e4SLinus Torvalds 	case PF_NETLINK:
12211da177e4SLinus Torvalds 		switch (protocol) {
12221da177e4SLinus Torvalds 		case NETLINK_ROUTE:
12231da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
12247f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
12251da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
12261da177e4SLinus Torvalds 		case NETLINK_NFLOG:
12271da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
12281da177e4SLinus Torvalds 		case NETLINK_XFRM:
12291da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
12301da177e4SLinus Torvalds 		case NETLINK_SELINUX:
12311da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
12326c6d2e9bSStephen Smalley 		case NETLINK_ISCSI:
12336c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_ISCSI_SOCKET;
12341da177e4SLinus Torvalds 		case NETLINK_AUDIT:
12351da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
12366c6d2e9bSStephen Smalley 		case NETLINK_FIB_LOOKUP:
12376c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
12386c6d2e9bSStephen Smalley 		case NETLINK_CONNECTOR:
12396c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CONNECTOR_SOCKET;
12406c6d2e9bSStephen Smalley 		case NETLINK_NETFILTER:
12416c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_NETFILTER_SOCKET;
12421da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12431da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12440c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12450c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12466c6d2e9bSStephen Smalley 		case NETLINK_GENERIC:
12476c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_GENERIC_SOCKET;
12486c6d2e9bSStephen Smalley 		case NETLINK_SCSITRANSPORT:
12496c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
12506c6d2e9bSStephen Smalley 		case NETLINK_RDMA:
12516c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_RDMA_SOCKET;
12526c6d2e9bSStephen Smalley 		case NETLINK_CRYPTO:
12536c6d2e9bSStephen Smalley 			return SECCLASS_NETLINK_CRYPTO_SOCKET;
12541da177e4SLinus Torvalds 		default:
12551da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 	case PF_PACKET:
12581da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12591da177e4SLinus Torvalds 	case PF_KEY:
12601da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12613e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12623e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
1265da69a530SStephen Smalley 	if (extsockclass) {
1266da69a530SStephen Smalley 		switch (family) {
1267da69a530SStephen Smalley 		case PF_AX25:
1268da69a530SStephen Smalley 			return SECCLASS_AX25_SOCKET;
1269da69a530SStephen Smalley 		case PF_IPX:
1270da69a530SStephen Smalley 			return SECCLASS_IPX_SOCKET;
1271da69a530SStephen Smalley 		case PF_NETROM:
1272da69a530SStephen Smalley 			return SECCLASS_NETROM_SOCKET;
1273da69a530SStephen Smalley 		case PF_ATMPVC:
1274da69a530SStephen Smalley 			return SECCLASS_ATMPVC_SOCKET;
1275da69a530SStephen Smalley 		case PF_X25:
1276da69a530SStephen Smalley 			return SECCLASS_X25_SOCKET;
1277da69a530SStephen Smalley 		case PF_ROSE:
1278da69a530SStephen Smalley 			return SECCLASS_ROSE_SOCKET;
1279da69a530SStephen Smalley 		case PF_DECnet:
1280da69a530SStephen Smalley 			return SECCLASS_DECNET_SOCKET;
1281da69a530SStephen Smalley 		case PF_ATMSVC:
1282da69a530SStephen Smalley 			return SECCLASS_ATMSVC_SOCKET;
1283da69a530SStephen Smalley 		case PF_RDS:
1284da69a530SStephen Smalley 			return SECCLASS_RDS_SOCKET;
1285da69a530SStephen Smalley 		case PF_IRDA:
1286da69a530SStephen Smalley 			return SECCLASS_IRDA_SOCKET;
1287da69a530SStephen Smalley 		case PF_PPPOX:
1288da69a530SStephen Smalley 			return SECCLASS_PPPOX_SOCKET;
1289da69a530SStephen Smalley 		case PF_LLC:
1290da69a530SStephen Smalley 			return SECCLASS_LLC_SOCKET;
1291da69a530SStephen Smalley 		case PF_CAN:
1292da69a530SStephen Smalley 			return SECCLASS_CAN_SOCKET;
1293da69a530SStephen Smalley 		case PF_TIPC:
1294da69a530SStephen Smalley 			return SECCLASS_TIPC_SOCKET;
1295da69a530SStephen Smalley 		case PF_BLUETOOTH:
1296da69a530SStephen Smalley 			return SECCLASS_BLUETOOTH_SOCKET;
1297da69a530SStephen Smalley 		case PF_IUCV:
1298da69a530SStephen Smalley 			return SECCLASS_IUCV_SOCKET;
1299da69a530SStephen Smalley 		case PF_RXRPC:
1300da69a530SStephen Smalley 			return SECCLASS_RXRPC_SOCKET;
1301da69a530SStephen Smalley 		case PF_ISDN:
1302da69a530SStephen Smalley 			return SECCLASS_ISDN_SOCKET;
1303da69a530SStephen Smalley 		case PF_PHONET:
1304da69a530SStephen Smalley 			return SECCLASS_PHONET_SOCKET;
1305da69a530SStephen Smalley 		case PF_IEEE802154:
1306da69a530SStephen Smalley 			return SECCLASS_IEEE802154_SOCKET;
1307da69a530SStephen Smalley 		case PF_CAIF:
1308da69a530SStephen Smalley 			return SECCLASS_CAIF_SOCKET;
1309da69a530SStephen Smalley 		case PF_ALG:
1310da69a530SStephen Smalley 			return SECCLASS_ALG_SOCKET;
1311da69a530SStephen Smalley 		case PF_NFC:
1312da69a530SStephen Smalley 			return SECCLASS_NFC_SOCKET;
1313da69a530SStephen Smalley 		case PF_VSOCK:
1314da69a530SStephen Smalley 			return SECCLASS_VSOCK_SOCKET;
1315da69a530SStephen Smalley 		case PF_KCM:
1316da69a530SStephen Smalley 			return SECCLASS_KCM_SOCKET;
1317da69a530SStephen Smalley 		case PF_QIPCRTR:
1318da69a530SStephen Smalley 			return SECCLASS_QIPCRTR_SOCKET;
13193051bf36SLinus Torvalds 		case PF_SMC:
13203051bf36SLinus Torvalds 			return SECCLASS_SMC_SOCKET;
132168e8b849SBjörn Töpel 		case PF_XDP:
132268e8b849SBjörn Töpel 			return SECCLASS_XDP_SOCKET;
132368e8b849SBjörn Töpel #if PF_MAX > 45
1324da69a530SStephen Smalley #error New address family defined, please update this function.
1325da69a530SStephen Smalley #endif
1326da69a530SStephen Smalley 		}
1327da69a530SStephen Smalley 	}
1328da69a530SStephen Smalley 
13291da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
1332134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry,
13331da177e4SLinus Torvalds 				 u16 tclass,
1334134509d5SStephen Smalley 				 u16 flags,
13351da177e4SLinus Torvalds 				 u32 *sid)
13361da177e4SLinus Torvalds {
13378e6c9693SLucian Adrian Grijincu 	int rc;
1338fc64005cSAl Viro 	struct super_block *sb = dentry->d_sb;
13398e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
13421da177e4SLinus Torvalds 	if (!buffer)
13431da177e4SLinus Torvalds 		return -ENOMEM;
13441da177e4SLinus Torvalds 
13458e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
13468e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
13478e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
13488e6c9693SLucian Adrian Grijincu 	else {
1349134509d5SStephen Smalley 		if (flags & SE_SBPROC) {
13508e6c9693SLucian Adrian Grijincu 			/* each process gets a /proc/PID/ entry. Strip off the
13518e6c9693SLucian Adrian Grijincu 			 * PID part to get a valid selinux labeling.
13528e6c9693SLucian Adrian Grijincu 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
13538e6c9693SLucian Adrian Grijincu 			while (path[1] >= '0' && path[1] <= '9') {
13548e6c9693SLucian Adrian Grijincu 				path[1] = '/';
13558e6c9693SLucian Adrian Grijincu 				path++;
13561da177e4SLinus Torvalds 			}
1357134509d5SStephen Smalley 		}
1358aa8e712cSStephen Smalley 		rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1359aa8e712cSStephen Smalley 					path, tclass, sid);
13607bb185edSStephen Smalley 		if (rc == -ENOENT) {
13617bb185edSStephen Smalley 			/* No match in policy, mark as unlabeled. */
13627bb185edSStephen Smalley 			*sid = SECINITSID_UNLABELED;
13637bb185edSStephen Smalley 			rc = 0;
13647bb185edSStephen Smalley 		}
13658e6c9693SLucian Adrian Grijincu 	}
13661da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
13671da177e4SLinus Torvalds 	return rc;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
13711da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
13741da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
13759287aed2SAndreas Gruenbacher 	u32 task_sid, sid = 0;
13769287aed2SAndreas Gruenbacher 	u16 sclass;
13771da177e4SLinus Torvalds 	struct dentry *dentry;
13781da177e4SLinus Torvalds #define INITCONTEXTLEN 255
13791da177e4SLinus Torvalds 	char *context = NULL;
13801da177e4SLinus Torvalds 	unsigned len = 0;
13811da177e4SLinus Torvalds 	int rc = 0;
13821da177e4SLinus Torvalds 
13836f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
138413457d07SAndreas Gruenbacher 		return 0;
13851da177e4SLinus Torvalds 
13869287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
13876f3be9f5SAndreas Gruenbacher 	if (isec->initialized == LABEL_INITIALIZED)
138823970741SEric Paris 		goto out_unlock;
13891da177e4SLinus Torvalds 
139013457d07SAndreas Gruenbacher 	if (isec->sclass == SECCLASS_FILE)
139113457d07SAndreas Gruenbacher 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
139213457d07SAndreas Gruenbacher 
13931da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
13940d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
13951da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
13961da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
13971da177e4SLinus Torvalds 		   server is ready to handle calls. */
13981da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
13991da177e4SLinus Torvalds 		if (list_empty(&isec->list))
14001da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
14011da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
140223970741SEric Paris 		goto out_unlock;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14059287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
14069287aed2SAndreas Gruenbacher 	task_sid = isec->task_sid;
14079287aed2SAndreas Gruenbacher 	sid = isec->sid;
14089287aed2SAndreas Gruenbacher 	isec->initialized = LABEL_PENDING;
14099287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
14109287aed2SAndreas Gruenbacher 
14111da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1412eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1413eb9ae686SDavid Quigley 		break;
14141da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
14155d6c3191SAndreas Gruenbacher 		if (!(inode->i_opflags & IOP_XATTR)) {
14169287aed2SAndreas Gruenbacher 			sid = sbsec->def_sid;
14171da177e4SLinus Torvalds 			break;
14181da177e4SLinus Torvalds 		}
14191da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
14201da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
14211da177e4SLinus Torvalds 		if (opt_dentry) {
14221da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
14231da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
14241da177e4SLinus Torvalds 		} else {
1425b127125dSAl Viro 			/*
1426b127125dSAl Viro 			 * Called from selinux_complete_init, try to find a dentry.
1427b127125dSAl Viro 			 * Some filesystems really want a connected one, so try
1428b127125dSAl Viro 			 * that first.  We could split SECURITY_FS_USE_XATTR in
1429b127125dSAl Viro 			 * two, depending upon that...
1430b127125dSAl Viro 			 */
14311da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
1432b127125dSAl Viro 			if (!dentry)
1433b127125dSAl Viro 				dentry = d_find_any_alias(inode);
14341da177e4SLinus Torvalds 		}
14351da177e4SLinus Torvalds 		if (!dentry) {
1436df7f54c0SEric Paris 			/*
1437df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1438df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1439df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1440df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1441df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1442df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1443df7f54c0SEric Paris 			 * be used again by userspace.
1444df7f54c0SEric Paris 			 */
14459287aed2SAndreas Gruenbacher 			goto out;
14461da177e4SLinus Torvalds 		}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
14494cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
14501da177e4SLinus Torvalds 		if (!context) {
14511da177e4SLinus Torvalds 			rc = -ENOMEM;
14521da177e4SLinus Torvalds 			dput(dentry);
14539287aed2SAndreas Gruenbacher 			goto out;
14541da177e4SLinus Torvalds 		}
14554cb912f1SEric Paris 		context[len] = '\0';
14565d6c3191SAndreas Gruenbacher 		rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
14571da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1458314dabb8SJames Morris 			kfree(context);
1459314dabb8SJames Morris 
14601da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
14615d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
14621da177e4SLinus Torvalds 			if (rc < 0) {
14631da177e4SLinus Torvalds 				dput(dentry);
14649287aed2SAndreas Gruenbacher 				goto out;
14651da177e4SLinus Torvalds 			}
14661da177e4SLinus Torvalds 			len = rc;
14674cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
14681da177e4SLinus Torvalds 			if (!context) {
14691da177e4SLinus Torvalds 				rc = -ENOMEM;
14701da177e4SLinus Torvalds 				dput(dentry);
14719287aed2SAndreas Gruenbacher 				goto out;
14721da177e4SLinus Torvalds 			}
14734cb912f1SEric Paris 			context[len] = '\0';
14745d6c3191SAndreas Gruenbacher 			rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
14751da177e4SLinus Torvalds 		}
14761da177e4SLinus Torvalds 		dput(dentry);
14771da177e4SLinus Torvalds 		if (rc < 0) {
14781da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1479c103a91eSpeter enderborg 				pr_warn("SELinux: %s:  getxattr returned "
1480dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
14811da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
14821da177e4SLinus Torvalds 				kfree(context);
14839287aed2SAndreas Gruenbacher 				goto out;
14841da177e4SLinus Torvalds 			}
14851da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
14861da177e4SLinus Torvalds 			sid = sbsec->def_sid;
14871da177e4SLinus Torvalds 			rc = 0;
14881da177e4SLinus Torvalds 		} else {
1489aa8e712cSStephen Smalley 			rc = security_context_to_sid_default(&selinux_state,
1490aa8e712cSStephen Smalley 							     context, rc, &sid,
1491869ab514SStephen Smalley 							     sbsec->def_sid,
1492869ab514SStephen Smalley 							     GFP_NOFS);
14931da177e4SLinus Torvalds 			if (rc) {
14944ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
14954ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
14964ba0a8adSEric Paris 
14974ba0a8adSEric Paris 				if (rc == -EINVAL) {
14984ba0a8adSEric Paris 					if (printk_ratelimit())
1499c103a91eSpeter enderborg 						pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
15004ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
15014ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
15024ba0a8adSEric Paris 				} else {
1503c103a91eSpeter enderborg 					pr_warn("SELinux: %s:  context_to_sid(%s) "
15041da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
15054ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
15064ba0a8adSEric Paris 				}
15071da177e4SLinus Torvalds 				kfree(context);
15081da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
15091da177e4SLinus Torvalds 				rc = 0;
15101da177e4SLinus Torvalds 				break;
15111da177e4SLinus Torvalds 			}
15121da177e4SLinus Torvalds 		}
15131da177e4SLinus Torvalds 		kfree(context);
15141da177e4SLinus Torvalds 		break;
15151da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
15169287aed2SAndreas Gruenbacher 		sid = task_sid;
15171da177e4SLinus Torvalds 		break;
15181da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
15191da177e4SLinus Torvalds 		/* Default to the fs SID. */
15209287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
1523aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, task_sid, sid,
1524aa8e712cSStephen Smalley 					     sclass, NULL, &sid);
15251da177e4SLinus Torvalds 		if (rc)
15269287aed2SAndreas Gruenbacher 			goto out;
15271da177e4SLinus Torvalds 		break;
1528c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
15299287aed2SAndreas Gruenbacher 		sid = sbsec->mntpoint_sid;
1530c312feb2SEric Paris 		break;
15311da177e4SLinus Torvalds 	default:
1532c312feb2SEric Paris 		/* Default to the fs superblock SID. */
15339287aed2SAndreas Gruenbacher 		sid = sbsec->sid;
15341da177e4SLinus Torvalds 
1535134509d5SStephen Smalley 		if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1536f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1537f64410ecSPaul Moore 			 * procfs inodes */
1538b127125dSAl Viro 			if (opt_dentry) {
1539f64410ecSPaul Moore 				/* Called from d_instantiate or
1540f64410ecSPaul Moore 				 * d_splice_alias. */
1541f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1542b127125dSAl Viro 			} else {
1543f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1544b127125dSAl Viro 				 * find a dentry.  Some filesystems really want
1545b127125dSAl Viro 				 * a connected one, so try that first.
1546b127125dSAl Viro 				 */
1547f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1548b127125dSAl Viro 				if (!dentry)
1549b127125dSAl Viro 					dentry = d_find_any_alias(inode);
1550b127125dSAl Viro 			}
1551f64410ecSPaul Moore 			/*
1552f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1553f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1554f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1555f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1556f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1557f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1558f64410ecSPaul Moore 			 * could be used again by userspace.
1559f64410ecSPaul Moore 			 */
1560f64410ecSPaul Moore 			if (!dentry)
15619287aed2SAndreas Gruenbacher 				goto out;
15629287aed2SAndreas Gruenbacher 			rc = selinux_genfs_get_sid(dentry, sclass,
1563134509d5SStephen Smalley 						   sbsec->flags, &sid);
1564f64410ecSPaul Moore 			dput(dentry);
15651da177e4SLinus Torvalds 			if (rc)
15669287aed2SAndreas Gruenbacher 				goto out;
15671da177e4SLinus Torvalds 		}
15681da177e4SLinus Torvalds 		break;
15691da177e4SLinus Torvalds 	}
15701da177e4SLinus Torvalds 
15719287aed2SAndreas Gruenbacher out:
15729287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
15739287aed2SAndreas Gruenbacher 	if (isec->initialized == LABEL_PENDING) {
15749287aed2SAndreas Gruenbacher 		if (!sid || rc) {
15759287aed2SAndreas Gruenbacher 			isec->initialized = LABEL_INVALID;
15769287aed2SAndreas Gruenbacher 			goto out_unlock;
15779287aed2SAndreas Gruenbacher 		}
15789287aed2SAndreas Gruenbacher 
15796f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
15809287aed2SAndreas Gruenbacher 		isec->sid = sid;
15819287aed2SAndreas Gruenbacher 	}
15821da177e4SLinus Torvalds 
158323970741SEric Paris out_unlock:
15849287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
15851da177e4SLinus Torvalds 	return rc;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
15891da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	u32 perm = 0;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	switch (sig) {
15941da177e4SLinus Torvalds 	case SIGCHLD:
15951da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
15961da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
15971da177e4SLinus Torvalds 		break;
15981da177e4SLinus Torvalds 	case SIGKILL:
15991da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
16001da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
16011da177e4SLinus Torvalds 		break;
16021da177e4SLinus Torvalds 	case SIGSTOP:
16031da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
16041da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
16051da177e4SLinus Torvalds 		break;
16061da177e4SLinus Torvalds 	default:
16071da177e4SLinus Torvalds 		/* All other signals. */
16081da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
16091da177e4SLinus Torvalds 		break;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	return perm;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
1615b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1616b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1617b68e418cSStephen Smalley #endif
1618b68e418cSStephen Smalley 
16191da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
16206a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
16218e4ff6f2SStephen Smalley 			       int cap, int audit, bool initns)
16221da177e4SLinus Torvalds {
16232bf49690SThomas Liu 	struct common_audit_data ad;
162406112163SEric Paris 	struct av_decision avd;
1625b68e418cSStephen Smalley 	u16 sclass;
16263699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1627b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
162806112163SEric Paris 	int rc;
16291da177e4SLinus Torvalds 
163050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
16311da177e4SLinus Torvalds 	ad.u.cap = cap;
16321da177e4SLinus Torvalds 
1633b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1634b68e418cSStephen Smalley 	case 0:
16358e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1636b68e418cSStephen Smalley 		break;
1637b68e418cSStephen Smalley 	case 1:
16388e4ff6f2SStephen Smalley 		sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1639b68e418cSStephen Smalley 		break;
1640b68e418cSStephen Smalley 	default:
1641c103a91eSpeter enderborg 		pr_err("SELinux:  out of range capability %d\n", cap);
1642b68e418cSStephen Smalley 		BUG();
1643a35c6c83SEric Paris 		return -EINVAL;
1644b68e418cSStephen Smalley 	}
164506112163SEric Paris 
16466b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
16476b6bc620SStephen Smalley 				  sid, sid, sclass, av, 0, &avd);
16489ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
16496b6bc620SStephen Smalley 		int rc2 = avc_audit(&selinux_state,
16506b6bc620SStephen Smalley 				    sid, sid, sclass, av, &avd, rc, &ad, 0);
16519ade0cf4SEric Paris 		if (rc2)
16529ade0cf4SEric Paris 			return rc2;
16539ade0cf4SEric Paris 	}
165406112163SEric Paris 	return rc;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
16581da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
16591da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
166088e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
16611da177e4SLinus Torvalds 			  struct inode *inode,
16621da177e4SLinus Torvalds 			  u32 perms,
166319e49834SLinus Torvalds 			  struct common_audit_data *adp)
16641da177e4SLinus Torvalds {
16651da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1666275bb41eSDavid Howells 	u32 sid;
16671da177e4SLinus Torvalds 
1668e0e81739SDavid Howells 	validate_creds(cred);
1669e0e81739SDavid Howells 
1670bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1671bbaca6c2SStephen Smalley 		return 0;
1672bbaca6c2SStephen Smalley 
167388e67f3bSDavid Howells 	sid = cred_sid(cred);
16741da177e4SLinus Torvalds 	isec = inode->i_security;
16751da177e4SLinus Torvalds 
16766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
16776b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, adp);
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16811da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16821da177e4SLinus Torvalds    pathname if needed. */
168388e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16841da177e4SLinus Torvalds 				  struct dentry *dentry,
16851da177e4SLinus Torvalds 				  u32 av)
16861da177e4SLinus Torvalds {
1687c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16882bf49690SThomas Liu 	struct common_audit_data ad;
168988e67f3bSDavid Howells 
169050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16912875fa00SEric Paris 	ad.u.dentry = dentry;
16925d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, dentry, true);
169319e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16942875fa00SEric Paris }
16952875fa00SEric Paris 
16962875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16972875fa00SEric Paris    the path to help the auditing code to more easily generate the
16982875fa00SEric Paris    pathname if needed. */
16992875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
17003f7036a0SAl Viro 				const struct path *path,
17012875fa00SEric Paris 				u32 av)
17022875fa00SEric Paris {
1703c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
17042875fa00SEric Paris 	struct common_audit_data ad;
17052875fa00SEric Paris 
170650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
17072875fa00SEric Paris 	ad.u.path = *path;
17085d226df4SAndreas Gruenbacher 	__inode_security_revalidate(inode, path->dentry, true);
170919e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
171213f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
171313f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
171413f8e981SDavid Howells 				     struct file *file,
171513f8e981SDavid Howells 				     u32 av)
171613f8e981SDavid Howells {
171713f8e981SDavid Howells 	struct common_audit_data ad;
171813f8e981SDavid Howells 
171943af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
172043af5de7SVivek Goyal 	ad.u.file = file;
172119e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
172213f8e981SDavid Howells }
172313f8e981SDavid Howells 
1724f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1725f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid);
1726f66e448cSChenbo Feng #endif
1727f66e448cSChenbo Feng 
17281da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
17291da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
17301da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
17311da177e4SLinus Torvalds    check a particular permission to the file.
17321da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
17331da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
17341da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
17351da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
173688e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
17371da177e4SLinus Torvalds 			 struct file *file,
17381da177e4SLinus Torvalds 			 u32 av)
17391da177e4SLinus Torvalds {
17401da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1741496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17422bf49690SThomas Liu 	struct common_audit_data ad;
174388e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17441da177e4SLinus Torvalds 	int rc;
17451da177e4SLinus Torvalds 
174643af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
174743af5de7SVivek Goyal 	ad.u.file = file;
17481da177e4SLinus Torvalds 
1749275bb41eSDavid Howells 	if (sid != fsec->sid) {
17506b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
17516b6bc620SStephen Smalley 				  sid, fsec->sid,
17521da177e4SLinus Torvalds 				  SECCLASS_FD,
17531da177e4SLinus Torvalds 				  FD__USE,
17541da177e4SLinus Torvalds 				  &ad);
17551da177e4SLinus Torvalds 		if (rc)
175688e67f3bSDavid Howells 			goto out;
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 
1759f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
1760f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, cred_sid(cred));
1761f66e448cSChenbo Feng 	if (rc)
1762f66e448cSChenbo Feng 		return rc;
1763f66e448cSChenbo Feng #endif
1764f66e448cSChenbo Feng 
17651da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
176688e67f3bSDavid Howells 	rc = 0;
17671da177e4SLinus Torvalds 	if (av)
176819e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
17691da177e4SLinus Torvalds 
177088e67f3bSDavid Howells out:
177188e67f3bSDavid Howells 	return rc;
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
1774c3c188b2SDavid Howells /*
1775c3c188b2SDavid Howells  * Determine the label for an inode that might be unioned.
1776c3c188b2SDavid Howells  */
1777c957f6dfSVivek Goyal static int
1778c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec,
1779c957f6dfSVivek Goyal 				 struct inode *dir,
1780c957f6dfSVivek Goyal 				 const struct qstr *name, u16 tclass,
1781c3c188b2SDavid Howells 				 u32 *_new_isid)
1782c3c188b2SDavid Howells {
1783c3c188b2SDavid Howells 	const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1784c3c188b2SDavid Howells 
1785c3c188b2SDavid Howells 	if ((sbsec->flags & SE_SBINITIALIZED) &&
1786c3c188b2SDavid Howells 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1787c3c188b2SDavid Howells 		*_new_isid = sbsec->mntpoint_sid;
1788c3c188b2SDavid Howells 	} else if ((sbsec->flags & SBLABEL_MNT) &&
1789c3c188b2SDavid Howells 		   tsec->create_sid) {
1790c3c188b2SDavid Howells 		*_new_isid = tsec->create_sid;
1791c3c188b2SDavid Howells 	} else {
179220cdef8dSPaul Moore 		const struct inode_security_struct *dsec = inode_security(dir);
1793aa8e712cSStephen Smalley 		return security_transition_sid(&selinux_state, tsec->sid,
1794aa8e712cSStephen Smalley 					       dsec->sid, tclass,
1795c3c188b2SDavid Howells 					       name, _new_isid);
1796c3c188b2SDavid Howells 	}
1797c3c188b2SDavid Howells 
1798c3c188b2SDavid Howells 	return 0;
1799c3c188b2SDavid Howells }
1800c3c188b2SDavid Howells 
18011da177e4SLinus Torvalds /* Check whether a task can create a file. */
18021da177e4SLinus Torvalds static int may_create(struct inode *dir,
18031da177e4SLinus Torvalds 		      struct dentry *dentry,
18041da177e4SLinus Torvalds 		      u16 tclass)
18051da177e4SLinus Torvalds {
18065fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
18071da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
18081da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1809275bb41eSDavid Howells 	u32 sid, newsid;
18102bf49690SThomas Liu 	struct common_audit_data ad;
18111da177e4SLinus Torvalds 	int rc;
18121da177e4SLinus Torvalds 
181383da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
18141da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
18151da177e4SLinus Torvalds 
1816275bb41eSDavid Howells 	sid = tsec->sid;
1817275bb41eSDavid Howells 
181850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1819a269434dSEric Paris 	ad.u.dentry = dentry;
18201da177e4SLinus Torvalds 
18216b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18226b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR,
18231da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
18241da177e4SLinus Torvalds 			  &ad);
18251da177e4SLinus Torvalds 	if (rc)
18261da177e4SLinus Torvalds 		return rc;
18271da177e4SLinus Torvalds 
1828c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(), dir,
1829c957f6dfSVivek Goyal 					   &dentry->d_name, tclass, &newsid);
18301da177e4SLinus Torvalds 	if (rc)
18311da177e4SLinus Torvalds 		return rc;
18321da177e4SLinus Torvalds 
18336b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18346b6bc620SStephen Smalley 			  sid, newsid, tclass, FILE__CREATE, &ad);
18351da177e4SLinus Torvalds 	if (rc)
18361da177e4SLinus Torvalds 		return rc;
18371da177e4SLinus Torvalds 
18386b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
18396b6bc620SStephen Smalley 			    newsid, sbsec->sid,
18401da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
18411da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds #define MAY_LINK	0
18451da177e4SLinus Torvalds #define MAY_UNLINK	1
18461da177e4SLinus Torvalds #define MAY_RMDIR	2
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
18491da177e4SLinus Torvalds static int may_link(struct inode *dir,
18501da177e4SLinus Torvalds 		    struct dentry *dentry,
18511da177e4SLinus Torvalds 		    int kind)
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds {
18541da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
18552bf49690SThomas Liu 	struct common_audit_data ad;
1856275bb41eSDavid Howells 	u32 sid = current_sid();
18571da177e4SLinus Torvalds 	u32 av;
18581da177e4SLinus Torvalds 	int rc;
18591da177e4SLinus Torvalds 
186083da53c5SAndreas Gruenbacher 	dsec = inode_security(dir);
186183da53c5SAndreas Gruenbacher 	isec = backing_inode_security(dentry);
18621da177e4SLinus Torvalds 
186350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1864a269434dSEric Paris 	ad.u.dentry = dentry;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	av = DIR__SEARCH;
18671da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
18686b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18696b6bc620SStephen Smalley 			  sid, dsec->sid, SECCLASS_DIR, av, &ad);
18701da177e4SLinus Torvalds 	if (rc)
18711da177e4SLinus Torvalds 		return rc;
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	switch (kind) {
18741da177e4SLinus Torvalds 	case MAY_LINK:
18751da177e4SLinus Torvalds 		av = FILE__LINK;
18761da177e4SLinus Torvalds 		break;
18771da177e4SLinus Torvalds 	case MAY_UNLINK:
18781da177e4SLinus Torvalds 		av = FILE__UNLINK;
18791da177e4SLinus Torvalds 		break;
18801da177e4SLinus Torvalds 	case MAY_RMDIR:
18811da177e4SLinus Torvalds 		av = DIR__RMDIR;
18821da177e4SLinus Torvalds 		break;
18831da177e4SLinus Torvalds 	default:
1884c103a91eSpeter enderborg 		pr_warn("SELinux: %s:  unrecognized kind %d\n",
1885744ba35eSEric Paris 			__func__, kind);
18861da177e4SLinus Torvalds 		return 0;
18871da177e4SLinus Torvalds 	}
18881da177e4SLinus Torvalds 
18896b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
18906b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass, av, &ad);
18911da177e4SLinus Torvalds 	return rc;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
18951da177e4SLinus Torvalds 			     struct dentry *old_dentry,
18961da177e4SLinus Torvalds 			     struct inode *new_dir,
18971da177e4SLinus Torvalds 			     struct dentry *new_dentry)
18981da177e4SLinus Torvalds {
18991da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
19002bf49690SThomas Liu 	struct common_audit_data ad;
1901275bb41eSDavid Howells 	u32 sid = current_sid();
19021da177e4SLinus Torvalds 	u32 av;
19031da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
19041da177e4SLinus Torvalds 	int rc;
19051da177e4SLinus Torvalds 
190683da53c5SAndreas Gruenbacher 	old_dsec = inode_security(old_dir);
190783da53c5SAndreas Gruenbacher 	old_isec = backing_inode_security(old_dentry);
1908e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
190983da53c5SAndreas Gruenbacher 	new_dsec = inode_security(new_dir);
19101da177e4SLinus Torvalds 
191150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
19121da177e4SLinus Torvalds 
1913a269434dSEric Paris 	ad.u.dentry = old_dentry;
19146b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19156b6bc620SStephen Smalley 			  sid, old_dsec->sid, SECCLASS_DIR,
19161da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
19171da177e4SLinus Torvalds 	if (rc)
19181da177e4SLinus Torvalds 		return rc;
19196b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19206b6bc620SStephen Smalley 			  sid, old_isec->sid,
19211da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
19221da177e4SLinus Torvalds 	if (rc)
19231da177e4SLinus Torvalds 		return rc;
19241da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
19256b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19266b6bc620SStephen Smalley 				  sid, old_isec->sid,
19271da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
19281da177e4SLinus Torvalds 		if (rc)
19291da177e4SLinus Torvalds 			return rc;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
1932a269434dSEric Paris 	ad.u.dentry = new_dentry;
19331da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
19342c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
19351da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
19366b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
19376b6bc620SStephen Smalley 			  sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
19381da177e4SLinus Torvalds 	if (rc)
19391da177e4SLinus Torvalds 		return rc;
19402c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
194183da53c5SAndreas Gruenbacher 		new_isec = backing_inode_security(new_dentry);
1942e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
19436b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
19446b6bc620SStephen Smalley 				  sid, new_isec->sid,
19451da177e4SLinus Torvalds 				  new_isec->sclass,
19461da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
19471da177e4SLinus Torvalds 		if (rc)
19481da177e4SLinus Torvalds 			return rc;
19491da177e4SLinus Torvalds 	}
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	return 0;
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
195588e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
19561da177e4SLinus Torvalds 			       struct super_block *sb,
19571da177e4SLinus Torvalds 			       u32 perms,
19582bf49690SThomas Liu 			       struct common_audit_data *ad)
19591da177e4SLinus Torvalds {
19601da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
196188e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	sbsec = sb->s_security;
19646b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
19656b6bc620SStephen Smalley 			    sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
19691da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	u32 av = 0;
19721da177e4SLinus Torvalds 
1973dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
19741da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19751da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
19761da177e4SLinus Torvalds 		if (mask & MAY_READ)
19771da177e4SLinus Torvalds 			av |= FILE__READ;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
19801da177e4SLinus Torvalds 			av |= FILE__APPEND;
19811da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
19821da177e4SLinus Torvalds 			av |= FILE__WRITE;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	} else {
19851da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
19861da177e4SLinus Torvalds 			av |= DIR__SEARCH;
19871da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
19881da177e4SLinus Torvalds 			av |= DIR__WRITE;
19891da177e4SLinus Torvalds 		if (mask & MAY_READ)
19901da177e4SLinus Torvalds 			av |= DIR__READ;
19911da177e4SLinus Torvalds 	}
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	return av;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
19971da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
19981da177e4SLinus Torvalds {
19991da177e4SLinus Torvalds 	u32 av = 0;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
20021da177e4SLinus Torvalds 		av |= FILE__READ;
20031da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
20041da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
20051da177e4SLinus Torvalds 			av |= FILE__APPEND;
20061da177e4SLinus Torvalds 		else
20071da177e4SLinus Torvalds 			av |= FILE__WRITE;
20081da177e4SLinus Torvalds 	}
20090794c66dSStephen Smalley 	if (!av) {
20100794c66dSStephen Smalley 		/*
20110794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
20120794c66dSStephen Smalley 		 */
20130794c66dSStephen Smalley 		av = FILE__IOCTL;
20140794c66dSStephen Smalley 	}
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	return av;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
20198b6a5a37SEric Paris /*
20208b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
20218b6a5a37SEric Paris  * open permission.
20228b6a5a37SEric Paris  */
20238b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
20248b6a5a37SEric Paris {
20258b6a5a37SEric Paris 	u32 av = file_to_av(file);
2026ccb54478SStephen Smalley 	struct inode *inode = file_inode(file);
20278b6a5a37SEric Paris 
2028aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
2029aa8e712cSStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC)
20308b6a5a37SEric Paris 		av |= FILE__OPEN;
203149b7b8deSEric Paris 
20328b6a5a37SEric Paris 	return av;
20338b6a5a37SEric Paris }
20348b6a5a37SEric Paris 
20351da177e4SLinus Torvalds /* Hook functions begin here. */
20361da177e4SLinus Torvalds 
203779af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
203879af7307SStephen Smalley {
203979af7307SStephen Smalley 	u32 mysid = current_sid();
204079af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
204179af7307SStephen Smalley 
20426b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20436b6bc620SStephen Smalley 			    mysid, mgrsid, SECCLASS_BINDER,
204479af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
204579af7307SStephen Smalley }
204679af7307SStephen Smalley 
204779af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
204879af7307SStephen Smalley 				      struct task_struct *to)
204979af7307SStephen Smalley {
205079af7307SStephen Smalley 	u32 mysid = current_sid();
205179af7307SStephen Smalley 	u32 fromsid = task_sid(from);
205279af7307SStephen Smalley 	u32 tosid = task_sid(to);
205379af7307SStephen Smalley 	int rc;
205479af7307SStephen Smalley 
205579af7307SStephen Smalley 	if (mysid != fromsid) {
20566b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20576b6bc620SStephen Smalley 				  mysid, fromsid, SECCLASS_BINDER,
205879af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
205979af7307SStephen Smalley 		if (rc)
206079af7307SStephen Smalley 			return rc;
206179af7307SStephen Smalley 	}
206279af7307SStephen Smalley 
20636b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20646b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
206579af7307SStephen Smalley 			    NULL);
206679af7307SStephen Smalley }
206779af7307SStephen Smalley 
206879af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
206979af7307SStephen Smalley 					  struct task_struct *to)
207079af7307SStephen Smalley {
207179af7307SStephen Smalley 	u32 fromsid = task_sid(from);
207279af7307SStephen Smalley 	u32 tosid = task_sid(to);
207379af7307SStephen Smalley 
20746b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
20756b6bc620SStephen Smalley 			    fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
207679af7307SStephen Smalley 			    NULL);
207779af7307SStephen Smalley }
207879af7307SStephen Smalley 
207979af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
208079af7307SStephen Smalley 					struct task_struct *to,
208179af7307SStephen Smalley 					struct file *file)
208279af7307SStephen Smalley {
208379af7307SStephen Smalley 	u32 sid = task_sid(to);
208479af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
208583da53c5SAndreas Gruenbacher 	struct dentry *dentry = file->f_path.dentry;
208620cdef8dSPaul Moore 	struct inode_security_struct *isec;
208779af7307SStephen Smalley 	struct common_audit_data ad;
208879af7307SStephen Smalley 	int rc;
208979af7307SStephen Smalley 
209079af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
209179af7307SStephen Smalley 	ad.u.path = file->f_path;
209279af7307SStephen Smalley 
209379af7307SStephen Smalley 	if (sid != fsec->sid) {
20946b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
20956b6bc620SStephen Smalley 				  sid, fsec->sid,
209679af7307SStephen Smalley 				  SECCLASS_FD,
209779af7307SStephen Smalley 				  FD__USE,
209879af7307SStephen Smalley 				  &ad);
209979af7307SStephen Smalley 		if (rc)
210079af7307SStephen Smalley 			return rc;
210179af7307SStephen Smalley 	}
210279af7307SStephen Smalley 
2103f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL
2104f66e448cSChenbo Feng 	rc = bpf_fd_pass(file, sid);
2105f66e448cSChenbo Feng 	if (rc)
2106f66e448cSChenbo Feng 		return rc;
2107f66e448cSChenbo Feng #endif
2108f66e448cSChenbo Feng 
210983da53c5SAndreas Gruenbacher 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
211079af7307SStephen Smalley 		return 0;
211179af7307SStephen Smalley 
211220cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
21136b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21146b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, file_to_av(file),
211579af7307SStephen Smalley 			    &ad);
211679af7307SStephen Smalley }
211779af7307SStephen Smalley 
21189e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
2119006ebb40SStephen Smalley 				     unsigned int mode)
21201da177e4SLinus Torvalds {
2121275bb41eSDavid Howells 	u32 sid = current_sid();
2122275bb41eSDavid Howells 	u32 csid = task_sid(child);
2123006ebb40SStephen Smalley 
2124be0554c9SStephen Smalley 	if (mode & PTRACE_MODE_READ)
21256b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
21266b6bc620SStephen Smalley 				    sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2127be0554c9SStephen Smalley 
21286b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21296b6bc620SStephen Smalley 			    sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
21305cd9c58fSDavid Howells }
21315cd9c58fSDavid Howells 
21325cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
21335cd9c58fSDavid Howells {
21346b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21356b6bc620SStephen Smalley 			    task_sid(parent), current_sid(), SECCLASS_PROCESS,
2136be0554c9SStephen Smalley 			    PROCESS__PTRACE, NULL);
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
21401da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
21411da177e4SLinus Torvalds {
21426b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21436b6bc620SStephen Smalley 			    current_sid(), task_sid(target), SECCLASS_PROCESS,
2144be0554c9SStephen Smalley 			    PROCESS__GETCAP, NULL);
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
2147d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2148d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
214915a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
215015a2460eSDavid Howells 			  const kernel_cap_t *permitted)
21511da177e4SLinus Torvalds {
21526b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
21536b6bc620SStephen Smalley 			    cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2154be0554c9SStephen Smalley 			    PROCESS__SETCAP, NULL);
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds 
21575626d3e8SJames Morris /*
21585626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
21595626d3e8SJames Morris  * which was removed).
21605626d3e8SJames Morris  *
21615626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
21625626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
21635626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
21645626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
21655626d3e8SJames Morris  */
21665626d3e8SJames Morris 
21676a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
21686a9de491SEric Paris 			   int cap, int audit)
21691da177e4SLinus Torvalds {
21708e4ff6f2SStephen Smalley 	return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
21741da177e4SLinus Torvalds {
217588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
21761da177e4SLinus Torvalds 	int rc = 0;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	if (!sb)
21791da177e4SLinus Torvalds 		return 0;
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	switch (cmds) {
21821da177e4SLinus Torvalds 	case Q_SYNC:
21831da177e4SLinus Torvalds 	case Q_QUOTAON:
21841da177e4SLinus Torvalds 	case Q_QUOTAOFF:
21851da177e4SLinus Torvalds 	case Q_SETINFO:
21861da177e4SLinus Torvalds 	case Q_SETQUOTA:
218788e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
21881da177e4SLinus Torvalds 		break;
21891da177e4SLinus Torvalds 	case Q_GETFMT:
21901da177e4SLinus Torvalds 	case Q_GETINFO:
21911da177e4SLinus Torvalds 	case Q_GETQUOTA:
219288e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
21931da177e4SLinus Torvalds 		break;
21941da177e4SLinus Torvalds 	default:
21951da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
21961da177e4SLinus Torvalds 		break;
21971da177e4SLinus Torvalds 	}
21981da177e4SLinus Torvalds 	return rc;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
22021da177e4SLinus Torvalds {
220388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
220488e67f3bSDavid Howells 
22052875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
220812b3052cSEric Paris static int selinux_syslog(int type)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	switch (type) {
2211d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2212d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
22136b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22146b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2215be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2216d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2217d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2218d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2219d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
22206b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
22216b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
2222be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2223be0554c9SStephen Smalley 				    NULL);
22241da177e4SLinus Torvalds 	}
2225be0554c9SStephen Smalley 	/* All other syslog types */
22266b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
22276b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL,
2228be0554c9SStephen Smalley 			    SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds /*
22321da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
22331da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
22341da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
22351da177e4SLinus Torvalds  *
22361da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
22371da177e4SLinus Torvalds  * processes that allocate mappings.
22381da177e4SLinus Torvalds  */
223934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
22421da177e4SLinus Torvalds 
2243b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
22448e4ff6f2SStephen Smalley 				 SECURITY_CAP_NOAUDIT, true);
22451da177e4SLinus Torvalds 	if (rc == 0)
22461da177e4SLinus Torvalds 		cap_sys_admin = 1;
22471da177e4SLinus Torvalds 
2248b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds /* binprm security operations */
22521da177e4SLinus Torvalds 
2253be0554c9SStephen Smalley static u32 ptrace_parent_sid(void)
22540c6181cbSPaul Moore {
22550c6181cbSPaul Moore 	u32 sid = 0;
22560c6181cbSPaul Moore 	struct task_struct *tracer;
22570c6181cbSPaul Moore 
22580c6181cbSPaul Moore 	rcu_read_lock();
2259be0554c9SStephen Smalley 	tracer = ptrace_parent(current);
22600c6181cbSPaul Moore 	if (tracer)
22610c6181cbSPaul Moore 		sid = task_sid(tracer);
22620c6181cbSPaul Moore 	rcu_read_unlock();
22630c6181cbSPaul Moore 
22640c6181cbSPaul Moore 	return sid;
22650c6181cbSPaul Moore }
22660c6181cbSPaul Moore 
22677b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
22687b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
22697b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
22707b0d0b40SStephen Smalley {
22717b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2272380cf5baSAndy Lutomirski 	int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
22737b0d0b40SStephen Smalley 	int rc;
2274af63f419SStephen Smalley 	u32 av;
22757b0d0b40SStephen Smalley 
22767b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
22777b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
22787b0d0b40SStephen Smalley 
22797b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
22807b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
22817b0d0b40SStephen Smalley 
22827b0d0b40SStephen Smalley 	/*
2283af63f419SStephen Smalley 	 * If the policy enables the nnp_nosuid_transition policy capability,
2284af63f419SStephen Smalley 	 * then we permit transitions under NNP or nosuid if the
2285af63f419SStephen Smalley 	 * policy allows the corresponding permission between
2286af63f419SStephen Smalley 	 * the old and new contexts.
2287af63f419SStephen Smalley 	 */
2288aa8e712cSStephen Smalley 	if (selinux_policycap_nnp_nosuid_transition()) {
2289af63f419SStephen Smalley 		av = 0;
2290af63f419SStephen Smalley 		if (nnp)
2291af63f419SStephen Smalley 			av |= PROCESS2__NNP_TRANSITION;
2292af63f419SStephen Smalley 		if (nosuid)
2293af63f419SStephen Smalley 			av |= PROCESS2__NOSUID_TRANSITION;
22946b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
22956b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
2296af63f419SStephen Smalley 				  SECCLASS_PROCESS2, av, NULL);
2297af63f419SStephen Smalley 		if (!rc)
2298af63f419SStephen Smalley 			return 0;
2299af63f419SStephen Smalley 	}
2300af63f419SStephen Smalley 
2301af63f419SStephen Smalley 	/*
2302af63f419SStephen Smalley 	 * We also permit NNP or nosuid transitions to bounded SIDs,
2303af63f419SStephen Smalley 	 * i.e. SIDs that are guaranteed to only be allowed a subset
2304af63f419SStephen Smalley 	 * of the permissions of the current SID.
23057b0d0b40SStephen Smalley 	 */
2306aa8e712cSStephen Smalley 	rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2307aa8e712cSStephen Smalley 					 new_tsec->sid);
2308af63f419SStephen Smalley 	if (!rc)
2309af63f419SStephen Smalley 		return 0;
2310af63f419SStephen Smalley 
23117b0d0b40SStephen Smalley 	/*
23127b0d0b40SStephen Smalley 	 * On failure, preserve the errno values for NNP vs nosuid.
23137b0d0b40SStephen Smalley 	 * NNP:  Operation not permitted for caller.
23147b0d0b40SStephen Smalley 	 * nosuid:  Permission denied to file.
23157b0d0b40SStephen Smalley 	 */
23167b0d0b40SStephen Smalley 	if (nnp)
23177b0d0b40SStephen Smalley 		return -EPERM;
23187b0d0b40SStephen Smalley 	return -EACCES;
23197b0d0b40SStephen Smalley }
23207b0d0b40SStephen Smalley 
2321a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
23221da177e4SLinus Torvalds {
2323a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2324a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23251da177e4SLinus Torvalds 	struct inode_security_struct *isec;
23262bf49690SThomas Liu 	struct common_audit_data ad;
2327496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
23281da177e4SLinus Torvalds 	int rc;
23291da177e4SLinus Torvalds 
2330a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2331a6f76f23SDavid Howells 	 * the script interpreter */
2332ddb4a144SKees Cook 	if (bprm->called_set_creds)
23331da177e4SLinus Torvalds 		return 0;
23341da177e4SLinus Torvalds 
2335a6f76f23SDavid Howells 	old_tsec = current_security();
2336a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
233783da53c5SAndreas Gruenbacher 	isec = inode_security(inode);
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	/* Default to the current task SID. */
2340a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2341a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
23421da177e4SLinus Torvalds 
234328eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2344a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2345a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2346a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
23471da177e4SLinus Torvalds 
2348a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2349a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
23501da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2351a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2352259e5e6cSAndy Lutomirski 
23537b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
23547b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23557b0d0b40SStephen Smalley 		if (rc)
23567b0d0b40SStephen Smalley 			return rc;
23571da177e4SLinus Torvalds 	} else {
23581da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2359aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, old_tsec->sid,
2360aa8e712cSStephen Smalley 					     isec->sid, SECCLASS_PROCESS, NULL,
2361652bb9b0SEric Paris 					     &new_tsec->sid);
23621da177e4SLinus Torvalds 		if (rc)
23631da177e4SLinus Torvalds 			return rc;
23647b0d0b40SStephen Smalley 
23657b0d0b40SStephen Smalley 		/*
23667b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
23677b0d0b40SStephen Smalley 		 * transition.
23687b0d0b40SStephen Smalley 		 */
23697b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
23707b0d0b40SStephen Smalley 		if (rc)
23717b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
23721da177e4SLinus Torvalds 	}
23731da177e4SLinus Torvalds 
237443af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
237543af5de7SVivek Goyal 	ad.u.file = bprm->file;
23761da177e4SLinus Torvalds 
2377a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
23786b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23796b6bc620SStephen Smalley 				  old_tsec->sid, isec->sid,
23801da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
23811da177e4SLinus Torvalds 		if (rc)
23821da177e4SLinus Torvalds 			return rc;
23831da177e4SLinus Torvalds 	} else {
23841da177e4SLinus Torvalds 		/* Check permissions for the transition. */
23856b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23866b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
23871da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
23881da177e4SLinus Torvalds 		if (rc)
23891da177e4SLinus Torvalds 			return rc;
23901da177e4SLinus Torvalds 
23916b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
23926b6bc620SStephen Smalley 				  new_tsec->sid, isec->sid,
23931da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
23941da177e4SLinus Torvalds 		if (rc)
23951da177e4SLinus Torvalds 			return rc;
23961da177e4SLinus Torvalds 
2397a6f76f23SDavid Howells 		/* Check for shared state */
2398a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
23996b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
24006b6bc620SStephen Smalley 					  old_tsec->sid, new_tsec->sid,
2401a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2402a6f76f23SDavid Howells 					  NULL);
2403a6f76f23SDavid Howells 			if (rc)
2404a6f76f23SDavid Howells 				return -EPERM;
24051da177e4SLinus Torvalds 		}
24061da177e4SLinus Torvalds 
2407a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2408a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
24099227dd2aSEric W. Biederman 		if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2410be0554c9SStephen Smalley 			u32 ptsid = ptrace_parent_sid();
2411a6f76f23SDavid Howells 			if (ptsid != 0) {
24126b6bc620SStephen Smalley 				rc = avc_has_perm(&selinux_state,
24136b6bc620SStephen Smalley 						  ptsid, new_tsec->sid,
2414a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2415a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2416a6f76f23SDavid Howells 				if (rc)
2417a6f76f23SDavid Howells 					return -EPERM;
2418a6f76f23SDavid Howells 			}
2419a6f76f23SDavid Howells 		}
2420a6f76f23SDavid Howells 
2421a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2422a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2423a6f76f23SDavid Howells 
24241da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
24251da177e4SLinus Torvalds 		   the noatsecure permission is granted between
24261da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
24276b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
24286b6bc620SStephen Smalley 				  old_tsec->sid, new_tsec->sid,
242962874c3aSKees Cook 				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
243062874c3aSKees Cook 				  NULL);
243162874c3aSKees Cook 		bprm->secureexec |= !!rc;
24321da177e4SLinus Torvalds 	}
24331da177e4SLinus Torvalds 
243462874c3aSKees Cook 	return 0;
24351da177e4SLinus Torvalds }
24361da177e4SLinus Torvalds 
2437c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2438c3c073f8SAl Viro {
2439c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2440c3c073f8SAl Viro }
2441c3c073f8SAl Viro 
24421da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2443745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2444745ca247SDavid Howells 					    struct files_struct *files)
24451da177e4SLinus Torvalds {
24461da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2447b20c8122SStephen Smalley 	struct tty_struct *tty;
244824ec839cSPeter Zijlstra 	int drop_tty = 0;
2449c3c073f8SAl Viro 	unsigned n;
24501da177e4SLinus Torvalds 
245124ec839cSPeter Zijlstra 	tty = get_current_tty();
24521da177e4SLinus Torvalds 	if (tty) {
24534a510969SPeter Hurley 		spin_lock(&tty->files_lock);
245437dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2455d996b62aSNick Piggin 			struct tty_file_private *file_priv;
245637dd0bd0SEric Paris 
24571da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
245813f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
245913f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
246013f8e981SDavid Howells 			   open file may belong to another process and we are
246113f8e981SDavid Howells 			   only interested in the inode-based check here. */
2462d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2463d996b62aSNick Piggin 						struct tty_file_private, list);
2464d996b62aSNick Piggin 			file = file_priv->file;
246513f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
246624ec839cSPeter Zijlstra 				drop_tty = 1;
24671da177e4SLinus Torvalds 		}
24684a510969SPeter Hurley 		spin_unlock(&tty->files_lock);
2469452a00d2SAlan Cox 		tty_kref_put(tty);
24701da177e4SLinus Torvalds 	}
247198a27ba4SEric W. Biederman 	/* Reset controlling tty. */
247298a27ba4SEric W. Biederman 	if (drop_tty)
247398a27ba4SEric W. Biederman 		no_tty();
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2476c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2477c3c073f8SAl Viro 	if (!n) /* none found? */
2478c3c073f8SAl Viro 		return;
24791da177e4SLinus Torvalds 
2480c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
248145525b26SAl Viro 	if (IS_ERR(devnull))
248245525b26SAl Viro 		devnull = NULL;
2483c3c073f8SAl Viro 	/* replace all the matching ones with this */
2484c3c073f8SAl Viro 	do {
248545525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2486c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
248745525b26SAl Viro 	if (devnull)
2488c3c073f8SAl Viro 		fput(devnull);
24891da177e4SLinus Torvalds }
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds /*
2492a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
24931da177e4SLinus Torvalds  */
2494a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
24951da177e4SLinus Torvalds {
2496a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
24971da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
24981da177e4SLinus Torvalds 	int rc, i;
24991da177e4SLinus Torvalds 
2500a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2501a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
25021da177e4SLinus Torvalds 		return;
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2505a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
25061da177e4SLinus Torvalds 
2507a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2508a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2509a6f76f23SDavid Howells 
2510a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2511a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2512a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2513a6f76f23SDavid Howells 	 *
2514a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2515a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2516a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2517a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2518a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2519a6f76f23SDavid Howells 	 */
25206b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25216b6bc620SStephen Smalley 			  new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2522a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2523a6f76f23SDavid Howells 	if (rc) {
2524eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2525eb2d55a3SOleg Nesterov 		task_lock(current);
2526a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2527a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2528a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2529a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2530a6f76f23SDavid Howells 		}
2531eb2d55a3SOleg Nesterov 		task_unlock(current);
2532baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2533eb2d55a3SOleg Nesterov 			update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2534a6f76f23SDavid Howells 	}
2535a6f76f23SDavid Howells }
2536a6f76f23SDavid Howells 
2537a6f76f23SDavid Howells /*
2538a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2539a6f76f23SDavid Howells  * due to exec
2540a6f76f23SDavid Howells  */
2541a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2542a6f76f23SDavid Howells {
2543a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2544a6f76f23SDavid Howells 	struct itimerval itimer;
2545a6f76f23SDavid Howells 	u32 osid, sid;
2546a6f76f23SDavid Howells 	int rc, i;
2547a6f76f23SDavid Howells 
2548a6f76f23SDavid Howells 	osid = tsec->osid;
2549a6f76f23SDavid Howells 	sid = tsec->sid;
2550a6f76f23SDavid Howells 
2551a6f76f23SDavid Howells 	if (sid == osid)
2552a6f76f23SDavid Howells 		return;
2553a6f76f23SDavid Howells 
2554a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2555a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2556a6f76f23SDavid Howells 	 * flush and unblock signals.
2557a6f76f23SDavid Howells 	 *
2558a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2559a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2560a6f76f23SDavid Howells 	 */
25616b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
25626b6bc620SStephen Smalley 			  osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
25631da177e4SLinus Torvalds 	if (rc) {
2564baa73d9eSNicolas Pitre 		if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
25651da177e4SLinus Torvalds 			memset(&itimer, 0, sizeof itimer);
25661da177e4SLinus Torvalds 			for (i = 0; i < 3; i++)
25671da177e4SLinus Torvalds 				do_setitimer(i, &itimer, NULL);
2568baa73d9eSNicolas Pitre 		}
25691da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
25709e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
25719e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
25729e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
25731da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
25741da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
25759e7c8f8cSOleg Nesterov 			recalc_sigpending();
25763bcac026SDavid Howells 		}
25771da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
25781da177e4SLinus Torvalds 	}
25791da177e4SLinus Torvalds 
2580a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2581a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2582ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
25830b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2584ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds /* superblock security operations */
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
25901da177e4SLinus Torvalds {
25911da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
25921da177e4SLinus Torvalds }
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
25951da177e4SLinus Torvalds {
25961da177e4SLinus Torvalds 	superblock_free_security(sb);
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
259999dbbb59SAl Viro static inline int opt_len(const char *s)
26001da177e4SLinus Torvalds {
260199dbbb59SAl Viro 	bool open_quote = false;
260299dbbb59SAl Viro 	int len;
260399dbbb59SAl Viro 	char c;
26041da177e4SLinus Torvalds 
260599dbbb59SAl Viro 	for (len = 0; (c = s[len]) != '\0'; len++) {
260699dbbb59SAl Viro 		if (c == '"')
26073528a953SCory Olmo 			open_quote = !open_quote;
260899dbbb59SAl Viro 		if (c == ',' && !open_quote)
260999dbbb59SAl Viro 			break;
26101da177e4SLinus Torvalds 	}
261199dbbb59SAl Viro 	return len;
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
2614204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26155b400239SAl Viro {
261699dbbb59SAl Viro 	char *from = options;
261799dbbb59SAl Viro 	char *to = options;
261899dbbb59SAl Viro 	bool first = true;
26195b400239SAl Viro 
262099dbbb59SAl Viro 	while (1) {
262199dbbb59SAl Viro 		int len = opt_len(from);
262299dbbb59SAl Viro 		int token, rc;
262399dbbb59SAl Viro 		char *arg = NULL;
262499dbbb59SAl Viro 
262599dbbb59SAl Viro 		token = match_opt_prefix(from, len, &arg);
262699dbbb59SAl Viro 
262799dbbb59SAl Viro 		if (token != Opt_error) {
262899dbbb59SAl Viro 			char *p, *q;
262999dbbb59SAl Viro 
263099dbbb59SAl Viro 			/* strip quotes */
263199dbbb59SAl Viro 			if (arg) {
263299dbbb59SAl Viro 				for (p = q = arg; p < from + len; p++) {
263399dbbb59SAl Viro 					char c = *p;
263499dbbb59SAl Viro 					if (c != '"')
263599dbbb59SAl Viro 						*q++ = c;
263699dbbb59SAl Viro 				}
263799dbbb59SAl Viro 				arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
263899dbbb59SAl Viro 			}
263999dbbb59SAl Viro 			rc = selinux_add_opt(token, arg, mnt_opts);
264099dbbb59SAl Viro 			if (unlikely(rc)) {
264199dbbb59SAl Viro 				kfree(arg);
264299dbbb59SAl Viro 				if (*mnt_opts) {
264399dbbb59SAl Viro 					selinux_free_mnt_opts(*mnt_opts);
264499dbbb59SAl Viro 					*mnt_opts = NULL;
264599dbbb59SAl Viro 				}
26461da177e4SLinus Torvalds 				return rc;
26471da177e4SLinus Torvalds 			}
264899dbbb59SAl Viro 		} else {
264999dbbb59SAl Viro 			if (!first) {	// copy with preceding comma
265099dbbb59SAl Viro 				from--;
265199dbbb59SAl Viro 				len++;
265299dbbb59SAl Viro 			}
265399dbbb59SAl Viro 			if (to != from)
265499dbbb59SAl Viro 				memmove(to, from, len);
265599dbbb59SAl Viro 			to += len;
265699dbbb59SAl Viro 			first = false;
265799dbbb59SAl Viro 		}
265899dbbb59SAl Viro 		if (!from[len])
265999dbbb59SAl Viro 			break;
266099dbbb59SAl Viro 		from += len + 1;
266199dbbb59SAl Viro 	}
266299dbbb59SAl Viro 	*to = '\0';
266399dbbb59SAl Viro 	return 0;
26645b400239SAl Viro }
26651da177e4SLinus Torvalds 
2666204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2667026eb167SEric Paris {
2668bd323655SAl Viro 	struct selinux_mnt_opts *opts = mnt_opts;
2669026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2670bd323655SAl Viro 	u32 sid;
2671bd323655SAl Viro 	int rc;
2672026eb167SEric Paris 
2673026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2674026eb167SEric Paris 		return 0;
2675026eb167SEric Paris 
2676204cc0ccSAl Viro 	if (!opts)
2677026eb167SEric Paris 		return 0;
2678026eb167SEric Paris 
2679bd323655SAl Viro 	if (opts->fscontext) {
2680bd323655SAl Viro 		rc = parse_sid(sb, opts->fscontext, &sid);
2681026eb167SEric Paris 		if (rc)
2682c039bc3cSAl Viro 			return rc;
2683026eb167SEric Paris 		if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2684026eb167SEric Paris 			goto out_bad_option;
2685bd323655SAl Viro 	}
2686bd323655SAl Viro 	if (opts->context) {
2687bd323655SAl Viro 		rc = parse_sid(sb, opts->context, &sid);
2688bd323655SAl Viro 		if (rc)
2689bd323655SAl Viro 			return rc;
2690026eb167SEric Paris 		if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2691026eb167SEric Paris 			goto out_bad_option;
2692bd323655SAl Viro 	}
2693bd323655SAl Viro 	if (opts->rootcontext) {
2694026eb167SEric Paris 		struct inode_security_struct *root_isec;
269583da53c5SAndreas Gruenbacher 		root_isec = backing_inode_security(sb->s_root);
2696bd323655SAl Viro 		rc = parse_sid(sb, opts->rootcontext, &sid);
2697bd323655SAl Viro 		if (rc)
2698bd323655SAl Viro 			return rc;
2699026eb167SEric Paris 		if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2700026eb167SEric Paris 			goto out_bad_option;
2701026eb167SEric Paris 	}
2702bd323655SAl Viro 	if (opts->defcontext) {
2703bd323655SAl Viro 		rc = parse_sid(sb, opts->defcontext, &sid);
2704bd323655SAl Viro 		if (rc)
2705bd323655SAl Viro 			return rc;
2706026eb167SEric Paris 		if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2707026eb167SEric Paris 			goto out_bad_option;
2708026eb167SEric Paris 	}
2709c039bc3cSAl Viro 	return 0;
2710026eb167SEric Paris 
2711026eb167SEric Paris out_bad_option:
2712c103a91eSpeter enderborg 	pr_warn("SELinux: unable to change security options "
271329b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
271429b1deb2SLinus Torvalds 	       sb->s_type->name);
2715c039bc3cSAl Viro 	return -EINVAL;
2716026eb167SEric Paris }
2717026eb167SEric Paris 
2718a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb)
27191da177e4SLinus Torvalds {
272088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27212bf49690SThomas Liu 	struct common_audit_data ad;
272274192246SJames Morris 
272350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2724a269434dSEric Paris 	ad.u.dentry = sb->s_root;
272588e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
2728726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
27291da177e4SLinus Torvalds {
273088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27312bf49690SThomas Liu 	struct common_audit_data ad;
27321da177e4SLinus Torvalds 
273350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2734a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
273588e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
27361da177e4SLinus Torvalds }
27371da177e4SLinus Torvalds 
2738808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
27398a04c43bSAl Viro 			 const struct path *path,
2740808d4e3cSAl Viro 			 const char *type,
27411da177e4SLinus Torvalds 			 unsigned long flags,
27421da177e4SLinus Torvalds 			 void *data)
27431da177e4SLinus Torvalds {
274488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2747d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
27481da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
27491da177e4SLinus Torvalds 	else
27502875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27511da177e4SLinus Torvalds }
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27541da177e4SLinus Torvalds {
275588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27561da177e4SLinus Torvalds 
275788e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27581da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds /* inode security operations */
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	return inode_alloc_security(inode);
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
27691da177e4SLinus Torvalds {
27701da177e4SLinus Torvalds 	inode_free_security(inode);
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds 
2773d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
27744f3ccd76SAl Viro 					const struct qstr *name, void **ctx,
2775d47be3dfSDavid Quigley 					u32 *ctxlen)
2776d47be3dfSDavid Quigley {
2777d47be3dfSDavid Quigley 	u32 newsid;
2778d47be3dfSDavid Quigley 	int rc;
2779d47be3dfSDavid Quigley 
2780c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2781c957f6dfSVivek Goyal 					   d_inode(dentry->d_parent), name,
2782d47be3dfSDavid Quigley 					   inode_mode_to_security_class(mode),
2783d47be3dfSDavid Quigley 					   &newsid);
2784c3c188b2SDavid Howells 	if (rc)
2785d47be3dfSDavid Quigley 		return rc;
2786d47be3dfSDavid Quigley 
2787aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2788aa8e712cSStephen Smalley 				       ctxlen);
2789d47be3dfSDavid Quigley }
2790d47be3dfSDavid Quigley 
2791a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2792a518b0a5SVivek Goyal 					  struct qstr *name,
2793a518b0a5SVivek Goyal 					  const struct cred *old,
2794a518b0a5SVivek Goyal 					  struct cred *new)
2795a518b0a5SVivek Goyal {
2796a518b0a5SVivek Goyal 	u32 newsid;
2797a518b0a5SVivek Goyal 	int rc;
2798a518b0a5SVivek Goyal 	struct task_security_struct *tsec;
2799a518b0a5SVivek Goyal 
2800a518b0a5SVivek Goyal 	rc = selinux_determine_inode_label(old->security,
2801a518b0a5SVivek Goyal 					   d_inode(dentry->d_parent), name,
2802a518b0a5SVivek Goyal 					   inode_mode_to_security_class(mode),
2803a518b0a5SVivek Goyal 					   &newsid);
2804a518b0a5SVivek Goyal 	if (rc)
2805a518b0a5SVivek Goyal 		return rc;
2806a518b0a5SVivek Goyal 
2807a518b0a5SVivek Goyal 	tsec = new->security;
2808a518b0a5SVivek Goyal 	tsec->create_sid = newsid;
2809a518b0a5SVivek Goyal 	return 0;
2810a518b0a5SVivek Goyal }
2811a518b0a5SVivek Goyal 
28125e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
28139548906bSTetsuo Handa 				       const struct qstr *qstr,
28149548906bSTetsuo Handa 				       const char **name,
28152a7dba39SEric Paris 				       void **value, size_t *len)
28165e41ff9eSStephen Smalley {
28175fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
28185e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2819c0d4f464SCorentin LABBE 	u32 newsid, clen;
28205e41ff9eSStephen Smalley 	int rc;
28219548906bSTetsuo Handa 	char *context;
28225e41ff9eSStephen Smalley 
28235e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
28245e41ff9eSStephen Smalley 
28255e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2826275bb41eSDavid Howells 
2827c957f6dfSVivek Goyal 	rc = selinux_determine_inode_label(current_security(),
2828c3c188b2SDavid Howells 		dir, qstr,
28295e41ff9eSStephen Smalley 		inode_mode_to_security_class(inode->i_mode),
2830c3c188b2SDavid Howells 		&newsid);
2831c3c188b2SDavid Howells 	if (rc)
28325e41ff9eSStephen Smalley 		return rc;
28335e41ff9eSStephen Smalley 
2834296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
28350d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2836296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2837296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2838296fddf7SEric Paris 		isec->sid = newsid;
28396f3be9f5SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
2840296fddf7SEric Paris 	}
28415e41ff9eSStephen Smalley 
2842aa8e712cSStephen Smalley 	if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
284325a74f3bSStephen Smalley 		return -EOPNOTSUPP;
284425a74f3bSStephen Smalley 
28459548906bSTetsuo Handa 	if (name)
28469548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
28475e41ff9eSStephen Smalley 
2848570bc1c2SStephen Smalley 	if (value && len) {
2849aa8e712cSStephen Smalley 		rc = security_sid_to_context_force(&selinux_state, newsid,
2850aa8e712cSStephen Smalley 						   &context, &clen);
28519548906bSTetsuo Handa 		if (rc)
28525e41ff9eSStephen Smalley 			return rc;
28535e41ff9eSStephen Smalley 		*value = context;
2854570bc1c2SStephen Smalley 		*len = clen;
2855570bc1c2SStephen Smalley 	}
28565e41ff9eSStephen Smalley 
28575e41ff9eSStephen Smalley 	return 0;
28585e41ff9eSStephen Smalley }
28595e41ff9eSStephen Smalley 
28604acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
28611da177e4SLinus Torvalds {
28621da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
28631da177e4SLinus Torvalds }
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28661da177e4SLinus Torvalds {
28671da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
28711da177e4SLinus Torvalds {
28721da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
28731da177e4SLinus Torvalds }
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
28761da177e4SLinus Torvalds {
28771da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
28781da177e4SLinus Torvalds }
28791da177e4SLinus Torvalds 
288018bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
28811da177e4SLinus Torvalds {
28821da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
28861da177e4SLinus Torvalds {
28871da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
28881da177e4SLinus Torvalds }
28891da177e4SLinus Torvalds 
28901a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
28911da177e4SLinus Torvalds {
28921da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
28931da177e4SLinus Torvalds }
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
28961da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
28971da177e4SLinus Torvalds {
28981da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
29021da177e4SLinus Torvalds {
290388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
290488e67f3bSDavid Howells 
29052875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
2908bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2909bda0be7aSNeilBrown 				     bool rcu)
29101da177e4SLinus Torvalds {
291188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2912bda0be7aSNeilBrown 	struct common_audit_data ad;
2913bda0be7aSNeilBrown 	struct inode_security_struct *isec;
2914bda0be7aSNeilBrown 	u32 sid;
29151da177e4SLinus Torvalds 
2916bda0be7aSNeilBrown 	validate_creds(cred);
2917bda0be7aSNeilBrown 
2918bda0be7aSNeilBrown 	ad.type = LSM_AUDIT_DATA_DENTRY;
2919bda0be7aSNeilBrown 	ad.u.dentry = dentry;
2920bda0be7aSNeilBrown 	sid = cred_sid(cred);
29215d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, rcu);
29225d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
29235d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
2924bda0be7aSNeilBrown 
29256b6bc620SStephen Smalley 	return avc_has_perm_flags(&selinux_state,
29266b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, FILE__READ, &ad,
2927bda0be7aSNeilBrown 				  rcu ? MAY_NOT_BLOCK : 0);
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
2930d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2931d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2932626b9740SStephen Smalley 					   int result,
2933d4cf970dSEric Paris 					   unsigned flags)
2934d4cf970dSEric Paris {
2935d4cf970dSEric Paris 	struct common_audit_data ad;
2936d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2937d4cf970dSEric Paris 	int rc;
2938d4cf970dSEric Paris 
293950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2940d4cf970dSEric Paris 	ad.u.inode = inode;
2941d4cf970dSEric Paris 
29426b6bc620SStephen Smalley 	rc = slow_avc_audit(&selinux_state,
29436b6bc620SStephen Smalley 			    current_sid(), isec->sid, isec->sclass, perms,
2944626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
2945d4cf970dSEric Paris 	if (rc)
2946d4cf970dSEric Paris 		return rc;
2947d4cf970dSEric Paris 	return 0;
2948d4cf970dSEric Paris }
2949d4cf970dSEric Paris 
2950e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
29511da177e4SLinus Torvalds {
295288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2953b782e0a6SEric Paris 	u32 perms;
2954b782e0a6SEric Paris 	bool from_access;
2955cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
29562e334057SEric Paris 	struct inode_security_struct *isec;
29572e334057SEric Paris 	u32 sid;
29582e334057SEric Paris 	struct av_decision avd;
29592e334057SEric Paris 	int rc, rc2;
29602e334057SEric Paris 	u32 audited, denied;
29611da177e4SLinus Torvalds 
2962b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2963d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2964d09ca739SEric Paris 
29651da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2966b782e0a6SEric Paris 	if (!mask)
29671da177e4SLinus Torvalds 		return 0;
29681da177e4SLinus Torvalds 
29692e334057SEric Paris 	validate_creds(cred);
2970b782e0a6SEric Paris 
29712e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
29722e334057SEric Paris 		return 0;
2973b782e0a6SEric Paris 
2974b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2975b782e0a6SEric Paris 
29762e334057SEric Paris 	sid = cred_sid(cred);
29775d226df4SAndreas Gruenbacher 	isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
29785d226df4SAndreas Gruenbacher 	if (IS_ERR(isec))
29795d226df4SAndreas Gruenbacher 		return PTR_ERR(isec);
29802e334057SEric Paris 
29816b6bc620SStephen Smalley 	rc = avc_has_perm_noaudit(&selinux_state,
29826b6bc620SStephen Smalley 				  sid, isec->sid, isec->sclass, perms, 0, &avd);
29832e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
29842e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
29852e334057SEric Paris 				     &denied);
29862e334057SEric Paris 	if (likely(!audited))
29872e334057SEric Paris 		return rc;
29882e334057SEric Paris 
2989626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
29902e334057SEric Paris 	if (rc2)
29912e334057SEric Paris 		return rc2;
29922e334057SEric Paris 	return rc;
29931da177e4SLinus Torvalds }
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
29961da177e4SLinus Torvalds {
299788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2998ccb54478SStephen Smalley 	struct inode *inode = d_backing_inode(dentry);
2999bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
300095dbf739SEric Paris 	__u32 av = FILE__WRITE;
30011da177e4SLinus Torvalds 
3002bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3003bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
3004bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3005bc6a6008SAmerigo Wang 			      ATTR_FORCE);
3006bc6a6008SAmerigo Wang 		if (!ia_valid)
30071da177e4SLinus Torvalds 			return 0;
3008bc6a6008SAmerigo Wang 	}
30091da177e4SLinus Torvalds 
3010bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3011bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
30122875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
30131da177e4SLinus Torvalds 
3014aa8e712cSStephen Smalley 	if (selinux_policycap_openperm() &&
3015ccb54478SStephen Smalley 	    inode->i_sb->s_magic != SOCKFS_MAGIC &&
3016ccb54478SStephen Smalley 	    (ia_valid & ATTR_SIZE) &&
3017ccb54478SStephen Smalley 	    !(ia_valid & ATTR_FILE))
301895dbf739SEric Paris 		av |= FILE__OPEN;
301995dbf739SEric Paris 
302095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
30211da177e4SLinus Torvalds }
30221da177e4SLinus Torvalds 
30233f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
30241da177e4SLinus Torvalds {
30253f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
30261da177e4SLinus Torvalds }
30271da177e4SLinus Torvalds 
3028db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit)
3029db59000aSStephen Smalley {
3030db59000aSStephen Smalley 	const struct cred *cred = current_cred();
3031db59000aSStephen Smalley 	int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3032db59000aSStephen Smalley 
3033db59000aSStephen Smalley 	if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3034db59000aSStephen Smalley 		return false;
3035db59000aSStephen Smalley 	if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3036db59000aSStephen Smalley 		return false;
3037db59000aSStephen Smalley 	return true;
3038db59000aSStephen Smalley }
3039db59000aSStephen Smalley 
30408f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
30418f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
30421da177e4SLinus Torvalds {
3043c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
304420cdef8dSPaul Moore 	struct inode_security_struct *isec;
30451da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
30462bf49690SThomas Liu 	struct common_audit_data ad;
3047275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
30481da177e4SLinus Torvalds 	int rc = 0;
30491da177e4SLinus Torvalds 
30506b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
30516b240306SEric W. Biederman 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
30526b240306SEric W. Biederman 		if (rc)
30536b240306SEric W. Biederman 			return rc;
30546b240306SEric W. Biederman 
30556b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
30566b240306SEric W. Biederman 		   ordinary setattr permission. */
30576b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
30586b240306SEric W. Biederman 	}
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
306112f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
30621da177e4SLinus Torvalds 		return -EOPNOTSUPP;
30631da177e4SLinus Torvalds 
30642e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
30651da177e4SLinus Torvalds 		return -EPERM;
30661da177e4SLinus Torvalds 
306750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3068a269434dSEric Paris 	ad.u.dentry = dentry;
30691da177e4SLinus Torvalds 
307020cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
30716b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
30726b6bc620SStephen Smalley 			  sid, isec->sid, isec->sclass,
30731da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
30741da177e4SLinus Torvalds 	if (rc)
30751da177e4SLinus Torvalds 		return rc;
30761da177e4SLinus Torvalds 
3077aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3078aa8e712cSStephen Smalley 				     GFP_KERNEL);
307912b29f34SStephen Smalley 	if (rc == -EINVAL) {
3080db59000aSStephen Smalley 		if (!has_cap_mac_admin(true)) {
3081d6ea83ecSEric Paris 			struct audit_buffer *ab;
3082d6ea83ecSEric Paris 			size_t audit_size;
3083d6ea83ecSEric Paris 
3084d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3085d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3086e3fea3f7SAl Viro 			if (value) {
3087add24372SColin Ian King 				const char *str = value;
3088add24372SColin Ian King 
3089d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3090d6ea83ecSEric Paris 					audit_size = size - 1;
3091d6ea83ecSEric Paris 				else
3092d6ea83ecSEric Paris 					audit_size = size;
3093e3fea3f7SAl Viro 			} else {
3094e3fea3f7SAl Viro 				audit_size = 0;
3095e3fea3f7SAl Viro 			}
3096cdfb6b34SRichard Guy Briggs 			ab = audit_log_start(audit_context(),
3097cdfb6b34SRichard Guy Briggs 					     GFP_ATOMIC, AUDIT_SELINUX_ERR);
3098d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3099d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3100d6ea83ecSEric Paris 			audit_log_end(ab);
3101d6ea83ecSEric Paris 
310212b29f34SStephen Smalley 			return rc;
3103d6ea83ecSEric Paris 		}
3104aa8e712cSStephen Smalley 		rc = security_context_to_sid_force(&selinux_state, value,
3105aa8e712cSStephen Smalley 						   size, &newsid);
310612b29f34SStephen Smalley 	}
31071da177e4SLinus Torvalds 	if (rc)
31081da177e4SLinus Torvalds 		return rc;
31091da177e4SLinus Torvalds 
31106b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
31116b6bc620SStephen Smalley 			  sid, newsid, isec->sclass,
31121da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
31131da177e4SLinus Torvalds 	if (rc)
31141da177e4SLinus Torvalds 		return rc;
31151da177e4SLinus Torvalds 
3116aa8e712cSStephen Smalley 	rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3117aa8e712cSStephen Smalley 					  sid, isec->sclass);
31181da177e4SLinus Torvalds 	if (rc)
31191da177e4SLinus Torvalds 		return rc;
31201da177e4SLinus Torvalds 
31216b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
31226b6bc620SStephen Smalley 			    newsid,
31231da177e4SLinus Torvalds 			    sbsec->sid,
31241da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
31251da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
31261da177e4SLinus Torvalds 			    &ad);
31271da177e4SLinus Torvalds }
31281da177e4SLinus Torvalds 
31298f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
31308f0cfa52SDavid Howells 					const void *value, size_t size,
31318f0cfa52SDavid Howells 					int flags)
31321da177e4SLinus Torvalds {
3133c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
313420cdef8dSPaul Moore 	struct inode_security_struct *isec;
31351da177e4SLinus Torvalds 	u32 newsid;
31361da177e4SLinus Torvalds 	int rc;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
31391da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
31401da177e4SLinus Torvalds 		return;
31411da177e4SLinus Torvalds 	}
31421da177e4SLinus Torvalds 
3143aa8e712cSStephen Smalley 	rc = security_context_to_sid_force(&selinux_state, value, size,
3144aa8e712cSStephen Smalley 					   &newsid);
31451da177e4SLinus Torvalds 	if (rc) {
3146c103a91eSpeter enderborg 		pr_err("SELinux:  unable to map context to SID"
314712b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
314812b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
31491da177e4SLinus Torvalds 		return;
31501da177e4SLinus Torvalds 	}
31511da177e4SLinus Torvalds 
315220cdef8dSPaul Moore 	isec = backing_inode_security(dentry);
31539287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3154aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
31551da177e4SLinus Torvalds 	isec->sid = newsid;
31566f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
31579287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
3158aa9c2669SDavid Quigley 
31591da177e4SLinus Torvalds 	return;
31601da177e4SLinus Torvalds }
31611da177e4SLinus Torvalds 
31628f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
31631da177e4SLinus Torvalds {
316488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
316588e67f3bSDavid Howells 
31662875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
31671da177e4SLinus Torvalds }
31681da177e4SLinus Torvalds 
31691da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
31701da177e4SLinus Torvalds {
317188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
317288e67f3bSDavid Howells 
31732875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
31741da177e4SLinus Torvalds }
31751da177e4SLinus Torvalds 
31768f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
31771da177e4SLinus Torvalds {
31786b240306SEric W. Biederman 	if (strcmp(name, XATTR_NAME_SELINUX)) {
31796b240306SEric W. Biederman 		int rc = cap_inode_removexattr(dentry, name);
31806b240306SEric W. Biederman 		if (rc)
31816b240306SEric W. Biederman 			return rc;
31826b240306SEric W. Biederman 
31836b240306SEric W. Biederman 		/* Not an attribute we recognize, so just check the
31846b240306SEric W. Biederman 		   ordinary setattr permission. */
31856b240306SEric W. Biederman 		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
31866b240306SEric W. Biederman 	}
31871da177e4SLinus Torvalds 
31881da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
31891da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
31901da177e4SLinus Torvalds 	return -EACCES;
31911da177e4SLinus Torvalds }
31921da177e4SLinus Torvalds 
3193d381d8a9SJames Morris /*
3194abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3195d381d8a9SJames Morris  *
3196d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3197d381d8a9SJames Morris  */
3198ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
31991da177e4SLinus Torvalds {
320042492594SDavid P. Quigley 	u32 size;
320142492594SDavid P. Quigley 	int error;
320242492594SDavid P. Quigley 	char *context = NULL;
320320cdef8dSPaul Moore 	struct inode_security_struct *isec;
32041da177e4SLinus Torvalds 
32058c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
32068c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
32071da177e4SLinus Torvalds 
3208abc69bb6SStephen Smalley 	/*
3209abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3210abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3211abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3212abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3213abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3214abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3215abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3216abc69bb6SStephen Smalley 	 */
321720cdef8dSPaul Moore 	isec = inode_security(inode);
3218db59000aSStephen Smalley 	if (has_cap_mac_admin(false))
3219aa8e712cSStephen Smalley 		error = security_sid_to_context_force(&selinux_state,
3220aa8e712cSStephen Smalley 						      isec->sid, &context,
3221abc69bb6SStephen Smalley 						      &size);
3222abc69bb6SStephen Smalley 	else
3223aa8e712cSStephen Smalley 		error = security_sid_to_context(&selinux_state, isec->sid,
3224aa8e712cSStephen Smalley 						&context, &size);
322542492594SDavid P. Quigley 	if (error)
322642492594SDavid P. Quigley 		return error;
322742492594SDavid P. Quigley 	error = size;
322842492594SDavid P. Quigley 	if (alloc) {
322942492594SDavid P. Quigley 		*buffer = context;
323042492594SDavid P. Quigley 		goto out_nofree;
323142492594SDavid P. Quigley 	}
323242492594SDavid P. Quigley 	kfree(context);
323342492594SDavid P. Quigley out_nofree:
323442492594SDavid P. Quigley 	return error;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
32381da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
32391da177e4SLinus Torvalds {
32402c97165bSPaul Moore 	struct inode_security_struct *isec = inode_security_novalidate(inode);
32411da177e4SLinus Torvalds 	u32 newsid;
32421da177e4SLinus Torvalds 	int rc;
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
32451da177e4SLinus Torvalds 		return -EOPNOTSUPP;
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 	if (!value || !size)
32481da177e4SLinus Torvalds 		return -EACCES;
32491da177e4SLinus Torvalds 
3250aa8e712cSStephen Smalley 	rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3251aa8e712cSStephen Smalley 				     GFP_KERNEL);
32521da177e4SLinus Torvalds 	if (rc)
32531da177e4SLinus Torvalds 		return rc;
32541da177e4SLinus Torvalds 
32559287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
3256aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
32571da177e4SLinus Torvalds 	isec->sid = newsid;
32586f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
32599287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
32601da177e4SLinus Torvalds 	return 0;
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
32641da177e4SLinus Torvalds {
32651da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
32661da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
32671da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
32681da177e4SLinus Torvalds 	return len;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
3271d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3272713a04aeSAhmed S. Darwish {
3273e817c2f3SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(inode);
3274713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3275713a04aeSAhmed S. Darwish }
3276713a04aeSAhmed S. Darwish 
327756909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
327856909eb3SVivek Goyal {
327956909eb3SVivek Goyal 	u32 sid;
328056909eb3SVivek Goyal 	struct task_security_struct *tsec;
328156909eb3SVivek Goyal 	struct cred *new_creds = *new;
328256909eb3SVivek Goyal 
328356909eb3SVivek Goyal 	if (new_creds == NULL) {
328456909eb3SVivek Goyal 		new_creds = prepare_creds();
328556909eb3SVivek Goyal 		if (!new_creds)
328656909eb3SVivek Goyal 			return -ENOMEM;
328756909eb3SVivek Goyal 	}
328856909eb3SVivek Goyal 
328956909eb3SVivek Goyal 	tsec = new_creds->security;
329056909eb3SVivek Goyal 	/* Get label from overlay inode and set it in create_sid */
329156909eb3SVivek Goyal 	selinux_inode_getsecid(d_inode(src), &sid);
329256909eb3SVivek Goyal 	tsec->create_sid = sid;
329356909eb3SVivek Goyal 	*new = new_creds;
329456909eb3SVivek Goyal 	return 0;
329556909eb3SVivek Goyal }
329656909eb3SVivek Goyal 
329719472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name)
329819472b69SVivek Goyal {
329919472b69SVivek Goyal 	/* The copy_up hook above sets the initial context on an inode, but we
330019472b69SVivek Goyal 	 * don't then want to overwrite it by blindly copying all the lower
330119472b69SVivek Goyal 	 * xattrs up.  Instead, we have to filter out SELinux-related xattrs.
330219472b69SVivek Goyal 	 */
330319472b69SVivek Goyal 	if (strcmp(name, XATTR_NAME_SELINUX) == 0)
330419472b69SVivek Goyal 		return 1; /* Discard */
330519472b69SVivek Goyal 	/*
330619472b69SVivek Goyal 	 * Any other attribute apart from SELINUX is not claimed, supported
330719472b69SVivek Goyal 	 * by selinux.
330819472b69SVivek Goyal 	 */
330919472b69SVivek Goyal 	return -EOPNOTSUPP;
331019472b69SVivek Goyal }
331119472b69SVivek Goyal 
33121da177e4SLinus Torvalds /* file security operations */
33131da177e4SLinus Torvalds 
3314788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
33151da177e4SLinus Torvalds {
331688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3317496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
33181da177e4SLinus Torvalds 
33191da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
33201da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
33211da177e4SLinus Torvalds 		mask |= MAY_APPEND;
33221da177e4SLinus Torvalds 
3323389fb800SPaul Moore 	return file_has_perm(cred, file,
33241da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
33251da177e4SLinus Torvalds }
33261da177e4SLinus Torvalds 
3327788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3328788e7dd4SYuichi Nakamura {
3329496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
333020dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
3331b197367eSAndreas Gruenbacher 	struct inode_security_struct *isec;
333220dda18bSStephen Smalley 	u32 sid = current_sid();
333320dda18bSStephen Smalley 
3334389fb800SPaul Moore 	if (!mask)
3335788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3336788e7dd4SYuichi Nakamura 		return 0;
3337788e7dd4SYuichi Nakamura 
3338b197367eSAndreas Gruenbacher 	isec = inode_security(inode);
333920dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
33406b6bc620SStephen Smalley 	    fsec->pseqno == avc_policy_seqno(&selinux_state))
334183d49856SEric Paris 		/* No change since file_open check. */
334220dda18bSStephen Smalley 		return 0;
334320dda18bSStephen Smalley 
3344788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3345788e7dd4SYuichi Nakamura }
3346788e7dd4SYuichi Nakamura 
33471da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
33481da177e4SLinus Torvalds {
33491da177e4SLinus Torvalds 	return file_alloc_security(file);
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
33531da177e4SLinus Torvalds {
33541da177e4SLinus Torvalds 	file_free_security(file);
33551da177e4SLinus Torvalds }
33561da177e4SLinus Torvalds 
3357fa1aa143SJeff Vander Stoep /*
3358fa1aa143SJeff Vander Stoep  * Check whether a task has the ioctl permission and cmd
3359fa1aa143SJeff Vander Stoep  * operation to an inode.
3360fa1aa143SJeff Vander Stoep  */
33611d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file,
3362fa1aa143SJeff Vander Stoep 		u32 requested, u16 cmd)
3363fa1aa143SJeff Vander Stoep {
3364fa1aa143SJeff Vander Stoep 	struct common_audit_data ad;
3365fa1aa143SJeff Vander Stoep 	struct file_security_struct *fsec = file->f_security;
3366fa1aa143SJeff Vander Stoep 	struct inode *inode = file_inode(file);
336720cdef8dSPaul Moore 	struct inode_security_struct *isec;
3368fa1aa143SJeff Vander Stoep 	struct lsm_ioctlop_audit ioctl;
3369fa1aa143SJeff Vander Stoep 	u32 ssid = cred_sid(cred);
3370fa1aa143SJeff Vander Stoep 	int rc;
3371fa1aa143SJeff Vander Stoep 	u8 driver = cmd >> 8;
3372fa1aa143SJeff Vander Stoep 	u8 xperm = cmd & 0xff;
3373fa1aa143SJeff Vander Stoep 
3374fa1aa143SJeff Vander Stoep 	ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3375fa1aa143SJeff Vander Stoep 	ad.u.op = &ioctl;
3376fa1aa143SJeff Vander Stoep 	ad.u.op->cmd = cmd;
3377fa1aa143SJeff Vander Stoep 	ad.u.op->path = file->f_path;
3378fa1aa143SJeff Vander Stoep 
3379fa1aa143SJeff Vander Stoep 	if (ssid != fsec->sid) {
33806b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
33816b6bc620SStephen Smalley 				  ssid, fsec->sid,
3382fa1aa143SJeff Vander Stoep 				SECCLASS_FD,
3383fa1aa143SJeff Vander Stoep 				FD__USE,
3384fa1aa143SJeff Vander Stoep 				&ad);
3385fa1aa143SJeff Vander Stoep 		if (rc)
3386fa1aa143SJeff Vander Stoep 			goto out;
3387fa1aa143SJeff Vander Stoep 	}
3388fa1aa143SJeff Vander Stoep 
3389fa1aa143SJeff Vander Stoep 	if (unlikely(IS_PRIVATE(inode)))
3390fa1aa143SJeff Vander Stoep 		return 0;
3391fa1aa143SJeff Vander Stoep 
339220cdef8dSPaul Moore 	isec = inode_security(inode);
33936b6bc620SStephen Smalley 	rc = avc_has_extended_perms(&selinux_state,
33946b6bc620SStephen Smalley 				    ssid, isec->sid, isec->sclass,
3395fa1aa143SJeff Vander Stoep 				    requested, driver, xperm, &ad);
3396fa1aa143SJeff Vander Stoep out:
3397fa1aa143SJeff Vander Stoep 	return rc;
3398fa1aa143SJeff Vander Stoep }
3399fa1aa143SJeff Vander Stoep 
34001da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
34011da177e4SLinus Torvalds 			      unsigned long arg)
34021da177e4SLinus Torvalds {
340388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
34040b24dcb7SEric Paris 	int error = 0;
34051da177e4SLinus Torvalds 
34060b24dcb7SEric Paris 	switch (cmd) {
34070b24dcb7SEric Paris 	case FIONREAD:
34080b24dcb7SEric Paris 	/* fall through */
34090b24dcb7SEric Paris 	case FIBMAP:
34100b24dcb7SEric Paris 	/* fall through */
34110b24dcb7SEric Paris 	case FIGETBSZ:
34120b24dcb7SEric Paris 	/* fall through */
34132f99c369SAl Viro 	case FS_IOC_GETFLAGS:
34140b24dcb7SEric Paris 	/* fall through */
34152f99c369SAl Viro 	case FS_IOC_GETVERSION:
34160b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
34170b24dcb7SEric Paris 		break;
34181da177e4SLinus Torvalds 
34192f99c369SAl Viro 	case FS_IOC_SETFLAGS:
34200b24dcb7SEric Paris 	/* fall through */
34212f99c369SAl Viro 	case FS_IOC_SETVERSION:
34220b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
34230b24dcb7SEric Paris 		break;
34240b24dcb7SEric Paris 
34250b24dcb7SEric Paris 	/* sys_ioctl() checks */
34260b24dcb7SEric Paris 	case FIONBIO:
34270b24dcb7SEric Paris 	/* fall through */
34280b24dcb7SEric Paris 	case FIOASYNC:
34290b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
34300b24dcb7SEric Paris 		break;
34310b24dcb7SEric Paris 
34320b24dcb7SEric Paris 	case KDSKBENT:
34330b24dcb7SEric Paris 	case KDSKBSENT:
34346a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
34358e4ff6f2SStephen Smalley 					    SECURITY_CAP_AUDIT, true);
34360b24dcb7SEric Paris 		break;
34370b24dcb7SEric Paris 
34380b24dcb7SEric Paris 	/* default case assumes that the command will go
34390b24dcb7SEric Paris 	 * to the file's ioctl() function.
34400b24dcb7SEric Paris 	 */
34410b24dcb7SEric Paris 	default:
3442fa1aa143SJeff Vander Stoep 		error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
34430b24dcb7SEric Paris 	}
34440b24dcb7SEric Paris 	return error;
34451da177e4SLinus Torvalds }
34461da177e4SLinus Torvalds 
3447fcaaade1SStephen Smalley static int default_noexec;
3448fcaaade1SStephen Smalley 
34491da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
34501da177e4SLinus Torvalds {
345188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3452be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
3453d84f4f99SDavid Howells 	int rc = 0;
345488e67f3bSDavid Howells 
3455fcaaade1SStephen Smalley 	if (default_noexec &&
3456892e8cacSStephen Smalley 	    (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3457892e8cacSStephen Smalley 				   (!shared && (prot & PROT_WRITE)))) {
34581da177e4SLinus Torvalds 		/*
34591da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
34601da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
34611da177e4SLinus Torvalds 		 * This has an additional check.
34621da177e4SLinus Torvalds 		 */
34636b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
34646b6bc620SStephen Smalley 				  sid, sid, SECCLASS_PROCESS,
3465be0554c9SStephen Smalley 				  PROCESS__EXECMEM, NULL);
34661da177e4SLinus Torvalds 		if (rc)
3467d84f4f99SDavid Howells 			goto error;
34681da177e4SLinus Torvalds 	}
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 	if (file) {
34711da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
34721da177e4SLinus Torvalds 		u32 av = FILE__READ;
34731da177e4SLinus Torvalds 
34741da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
34751da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
34761da177e4SLinus Torvalds 			av |= FILE__WRITE;
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
34791da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
34801da177e4SLinus Torvalds 
348188e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
34821da177e4SLinus Torvalds 	}
3483d84f4f99SDavid Howells 
3484d84f4f99SDavid Howells error:
3485d84f4f99SDavid Howells 	return rc;
34861da177e4SLinus Torvalds }
34871da177e4SLinus Torvalds 
3488e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
34891da177e4SLinus Torvalds {
3490b1d9e6b0SCasey Schaufler 	int rc = 0;
349198883bfdSPaul Moore 
349298883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
349398883bfdSPaul Moore 		u32 sid = current_sid();
34946b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
34956b6bc620SStephen Smalley 				  sid, sid, SECCLASS_MEMPROTECT,
349698883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
349798883bfdSPaul Moore 	}
349898883bfdSPaul Moore 
349998883bfdSPaul Moore 	return rc;
3500e5467859SAl Viro }
35011da177e4SLinus Torvalds 
3502e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3503e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3504e5467859SAl Viro {
35053ba4bf5fSStephen Smalley 	struct common_audit_data ad;
35063ba4bf5fSStephen Smalley 	int rc;
35073ba4bf5fSStephen Smalley 
35083ba4bf5fSStephen Smalley 	if (file) {
35093ba4bf5fSStephen Smalley 		ad.type = LSM_AUDIT_DATA_FILE;
35103ba4bf5fSStephen Smalley 		ad.u.file = file;
35113ba4bf5fSStephen Smalley 		rc = inode_has_perm(current_cred(), file_inode(file),
35123ba4bf5fSStephen Smalley 				    FILE__MAP, &ad);
35133ba4bf5fSStephen Smalley 		if (rc)
35143ba4bf5fSStephen Smalley 			return rc;
35153ba4bf5fSStephen Smalley 	}
35163ba4bf5fSStephen Smalley 
3517aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
35181da177e4SLinus Torvalds 		prot = reqprot;
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
35211da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
35221da177e4SLinus Torvalds }
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
35251da177e4SLinus Torvalds 				 unsigned long reqprot,
35261da177e4SLinus Torvalds 				 unsigned long prot)
35271da177e4SLinus Torvalds {
352888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3529be0554c9SStephen Smalley 	u32 sid = cred_sid(cred);
35301da177e4SLinus Torvalds 
3531aa8e712cSStephen Smalley 	if (selinux_state.checkreqprot)
35321da177e4SLinus Torvalds 		prot = reqprot;
35331da177e4SLinus Torvalds 
3534fcaaade1SStephen Smalley 	if (default_noexec &&
3535fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3536d541bbeeSJames Morris 		int rc = 0;
3537db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3538db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
35396b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
35406b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3541be0554c9SStephen Smalley 					  PROCESS__EXECHEAP, NULL);
3542db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
3543c2316dbfSStephen Smalley 			   ((vma->vm_start <= vma->vm_mm->start_stack &&
3544c2316dbfSStephen Smalley 			     vma->vm_end >= vma->vm_mm->start_stack) ||
3545d17af505SAndy Lutomirski 			    vma_is_stack_for_current(vma))) {
35466b6bc620SStephen Smalley 			rc = avc_has_perm(&selinux_state,
35476b6bc620SStephen Smalley 					  sid, sid, SECCLASS_PROCESS,
3548be0554c9SStephen Smalley 					  PROCESS__EXECSTACK, NULL);
3549db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3550db4c9641SStephen Smalley 			/*
3551db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3552db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3553db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3554db4c9641SStephen Smalley 			 * modified content.  This typically should only
3555db4c9641SStephen Smalley 			 * occur for text relocations.
3556db4c9641SStephen Smalley 			 */
3557d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3558db4c9641SStephen Smalley 		}
35596b992197SLorenzo Hernandez García-Hierro 		if (rc)
35606b992197SLorenzo Hernandez García-Hierro 			return rc;
35616b992197SLorenzo Hernandez García-Hierro 	}
35621da177e4SLinus Torvalds 
35631da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
35641da177e4SLinus Torvalds }
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
35671da177e4SLinus Torvalds {
356888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
356988e67f3bSDavid Howells 
357088e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
35711da177e4SLinus Torvalds }
35721da177e4SLinus Torvalds 
35731da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
35741da177e4SLinus Torvalds 			      unsigned long arg)
35751da177e4SLinus Torvalds {
357688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
35771da177e4SLinus Torvalds 	int err = 0;
35781da177e4SLinus Torvalds 
35791da177e4SLinus Torvalds 	switch (cmd) {
35801da177e4SLinus Torvalds 	case F_SETFL:
35811da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
358288e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
35831da177e4SLinus Torvalds 			break;
35841da177e4SLinus Torvalds 		}
35851da177e4SLinus Torvalds 		/* fall through */
35861da177e4SLinus Torvalds 	case F_SETOWN:
35871da177e4SLinus Torvalds 	case F_SETSIG:
35881da177e4SLinus Torvalds 	case F_GETFL:
35891da177e4SLinus Torvalds 	case F_GETOWN:
35901da177e4SLinus Torvalds 	case F_GETSIG:
35911d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
35921da177e4SLinus Torvalds 		/* Just check FD__USE permission */
359388e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
35941da177e4SLinus Torvalds 		break;
35951da177e4SLinus Torvalds 	case F_GETLK:
35961da177e4SLinus Torvalds 	case F_SETLK:
35971da177e4SLinus Torvalds 	case F_SETLKW:
35980d3f7a2dSJeff Layton 	case F_OFD_GETLK:
35990d3f7a2dSJeff Layton 	case F_OFD_SETLK:
36000d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
36011da177e4SLinus Torvalds #if BITS_PER_LONG == 32
36021da177e4SLinus Torvalds 	case F_GETLK64:
36031da177e4SLinus Torvalds 	case F_SETLK64:
36041da177e4SLinus Torvalds 	case F_SETLKW64:
36051da177e4SLinus Torvalds #endif
360688e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
36071da177e4SLinus Torvalds 		break;
36081da177e4SLinus Torvalds 	}
36091da177e4SLinus Torvalds 
36101da177e4SLinus Torvalds 	return err;
36111da177e4SLinus Torvalds }
36121da177e4SLinus Torvalds 
3613e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
36141da177e4SLinus Torvalds {
36151da177e4SLinus Torvalds 	struct file_security_struct *fsec;
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds 	fsec = file->f_security;
3618275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
36191da177e4SLinus Torvalds }
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
36221da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
36231da177e4SLinus Torvalds {
36241da177e4SLinus Torvalds 	struct file *file;
362565c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
36261da177e4SLinus Torvalds 	u32 perm;
36271da177e4SLinus Torvalds 	struct file_security_struct *fsec;
36281da177e4SLinus Torvalds 
36291da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3630b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
36311da177e4SLinus Torvalds 
36321da177e4SLinus Torvalds 	fsec = file->f_security;
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 	if (!signum)
36351da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
36361da177e4SLinus Torvalds 	else
36371da177e4SLinus Torvalds 		perm = signal_to_av(signum);
36381da177e4SLinus Torvalds 
36396b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
36406b6bc620SStephen Smalley 			    fsec->fown_sid, sid,
36411da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
36421da177e4SLinus Torvalds }
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
36451da177e4SLinus Torvalds {
364688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
364788e67f3bSDavid Howells 
364888e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
36491da177e4SLinus Torvalds }
36501da177e4SLinus Torvalds 
365194817692SAl Viro static int selinux_file_open(struct file *file)
3652788e7dd4SYuichi Nakamura {
3653788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3654788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3655d84f4f99SDavid Howells 
3656788e7dd4SYuichi Nakamura 	fsec = file->f_security;
365783da53c5SAndreas Gruenbacher 	isec = inode_security(file_inode(file));
3658788e7dd4SYuichi Nakamura 	/*
3659788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3660788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3661788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3662788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3663788e7dd4SYuichi Nakamura 	 * struct as its SID.
3664788e7dd4SYuichi Nakamura 	 */
3665788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
36666b6bc620SStephen Smalley 	fsec->pseqno = avc_policy_seqno(&selinux_state);
3667788e7dd4SYuichi Nakamura 	/*
3668788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3669788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3670788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3671788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3672788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3673788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3674788e7dd4SYuichi Nakamura 	 */
367594817692SAl Viro 	return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3676788e7dd4SYuichi Nakamura }
3677788e7dd4SYuichi Nakamura 
36781da177e4SLinus Torvalds /* task security operations */
36791da177e4SLinus Torvalds 
3680a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task,
3681a79be238STetsuo Handa 			      unsigned long clone_flags)
36821da177e4SLinus Torvalds {
3683be0554c9SStephen Smalley 	u32 sid = current_sid();
3684be0554c9SStephen Smalley 
36856b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
36866b6bc620SStephen Smalley 			    sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
36871da177e4SLinus Torvalds }
36881da177e4SLinus Torvalds 
3689f1752eecSDavid Howells /*
3690ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3691ee18d64cSDavid Howells  */
3692ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3693ee18d64cSDavid Howells {
3694ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3695ee18d64cSDavid Howells 
3696ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3697ee18d64cSDavid Howells 	if (!tsec)
3698ee18d64cSDavid Howells 		return -ENOMEM;
3699ee18d64cSDavid Howells 
3700ee18d64cSDavid Howells 	cred->security = tsec;
3701ee18d64cSDavid Howells 	return 0;
3702ee18d64cSDavid Howells }
3703ee18d64cSDavid Howells 
3704ee18d64cSDavid Howells /*
3705f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3706f1752eecSDavid Howells  */
3707f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
37081da177e4SLinus Torvalds {
3709f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3710e0e81739SDavid Howells 
37112edeaa34STetsuo Handa 	/*
37122edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
37132edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
37142edeaa34STetsuo Handa 	 */
37152edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3716e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3717f1752eecSDavid Howells 	kfree(tsec);
37181da177e4SLinus Torvalds }
37191da177e4SLinus Torvalds 
3720d84f4f99SDavid Howells /*
3721d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3722d84f4f99SDavid Howells  */
3723d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3724d84f4f99SDavid Howells 				gfp_t gfp)
3725d84f4f99SDavid Howells {
3726d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3727d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3728d84f4f99SDavid Howells 
3729d84f4f99SDavid Howells 	old_tsec = old->security;
3730d84f4f99SDavid Howells 
3731d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3732d84f4f99SDavid Howells 	if (!tsec)
3733d84f4f99SDavid Howells 		return -ENOMEM;
3734d84f4f99SDavid Howells 
3735d84f4f99SDavid Howells 	new->security = tsec;
3736d84f4f99SDavid Howells 	return 0;
3737d84f4f99SDavid Howells }
3738d84f4f99SDavid Howells 
3739d84f4f99SDavid Howells /*
3740ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3741ee18d64cSDavid Howells  */
3742ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3743ee18d64cSDavid Howells {
3744ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3745ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3746ee18d64cSDavid Howells 
3747ee18d64cSDavid Howells 	*tsec = *old_tsec;
3748ee18d64cSDavid Howells }
3749ee18d64cSDavid Howells 
37503ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
37513ec30113SMatthew Garrett {
37523ec30113SMatthew Garrett 	*secid = cred_sid(c);
37533ec30113SMatthew Garrett }
37543ec30113SMatthew Garrett 
3755ee18d64cSDavid Howells /*
37563a3b7ce9SDavid Howells  * set the security data for a kernel service
37573a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
37583a3b7ce9SDavid Howells  */
37593a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
37603a3b7ce9SDavid Howells {
37613a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
37623a3b7ce9SDavid Howells 	u32 sid = current_sid();
37633a3b7ce9SDavid Howells 	int ret;
37643a3b7ce9SDavid Howells 
37656b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
37666b6bc620SStephen Smalley 			   sid, secid,
37673a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
37683a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
37693a3b7ce9SDavid Howells 			   NULL);
37703a3b7ce9SDavid Howells 	if (ret == 0) {
37713a3b7ce9SDavid Howells 		tsec->sid = secid;
37723a3b7ce9SDavid Howells 		tsec->create_sid = 0;
37733a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
37743a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
37753a3b7ce9SDavid Howells 	}
37763a3b7ce9SDavid Howells 	return ret;
37773a3b7ce9SDavid Howells }
37783a3b7ce9SDavid Howells 
37793a3b7ce9SDavid Howells /*
37803a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
37813a3b7ce9SDavid Howells  * objective context of the specified inode
37823a3b7ce9SDavid Howells  */
37833a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
37843a3b7ce9SDavid Howells {
378583da53c5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security(inode);
37863a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
37873a3b7ce9SDavid Howells 	u32 sid = current_sid();
37883a3b7ce9SDavid Howells 	int ret;
37893a3b7ce9SDavid Howells 
37906b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
37916b6bc620SStephen Smalley 			   sid, isec->sid,
37923a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
37933a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
37943a3b7ce9SDavid Howells 			   NULL);
37953a3b7ce9SDavid Howells 
37963a3b7ce9SDavid Howells 	if (ret == 0)
37973a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3798ef57471aSDavid Howells 	return ret;
37993a3b7ce9SDavid Howells }
38003a3b7ce9SDavid Howells 
3801dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
380225354c4fSEric Paris {
3803dd8dbf2eSEric Paris 	struct common_audit_data ad;
3804dd8dbf2eSEric Paris 
380550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3806dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3807dd8dbf2eSEric Paris 
38086b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38096b6bc620SStephen Smalley 			    current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3810dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
381125354c4fSEric Paris }
381225354c4fSEric Paris 
381361d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file)
381461d612eaSJeff Vander Stoep {
381561d612eaSJeff Vander Stoep 	struct common_audit_data ad;
381661d612eaSJeff Vander Stoep 	struct inode_security_struct *isec;
381761d612eaSJeff Vander Stoep 	struct file_security_struct *fsec;
381861d612eaSJeff Vander Stoep 	u32 sid = current_sid();
381961d612eaSJeff Vander Stoep 	int rc;
382061d612eaSJeff Vander Stoep 
382161d612eaSJeff Vander Stoep 	/* init_module */
382261d612eaSJeff Vander Stoep 	if (file == NULL)
38236b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
38246b6bc620SStephen Smalley 				    sid, sid, SECCLASS_SYSTEM,
382561d612eaSJeff Vander Stoep 					SYSTEM__MODULE_LOAD, NULL);
382661d612eaSJeff Vander Stoep 
382761d612eaSJeff Vander Stoep 	/* finit_module */
382820cdef8dSPaul Moore 
382943af5de7SVivek Goyal 	ad.type = LSM_AUDIT_DATA_FILE;
383043af5de7SVivek Goyal 	ad.u.file = file;
383161d612eaSJeff Vander Stoep 
383261d612eaSJeff Vander Stoep 	fsec = file->f_security;
383361d612eaSJeff Vander Stoep 	if (sid != fsec->sid) {
38346b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
38356b6bc620SStephen Smalley 				  sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
383661d612eaSJeff Vander Stoep 		if (rc)
383761d612eaSJeff Vander Stoep 			return rc;
383861d612eaSJeff Vander Stoep 	}
383961d612eaSJeff Vander Stoep 
384020cdef8dSPaul Moore 	isec = inode_security(file_inode(file));
38416b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38426b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SYSTEM,
384361d612eaSJeff Vander Stoep 				SYSTEM__MODULE_LOAD, &ad);
384461d612eaSJeff Vander Stoep }
384561d612eaSJeff Vander Stoep 
384661d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file,
384761d612eaSJeff Vander Stoep 				    enum kernel_read_file_id id)
384861d612eaSJeff Vander Stoep {
384961d612eaSJeff Vander Stoep 	int rc = 0;
385061d612eaSJeff Vander Stoep 
385161d612eaSJeff Vander Stoep 	switch (id) {
385261d612eaSJeff Vander Stoep 	case READING_MODULE:
385361d612eaSJeff Vander Stoep 		rc = selinux_kernel_module_from_file(file);
385461d612eaSJeff Vander Stoep 		break;
385561d612eaSJeff Vander Stoep 	default:
385661d612eaSJeff Vander Stoep 		break;
385761d612eaSJeff Vander Stoep 	}
385861d612eaSJeff Vander Stoep 
385961d612eaSJeff Vander Stoep 	return rc;
386061d612eaSJeff Vander Stoep }
386161d612eaSJeff Vander Stoep 
3862c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id)
3863c77b8cdfSMimi Zohar {
3864c77b8cdfSMimi Zohar 	int rc = 0;
3865c77b8cdfSMimi Zohar 
3866c77b8cdfSMimi Zohar 	switch (id) {
3867c77b8cdfSMimi Zohar 	case LOADING_MODULE:
3868c77b8cdfSMimi Zohar 		rc = selinux_kernel_module_from_file(NULL);
3869c77b8cdfSMimi Zohar 	default:
3870c77b8cdfSMimi Zohar 		break;
3871c77b8cdfSMimi Zohar 	}
3872c77b8cdfSMimi Zohar 
3873c77b8cdfSMimi Zohar 	return rc;
3874c77b8cdfSMimi Zohar }
3875c77b8cdfSMimi Zohar 
38761da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
38771da177e4SLinus Torvalds {
38786b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38796b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3880be0554c9SStephen Smalley 			    PROCESS__SETPGID, NULL);
38811da177e4SLinus Torvalds }
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
38841da177e4SLinus Torvalds {
38856b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38866b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3887be0554c9SStephen Smalley 			    PROCESS__GETPGID, NULL);
38881da177e4SLinus Torvalds }
38891da177e4SLinus Torvalds 
38901da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
38911da177e4SLinus Torvalds {
38926b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
38936b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3894be0554c9SStephen Smalley 			    PROCESS__GETSESSION, NULL);
38951da177e4SLinus Torvalds }
38961da177e4SLinus Torvalds 
3897f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3898f9008e4cSDavid Quigley {
3899275bb41eSDavid Howells 	*secid = task_sid(p);
3900f9008e4cSDavid Quigley }
3901f9008e4cSDavid Quigley 
39021da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
39031da177e4SLinus Torvalds {
39046b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39056b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3906be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
39071da177e4SLinus Torvalds }
39081da177e4SLinus Torvalds 
390903e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
391003e68060SJames Morris {
39116b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39126b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3913be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
391403e68060SJames Morris }
391503e68060SJames Morris 
3916a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3917a1836a42SDavid Quigley {
39186b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39196b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3920be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
3921a1836a42SDavid Quigley }
3922a1836a42SDavid Quigley 
39234298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3924791ec491SStephen Smalley 				unsigned int flags)
3925791ec491SStephen Smalley {
3926791ec491SStephen Smalley 	u32 av = 0;
3927791ec491SStephen Smalley 
392884e6885eSStephen Smalley 	if (!flags)
392984e6885eSStephen Smalley 		return 0;
3930791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_WRITE)
3931791ec491SStephen Smalley 		av |= PROCESS__SETRLIMIT;
3932791ec491SStephen Smalley 	if (flags & LSM_PRLIMIT_READ)
3933791ec491SStephen Smalley 		av |= PROCESS__GETRLIMIT;
39346b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39356b6bc620SStephen Smalley 			    cred_sid(cred), cred_sid(tcred),
3936791ec491SStephen Smalley 			    SECCLASS_PROCESS, av, NULL);
3937791ec491SStephen Smalley }
3938791ec491SStephen Smalley 
39398fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
39408fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
39411da177e4SLinus Torvalds {
39428fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
39451da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
39461da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3947d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
39481da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
39496b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
39506b6bc620SStephen Smalley 				    current_sid(), task_sid(p),
3951be0554c9SStephen Smalley 				    SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	return 0;
39541da177e4SLinus Torvalds }
39551da177e4SLinus Torvalds 
3956b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
39571da177e4SLinus Torvalds {
39586b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39596b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3960be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
39611da177e4SLinus Torvalds }
39621da177e4SLinus Torvalds 
39631da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
39641da177e4SLinus Torvalds {
39656b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39666b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3967be0554c9SStephen Smalley 			    PROCESS__GETSCHED, NULL);
39681da177e4SLinus Torvalds }
39691da177e4SLinus Torvalds 
397035601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
397135601547SDavid Quigley {
39726b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39736b6bc620SStephen Smalley 			    current_sid(), task_sid(p), SECCLASS_PROCESS,
3974be0554c9SStephen Smalley 			    PROCESS__SETSCHED, NULL);
397535601547SDavid Quigley }
397635601547SDavid Quigley 
3977ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
39786b4f3d01SStephen Smalley 				int sig, const struct cred *cred)
39791da177e4SLinus Torvalds {
39806b4f3d01SStephen Smalley 	u32 secid;
39811da177e4SLinus Torvalds 	u32 perm;
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds 	if (!sig)
39841da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
39851da177e4SLinus Torvalds 	else
39861da177e4SLinus Torvalds 		perm = signal_to_av(sig);
39876b4f3d01SStephen Smalley 	if (!cred)
3988be0554c9SStephen Smalley 		secid = current_sid();
39896b4f3d01SStephen Smalley 	else
39906b4f3d01SStephen Smalley 		secid = cred_sid(cred);
39916b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
39926b6bc620SStephen Smalley 			    secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
39931da177e4SLinus Torvalds }
39941da177e4SLinus Torvalds 
39951da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
39961da177e4SLinus Torvalds 				  struct inode *inode)
39971da177e4SLinus Torvalds {
39981da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3999275bb41eSDavid Howells 	u32 sid = task_sid(p);
40001da177e4SLinus Torvalds 
40019287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
4002db978da8SAndreas Gruenbacher 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
4003275bb41eSDavid Howells 	isec->sid = sid;
40046f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
40059287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
40061da177e4SLinus Torvalds }
40071da177e4SLinus Torvalds 
40081da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
400967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
40102bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
40111da177e4SLinus Torvalds {
40121da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
40131da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
40141da177e4SLinus Torvalds 
4015bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
40161da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
40171da177e4SLinus Torvalds 	if (ih == NULL)
40181da177e4SLinus Torvalds 		goto out;
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
40211da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
40221da177e4SLinus Torvalds 		goto out;
40231da177e4SLinus Torvalds 
402448c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
402548c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
40261da177e4SLinus Torvalds 	ret = 0;
40271da177e4SLinus Torvalds 
402867f83cbfSVenkat Yekkirala 	if (proto)
402967f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
403067f83cbfSVenkat Yekkirala 
40311da177e4SLinus Torvalds 	switch (ih->protocol) {
40321da177e4SLinus Torvalds 	case IPPROTO_TCP: {
40331da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
40361da177e4SLinus Torvalds 			break;
40371da177e4SLinus Torvalds 
40381da177e4SLinus Torvalds 		offset += ihlen;
40391da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
40401da177e4SLinus Torvalds 		if (th == NULL)
40411da177e4SLinus Torvalds 			break;
40421da177e4SLinus Torvalds 
404348c62af6SEric Paris 		ad->u.net->sport = th->source;
404448c62af6SEric Paris 		ad->u.net->dport = th->dest;
40451da177e4SLinus Torvalds 		break;
40461da177e4SLinus Torvalds 	}
40471da177e4SLinus Torvalds 
40481da177e4SLinus Torvalds 	case IPPROTO_UDP: {
40491da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
40521da177e4SLinus Torvalds 			break;
40531da177e4SLinus Torvalds 
40541da177e4SLinus Torvalds 		offset += ihlen;
40551da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
40561da177e4SLinus Torvalds 		if (uh == NULL)
40571da177e4SLinus Torvalds 			break;
40581da177e4SLinus Torvalds 
405948c62af6SEric Paris 		ad->u.net->sport = uh->source;
406048c62af6SEric Paris 		ad->u.net->dport = uh->dest;
40611da177e4SLinus Torvalds 		break;
40621da177e4SLinus Torvalds 	}
40631da177e4SLinus Torvalds 
40642ee92d46SJames Morris 	case IPPROTO_DCCP: {
40652ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
40662ee92d46SJames Morris 
40672ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
40682ee92d46SJames Morris 			break;
40692ee92d46SJames Morris 
40702ee92d46SJames Morris 		offset += ihlen;
40712ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
40722ee92d46SJames Morris 		if (dh == NULL)
40732ee92d46SJames Morris 			break;
40742ee92d46SJames Morris 
407548c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
407648c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
40772ee92d46SJames Morris 		break;
40782ee92d46SJames Morris 	}
40792ee92d46SJames Morris 
4080d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4081d452930fSRichard Haines 	case IPPROTO_SCTP: {
4082d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4083d452930fSRichard Haines 
4084d452930fSRichard Haines 		if (ntohs(ih->frag_off) & IP_OFFSET)
4085d452930fSRichard Haines 			break;
4086d452930fSRichard Haines 
4087d452930fSRichard Haines 		offset += ihlen;
4088d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4089d452930fSRichard Haines 		if (sh == NULL)
4090d452930fSRichard Haines 			break;
4091d452930fSRichard Haines 
4092d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4093d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4094d452930fSRichard Haines 		break;
4095d452930fSRichard Haines 	}
4096d452930fSRichard Haines #endif
40971da177e4SLinus Torvalds 	default:
40981da177e4SLinus Torvalds 		break;
40991da177e4SLinus Torvalds 	}
41001da177e4SLinus Torvalds out:
41011da177e4SLinus Torvalds 	return ret;
41021da177e4SLinus Torvalds }
41031da177e4SLinus Torvalds 
41041a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
41051da177e4SLinus Torvalds 
41061da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
410767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
41082bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
41091da177e4SLinus Torvalds {
41101da177e4SLinus Torvalds 	u8 nexthdr;
41111da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
41121da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
411375f2811cSJesse Gross 	__be16 frag_off;
41141da177e4SLinus Torvalds 
4115bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
41161da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
41171da177e4SLinus Torvalds 	if (ip6 == NULL)
41181da177e4SLinus Torvalds 		goto out;
41191da177e4SLinus Torvalds 
412048c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
412148c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
41221da177e4SLinus Torvalds 	ret = 0;
41231da177e4SLinus Torvalds 
41241da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
41251da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
412675f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
41271da177e4SLinus Torvalds 	if (offset < 0)
41281da177e4SLinus Torvalds 		goto out;
41291da177e4SLinus Torvalds 
413067f83cbfSVenkat Yekkirala 	if (proto)
413167f83cbfSVenkat Yekkirala 		*proto = nexthdr;
413267f83cbfSVenkat Yekkirala 
41331da177e4SLinus Torvalds 	switch (nexthdr) {
41341da177e4SLinus Torvalds 	case IPPROTO_TCP: {
41351da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
41361da177e4SLinus Torvalds 
41371da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
41381da177e4SLinus Torvalds 		if (th == NULL)
41391da177e4SLinus Torvalds 			break;
41401da177e4SLinus Torvalds 
414148c62af6SEric Paris 		ad->u.net->sport = th->source;
414248c62af6SEric Paris 		ad->u.net->dport = th->dest;
41431da177e4SLinus Torvalds 		break;
41441da177e4SLinus Torvalds 	}
41451da177e4SLinus Torvalds 
41461da177e4SLinus Torvalds 	case IPPROTO_UDP: {
41471da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
41481da177e4SLinus Torvalds 
41491da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
41501da177e4SLinus Torvalds 		if (uh == NULL)
41511da177e4SLinus Torvalds 			break;
41521da177e4SLinus Torvalds 
415348c62af6SEric Paris 		ad->u.net->sport = uh->source;
415448c62af6SEric Paris 		ad->u.net->dport = uh->dest;
41551da177e4SLinus Torvalds 		break;
41561da177e4SLinus Torvalds 	}
41571da177e4SLinus Torvalds 
41582ee92d46SJames Morris 	case IPPROTO_DCCP: {
41592ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
41602ee92d46SJames Morris 
41612ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
41622ee92d46SJames Morris 		if (dh == NULL)
41632ee92d46SJames Morris 			break;
41642ee92d46SJames Morris 
416548c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
416648c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
41672ee92d46SJames Morris 		break;
41682ee92d46SJames Morris 	}
41692ee92d46SJames Morris 
4170d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP)
4171d452930fSRichard Haines 	case IPPROTO_SCTP: {
4172d452930fSRichard Haines 		struct sctphdr _sctph, *sh;
4173d452930fSRichard Haines 
4174d452930fSRichard Haines 		sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4175d452930fSRichard Haines 		if (sh == NULL)
4176d452930fSRichard Haines 			break;
4177d452930fSRichard Haines 
4178d452930fSRichard Haines 		ad->u.net->sport = sh->source;
4179d452930fSRichard Haines 		ad->u.net->dport = sh->dest;
4180d452930fSRichard Haines 		break;
4181d452930fSRichard Haines 	}
4182d452930fSRichard Haines #endif
41831da177e4SLinus Torvalds 	/* includes fragments */
41841da177e4SLinus Torvalds 	default:
41851da177e4SLinus Torvalds 		break;
41861da177e4SLinus Torvalds 	}
41871da177e4SLinus Torvalds out:
41881da177e4SLinus Torvalds 	return ret;
41891da177e4SLinus Torvalds }
41901da177e4SLinus Torvalds 
41911da177e4SLinus Torvalds #endif /* IPV6 */
41921da177e4SLinus Torvalds 
41932bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4194cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
41951da177e4SLinus Torvalds {
4196cf9481e2SDavid Howells 	char *addrp;
4197cf9481e2SDavid Howells 	int ret;
41981da177e4SLinus Torvalds 
419948c62af6SEric Paris 	switch (ad->u.net->family) {
42001da177e4SLinus Torvalds 	case PF_INET:
420167f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
4202cf9481e2SDavid Howells 		if (ret)
4203cf9481e2SDavid Howells 			goto parse_error;
420448c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
420548c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
4206cf9481e2SDavid Howells 		goto okay;
42071da177e4SLinus Torvalds 
42081a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
42091da177e4SLinus Torvalds 	case PF_INET6:
421067f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
4211cf9481e2SDavid Howells 		if (ret)
4212cf9481e2SDavid Howells 			goto parse_error;
421348c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
421448c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
4215cf9481e2SDavid Howells 		goto okay;
42161da177e4SLinus Torvalds #endif	/* IPV6 */
42171da177e4SLinus Torvalds 	default:
4218cf9481e2SDavid Howells 		addrp = NULL;
4219cf9481e2SDavid Howells 		goto okay;
42201da177e4SLinus Torvalds 	}
42211da177e4SLinus Torvalds 
4222cf9481e2SDavid Howells parse_error:
4223c103a91eSpeter enderborg 	pr_warn(
422471f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
422571f1cb05SPaul Moore 	       " unable to parse packet\n");
42261da177e4SLinus Torvalds 	return ret;
4227cf9481e2SDavid Howells 
4228cf9481e2SDavid Howells okay:
4229cf9481e2SDavid Howells 	if (_addrp)
4230cf9481e2SDavid Howells 		*_addrp = addrp;
4231cf9481e2SDavid Howells 	return 0;
42321da177e4SLinus Torvalds }
42331da177e4SLinus Torvalds 
42344f6a993fSPaul Moore /**
4235220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
42364f6a993fSPaul Moore  * @skb: the packet
423775e22910SPaul Moore  * @family: protocol family
4238220deb96SPaul Moore  * @sid: the packet's peer label SID
42394f6a993fSPaul Moore  *
42404f6a993fSPaul Moore  * Description:
4241220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
4242220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
4243220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
4244220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4245220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
4246220deb96SPaul Moore  * peer labels.
42474f6a993fSPaul Moore  *
42484f6a993fSPaul Moore  */
4249220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
42504f6a993fSPaul Moore {
425171f1cb05SPaul Moore 	int err;
42524f6a993fSPaul Moore 	u32 xfrm_sid;
42534f6a993fSPaul Moore 	u32 nlbl_sid;
4254220deb96SPaul Moore 	u32 nlbl_type;
42554f6a993fSPaul Moore 
4256817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4257bed4d7efSPaul Moore 	if (unlikely(err))
4258bed4d7efSPaul Moore 		return -EACCES;
4259bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4260bed4d7efSPaul Moore 	if (unlikely(err))
4261bed4d7efSPaul Moore 		return -EACCES;
4262220deb96SPaul Moore 
4263aa8e712cSStephen Smalley 	err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4264aa8e712cSStephen Smalley 					   nlbl_type, xfrm_sid, sid);
426571f1cb05SPaul Moore 	if (unlikely(err)) {
4266c103a91eSpeter enderborg 		pr_warn(
426771f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
426871f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
4269220deb96SPaul Moore 		return -EACCES;
427071f1cb05SPaul Moore 	}
4271220deb96SPaul Moore 
4272220deb96SPaul Moore 	return 0;
42734f6a993fSPaul Moore }
42744f6a993fSPaul Moore 
4275446b8024SPaul Moore /**
4276446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
4277446b8024SPaul Moore  * @sk_sid: the parent socket's SID
4278446b8024SPaul Moore  * @skb_sid: the packet's SID
4279446b8024SPaul Moore  * @conn_sid: the resulting connection SID
4280446b8024SPaul Moore  *
4281446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
4282446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
4283446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
4284446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
4285446b8024SPaul Moore  *
4286446b8024SPaul Moore  */
4287446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4288446b8024SPaul Moore {
4289446b8024SPaul Moore 	int err = 0;
4290446b8024SPaul Moore 
4291446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
4292aa8e712cSStephen Smalley 		err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4293aa8e712cSStephen Smalley 					    conn_sid);
4294446b8024SPaul Moore 	else
4295446b8024SPaul Moore 		*conn_sid = sk_sid;
4296446b8024SPaul Moore 
4297446b8024SPaul Moore 	return err;
4298446b8024SPaul Moore }
4299446b8024SPaul Moore 
43001da177e4SLinus Torvalds /* socket security operations */
4301d4f2d978SPaul Moore 
43022ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
43032ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4304d4f2d978SPaul Moore {
43052ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
43062ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
43072ad18bdfSHarry Ciao 		return 0;
43082ad18bdfSHarry Ciao 	}
43092ad18bdfSHarry Ciao 
4310aa8e712cSStephen Smalley 	return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4311aa8e712cSStephen Smalley 				       secclass, NULL, socksid);
4312d4f2d978SPaul Moore }
4313d4f2d978SPaul Moore 
4314be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms)
43151da177e4SLinus Torvalds {
4316253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
43172bf49690SThomas Liu 	struct common_audit_data ad;
431848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
43191da177e4SLinus Torvalds 
4320253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4321253bfae6SPaul Moore 		return 0;
43221da177e4SLinus Torvalds 
432350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
432448c62af6SEric Paris 	ad.u.net = &net;
432548c62af6SEric Paris 	ad.u.net->sk = sk;
43261da177e4SLinus Torvalds 
43276b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
43286b6bc620SStephen Smalley 			    current_sid(), sksec->sid, sksec->sclass, perms,
4329be0554c9SStephen Smalley 			    &ad);
43301da177e4SLinus Torvalds }
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
43331da177e4SLinus Torvalds 				 int protocol, int kern)
43341da177e4SLinus Torvalds {
43355fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4336d4f2d978SPaul Moore 	u32 newsid;
4337275bb41eSDavid Howells 	u16 secclass;
43382ad18bdfSHarry Ciao 	int rc;
43391da177e4SLinus Torvalds 
43401da177e4SLinus Torvalds 	if (kern)
4341d4f2d978SPaul Moore 		return 0;
43421da177e4SLinus Torvalds 
4343275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
43442ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
43452ad18bdfSHarry Ciao 	if (rc)
43462ad18bdfSHarry Ciao 		return rc;
43472ad18bdfSHarry Ciao 
43486b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
43496b6bc620SStephen Smalley 			    tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
43501da177e4SLinus Torvalds }
43511da177e4SLinus Torvalds 
43527420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
43531da177e4SLinus Torvalds 				      int type, int protocol, int kern)
43541da177e4SLinus Torvalds {
43555fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
43565d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4357892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
43589287aed2SAndreas Gruenbacher 	u16 sclass = socket_type_to_security_class(family, type, protocol);
43599287aed2SAndreas Gruenbacher 	u32 sid = SECINITSID_KERNEL;
4360275bb41eSDavid Howells 	int err = 0;
4361275bb41eSDavid Howells 
43629287aed2SAndreas Gruenbacher 	if (!kern) {
43639287aed2SAndreas Gruenbacher 		err = socket_sockcreate_sid(tsec, sclass, &sid);
43642ad18bdfSHarry Ciao 		if (err)
43652ad18bdfSHarry Ciao 			return err;
43662ad18bdfSHarry Ciao 	}
4367275bb41eSDavid Howells 
43689287aed2SAndreas Gruenbacher 	isec->sclass = sclass;
43699287aed2SAndreas Gruenbacher 	isec->sid = sid;
43706f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
43711da177e4SLinus Torvalds 
4372892c141eSVenkat Yekkirala 	if (sock->sk) {
4373892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
43749287aed2SAndreas Gruenbacher 		sksec->sclass = sclass;
43759287aed2SAndreas Gruenbacher 		sksec->sid = sid;
4376d452930fSRichard Haines 		/* Allows detection of the first association on this socket */
4377d452930fSRichard Haines 		if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4378d452930fSRichard Haines 			sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4379d452930fSRichard Haines 
4380389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4381892c141eSVenkat Yekkirala 	}
4382892c141eSVenkat Yekkirala 
43837420ed23SVenkat Yekkirala 	return err;
43841da177e4SLinus Torvalds }
43851da177e4SLinus Torvalds 
43860b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka,
43870b811db2SDavid Herrmann 				     struct socket *sockb)
43880b811db2SDavid Herrmann {
43890b811db2SDavid Herrmann 	struct sk_security_struct *sksec_a = socka->sk->sk_security;
43900b811db2SDavid Herrmann 	struct sk_security_struct *sksec_b = sockb->sk->sk_security;
43910b811db2SDavid Herrmann 
43920b811db2SDavid Herrmann 	sksec_a->peer_sid = sksec_b->sid;
43930b811db2SDavid Herrmann 	sksec_b->peer_sid = sksec_a->sid;
43940b811db2SDavid Herrmann 
43950b811db2SDavid Herrmann 	return 0;
43960b811db2SDavid Herrmann }
43970b811db2SDavid Herrmann 
43981da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
43991da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
44001da177e4SLinus Torvalds    permission check between the socket and the port number. */
44011da177e4SLinus Torvalds 
44021da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
44031da177e4SLinus Torvalds {
4404253bfae6SPaul Moore 	struct sock *sk = sock->sk;
44050f8db8ccSAlexey Kodanev 	struct sk_security_struct *sksec = sk->sk_security;
44061da177e4SLinus Torvalds 	u16 family;
44071da177e4SLinus Torvalds 	int err;
44081da177e4SLinus Torvalds 
4409be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__BIND);
44101da177e4SLinus Torvalds 	if (err)
44111da177e4SLinus Torvalds 		goto out;
44121da177e4SLinus Torvalds 
4413d452930fSRichard Haines 	/* If PF_INET or PF_INET6, check name_bind permission for the port. */
4414253bfae6SPaul Moore 	family = sk->sk_family;
44151da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
44161da177e4SLinus Torvalds 		char *addrp;
44172bf49690SThomas Liu 		struct common_audit_data ad;
441848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
44191da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
44201da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
44210f8db8ccSAlexey Kodanev 		u16 family_sa = address->sa_family;
44221da177e4SLinus Torvalds 		unsigned short snum;
4423e399f982SJames Morris 		u32 sid, node_perm;
44241da177e4SLinus Torvalds 
4425d452930fSRichard Haines 		/*
4426d452930fSRichard Haines 		 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4427d452930fSRichard Haines 		 * that validates multiple binding addresses. Because of this
4428d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4429d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4430d452930fSRichard Haines 		 */
44310f8db8ccSAlexey Kodanev 		switch (family_sa) {
44320f8db8ccSAlexey Kodanev 		case AF_UNSPEC:
443368741a8aSRichard Haines 		case AF_INET:
443468741a8aSRichard Haines 			if (addrlen < sizeof(struct sockaddr_in))
443568741a8aSRichard Haines 				return -EINVAL;
44361da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
44370f8db8ccSAlexey Kodanev 			if (family_sa == AF_UNSPEC) {
44380f8db8ccSAlexey Kodanev 				/* see __inet_bind(), we only want to allow
44390f8db8ccSAlexey Kodanev 				 * AF_UNSPEC if the address is INADDR_ANY
44400f8db8ccSAlexey Kodanev 				 */
44410f8db8ccSAlexey Kodanev 				if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
44420f8db8ccSAlexey Kodanev 					goto err_af;
44430f8db8ccSAlexey Kodanev 				family_sa = AF_INET;
44440f8db8ccSAlexey Kodanev 			}
44451da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
44461da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
444768741a8aSRichard Haines 			break;
444868741a8aSRichard Haines 		case AF_INET6:
444968741a8aSRichard Haines 			if (addrlen < SIN6_LEN_RFC2133)
445068741a8aSRichard Haines 				return -EINVAL;
44511da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
44521da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
44531da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
445468741a8aSRichard Haines 			break;
445568741a8aSRichard Haines 		default:
44560f8db8ccSAlexey Kodanev 			goto err_af;
44571da177e4SLinus Torvalds 		}
44581da177e4SLinus Torvalds 
445988b7d370SAlexey Kodanev 		ad.type = LSM_AUDIT_DATA_NET;
446088b7d370SAlexey Kodanev 		ad.u.net = &net;
446188b7d370SAlexey Kodanev 		ad.u.net->sport = htons(snum);
446288b7d370SAlexey Kodanev 		ad.u.net->family = family_sa;
446388b7d370SAlexey Kodanev 
4464227b60f5SStephen Hemminger 		if (snum) {
4465227b60f5SStephen Hemminger 			int low, high;
4466227b60f5SStephen Hemminger 
44670bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4468227b60f5SStephen Hemminger 
44694548b683SKrister Johansen 			if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
44704548b683SKrister Johansen 			    snum > high) {
44713e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
44723e112172SPaul Moore 						      snum, &sid);
44731da177e4SLinus Torvalds 				if (err)
44741da177e4SLinus Torvalds 					goto out;
44756b6bc620SStephen Smalley 				err = avc_has_perm(&selinux_state,
44766b6bc620SStephen Smalley 						   sksec->sid, sid,
4477253bfae6SPaul Moore 						   sksec->sclass,
44781da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
44791da177e4SLinus Torvalds 				if (err)
44801da177e4SLinus Torvalds 					goto out;
44811da177e4SLinus Torvalds 			}
4482227b60f5SStephen Hemminger 		}
44831da177e4SLinus Torvalds 
4484253bfae6SPaul Moore 		switch (sksec->sclass) {
448513402580SJames Morris 		case SECCLASS_TCP_SOCKET:
44861da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
44871da177e4SLinus Torvalds 			break;
44881da177e4SLinus Torvalds 
448913402580SJames Morris 		case SECCLASS_UDP_SOCKET:
44901da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
44911da177e4SLinus Torvalds 			break;
44921da177e4SLinus Torvalds 
44932ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
44942ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
44952ee92d46SJames Morris 			break;
44962ee92d46SJames Morris 
4497d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4498d452930fSRichard Haines 			node_perm = SCTP_SOCKET__NODE_BIND;
4499d452930fSRichard Haines 			break;
4500d452930fSRichard Haines 
45011da177e4SLinus Torvalds 		default:
45021da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
45031da177e4SLinus Torvalds 			break;
45041da177e4SLinus Torvalds 		}
45051da177e4SLinus Torvalds 
450688b7d370SAlexey Kodanev 		err = sel_netnode_sid(addrp, family_sa, &sid);
45071da177e4SLinus Torvalds 		if (err)
45081da177e4SLinus Torvalds 			goto out;
45091da177e4SLinus Torvalds 
45100f8db8ccSAlexey Kodanev 		if (family_sa == AF_INET)
451148c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
45121da177e4SLinus Torvalds 		else
451348c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
45141da177e4SLinus Torvalds 
45156b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
45166b6bc620SStephen Smalley 				   sksec->sid, sid,
4517253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
45181da177e4SLinus Torvalds 		if (err)
45191da177e4SLinus Torvalds 			goto out;
45201da177e4SLinus Torvalds 	}
45211da177e4SLinus Torvalds out:
45221da177e4SLinus Torvalds 	return err;
45230f8db8ccSAlexey Kodanev err_af:
45240f8db8ccSAlexey Kodanev 	/* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
45250f8db8ccSAlexey Kodanev 	if (sksec->sclass == SECCLASS_SCTP_SOCKET)
45260f8db8ccSAlexey Kodanev 		return -EINVAL;
45270f8db8ccSAlexey Kodanev 	return -EAFNOSUPPORT;
45281da177e4SLinus Torvalds }
45291da177e4SLinus Torvalds 
4530d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
45315fb94e9cSMauro Carvalho Chehab  * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
4532d452930fSRichard Haines  */
4533d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock,
4534d452930fSRichard Haines 					 struct sockaddr *address, int addrlen)
45351da177e4SLinus Torvalds {
4536014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4537253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45381da177e4SLinus Torvalds 	int err;
45391da177e4SLinus Torvalds 
4540be0554c9SStephen Smalley 	err = sock_has_perm(sk, SOCKET__CONNECT);
45411da177e4SLinus Torvalds 	if (err)
45421da177e4SLinus Torvalds 		return err;
45431da177e4SLinus Torvalds 
45441da177e4SLinus Torvalds 	/*
4545d452930fSRichard Haines 	 * If a TCP, DCCP or SCTP socket, check name_connect permission
4546d452930fSRichard Haines 	 * for the port.
45471da177e4SLinus Torvalds 	 */
4548253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4549d452930fSRichard Haines 	    sksec->sclass == SECCLASS_DCCP_SOCKET ||
4550d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET) {
45512bf49690SThomas Liu 		struct common_audit_data ad;
455248c62af6SEric Paris 		struct lsm_network_audit net = {0,};
45531da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
45541da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
45551da177e4SLinus Torvalds 		unsigned short snum;
45562ee92d46SJames Morris 		u32 sid, perm;
45571da177e4SLinus Torvalds 
4558d452930fSRichard Haines 		/* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4559d452930fSRichard Haines 		 * that validates multiple connect addresses. Because of this
4560d452930fSRichard Haines 		 * need to check address->sa_family as it is possible to have
4561d452930fSRichard Haines 		 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4562d452930fSRichard Haines 		 */
456368741a8aSRichard Haines 		switch (address->sa_family) {
456468741a8aSRichard Haines 		case AF_INET:
45651da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4566911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
45671da177e4SLinus Torvalds 				return -EINVAL;
45681da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
456968741a8aSRichard Haines 			break;
457068741a8aSRichard Haines 		case AF_INET6:
45711da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4572911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
45731da177e4SLinus Torvalds 				return -EINVAL;
45741da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
457568741a8aSRichard Haines 			break;
457668741a8aSRichard Haines 		default:
457768741a8aSRichard Haines 			/* Note that SCTP services expect -EINVAL, whereas
457868741a8aSRichard Haines 			 * others expect -EAFNOSUPPORT.
457968741a8aSRichard Haines 			 */
458068741a8aSRichard Haines 			if (sksec->sclass == SECCLASS_SCTP_SOCKET)
458168741a8aSRichard Haines 				return -EINVAL;
458268741a8aSRichard Haines 			else
458368741a8aSRichard Haines 				return -EAFNOSUPPORT;
45841da177e4SLinus Torvalds 		}
45851da177e4SLinus Torvalds 
45863e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
45871da177e4SLinus Torvalds 		if (err)
4588d452930fSRichard Haines 			return err;
45891da177e4SLinus Torvalds 
4590d452930fSRichard Haines 		switch (sksec->sclass) {
4591d452930fSRichard Haines 		case SECCLASS_TCP_SOCKET:
4592d452930fSRichard Haines 			perm = TCP_SOCKET__NAME_CONNECT;
4593d452930fSRichard Haines 			break;
4594d452930fSRichard Haines 		case SECCLASS_DCCP_SOCKET:
4595d452930fSRichard Haines 			perm = DCCP_SOCKET__NAME_CONNECT;
4596d452930fSRichard Haines 			break;
4597d452930fSRichard Haines 		case SECCLASS_SCTP_SOCKET:
4598d452930fSRichard Haines 			perm = SCTP_SOCKET__NAME_CONNECT;
4599d452930fSRichard Haines 			break;
4600d452930fSRichard Haines 		}
46012ee92d46SJames Morris 
460250c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
460348c62af6SEric Paris 		ad.u.net = &net;
460448c62af6SEric Paris 		ad.u.net->dport = htons(snum);
460588b7d370SAlexey Kodanev 		ad.u.net->family = address->sa_family;
46066b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
46076b6bc620SStephen Smalley 				   sksec->sid, sid, sksec->sclass, perm, &ad);
46081da177e4SLinus Torvalds 		if (err)
4609d452930fSRichard Haines 			return err;
46101da177e4SLinus Torvalds 	}
46111da177e4SLinus Torvalds 
4612d452930fSRichard Haines 	return 0;
4613d452930fSRichard Haines }
4614014ab19aSPaul Moore 
4615d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4616d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock,
4617d452930fSRichard Haines 				  struct sockaddr *address, int addrlen)
4618d452930fSRichard Haines {
4619d452930fSRichard Haines 	int err;
4620d452930fSRichard Haines 	struct sock *sk = sock->sk;
4621d452930fSRichard Haines 
4622d452930fSRichard Haines 	err = selinux_socket_connect_helper(sock, address, addrlen);
4623d452930fSRichard Haines 	if (err)
46241da177e4SLinus Torvalds 		return err;
4625d452930fSRichard Haines 
4626d452930fSRichard Haines 	return selinux_netlbl_socket_connect(sk, address);
46271da177e4SLinus Torvalds }
46281da177e4SLinus Torvalds 
46291da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
46301da177e4SLinus Torvalds {
4631be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__LISTEN);
46321da177e4SLinus Torvalds }
46331da177e4SLinus Torvalds 
46341da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
46351da177e4SLinus Torvalds {
46361da177e4SLinus Torvalds 	int err;
46371da177e4SLinus Torvalds 	struct inode_security_struct *isec;
46381da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
46399287aed2SAndreas Gruenbacher 	u16 sclass;
46409287aed2SAndreas Gruenbacher 	u32 sid;
46411da177e4SLinus Torvalds 
4642be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
46431da177e4SLinus Torvalds 	if (err)
46441da177e4SLinus Torvalds 		return err;
46451da177e4SLinus Torvalds 
46465d226df4SAndreas Gruenbacher 	isec = inode_security_novalidate(SOCK_INODE(sock));
46479287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
46489287aed2SAndreas Gruenbacher 	sclass = isec->sclass;
46499287aed2SAndreas Gruenbacher 	sid = isec->sid;
46509287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
46519287aed2SAndreas Gruenbacher 
46529287aed2SAndreas Gruenbacher 	newisec = inode_security_novalidate(SOCK_INODE(newsock));
46539287aed2SAndreas Gruenbacher 	newisec->sclass = sclass;
46549287aed2SAndreas Gruenbacher 	newisec->sid = sid;
46556f3be9f5SAndreas Gruenbacher 	newisec->initialized = LABEL_INITIALIZED;
46561da177e4SLinus Torvalds 
46571da177e4SLinus Torvalds 	return 0;
46581da177e4SLinus Torvalds }
46591da177e4SLinus Torvalds 
46601da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
46611da177e4SLinus Torvalds 				  int size)
46621da177e4SLinus Torvalds {
4663be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__WRITE);
46641da177e4SLinus Torvalds }
46651da177e4SLinus Torvalds 
46661da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
46671da177e4SLinus Torvalds 				  int size, int flags)
46681da177e4SLinus Torvalds {
4669be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__READ);
46701da177e4SLinus Torvalds }
46711da177e4SLinus Torvalds 
46721da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
46731da177e4SLinus Torvalds {
4674be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
46751da177e4SLinus Torvalds }
46761da177e4SLinus Torvalds 
46771da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
46781da177e4SLinus Torvalds {
4679be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETATTR);
46801da177e4SLinus Torvalds }
46811da177e4SLinus Torvalds 
46821da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
46831da177e4SLinus Torvalds {
4684f8687afeSPaul Moore 	int err;
4685f8687afeSPaul Moore 
4686be0554c9SStephen Smalley 	err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4687f8687afeSPaul Moore 	if (err)
4688f8687afeSPaul Moore 		return err;
4689f8687afeSPaul Moore 
4690f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
46911da177e4SLinus Torvalds }
46921da177e4SLinus Torvalds 
46931da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
46941da177e4SLinus Torvalds 				     int optname)
46951da177e4SLinus Torvalds {
4696be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__GETOPT);
46971da177e4SLinus Torvalds }
46981da177e4SLinus Torvalds 
46991da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
47001da177e4SLinus Torvalds {
4701be0554c9SStephen Smalley 	return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
47021da177e4SLinus Torvalds }
47031da177e4SLinus Torvalds 
47043610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
47053610cda5SDavid S. Miller 					      struct sock *other,
47061da177e4SLinus Torvalds 					      struct sock *newsk)
47071da177e4SLinus Torvalds {
47083610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
47093610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
47104d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
47112bf49690SThomas Liu 	struct common_audit_data ad;
471248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
47131da177e4SLinus Torvalds 	int err;
47141da177e4SLinus Torvalds 
471550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
471648c62af6SEric Paris 	ad.u.net = &net;
471748c62af6SEric Paris 	ad.u.net->sk = other;
47181da177e4SLinus Torvalds 
47196b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
47206b6bc620SStephen Smalley 			   sksec_sock->sid, sksec_other->sid,
47214d1e2451SPaul Moore 			   sksec_other->sclass,
47221da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
47231da177e4SLinus Torvalds 	if (err)
47241da177e4SLinus Torvalds 		return err;
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds 	/* server child socket */
47274d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
4728aa8e712cSStephen Smalley 	err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4729aa8e712cSStephen Smalley 				    sksec_sock->sid, &sksec_new->sid);
47304d1e2451SPaul Moore 	if (err)
47314237c75cSVenkat Yekkirala 		return err;
47324d1e2451SPaul Moore 
47334d1e2451SPaul Moore 	/* connecting socket */
47344d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
47354d1e2451SPaul Moore 
47364d1e2451SPaul Moore 	return 0;
47371da177e4SLinus Torvalds }
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
47401da177e4SLinus Torvalds 					struct socket *other)
47411da177e4SLinus Torvalds {
4742253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4743253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
47442bf49690SThomas Liu 	struct common_audit_data ad;
474548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
47461da177e4SLinus Torvalds 
474750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
474848c62af6SEric Paris 	ad.u.net = &net;
474948c62af6SEric Paris 	ad.u.net->sk = other->sk;
47501da177e4SLinus Torvalds 
47516b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
47526b6bc620SStephen Smalley 			    ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4753253bfae6SPaul Moore 			    &ad);
47541da177e4SLinus Torvalds }
47551da177e4SLinus Torvalds 
4756cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4757cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
47582bf49690SThomas Liu 				    struct common_audit_data *ad)
4759effad8dfSPaul Moore {
4760effad8dfSPaul Moore 	int err;
4761effad8dfSPaul Moore 	u32 if_sid;
4762effad8dfSPaul Moore 	u32 node_sid;
4763effad8dfSPaul Moore 
4764cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4765effad8dfSPaul Moore 	if (err)
4766effad8dfSPaul Moore 		return err;
47676b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
47686b6bc620SStephen Smalley 			   peer_sid, if_sid,
4769effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4770effad8dfSPaul Moore 	if (err)
4771effad8dfSPaul Moore 		return err;
4772effad8dfSPaul Moore 
4773effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4774effad8dfSPaul Moore 	if (err)
4775effad8dfSPaul Moore 		return err;
47766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
47776b6bc620SStephen Smalley 			    peer_sid, node_sid,
4778effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4779effad8dfSPaul Moore }
4780effad8dfSPaul Moore 
4781220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4782d8395c87SPaul Moore 				       u16 family)
4783220deb96SPaul Moore {
4784277d342fSPaul Moore 	int err = 0;
4785220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4786220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
47872bf49690SThomas Liu 	struct common_audit_data ad;
478848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4789d8395c87SPaul Moore 	char *addrp;
4790d8395c87SPaul Moore 
479150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
479248c62af6SEric Paris 	ad.u.net = &net;
479348c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
479448c62af6SEric Paris 	ad.u.net->family = family;
4795d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4796d8395c87SPaul Moore 	if (err)
4797d8395c87SPaul Moore 		return err;
4798220deb96SPaul Moore 
479958bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
48006b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48016b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4802d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4803220deb96SPaul Moore 		if (err)
4804220deb96SPaul Moore 			return err;
480558bfbb51SPaul Moore 	}
4806220deb96SPaul Moore 
4807d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4808220deb96SPaul Moore 	if (err)
4809220deb96SPaul Moore 		return err;
4810d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4811220deb96SPaul Moore 
48124e5ab4cbSJames Morris 	return err;
48134e5ab4cbSJames Morris }
4814d28d1e08STrent Jaeger 
48154e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
48164e5ab4cbSJames Morris {
4817220deb96SPaul Moore 	int err;
48184237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4819220deb96SPaul Moore 	u16 family = sk->sk_family;
4820220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
48212bf49690SThomas Liu 	struct common_audit_data ad;
482248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4823220deb96SPaul Moore 	char *addrp;
4824d8395c87SPaul Moore 	u8 secmark_active;
4825d8395c87SPaul Moore 	u8 peerlbl_active;
48264e5ab4cbSJames Morris 
48274e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4828220deb96SPaul Moore 		return 0;
48294e5ab4cbSJames Morris 
48304e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
483187fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
48324e5ab4cbSJames Morris 		family = PF_INET;
48334e5ab4cbSJames Morris 
4834d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4835d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4836d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4837d8395c87SPaul Moore 	 * as fast and as clean as possible. */
4838aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
4839d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4840d8395c87SPaul Moore 
4841d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
48422be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4843d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4844d8395c87SPaul Moore 		return 0;
4845d8395c87SPaul Moore 
484650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
484748c62af6SEric Paris 	ad.u.net = &net;
484848c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
484948c62af6SEric Paris 	ad.u.net->family = family;
4850224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
48514e5ab4cbSJames Morris 	if (err)
4852220deb96SPaul Moore 		return err;
48534e5ab4cbSJames Morris 
4854d8395c87SPaul Moore 	if (peerlbl_active) {
4855d621d35eSPaul Moore 		u32 peer_sid;
4856220deb96SPaul Moore 
4857220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4858220deb96SPaul Moore 		if (err)
4859220deb96SPaul Moore 			return err;
4860cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4861cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4862dfaebe98SPaul Moore 		if (err) {
4863a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
4864effad8dfSPaul Moore 			return err;
4865dfaebe98SPaul Moore 		}
48666b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48676b6bc620SStephen Smalley 				   sk_sid, peer_sid, SECCLASS_PEER,
4868d621d35eSPaul Moore 				   PEER__RECV, &ad);
486946d01d63SChad Hanson 		if (err) {
4870a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 0);
487146d01d63SChad Hanson 			return err;
487246d01d63SChad Hanson 		}
4873d621d35eSPaul Moore 	}
4874d621d35eSPaul Moore 
4875d8395c87SPaul Moore 	if (secmark_active) {
48766b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
48776b6bc620SStephen Smalley 				   sk_sid, skb->secmark, SECCLASS_PACKET,
4878effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4879effad8dfSPaul Moore 		if (err)
4880effad8dfSPaul Moore 			return err;
4881effad8dfSPaul Moore 	}
4882effad8dfSPaul Moore 
4883d621d35eSPaul Moore 	return err;
48841da177e4SLinus Torvalds }
48851da177e4SLinus Torvalds 
48862c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
48871da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
48881da177e4SLinus Torvalds {
48891da177e4SLinus Torvalds 	int err = 0;
48901da177e4SLinus Torvalds 	char *scontext;
48911da177e4SLinus Torvalds 	u32 scontext_len;
4892253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
48933de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
48941da177e4SLinus Torvalds 
4895253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4896d452930fSRichard Haines 	    sksec->sclass == SECCLASS_TCP_SOCKET ||
4897d452930fSRichard Haines 	    sksec->sclass == SECCLASS_SCTP_SOCKET)
4898dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4899253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4900253bfae6SPaul Moore 		return -ENOPROTOOPT;
49011da177e4SLinus Torvalds 
4902aa8e712cSStephen Smalley 	err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4903aa8e712cSStephen Smalley 				      &scontext_len);
49041da177e4SLinus Torvalds 	if (err)
4905253bfae6SPaul Moore 		return err;
49061da177e4SLinus Torvalds 
49071da177e4SLinus Torvalds 	if (scontext_len > len) {
49081da177e4SLinus Torvalds 		err = -ERANGE;
49091da177e4SLinus Torvalds 		goto out_len;
49101da177e4SLinus Torvalds 	}
49111da177e4SLinus Torvalds 
49121da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
49131da177e4SLinus Torvalds 		err = -EFAULT;
49141da177e4SLinus Torvalds 
49151da177e4SLinus Torvalds out_len:
49161da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
49171da177e4SLinus Torvalds 		err = -EFAULT;
49181da177e4SLinus Torvalds 	kfree(scontext);
49191da177e4SLinus Torvalds 	return err;
49201da177e4SLinus Torvalds }
49211da177e4SLinus Torvalds 
4922dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
49232c7946a7SCatherine Zhang {
4924dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
492575e22910SPaul Moore 	u16 family;
4926899134f2SPaul Moore 	struct inode_security_struct *isec;
4927877ce7c1SCatherine Zhang 
4928aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4929aa862900SPaul Moore 		family = PF_INET;
4930aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4931aa862900SPaul Moore 		family = PF_INET6;
4932aa862900SPaul Moore 	else if (sock)
493375e22910SPaul Moore 		family = sock->sk->sk_family;
493475e22910SPaul Moore 	else
493575e22910SPaul Moore 		goto out;
493675e22910SPaul Moore 
4937899134f2SPaul Moore 	if (sock && family == PF_UNIX) {
4938899134f2SPaul Moore 		isec = inode_security_novalidate(SOCK_INODE(sock));
4939899134f2SPaul Moore 		peer_secid = isec->sid;
4940899134f2SPaul Moore 	} else if (skb)
4941220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
49422c7946a7SCatherine Zhang 
494375e22910SPaul Moore out:
4944dc49c1f9SCatherine Zhang 	*secid = peer_secid;
494575e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
494675e22910SPaul Moore 		return -EINVAL;
494775e22910SPaul Moore 	return 0;
49482c7946a7SCatherine Zhang }
49492c7946a7SCatherine Zhang 
49507d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
49511da177e4SLinus Torvalds {
495284914b7eSPaul Moore 	struct sk_security_struct *sksec;
495384914b7eSPaul Moore 
495484914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
495584914b7eSPaul Moore 	if (!sksec)
495684914b7eSPaul Moore 		return -ENOMEM;
495784914b7eSPaul Moore 
495884914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
495984914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
49605dee25d0SStephen Smalley 	sksec->sclass = SECCLASS_SOCKET;
496184914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
496284914b7eSPaul Moore 	sk->sk_security = sksec;
496384914b7eSPaul Moore 
496484914b7eSPaul Moore 	return 0;
49651da177e4SLinus Torvalds }
49661da177e4SLinus Torvalds 
49671da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
49681da177e4SLinus Torvalds {
496984914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
497084914b7eSPaul Moore 
497184914b7eSPaul Moore 	sk->sk_security = NULL;
497284914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
497384914b7eSPaul Moore 	kfree(sksec);
49741da177e4SLinus Torvalds }
49751da177e4SLinus Torvalds 
4976892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4977892c141eSVenkat Yekkirala {
4978dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4979dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4980892c141eSVenkat Yekkirala 
4981dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4982dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4983dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
498499f59ed0SPaul Moore 
4985dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4986892c141eSVenkat Yekkirala }
4987892c141eSVenkat Yekkirala 
4988beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4989d28d1e08STrent Jaeger {
4990d28d1e08STrent Jaeger 	if (!sk)
4991beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4992892c141eSVenkat Yekkirala 	else {
4993892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4994d28d1e08STrent Jaeger 
4995beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4996892c141eSVenkat Yekkirala 	}
4997d28d1e08STrent Jaeger }
4998d28d1e08STrent Jaeger 
49999a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
50004237c75cSVenkat Yekkirala {
50015d226df4SAndreas Gruenbacher 	struct inode_security_struct *isec =
50025d226df4SAndreas Gruenbacher 		inode_security_novalidate(SOCK_INODE(parent));
50034237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
50044237c75cSVenkat Yekkirala 
50052873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
50062873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
50074237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
5008220deb96SPaul Moore 	sksec->sclass = isec->sclass;
50094237c75cSVenkat Yekkirala }
50104237c75cSVenkat Yekkirala 
5011d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5012d452930fSRichard Haines  * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5013d452930fSRichard Haines  * already present).
5014d452930fSRichard Haines  */
5015d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5016d452930fSRichard Haines 				      struct sk_buff *skb)
5017d452930fSRichard Haines {
5018d452930fSRichard Haines 	struct sk_security_struct *sksec = ep->base.sk->sk_security;
5019d452930fSRichard Haines 	struct common_audit_data ad;
5020d452930fSRichard Haines 	struct lsm_network_audit net = {0,};
5021d452930fSRichard Haines 	u8 peerlbl_active;
5022d452930fSRichard Haines 	u32 peer_sid = SECINITSID_UNLABELED;
5023d452930fSRichard Haines 	u32 conn_sid;
5024d452930fSRichard Haines 	int err = 0;
5025d452930fSRichard Haines 
5026aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5027d452930fSRichard Haines 		return 0;
5028d452930fSRichard Haines 
5029d452930fSRichard Haines 	peerlbl_active = selinux_peerlbl_enabled();
5030d452930fSRichard Haines 
5031d452930fSRichard Haines 	if (peerlbl_active) {
5032d452930fSRichard Haines 		/* This will return peer_sid = SECSID_NULL if there are
5033d452930fSRichard Haines 		 * no peer labels, see security_net_peersid_resolve().
5034d452930fSRichard Haines 		 */
5035d452930fSRichard Haines 		err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5036d452930fSRichard Haines 					      &peer_sid);
5037d452930fSRichard Haines 		if (err)
5038d452930fSRichard Haines 			return err;
5039d452930fSRichard Haines 
5040d452930fSRichard Haines 		if (peer_sid == SECSID_NULL)
5041d452930fSRichard Haines 			peer_sid = SECINITSID_UNLABELED;
5042d452930fSRichard Haines 	}
5043d452930fSRichard Haines 
5044d452930fSRichard Haines 	if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5045d452930fSRichard Haines 		sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5046d452930fSRichard Haines 
5047d452930fSRichard Haines 		/* Here as first association on socket. As the peer SID
5048d452930fSRichard Haines 		 * was allowed by peer recv (and the netif/node checks),
5049d452930fSRichard Haines 		 * then it is approved by policy and used as the primary
5050d452930fSRichard Haines 		 * peer SID for getpeercon(3).
5051d452930fSRichard Haines 		 */
5052d452930fSRichard Haines 		sksec->peer_sid = peer_sid;
5053d452930fSRichard Haines 	} else if  (sksec->peer_sid != peer_sid) {
5054d452930fSRichard Haines 		/* Other association peer SIDs are checked to enforce
5055d452930fSRichard Haines 		 * consistency among the peer SIDs.
5056d452930fSRichard Haines 		 */
5057d452930fSRichard Haines 		ad.type = LSM_AUDIT_DATA_NET;
5058d452930fSRichard Haines 		ad.u.net = &net;
5059d452930fSRichard Haines 		ad.u.net->sk = ep->base.sk;
50606b6bc620SStephen Smalley 		err = avc_has_perm(&selinux_state,
50616b6bc620SStephen Smalley 				   sksec->peer_sid, peer_sid, sksec->sclass,
5062d452930fSRichard Haines 				   SCTP_SOCKET__ASSOCIATION, &ad);
5063d452930fSRichard Haines 		if (err)
5064d452930fSRichard Haines 			return err;
5065d452930fSRichard Haines 	}
5066d452930fSRichard Haines 
5067d452930fSRichard Haines 	/* Compute the MLS component for the connection and store
5068d452930fSRichard Haines 	 * the information in ep. This will be used by SCTP TCP type
5069d452930fSRichard Haines 	 * sockets and peeled off connections as they cause a new
5070d452930fSRichard Haines 	 * socket to be generated. selinux_sctp_sk_clone() will then
5071d452930fSRichard Haines 	 * plug this into the new socket.
5072d452930fSRichard Haines 	 */
5073d452930fSRichard Haines 	err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5074d452930fSRichard Haines 	if (err)
5075d452930fSRichard Haines 		return err;
5076d452930fSRichard Haines 
5077d452930fSRichard Haines 	ep->secid = conn_sid;
5078d452930fSRichard Haines 	ep->peer_secid = peer_sid;
5079d452930fSRichard Haines 
5080d452930fSRichard Haines 	/* Set any NetLabel labels including CIPSO/CALIPSO options. */
5081d452930fSRichard Haines 	return selinux_netlbl_sctp_assoc_request(ep, skb);
5082d452930fSRichard Haines }
5083d452930fSRichard Haines 
5084d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5085d452930fSRichard Haines  * based on their @optname.
5086d452930fSRichard Haines  */
5087d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5088d452930fSRichard Haines 				     struct sockaddr *address,
5089d452930fSRichard Haines 				     int addrlen)
5090d452930fSRichard Haines {
5091d452930fSRichard Haines 	int len, err = 0, walk_size = 0;
5092d452930fSRichard Haines 	void *addr_buf;
5093d452930fSRichard Haines 	struct sockaddr *addr;
5094d452930fSRichard Haines 	struct socket *sock;
5095d452930fSRichard Haines 
5096aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5097d452930fSRichard Haines 		return 0;
5098d452930fSRichard Haines 
5099d452930fSRichard Haines 	/* Process one or more addresses that may be IPv4 or IPv6 */
5100d452930fSRichard Haines 	sock = sk->sk_socket;
5101d452930fSRichard Haines 	addr_buf = address;
5102d452930fSRichard Haines 
5103d452930fSRichard Haines 	while (walk_size < addrlen) {
5104c138325fSOndrej Mosnacek 		if (walk_size + sizeof(sa_family_t) > addrlen)
5105c138325fSOndrej Mosnacek 			return -EINVAL;
5106c138325fSOndrej Mosnacek 
5107d452930fSRichard Haines 		addr = addr_buf;
5108d452930fSRichard Haines 		switch (addr->sa_family) {
51094152dc91SAlexey Kodanev 		case AF_UNSPEC:
5110d452930fSRichard Haines 		case AF_INET:
5111d452930fSRichard Haines 			len = sizeof(struct sockaddr_in);
5112d452930fSRichard Haines 			break;
5113d452930fSRichard Haines 		case AF_INET6:
5114d452930fSRichard Haines 			len = sizeof(struct sockaddr_in6);
5115d452930fSRichard Haines 			break;
5116d452930fSRichard Haines 		default:
51174152dc91SAlexey Kodanev 			return -EINVAL;
5118d452930fSRichard Haines 		}
5119d452930fSRichard Haines 
5120d452930fSRichard Haines 		err = -EINVAL;
5121d452930fSRichard Haines 		switch (optname) {
5122d452930fSRichard Haines 		/* Bind checks */
5123d452930fSRichard Haines 		case SCTP_PRIMARY_ADDR:
5124d452930fSRichard Haines 		case SCTP_SET_PEER_PRIMARY_ADDR:
5125d452930fSRichard Haines 		case SCTP_SOCKOPT_BINDX_ADD:
5126d452930fSRichard Haines 			err = selinux_socket_bind(sock, addr, len);
5127d452930fSRichard Haines 			break;
5128d452930fSRichard Haines 		/* Connect checks */
5129d452930fSRichard Haines 		case SCTP_SOCKOPT_CONNECTX:
5130d452930fSRichard Haines 		case SCTP_PARAM_SET_PRIMARY:
5131d452930fSRichard Haines 		case SCTP_PARAM_ADD_IP:
5132d452930fSRichard Haines 		case SCTP_SENDMSG_CONNECT:
5133d452930fSRichard Haines 			err = selinux_socket_connect_helper(sock, addr, len);
5134d452930fSRichard Haines 			if (err)
5135d452930fSRichard Haines 				return err;
5136d452930fSRichard Haines 
5137d452930fSRichard Haines 			/* As selinux_sctp_bind_connect() is called by the
5138d452930fSRichard Haines 			 * SCTP protocol layer, the socket is already locked,
5139d452930fSRichard Haines 			 * therefore selinux_netlbl_socket_connect_locked() is
5140d452930fSRichard Haines 			 * is called here. The situations handled are:
5141d452930fSRichard Haines 			 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5142d452930fSRichard Haines 			 * whenever a new IP address is added or when a new
5143d452930fSRichard Haines 			 * primary address is selected.
5144d452930fSRichard Haines 			 * Note that an SCTP connect(2) call happens before
5145d452930fSRichard Haines 			 * the SCTP protocol layer and is handled via
5146d452930fSRichard Haines 			 * selinux_socket_connect().
5147d452930fSRichard Haines 			 */
5148d452930fSRichard Haines 			err = selinux_netlbl_socket_connect_locked(sk, addr);
5149d452930fSRichard Haines 			break;
5150d452930fSRichard Haines 		}
5151d452930fSRichard Haines 
5152d452930fSRichard Haines 		if (err)
5153d452930fSRichard Haines 			return err;
5154d452930fSRichard Haines 
5155d452930fSRichard Haines 		addr_buf += len;
5156d452930fSRichard Haines 		walk_size += len;
5157d452930fSRichard Haines 	}
5158d452930fSRichard Haines 
5159d452930fSRichard Haines 	return 0;
5160d452930fSRichard Haines }
5161d452930fSRichard Haines 
5162d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5163d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5164d452930fSRichard Haines 				  struct sock *newsk)
5165d452930fSRichard Haines {
5166d452930fSRichard Haines 	struct sk_security_struct *sksec = sk->sk_security;
5167d452930fSRichard Haines 	struct sk_security_struct *newsksec = newsk->sk_security;
5168d452930fSRichard Haines 
5169d452930fSRichard Haines 	/* If policy does not support SECCLASS_SCTP_SOCKET then call
5170d452930fSRichard Haines 	 * the non-sctp clone version.
5171d452930fSRichard Haines 	 */
5172aa8e712cSStephen Smalley 	if (!selinux_policycap_extsockclass())
5173d452930fSRichard Haines 		return selinux_sk_clone_security(sk, newsk);
5174d452930fSRichard Haines 
5175d452930fSRichard Haines 	newsksec->sid = ep->secid;
5176d452930fSRichard Haines 	newsksec->peer_sid = ep->peer_secid;
5177d452930fSRichard Haines 	newsksec->sclass = sksec->sclass;
5178d452930fSRichard Haines 	selinux_netlbl_sctp_sk_clone(sk, newsk);
5179d452930fSRichard Haines }
5180d452930fSRichard Haines 
51819a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
51824237c75cSVenkat Yekkirala 				     struct request_sock *req)
51834237c75cSVenkat Yekkirala {
51844237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
51854237c75cSVenkat Yekkirala 	int err;
51860b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
5187446b8024SPaul Moore 	u32 connsid;
51884237c75cSVenkat Yekkirala 	u32 peersid;
51894237c75cSVenkat Yekkirala 
5190aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5191220deb96SPaul Moore 	if (err)
5192220deb96SPaul Moore 		return err;
5193446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
51944237c75cSVenkat Yekkirala 	if (err)
51954237c75cSVenkat Yekkirala 		return err;
5196446b8024SPaul Moore 	req->secid = connsid;
51976b877699SVenkat Yekkirala 	req->peer_secid = peersid;
5198389fb800SPaul Moore 
5199389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
52004237c75cSVenkat Yekkirala }
52014237c75cSVenkat Yekkirala 
52029a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
52039a673e56SAdrian Bunk 				   const struct request_sock *req)
52044237c75cSVenkat Yekkirala {
52054237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
52064237c75cSVenkat Yekkirala 
52074237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
52086b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
52094237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
52104237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
52114237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
52124237c75cSVenkat Yekkirala 	   time it will have been created and available. */
521399f59ed0SPaul Moore 
52149f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
52159f2ad665SPaul Moore 	 * thread with access to newsksec */
5216389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
52174237c75cSVenkat Yekkirala }
52184237c75cSVenkat Yekkirala 
5219014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
52206b877699SVenkat Yekkirala {
5221aa862900SPaul Moore 	u16 family = sk->sk_family;
52226b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
52236b877699SVenkat Yekkirala 
5224aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
5225aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5226aa862900SPaul Moore 		family = PF_INET;
5227aa862900SPaul Moore 
5228aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
52296b877699SVenkat Yekkirala }
52306b877699SVenkat Yekkirala 
52312606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
52322606fd1fSEric Paris {
52332606fd1fSEric Paris 	const struct task_security_struct *__tsec;
52342606fd1fSEric Paris 	u32 tsid;
52352606fd1fSEric Paris 
52362606fd1fSEric Paris 	__tsec = current_security();
52372606fd1fSEric Paris 	tsid = __tsec->sid;
52382606fd1fSEric Paris 
52396b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
52406b6bc620SStephen Smalley 			    tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
52416b6bc620SStephen Smalley 			    NULL);
52422606fd1fSEric Paris }
52432606fd1fSEric Paris 
52442606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
52452606fd1fSEric Paris {
52462606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
52472606fd1fSEric Paris }
52482606fd1fSEric Paris 
52492606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
52502606fd1fSEric Paris {
52512606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
52522606fd1fSEric Paris }
52532606fd1fSEric Paris 
52549a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
52559a673e56SAdrian Bunk 				      struct flowi *fl)
52564237c75cSVenkat Yekkirala {
52571d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
52584237c75cSVenkat Yekkirala }
52594237c75cSVenkat Yekkirala 
52605dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
52615dbbaf2dSPaul Moore {
52625dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
52635dbbaf2dSPaul Moore 
52645dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
52655dbbaf2dSPaul Moore 	if (!tunsec)
52665dbbaf2dSPaul Moore 		return -ENOMEM;
52675dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
52685dbbaf2dSPaul Moore 
52695dbbaf2dSPaul Moore 	*security = tunsec;
52705dbbaf2dSPaul Moore 	return 0;
52715dbbaf2dSPaul Moore }
52725dbbaf2dSPaul Moore 
52735dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
52745dbbaf2dSPaul Moore {
52755dbbaf2dSPaul Moore 	kfree(security);
52765dbbaf2dSPaul Moore }
52775dbbaf2dSPaul Moore 
5278ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
5279ed6d76e4SPaul Moore {
5280ed6d76e4SPaul Moore 	u32 sid = current_sid();
5281ed6d76e4SPaul Moore 
5282ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
5283ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
5284ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
5285ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
5286ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
5287ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
5288ed6d76e4SPaul Moore 
52896b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
52906b6bc620SStephen Smalley 			    sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5291ed6d76e4SPaul Moore 			    NULL);
5292ed6d76e4SPaul Moore }
5293ed6d76e4SPaul Moore 
52945dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
5295ed6d76e4SPaul Moore {
52965dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
52975dbbaf2dSPaul Moore 
52986b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
52996b6bc620SStephen Smalley 			    current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
53005dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
53015dbbaf2dSPaul Moore }
53025dbbaf2dSPaul Moore 
53035dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
53045dbbaf2dSPaul Moore {
53055dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5306ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
5307ed6d76e4SPaul Moore 
5308ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
5309ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
5310ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
5311ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
5312ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
5313ed6d76e4SPaul Moore 	 * protocols were being used */
5314ed6d76e4SPaul Moore 
53155dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
5316ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
53175dbbaf2dSPaul Moore 
53185dbbaf2dSPaul Moore 	return 0;
5319ed6d76e4SPaul Moore }
5320ed6d76e4SPaul Moore 
53215dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
5322ed6d76e4SPaul Moore {
53235dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
5324ed6d76e4SPaul Moore 	u32 sid = current_sid();
5325ed6d76e4SPaul Moore 	int err;
5326ed6d76e4SPaul Moore 
53276b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
53286b6bc620SStephen Smalley 			   sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5329ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
5330ed6d76e4SPaul Moore 	if (err)
5331ed6d76e4SPaul Moore 		return err;
53326b6bc620SStephen Smalley 	err = avc_has_perm(&selinux_state,
53336b6bc620SStephen Smalley 			   sid, sid, SECCLASS_TUN_SOCKET,
5334ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
5335ed6d76e4SPaul Moore 	if (err)
5336ed6d76e4SPaul Moore 		return err;
53375dbbaf2dSPaul Moore 	tunsec->sid = sid;
5338ed6d76e4SPaul Moore 
5339ed6d76e4SPaul Moore 	return 0;
5340ed6d76e4SPaul Moore }
5341ed6d76e4SPaul Moore 
53421da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
53431da177e4SLinus Torvalds {
53441da177e4SLinus Torvalds 	int err = 0;
53451da177e4SLinus Torvalds 	u32 perm;
53461da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5347253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
53481da177e4SLinus Torvalds 
534977954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
53501da177e4SLinus Torvalds 		err = -EINVAL;
53511da177e4SLinus Torvalds 		goto out;
53521da177e4SLinus Torvalds 	}
5353b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
53541da177e4SLinus Torvalds 
5355253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
53561da177e4SLinus Torvalds 	if (err) {
53571da177e4SLinus Torvalds 		if (err == -EINVAL) {
535876319946SVladis Dronov 			pr_warn_ratelimited("SELinux: unrecognized netlink"
535976319946SVladis Dronov 			       " message: protocol=%hu nlmsg_type=%hu sclass=%s"
536076319946SVladis Dronov 			       " pig=%d comm=%s\n",
5361cded3fffSMarek Milkovic 			       sk->sk_protocol, nlh->nlmsg_type,
536276319946SVladis Dronov 			       secclass_map[sksec->sclass - 1].name,
536376319946SVladis Dronov 			       task_pid_nr(current), current->comm);
5364e5a5ca96SPaul Moore 			if (!enforcing_enabled(&selinux_state) ||
5365aa8e712cSStephen Smalley 			    security_get_allow_unknown(&selinux_state))
53661da177e4SLinus Torvalds 				err = 0;
53671da177e4SLinus Torvalds 		}
53681da177e4SLinus Torvalds 
53691da177e4SLinus Torvalds 		/* Ignore */
53701da177e4SLinus Torvalds 		if (err == -ENOENT)
53711da177e4SLinus Torvalds 			err = 0;
53721da177e4SLinus Torvalds 		goto out;
53731da177e4SLinus Torvalds 	}
53741da177e4SLinus Torvalds 
5375be0554c9SStephen Smalley 	err = sock_has_perm(sk, perm);
53761da177e4SLinus Torvalds out:
53771da177e4SLinus Torvalds 	return err;
53781da177e4SLinus Torvalds }
53791da177e4SLinus Torvalds 
53801da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
53811da177e4SLinus Torvalds 
5382cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
5383cbe0d6e8SPaul Moore 				       const struct net_device *indev,
5384effad8dfSPaul Moore 				       u16 family)
53851da177e4SLinus Torvalds {
5386dfaebe98SPaul Moore 	int err;
5387effad8dfSPaul Moore 	char *addrp;
5388effad8dfSPaul Moore 	u32 peer_sid;
53892bf49690SThomas Liu 	struct common_audit_data ad;
539048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5391effad8dfSPaul Moore 	u8 secmark_active;
5392948bf85cSPaul Moore 	u8 netlbl_active;
5393effad8dfSPaul Moore 	u8 peerlbl_active;
53944237c75cSVenkat Yekkirala 
5395aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5396effad8dfSPaul Moore 		return NF_ACCEPT;
53974237c75cSVenkat Yekkirala 
5398effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
5399948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
54002be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5401effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5402effad8dfSPaul Moore 		return NF_ACCEPT;
54034237c75cSVenkat Yekkirala 
5404d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5405d8395c87SPaul Moore 		return NF_DROP;
5406d8395c87SPaul Moore 
540750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
540848c62af6SEric Paris 	ad.u.net = &net;
5409cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
541048c62af6SEric Paris 	ad.u.net->family = family;
5411effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5412effad8dfSPaul Moore 		return NF_DROP;
54131da177e4SLinus Torvalds 
5414dfaebe98SPaul Moore 	if (peerlbl_active) {
5415cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5416cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
5417dfaebe98SPaul Moore 		if (err) {
5418a04e71f6SHuw Davies 			selinux_netlbl_err(skb, family, err, 1);
5419effad8dfSPaul Moore 			return NF_DROP;
5420dfaebe98SPaul Moore 		}
5421dfaebe98SPaul Moore 	}
5422effad8dfSPaul Moore 
5423effad8dfSPaul Moore 	if (secmark_active)
54246b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
54256b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5426effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5427effad8dfSPaul Moore 			return NF_DROP;
5428effad8dfSPaul Moore 
5429948bf85cSPaul Moore 	if (netlbl_active)
5430948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
5431948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
5432948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
5433948bf85cSPaul Moore 		 * protection */
5434948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5435948bf85cSPaul Moore 			return NF_DROP;
5436948bf85cSPaul Moore 
5437effad8dfSPaul Moore 	return NF_ACCEPT;
5438effad8dfSPaul Moore }
5439effad8dfSPaul Moore 
544006198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv,
5441effad8dfSPaul Moore 					 struct sk_buff *skb,
5442238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5443effad8dfSPaul Moore {
5444238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
5445effad8dfSPaul Moore }
5446effad8dfSPaul Moore 
54471a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
544806198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv,
5449effad8dfSPaul Moore 					 struct sk_buff *skb,
5450238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
5451effad8dfSPaul Moore {
5452238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
5453effad8dfSPaul Moore }
5454effad8dfSPaul Moore #endif	/* IPV6 */
5455effad8dfSPaul Moore 
5456948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
5457948bf85cSPaul Moore 				      u16 family)
5458948bf85cSPaul Moore {
545947180068SPaul Moore 	struct sock *sk;
5460948bf85cSPaul Moore 	u32 sid;
5461948bf85cSPaul Moore 
5462948bf85cSPaul Moore 	if (!netlbl_enabled())
5463948bf85cSPaul Moore 		return NF_ACCEPT;
5464948bf85cSPaul Moore 
5465948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5466948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
5467948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
546847180068SPaul Moore 	sk = skb->sk;
546947180068SPaul Moore 	if (sk) {
547047180068SPaul Moore 		struct sk_security_struct *sksec;
547147180068SPaul Moore 
5472e446f9dfSEric Dumazet 		if (sk_listener(sk))
547347180068SPaul Moore 			/* if the socket is the listening state then this
547447180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
547547180068SPaul Moore 			 * be labeled based on the connection/request_sock and
547647180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
547747180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
547847180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
547947180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
548047180068SPaul Moore 			 * as any IP option based labeling should be copied
548147180068SPaul Moore 			 * from the initial connection request (in the IP
548247180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
548347180068SPaul Moore 			 * security label in the packet itself this is the
548447180068SPaul Moore 			 * best we can do. */
548547180068SPaul Moore 			return NF_ACCEPT;
548647180068SPaul Moore 
548747180068SPaul Moore 		/* standard practice, label using the parent socket */
548847180068SPaul Moore 		sksec = sk->sk_security;
5489948bf85cSPaul Moore 		sid = sksec->sid;
5490948bf85cSPaul Moore 	} else
5491948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
5492948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5493948bf85cSPaul Moore 		return NF_DROP;
5494948bf85cSPaul Moore 
5495948bf85cSPaul Moore 	return NF_ACCEPT;
5496948bf85cSPaul Moore }
5497948bf85cSPaul Moore 
549806198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv,
5499948bf85cSPaul Moore 					struct sk_buff *skb,
5500238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
5501948bf85cSPaul Moore {
5502948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
5503948bf85cSPaul Moore }
5504948bf85cSPaul Moore 
55051a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
55062917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv,
55072917f57bSHuw Davies 					struct sk_buff *skb,
55082917f57bSHuw Davies 					const struct nf_hook_state *state)
55092917f57bSHuw Davies {
55102917f57bSHuw Davies 	return selinux_ip_output(skb, PF_INET6);
55112917f57bSHuw Davies }
55122917f57bSHuw Davies #endif	/* IPV6 */
55132917f57bSHuw Davies 
5514effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5515effad8dfSPaul Moore 						int ifindex,
5516d8395c87SPaul Moore 						u16 family)
55174e5ab4cbSJames Morris {
551854abc686SEric Dumazet 	struct sock *sk = skb_to_full_sk(skb);
55194237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
55202bf49690SThomas Liu 	struct common_audit_data ad;
552148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5522d8395c87SPaul Moore 	char *addrp;
5523d8395c87SPaul Moore 	u8 proto;
55244e5ab4cbSJames Morris 
5525effad8dfSPaul Moore 	if (sk == NULL)
5526effad8dfSPaul Moore 		return NF_ACCEPT;
55274237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
55284e5ab4cbSJames Morris 
552950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
553048c62af6SEric Paris 	ad.u.net = &net;
553148c62af6SEric Paris 	ad.u.net->netif = ifindex;
553248c62af6SEric Paris 	ad.u.net->family = family;
5533d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5534d8395c87SPaul Moore 		return NF_DROP;
5535d8395c87SPaul Moore 
553658bfbb51SPaul Moore 	if (selinux_secmark_enabled())
55376b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
55386b6bc620SStephen Smalley 				 sksec->sid, skb->secmark,
5539d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
55402fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
55411da177e4SLinus Torvalds 
5542d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
55432fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
5544effad8dfSPaul Moore 
5545effad8dfSPaul Moore 	return NF_ACCEPT;
5546effad8dfSPaul Moore }
5547effad8dfSPaul Moore 
5548cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5549cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
5550effad8dfSPaul Moore 					 u16 family)
5551effad8dfSPaul Moore {
5552effad8dfSPaul Moore 	u32 secmark_perm;
5553effad8dfSPaul Moore 	u32 peer_sid;
5554cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
5555effad8dfSPaul Moore 	struct sock *sk;
55562bf49690SThomas Liu 	struct common_audit_data ad;
555748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
5558effad8dfSPaul Moore 	char *addrp;
5559effad8dfSPaul Moore 	u8 secmark_active;
5560effad8dfSPaul Moore 	u8 peerlbl_active;
5561effad8dfSPaul Moore 
5562effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
5563effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
5564effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
5565effad8dfSPaul Moore 	 * as fast and as clean as possible. */
5566aa8e712cSStephen Smalley 	if (!selinux_policycap_netpeer())
5567d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
5568c0828e50SPaul Moore 
5569effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
55702be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
5571effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
5572effad8dfSPaul Moore 		return NF_ACCEPT;
5573effad8dfSPaul Moore 
557454abc686SEric Dumazet 	sk = skb_to_full_sk(skb);
5575c0828e50SPaul Moore 
5576effad8dfSPaul Moore #ifdef CONFIG_XFRM
5577effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5578effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
5579effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
5580effad8dfSPaul Moore 	 * when the packet is on it's final way out.
5581effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5582c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
5583c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5584c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
5585c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
5586c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
5587c0828e50SPaul Moore 	 *       connection. */
5588c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5589e446f9dfSEric Dumazet 	    !(sk && sk_listener(sk)))
5590effad8dfSPaul Moore 		return NF_ACCEPT;
5591effad8dfSPaul Moore #endif
5592effad8dfSPaul Moore 
5593d8395c87SPaul Moore 	if (sk == NULL) {
5594446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5595446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5596446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5597446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
55984a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5599d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5600d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
560104f6d70fSEric Paris 				return NF_DROP;
56024a7ab3dcSSteffen Klassert 		} else {
56034a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5604d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
56054a7ab3dcSSteffen Klassert 		}
5606e446f9dfSEric Dumazet 	} else if (sk_listener(sk)) {
5607446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5608446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5609446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5610446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5611446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5612446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5613446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5614446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5615446b8024SPaul Moore 		 * for similar problems. */
5616446b8024SPaul Moore 		u32 skb_sid;
5617e446f9dfSEric Dumazet 		struct sk_security_struct *sksec;
5618e446f9dfSEric Dumazet 
5619e446f9dfSEric Dumazet 		sksec = sk->sk_security;
5620446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5621446b8024SPaul Moore 			return NF_DROP;
5622c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5623c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5624c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5625c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5626c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5627c0828e50SPaul Moore 		 * pass the packet. */
5628c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5629c0828e50SPaul Moore 			switch (family) {
5630c0828e50SPaul Moore 			case PF_INET:
5631c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5632c0828e50SPaul Moore 					return NF_ACCEPT;
5633c0828e50SPaul Moore 				break;
5634c0828e50SPaul Moore 			case PF_INET6:
5635c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5636c0828e50SPaul Moore 					return NF_ACCEPT;
5637a7a91a19SPaul Moore 				break;
5638c0828e50SPaul Moore 			default:
5639c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5640c0828e50SPaul Moore 			}
5641c0828e50SPaul Moore 		}
5642446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5643446b8024SPaul Moore 			return NF_DROP;
5644446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5645d8395c87SPaul Moore 	} else {
5646446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5647446b8024SPaul Moore 		 * associated socket. */
5648effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5649effad8dfSPaul Moore 		peer_sid = sksec->sid;
5650effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5651effad8dfSPaul Moore 	}
5652effad8dfSPaul Moore 
565350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
565448c62af6SEric Paris 	ad.u.net = &net;
565548c62af6SEric Paris 	ad.u.net->netif = ifindex;
565648c62af6SEric Paris 	ad.u.net->family = family;
5657d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
565804f6d70fSEric Paris 		return NF_DROP;
5659d8395c87SPaul Moore 
5660effad8dfSPaul Moore 	if (secmark_active)
56616b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56626b6bc620SStephen Smalley 				 peer_sid, skb->secmark,
5663effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
56641f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5665effad8dfSPaul Moore 
5666effad8dfSPaul Moore 	if (peerlbl_active) {
5667effad8dfSPaul Moore 		u32 if_sid;
5668effad8dfSPaul Moore 		u32 node_sid;
5669effad8dfSPaul Moore 
5670cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
567104f6d70fSEric Paris 			return NF_DROP;
56726b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56736b6bc620SStephen Smalley 				 peer_sid, if_sid,
5674effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
56751f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5676effad8dfSPaul Moore 
5677effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
567804f6d70fSEric Paris 			return NF_DROP;
56796b6bc620SStephen Smalley 		if (avc_has_perm(&selinux_state,
56806b6bc620SStephen Smalley 				 peer_sid, node_sid,
5681effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
56821f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5683effad8dfSPaul Moore 	}
5684effad8dfSPaul Moore 
5685effad8dfSPaul Moore 	return NF_ACCEPT;
5686effad8dfSPaul Moore }
5687effad8dfSPaul Moore 
568806198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv,
5689a224be76SDavid S. Miller 					   struct sk_buff *skb,
5690238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
56911da177e4SLinus Torvalds {
5692238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
56931da177e4SLinus Torvalds }
56941da177e4SLinus Torvalds 
56951a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
569606198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv,
5697a224be76SDavid S. Miller 					   struct sk_buff *skb,
5698238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
56991da177e4SLinus Torvalds {
5700238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
57011da177e4SLinus Torvalds }
57021da177e4SLinus Torvalds #endif	/* IPV6 */
57031da177e4SLinus Torvalds 
57041da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
57051da177e4SLinus Torvalds 
57061da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
57071da177e4SLinus Torvalds {
5708941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
57091da177e4SLinus Torvalds }
57101da177e4SLinus Torvalds 
5711be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm,
57121da177e4SLinus Torvalds 			      u16 sclass)
57131da177e4SLinus Torvalds {
57141da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
57151da177e4SLinus Torvalds 
571689d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
57171da177e4SLinus Torvalds 	if (!isec)
57181da177e4SLinus Torvalds 		return -ENOMEM;
57191da177e4SLinus Torvalds 
57201da177e4SLinus Torvalds 	isec->sclass = sclass;
5721be0554c9SStephen Smalley 	isec->sid = current_sid();
57221da177e4SLinus Torvalds 	perm->security = isec;
57231da177e4SLinus Torvalds 
57241da177e4SLinus Torvalds 	return 0;
57251da177e4SLinus Torvalds }
57261da177e4SLinus Torvalds 
57271da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
57281da177e4SLinus Torvalds {
57291da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
57301da177e4SLinus Torvalds 	perm->security = NULL;
57311da177e4SLinus Torvalds 	kfree(isec);
57321da177e4SLinus Torvalds }
57331da177e4SLinus Torvalds 
57341da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
57351da177e4SLinus Torvalds {
57361da177e4SLinus Torvalds 	struct msg_security_struct *msec;
57371da177e4SLinus Torvalds 
573889d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
57391da177e4SLinus Torvalds 	if (!msec)
57401da177e4SLinus Torvalds 		return -ENOMEM;
57411da177e4SLinus Torvalds 
57421da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
57431da177e4SLinus Torvalds 	msg->security = msec;
57441da177e4SLinus Torvalds 
57451da177e4SLinus Torvalds 	return 0;
57461da177e4SLinus Torvalds }
57471da177e4SLinus Torvalds 
57481da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
57491da177e4SLinus Torvalds {
57501da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
57511da177e4SLinus Torvalds 
57521da177e4SLinus Torvalds 	msg->security = NULL;
57531da177e4SLinus Torvalds 	kfree(msec);
57541da177e4SLinus Torvalds }
57551da177e4SLinus Torvalds 
57561da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
57576af963f1SStephen Smalley 			u32 perms)
57581da177e4SLinus Torvalds {
57591da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
57602bf49690SThomas Liu 	struct common_audit_data ad;
5761275bb41eSDavid Howells 	u32 sid = current_sid();
57621da177e4SLinus Torvalds 
57631da177e4SLinus Torvalds 	isec = ipc_perms->security;
57641da177e4SLinus Torvalds 
576550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
57661da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
57671da177e4SLinus Torvalds 
57686b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
57696b6bc620SStephen Smalley 			    sid, isec->sid, isec->sclass, perms, &ad);
57701da177e4SLinus Torvalds }
57711da177e4SLinus Torvalds 
57721da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
57731da177e4SLinus Torvalds {
57741da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
57751da177e4SLinus Torvalds }
57761da177e4SLinus Torvalds 
57771da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
57781da177e4SLinus Torvalds {
57791da177e4SLinus Torvalds 	msg_msg_free_security(msg);
57801da177e4SLinus Torvalds }
57811da177e4SLinus Torvalds 
57821da177e4SLinus Torvalds /* message queue security operations */
5783d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
57841da177e4SLinus Torvalds {
57851da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
57862bf49690SThomas Liu 	struct common_audit_data ad;
5787275bb41eSDavid Howells 	u32 sid = current_sid();
57881da177e4SLinus Torvalds 	int rc;
57891da177e4SLinus Torvalds 
5790d8c6e854SEric W. Biederman 	rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
57911da177e4SLinus Torvalds 	if (rc)
57921da177e4SLinus Torvalds 		return rc;
57931da177e4SLinus Torvalds 
5794d8c6e854SEric W. Biederman 	isec = msq->security;
57951da177e4SLinus Torvalds 
579650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5797d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
57981da177e4SLinus Torvalds 
57996b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
58006b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
58011da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
58021da177e4SLinus Torvalds 	if (rc) {
5803d8c6e854SEric W. Biederman 		ipc_free_security(msq);
58041da177e4SLinus Torvalds 		return rc;
58051da177e4SLinus Torvalds 	}
58061da177e4SLinus Torvalds 	return 0;
58071da177e4SLinus Torvalds }
58081da177e4SLinus Torvalds 
5809d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
58101da177e4SLinus Torvalds {
5811d8c6e854SEric W. Biederman 	ipc_free_security(msq);
58121da177e4SLinus Torvalds }
58131da177e4SLinus Torvalds 
5814d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
58151da177e4SLinus Torvalds {
58161da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58172bf49690SThomas Liu 	struct common_audit_data ad;
5818275bb41eSDavid Howells 	u32 sid = current_sid();
58191da177e4SLinus Torvalds 
5820d8c6e854SEric W. Biederman 	isec = msq->security;
58211da177e4SLinus Torvalds 
582250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5823d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
58241da177e4SLinus Torvalds 
58256b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
58266b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_MSGQ,
58271da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
58281da177e4SLinus Torvalds }
58291da177e4SLinus Torvalds 
5830d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
58311da177e4SLinus Torvalds {
58321da177e4SLinus Torvalds 	int err;
58331da177e4SLinus Torvalds 	int perms;
58341da177e4SLinus Torvalds 
58351da177e4SLinus Torvalds 	switch (cmd) {
58361da177e4SLinus Torvalds 	case IPC_INFO:
58371da177e4SLinus Torvalds 	case MSG_INFO:
58381da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
58396b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
58406b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
5841be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
58421da177e4SLinus Torvalds 	case IPC_STAT:
58431da177e4SLinus Torvalds 	case MSG_STAT:
584423c8cec8SDavidlohr Bueso 	case MSG_STAT_ANY:
58451da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
58461da177e4SLinus Torvalds 		break;
58471da177e4SLinus Torvalds 	case IPC_SET:
58481da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
58491da177e4SLinus Torvalds 		break;
58501da177e4SLinus Torvalds 	case IPC_RMID:
58511da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
58521da177e4SLinus Torvalds 		break;
58531da177e4SLinus Torvalds 	default:
58541da177e4SLinus Torvalds 		return 0;
58551da177e4SLinus Torvalds 	}
58561da177e4SLinus Torvalds 
5857d8c6e854SEric W. Biederman 	err = ipc_has_perm(msq, perms);
58581da177e4SLinus Torvalds 	return err;
58591da177e4SLinus Torvalds }
58601da177e4SLinus Torvalds 
5861d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
58621da177e4SLinus Torvalds {
58631da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
58641da177e4SLinus Torvalds 	struct msg_security_struct *msec;
58652bf49690SThomas Liu 	struct common_audit_data ad;
5866275bb41eSDavid Howells 	u32 sid = current_sid();
58671da177e4SLinus Torvalds 	int rc;
58681da177e4SLinus Torvalds 
5869d8c6e854SEric W. Biederman 	isec = msq->security;
58701da177e4SLinus Torvalds 	msec = msg->security;
58711da177e4SLinus Torvalds 
58721da177e4SLinus Torvalds 	/*
58731da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
58741da177e4SLinus Torvalds 	 */
58751da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
58761da177e4SLinus Torvalds 		/*
58771da177e4SLinus Torvalds 		 * Compute new sid based on current process and
58781da177e4SLinus Torvalds 		 * message queue this message will be stored in
58791da177e4SLinus Torvalds 		 */
5880aa8e712cSStephen Smalley 		rc = security_transition_sid(&selinux_state, sid, isec->sid,
5881aa8e712cSStephen Smalley 					     SECCLASS_MSG, NULL, &msec->sid);
58821da177e4SLinus Torvalds 		if (rc)
58831da177e4SLinus Torvalds 			return rc;
58841da177e4SLinus Torvalds 	}
58851da177e4SLinus Torvalds 
588650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5887d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
58881da177e4SLinus Torvalds 
58891da177e4SLinus Torvalds 	/* Can this process write to the queue? */
58906b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
58916b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_MSGQ,
58921da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
58931da177e4SLinus Torvalds 	if (!rc)
58941da177e4SLinus Torvalds 		/* Can this process send the message */
58956b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
58966b6bc620SStephen Smalley 				  sid, msec->sid, SECCLASS_MSG,
5897275bb41eSDavid Howells 				  MSG__SEND, &ad);
58981da177e4SLinus Torvalds 	if (!rc)
58991da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
59006b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59016b6bc620SStephen Smalley 				  msec->sid, isec->sid, SECCLASS_MSGQ,
5902275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
59031da177e4SLinus Torvalds 
59041da177e4SLinus Torvalds 	return rc;
59051da177e4SLinus Torvalds }
59061da177e4SLinus Torvalds 
5907d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
59081da177e4SLinus Torvalds 				    struct task_struct *target,
59091da177e4SLinus Torvalds 				    long type, int mode)
59101da177e4SLinus Torvalds {
59111da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59121da177e4SLinus Torvalds 	struct msg_security_struct *msec;
59132bf49690SThomas Liu 	struct common_audit_data ad;
5914275bb41eSDavid Howells 	u32 sid = task_sid(target);
59151da177e4SLinus Torvalds 	int rc;
59161da177e4SLinus Torvalds 
5917d8c6e854SEric W. Biederman 	isec = msq->security;
59181da177e4SLinus Torvalds 	msec = msg->security;
59191da177e4SLinus Torvalds 
592050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
5921d8c6e854SEric W. Biederman 	ad.u.ipc_id = msq->key;
59221da177e4SLinus Torvalds 
59236b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59246b6bc620SStephen Smalley 			  sid, isec->sid,
59251da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
59261da177e4SLinus Torvalds 	if (!rc)
59276b6bc620SStephen Smalley 		rc = avc_has_perm(&selinux_state,
59286b6bc620SStephen Smalley 				  sid, msec->sid,
59291da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
59301da177e4SLinus Torvalds 	return rc;
59311da177e4SLinus Torvalds }
59321da177e4SLinus Torvalds 
59331da177e4SLinus Torvalds /* Shared Memory security operations */
59347191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
59351da177e4SLinus Torvalds {
59361da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59372bf49690SThomas Liu 	struct common_audit_data ad;
5938275bb41eSDavid Howells 	u32 sid = current_sid();
59391da177e4SLinus Torvalds 	int rc;
59401da177e4SLinus Torvalds 
59417191adffSEric W. Biederman 	rc = ipc_alloc_security(shp, SECCLASS_SHM);
59421da177e4SLinus Torvalds 	if (rc)
59431da177e4SLinus Torvalds 		return rc;
59441da177e4SLinus Torvalds 
59457191adffSEric W. Biederman 	isec = shp->security;
59461da177e4SLinus Torvalds 
594750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59487191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
59491da177e4SLinus Torvalds 
59506b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
59516b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SHM,
59521da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
59531da177e4SLinus Torvalds 	if (rc) {
59547191adffSEric W. Biederman 		ipc_free_security(shp);
59551da177e4SLinus Torvalds 		return rc;
59561da177e4SLinus Torvalds 	}
59571da177e4SLinus Torvalds 	return 0;
59581da177e4SLinus Torvalds }
59591da177e4SLinus Torvalds 
59607191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp)
59611da177e4SLinus Torvalds {
59627191adffSEric W. Biederman 	ipc_free_security(shp);
59631da177e4SLinus Torvalds }
59641da177e4SLinus Torvalds 
59657191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
59661da177e4SLinus Torvalds {
59671da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
59682bf49690SThomas Liu 	struct common_audit_data ad;
5969275bb41eSDavid Howells 	u32 sid = current_sid();
59701da177e4SLinus Torvalds 
59717191adffSEric W. Biederman 	isec = shp->security;
59721da177e4SLinus Torvalds 
597350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
59747191adffSEric W. Biederman 	ad.u.ipc_id = shp->key;
59751da177e4SLinus Torvalds 
59766b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
59776b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SHM,
59781da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
59791da177e4SLinus Torvalds }
59801da177e4SLinus Torvalds 
59811da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
59827191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
59831da177e4SLinus Torvalds {
59841da177e4SLinus Torvalds 	int perms;
59851da177e4SLinus Torvalds 	int err;
59861da177e4SLinus Torvalds 
59871da177e4SLinus Torvalds 	switch (cmd) {
59881da177e4SLinus Torvalds 	case IPC_INFO:
59891da177e4SLinus Torvalds 	case SHM_INFO:
59901da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
59916b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
59926b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
5993be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
59941da177e4SLinus Torvalds 	case IPC_STAT:
59951da177e4SLinus Torvalds 	case SHM_STAT:
5996c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
59971da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
59981da177e4SLinus Torvalds 		break;
59991da177e4SLinus Torvalds 	case IPC_SET:
60001da177e4SLinus Torvalds 		perms = SHM__SETATTR;
60011da177e4SLinus Torvalds 		break;
60021da177e4SLinus Torvalds 	case SHM_LOCK:
60031da177e4SLinus Torvalds 	case SHM_UNLOCK:
60041da177e4SLinus Torvalds 		perms = SHM__LOCK;
60051da177e4SLinus Torvalds 		break;
60061da177e4SLinus Torvalds 	case IPC_RMID:
60071da177e4SLinus Torvalds 		perms = SHM__DESTROY;
60081da177e4SLinus Torvalds 		break;
60091da177e4SLinus Torvalds 	default:
60101da177e4SLinus Torvalds 		return 0;
60111da177e4SLinus Torvalds 	}
60121da177e4SLinus Torvalds 
60137191adffSEric W. Biederman 	err = ipc_has_perm(shp, perms);
60141da177e4SLinus Torvalds 	return err;
60151da177e4SLinus Torvalds }
60161da177e4SLinus Torvalds 
60177191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp,
60181da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
60191da177e4SLinus Torvalds {
60201da177e4SLinus Torvalds 	u32 perms;
60211da177e4SLinus Torvalds 
60221da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
60231da177e4SLinus Torvalds 		perms = SHM__READ;
60241da177e4SLinus Torvalds 	else
60251da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
60261da177e4SLinus Torvalds 
60277191adffSEric W. Biederman 	return ipc_has_perm(shp, perms);
60281da177e4SLinus Torvalds }
60291da177e4SLinus Torvalds 
60301da177e4SLinus Torvalds /* Semaphore security operations */
6031aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
60321da177e4SLinus Torvalds {
60331da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60342bf49690SThomas Liu 	struct common_audit_data ad;
6035275bb41eSDavid Howells 	u32 sid = current_sid();
60361da177e4SLinus Torvalds 	int rc;
60371da177e4SLinus Torvalds 
6038aefad959SEric W. Biederman 	rc = ipc_alloc_security(sma, SECCLASS_SEM);
60391da177e4SLinus Torvalds 	if (rc)
60401da177e4SLinus Torvalds 		return rc;
60411da177e4SLinus Torvalds 
6042aefad959SEric W. Biederman 	isec = sma->security;
60431da177e4SLinus Torvalds 
604450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6045aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
60461da177e4SLinus Torvalds 
60476b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
60486b6bc620SStephen Smalley 			  sid, isec->sid, SECCLASS_SEM,
60491da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
60501da177e4SLinus Torvalds 	if (rc) {
6051aefad959SEric W. Biederman 		ipc_free_security(sma);
60521da177e4SLinus Torvalds 		return rc;
60531da177e4SLinus Torvalds 	}
60541da177e4SLinus Torvalds 	return 0;
60551da177e4SLinus Torvalds }
60561da177e4SLinus Torvalds 
6057aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma)
60581da177e4SLinus Torvalds {
6059aefad959SEric W. Biederman 	ipc_free_security(sma);
60601da177e4SLinus Torvalds }
60611da177e4SLinus Torvalds 
6062aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
60631da177e4SLinus Torvalds {
60641da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
60652bf49690SThomas Liu 	struct common_audit_data ad;
6066275bb41eSDavid Howells 	u32 sid = current_sid();
60671da177e4SLinus Torvalds 
6068aefad959SEric W. Biederman 	isec = sma->security;
60691da177e4SLinus Torvalds 
607050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
6071aefad959SEric W. Biederman 	ad.u.ipc_id = sma->key;
60721da177e4SLinus Torvalds 
60736b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
60746b6bc620SStephen Smalley 			    sid, isec->sid, SECCLASS_SEM,
60751da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
60761da177e4SLinus Torvalds }
60771da177e4SLinus Torvalds 
60781da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
6079aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
60801da177e4SLinus Torvalds {
60811da177e4SLinus Torvalds 	int err;
60821da177e4SLinus Torvalds 	u32 perms;
60831da177e4SLinus Torvalds 
60841da177e4SLinus Torvalds 	switch (cmd) {
60851da177e4SLinus Torvalds 	case IPC_INFO:
60861da177e4SLinus Torvalds 	case SEM_INFO:
60871da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
60886b6bc620SStephen Smalley 		return avc_has_perm(&selinux_state,
60896b6bc620SStephen Smalley 				    current_sid(), SECINITSID_KERNEL,
6090be0554c9SStephen Smalley 				    SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
60911da177e4SLinus Torvalds 	case GETPID:
60921da177e4SLinus Torvalds 	case GETNCNT:
60931da177e4SLinus Torvalds 	case GETZCNT:
60941da177e4SLinus Torvalds 		perms = SEM__GETATTR;
60951da177e4SLinus Torvalds 		break;
60961da177e4SLinus Torvalds 	case GETVAL:
60971da177e4SLinus Torvalds 	case GETALL:
60981da177e4SLinus Torvalds 		perms = SEM__READ;
60991da177e4SLinus Torvalds 		break;
61001da177e4SLinus Torvalds 	case SETVAL:
61011da177e4SLinus Torvalds 	case SETALL:
61021da177e4SLinus Torvalds 		perms = SEM__WRITE;
61031da177e4SLinus Torvalds 		break;
61041da177e4SLinus Torvalds 	case IPC_RMID:
61051da177e4SLinus Torvalds 		perms = SEM__DESTROY;
61061da177e4SLinus Torvalds 		break;
61071da177e4SLinus Torvalds 	case IPC_SET:
61081da177e4SLinus Torvalds 		perms = SEM__SETATTR;
61091da177e4SLinus Torvalds 		break;
61101da177e4SLinus Torvalds 	case IPC_STAT:
61111da177e4SLinus Torvalds 	case SEM_STAT:
6112a280d6dcSDavidlohr Bueso 	case SEM_STAT_ANY:
61131da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
61141da177e4SLinus Torvalds 		break;
61151da177e4SLinus Torvalds 	default:
61161da177e4SLinus Torvalds 		return 0;
61171da177e4SLinus Torvalds 	}
61181da177e4SLinus Torvalds 
6119aefad959SEric W. Biederman 	err = ipc_has_perm(sma, perms);
61201da177e4SLinus Torvalds 	return err;
61211da177e4SLinus Torvalds }
61221da177e4SLinus Torvalds 
6123aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma,
61241da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
61251da177e4SLinus Torvalds {
61261da177e4SLinus Torvalds 	u32 perms;
61271da177e4SLinus Torvalds 
61281da177e4SLinus Torvalds 	if (alter)
61291da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
61301da177e4SLinus Torvalds 	else
61311da177e4SLinus Torvalds 		perms = SEM__READ;
61321da177e4SLinus Torvalds 
6133aefad959SEric W. Biederman 	return ipc_has_perm(sma, perms);
61341da177e4SLinus Torvalds }
61351da177e4SLinus Torvalds 
61361da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
61371da177e4SLinus Torvalds {
61381da177e4SLinus Torvalds 	u32 av = 0;
61391da177e4SLinus Torvalds 
61401da177e4SLinus Torvalds 	av = 0;
61411da177e4SLinus Torvalds 	if (flag & S_IRUGO)
61421da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
61431da177e4SLinus Torvalds 	if (flag & S_IWUGO)
61441da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
61451da177e4SLinus Torvalds 
61461da177e4SLinus Torvalds 	if (av == 0)
61471da177e4SLinus Torvalds 		return 0;
61481da177e4SLinus Torvalds 
61496af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
61501da177e4SLinus Torvalds }
61511da177e4SLinus Torvalds 
6152713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6153713a04aeSAhmed S. Darwish {
6154713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
6155713a04aeSAhmed S. Darwish 	*secid = isec->sid;
6156713a04aeSAhmed S. Darwish }
6157713a04aeSAhmed S. Darwish 
61581da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
61591da177e4SLinus Torvalds {
61601da177e4SLinus Torvalds 	if (inode)
61611da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
61621da177e4SLinus Torvalds }
61631da177e4SLinus Torvalds 
61641da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
616504ff9708SAl Viro 			       char *name, char **value)
61661da177e4SLinus Torvalds {
6167275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
61688c8570fbSDustin Kirkland 	u32 sid;
61691da177e4SLinus Torvalds 	int error;
617004ff9708SAl Viro 	unsigned len;
61711da177e4SLinus Torvalds 
6172275bb41eSDavid Howells 	rcu_read_lock();
6173275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
61741da177e4SLinus Torvalds 
6175be0554c9SStephen Smalley 	if (current != p) {
61766b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
61776b6bc620SStephen Smalley 				     current_sid(), __tsec->sid,
6178be0554c9SStephen Smalley 				     SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6179be0554c9SStephen Smalley 		if (error)
6180be0554c9SStephen Smalley 			goto bad;
6181be0554c9SStephen Smalley 	}
6182be0554c9SStephen Smalley 
61831da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
6184275bb41eSDavid Howells 		sid = __tsec->sid;
61851da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
6186275bb41eSDavid Howells 		sid = __tsec->osid;
61871da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
6188275bb41eSDavid Howells 		sid = __tsec->exec_sid;
61891da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
6190275bb41eSDavid Howells 		sid = __tsec->create_sid;
61914eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
6192275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
619342c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
6194275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
6195be0554c9SStephen Smalley 	else {
6196be0554c9SStephen Smalley 		error = -EINVAL;
6197be0554c9SStephen Smalley 		goto bad;
6198be0554c9SStephen Smalley 	}
6199275bb41eSDavid Howells 	rcu_read_unlock();
62001da177e4SLinus Torvalds 
62011da177e4SLinus Torvalds 	if (!sid)
62021da177e4SLinus Torvalds 		return 0;
62031da177e4SLinus Torvalds 
6204aa8e712cSStephen Smalley 	error = security_sid_to_context(&selinux_state, sid, value, &len);
620504ff9708SAl Viro 	if (error)
620604ff9708SAl Viro 		return error;
620704ff9708SAl Viro 	return len;
6208275bb41eSDavid Howells 
6209be0554c9SStephen Smalley bad:
6210275bb41eSDavid Howells 	rcu_read_unlock();
6211be0554c9SStephen Smalley 	return error;
62121da177e4SLinus Torvalds }
62131da177e4SLinus Torvalds 
6214b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size)
62151da177e4SLinus Torvalds {
62161da177e4SLinus Torvalds 	struct task_security_struct *tsec;
6217d84f4f99SDavid Howells 	struct cred *new;
6218be0554c9SStephen Smalley 	u32 mysid = current_sid(), sid = 0, ptsid;
62191da177e4SLinus Torvalds 	int error;
62201da177e4SLinus Torvalds 	char *str = value;
62211da177e4SLinus Torvalds 
62221da177e4SLinus Torvalds 	/*
62231da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
62241da177e4SLinus Torvalds 	 */
62251da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
62266b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62276b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6228be0554c9SStephen Smalley 				     PROCESS__SETEXEC, NULL);
62291da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
62306b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62316b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6232be0554c9SStephen Smalley 				     PROCESS__SETFSCREATE, NULL);
62334eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
62346b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62356b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6236be0554c9SStephen Smalley 				     PROCESS__SETKEYCREATE, NULL);
623742c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
62386b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62396b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6240be0554c9SStephen Smalley 				     PROCESS__SETSOCKCREATE, NULL);
62411da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
62426b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
62436b6bc620SStephen Smalley 				     mysid, mysid, SECCLASS_PROCESS,
6244be0554c9SStephen Smalley 				     PROCESS__SETCURRENT, NULL);
62451da177e4SLinus Torvalds 	else
62461da177e4SLinus Torvalds 		error = -EINVAL;
62471da177e4SLinus Torvalds 	if (error)
62481da177e4SLinus Torvalds 		return error;
62491da177e4SLinus Torvalds 
62501da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
6251a050a570SStephen Smalley 	if (size && str[0] && str[0] != '\n') {
62521da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
62531da177e4SLinus Torvalds 			str[size-1] = 0;
62541da177e4SLinus Torvalds 			size--;
62551da177e4SLinus Torvalds 		}
6256aa8e712cSStephen Smalley 		error = security_context_to_sid(&selinux_state, value, size,
6257aa8e712cSStephen Smalley 						&sid, GFP_KERNEL);
625812b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
6259db59000aSStephen Smalley 			if (!has_cap_mac_admin(true)) {
6260d6ea83ecSEric Paris 				struct audit_buffer *ab;
6261d6ea83ecSEric Paris 				size_t audit_size;
6262d6ea83ecSEric Paris 
6263d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
6264d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
6265d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
6266d6ea83ecSEric Paris 					audit_size = size - 1;
6267d6ea83ecSEric Paris 				else
6268d6ea83ecSEric Paris 					audit_size = size;
6269cdfb6b34SRichard Guy Briggs 				ab = audit_log_start(audit_context(),
6270cdfb6b34SRichard Guy Briggs 						     GFP_ATOMIC,
6271cdfb6b34SRichard Guy Briggs 						     AUDIT_SELINUX_ERR);
6272d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
6273d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
6274d6ea83ecSEric Paris 				audit_log_end(ab);
6275d6ea83ecSEric Paris 
627612b29f34SStephen Smalley 				return error;
6277d6ea83ecSEric Paris 			}
6278aa8e712cSStephen Smalley 			error = security_context_to_sid_force(
6279aa8e712cSStephen Smalley 						      &selinux_state,
6280aa8e712cSStephen Smalley 						      value, size, &sid);
628112b29f34SStephen Smalley 		}
62821da177e4SLinus Torvalds 		if (error)
62831da177e4SLinus Torvalds 			return error;
62841da177e4SLinus Torvalds 	}
62851da177e4SLinus Torvalds 
6286d84f4f99SDavid Howells 	new = prepare_creds();
6287d84f4f99SDavid Howells 	if (!new)
6288d84f4f99SDavid Howells 		return -ENOMEM;
6289d84f4f99SDavid Howells 
62901da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
62911da177e4SLinus Torvalds 	   performed during the actual operation (execve,
62921da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
6293d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
62941da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
62951da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
6296d84f4f99SDavid Howells 	tsec = new->security;
6297d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
62981da177e4SLinus Torvalds 		tsec->exec_sid = sid;
6299d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
63001da177e4SLinus Torvalds 		tsec->create_sid = sid;
6301d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
63026b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63036b6bc620SStephen Smalley 				     mysid, sid, SECCLASS_KEY, KEY__CREATE,
6304be0554c9SStephen Smalley 				     NULL);
63054eb582cfSMichael LeMay 		if (error)
6306d84f4f99SDavid Howells 			goto abort_change;
63074eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
6308d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
630942c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
6310d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
6311d84f4f99SDavid Howells 		error = -EINVAL;
63121da177e4SLinus Torvalds 		if (sid == 0)
6313d84f4f99SDavid Howells 			goto abort_change;
6314d9250deaSKaiGai Kohei 
6315d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
6316d84f4f99SDavid Howells 		error = -EPERM;
63175bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
6318aa8e712cSStephen Smalley 			error = security_bounded_transition(&selinux_state,
6319aa8e712cSStephen Smalley 							    tsec->sid, sid);
6320d84f4f99SDavid Howells 			if (error)
6321d84f4f99SDavid Howells 				goto abort_change;
63221da177e4SLinus Torvalds 		}
63231da177e4SLinus Torvalds 
63241da177e4SLinus Torvalds 		/* Check permissions for the transition. */
63256b6bc620SStephen Smalley 		error = avc_has_perm(&selinux_state,
63266b6bc620SStephen Smalley 				     tsec->sid, sid, SECCLASS_PROCESS,
63271da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
63281da177e4SLinus Torvalds 		if (error)
6329d84f4f99SDavid Howells 			goto abort_change;
63301da177e4SLinus Torvalds 
63311da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
63321da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
6333be0554c9SStephen Smalley 		ptsid = ptrace_parent_sid();
63340c6181cbSPaul Moore 		if (ptsid != 0) {
63356b6bc620SStephen Smalley 			error = avc_has_perm(&selinux_state,
63366b6bc620SStephen Smalley 					     ptsid, sid, SECCLASS_PROCESS,
6337d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
6338d84f4f99SDavid Howells 			if (error)
6339d84f4f99SDavid Howells 				goto abort_change;
6340d84f4f99SDavid Howells 		}
6341d84f4f99SDavid Howells 
6342d84f4f99SDavid Howells 		tsec->sid = sid;
6343d84f4f99SDavid Howells 	} else {
6344d84f4f99SDavid Howells 		error = -EINVAL;
6345d84f4f99SDavid Howells 		goto abort_change;
6346d84f4f99SDavid Howells 	}
6347d84f4f99SDavid Howells 
6348d84f4f99SDavid Howells 	commit_creds(new);
63491da177e4SLinus Torvalds 	return size;
6350d84f4f99SDavid Howells 
6351d84f4f99SDavid Howells abort_change:
6352d84f4f99SDavid Howells 	abort_creds(new);
6353d84f4f99SDavid Howells 	return error;
63541da177e4SLinus Torvalds }
63551da177e4SLinus Torvalds 
6356746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
6357746df9b5SDavid Quigley {
6358746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6359746df9b5SDavid Quigley }
6360746df9b5SDavid Quigley 
6361dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6362dc49c1f9SCatherine Zhang {
6363aa8e712cSStephen Smalley 	return security_sid_to_context(&selinux_state, secid,
6364aa8e712cSStephen Smalley 				       secdata, seclen);
6365dc49c1f9SCatherine Zhang }
6366dc49c1f9SCatherine Zhang 
63677bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
636863cb3449SDavid Howells {
6369aa8e712cSStephen Smalley 	return security_context_to_sid(&selinux_state, secdata, seclen,
6370aa8e712cSStephen Smalley 				       secid, GFP_KERNEL);
637163cb3449SDavid Howells }
637263cb3449SDavid Howells 
6373dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
6374dc49c1f9SCatherine Zhang {
6375dc49c1f9SCatherine Zhang 	kfree(secdata);
6376dc49c1f9SCatherine Zhang }
6377dc49c1f9SCatherine Zhang 
63786f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode)
63796f3be9f5SAndreas Gruenbacher {
63806f3be9f5SAndreas Gruenbacher 	struct inode_security_struct *isec = inode->i_security;
63816f3be9f5SAndreas Gruenbacher 
63829287aed2SAndreas Gruenbacher 	spin_lock(&isec->lock);
63836f3be9f5SAndreas Gruenbacher 	isec->initialized = LABEL_INVALID;
63849287aed2SAndreas Gruenbacher 	spin_unlock(&isec->lock);
63856f3be9f5SAndreas Gruenbacher }
63866f3be9f5SAndreas Gruenbacher 
63871ee65e37SDavid P. Quigley /*
63881ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
63891ee65e37SDavid P. Quigley  */
63901ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
63911ee65e37SDavid P. Quigley {
63921ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
63931ee65e37SDavid P. Quigley }
63941ee65e37SDavid P. Quigley 
63951ee65e37SDavid P. Quigley /*
63961ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
63971ee65e37SDavid P. Quigley  */
63981ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
63991ee65e37SDavid P. Quigley {
64001ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
64011ee65e37SDavid P. Quigley }
64021ee65e37SDavid P. Quigley 
64031ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
64041ee65e37SDavid P. Quigley {
64051ee65e37SDavid P. Quigley 	int len = 0;
64061ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
64071ee65e37SDavid P. Quigley 						ctx, true);
64081ee65e37SDavid P. Quigley 	if (len < 0)
64091ee65e37SDavid P. Quigley 		return len;
64101ee65e37SDavid P. Quigley 	*ctxlen = len;
64111ee65e37SDavid P. Quigley 	return 0;
64121ee65e37SDavid P. Quigley }
6413d720024eSMichael LeMay #ifdef CONFIG_KEYS
6414d720024eSMichael LeMay 
6415d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
64167e047ef5SDavid Howells 			     unsigned long flags)
6417d720024eSMichael LeMay {
6418d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
6419d720024eSMichael LeMay 	struct key_security_struct *ksec;
6420d720024eSMichael LeMay 
6421d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6422d720024eSMichael LeMay 	if (!ksec)
6423d720024eSMichael LeMay 		return -ENOMEM;
6424d720024eSMichael LeMay 
6425d84f4f99SDavid Howells 	tsec = cred->security;
6426d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
6427d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
64284eb582cfSMichael LeMay 	else
6429d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
6430d720024eSMichael LeMay 
6431275bb41eSDavid Howells 	k->security = ksec;
6432d720024eSMichael LeMay 	return 0;
6433d720024eSMichael LeMay }
6434d720024eSMichael LeMay 
6435d720024eSMichael LeMay static void selinux_key_free(struct key *k)
6436d720024eSMichael LeMay {
6437d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
6438d720024eSMichael LeMay 
6439d720024eSMichael LeMay 	k->security = NULL;
6440d720024eSMichael LeMay 	kfree(ksec);
6441d720024eSMichael LeMay }
6442d720024eSMichael LeMay 
6443d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
6444d84f4f99SDavid Howells 				  const struct cred *cred,
6445f5895943SDavid Howells 				  unsigned perm)
6446d720024eSMichael LeMay {
6447d720024eSMichael LeMay 	struct key *key;
6448d720024eSMichael LeMay 	struct key_security_struct *ksec;
6449275bb41eSDavid Howells 	u32 sid;
6450d720024eSMichael LeMay 
6451d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
6452d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
6453d720024eSMichael LeMay 	   appear to be created. */
6454d720024eSMichael LeMay 	if (perm == 0)
6455d720024eSMichael LeMay 		return 0;
6456d720024eSMichael LeMay 
6457d84f4f99SDavid Howells 	sid = cred_sid(cred);
6458275bb41eSDavid Howells 
6459275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
6460275bb41eSDavid Howells 	ksec = key->security;
6461275bb41eSDavid Howells 
64626b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
64636b6bc620SStephen Smalley 			    sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6464d720024eSMichael LeMay }
6465d720024eSMichael LeMay 
646670a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
646770a5bb72SDavid Howells {
646870a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
646970a5bb72SDavid Howells 	char *context = NULL;
647070a5bb72SDavid Howells 	unsigned len;
647170a5bb72SDavid Howells 	int rc;
647270a5bb72SDavid Howells 
6473aa8e712cSStephen Smalley 	rc = security_sid_to_context(&selinux_state, ksec->sid,
6474aa8e712cSStephen Smalley 				     &context, &len);
647570a5bb72SDavid Howells 	if (!rc)
647670a5bb72SDavid Howells 		rc = len;
647770a5bb72SDavid Howells 	*_buffer = context;
647870a5bb72SDavid Howells 	return rc;
647970a5bb72SDavid Howells }
64803a976fa6SDaniel Jurgens #endif
648170a5bb72SDavid Howells 
64823a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6483cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6484cfc4d882SDaniel Jurgens {
6485cfc4d882SDaniel Jurgens 	struct common_audit_data ad;
6486cfc4d882SDaniel Jurgens 	int err;
6487cfc4d882SDaniel Jurgens 	u32 sid = 0;
6488cfc4d882SDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6489cfc4d882SDaniel Jurgens 	struct lsm_ibpkey_audit ibpkey;
6490cfc4d882SDaniel Jurgens 
6491409dcf31SDaniel Jurgens 	err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6492cfc4d882SDaniel Jurgens 	if (err)
6493cfc4d882SDaniel Jurgens 		return err;
6494cfc4d882SDaniel Jurgens 
6495cfc4d882SDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBPKEY;
6496cfc4d882SDaniel Jurgens 	ibpkey.subnet_prefix = subnet_prefix;
6497cfc4d882SDaniel Jurgens 	ibpkey.pkey = pkey_val;
6498cfc4d882SDaniel Jurgens 	ad.u.ibpkey = &ibpkey;
64996b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65006b6bc620SStephen Smalley 			    sec->sid, sid,
6501cfc4d882SDaniel Jurgens 			    SECCLASS_INFINIBAND_PKEY,
6502cfc4d882SDaniel Jurgens 			    INFINIBAND_PKEY__ACCESS, &ad);
6503cfc4d882SDaniel Jurgens }
6504cfc4d882SDaniel Jurgens 
6505ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6506ab861dfcSDaniel Jurgens 					    u8 port_num)
6507ab861dfcSDaniel Jurgens {
6508ab861dfcSDaniel Jurgens 	struct common_audit_data ad;
6509ab861dfcSDaniel Jurgens 	int err;
6510ab861dfcSDaniel Jurgens 	u32 sid = 0;
6511ab861dfcSDaniel Jurgens 	struct ib_security_struct *sec = ib_sec;
6512ab861dfcSDaniel Jurgens 	struct lsm_ibendport_audit ibendport;
6513ab861dfcSDaniel Jurgens 
6514aa8e712cSStephen Smalley 	err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6515aa8e712cSStephen Smalley 				      &sid);
6516ab861dfcSDaniel Jurgens 
6517ab861dfcSDaniel Jurgens 	if (err)
6518ab861dfcSDaniel Jurgens 		return err;
6519ab861dfcSDaniel Jurgens 
6520ab861dfcSDaniel Jurgens 	ad.type = LSM_AUDIT_DATA_IBENDPORT;
6521ab861dfcSDaniel Jurgens 	strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6522ab861dfcSDaniel Jurgens 	ibendport.port = port_num;
6523ab861dfcSDaniel Jurgens 	ad.u.ibendport = &ibendport;
65246b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
65256b6bc620SStephen Smalley 			    sec->sid, sid,
6526ab861dfcSDaniel Jurgens 			    SECCLASS_INFINIBAND_ENDPORT,
6527ab861dfcSDaniel Jurgens 			    INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6528ab861dfcSDaniel Jurgens }
6529ab861dfcSDaniel Jurgens 
65303a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec)
65313a976fa6SDaniel Jurgens {
65323a976fa6SDaniel Jurgens 	struct ib_security_struct *sec;
65333a976fa6SDaniel Jurgens 
65343a976fa6SDaniel Jurgens 	sec = kzalloc(sizeof(*sec), GFP_KERNEL);
65353a976fa6SDaniel Jurgens 	if (!sec)
65363a976fa6SDaniel Jurgens 		return -ENOMEM;
65373a976fa6SDaniel Jurgens 	sec->sid = current_sid();
65383a976fa6SDaniel Jurgens 
65393a976fa6SDaniel Jurgens 	*ib_sec = sec;
65403a976fa6SDaniel Jurgens 	return 0;
65413a976fa6SDaniel Jurgens }
65423a976fa6SDaniel Jurgens 
65433a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec)
65443a976fa6SDaniel Jurgens {
65453a976fa6SDaniel Jurgens 	kfree(ib_sec);
65463a976fa6SDaniel Jurgens }
6547d720024eSMichael LeMay #endif
6548d720024eSMichael LeMay 
6549ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6550ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr,
6551ec27c356SChenbo Feng 				     unsigned int size)
6552ec27c356SChenbo Feng {
6553ec27c356SChenbo Feng 	u32 sid = current_sid();
6554ec27c356SChenbo Feng 	int ret;
6555ec27c356SChenbo Feng 
6556ec27c356SChenbo Feng 	switch (cmd) {
6557ec27c356SChenbo Feng 	case BPF_MAP_CREATE:
65586b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
65596b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6560ec27c356SChenbo Feng 				   NULL);
6561ec27c356SChenbo Feng 		break;
6562ec27c356SChenbo Feng 	case BPF_PROG_LOAD:
65636b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
65646b6bc620SStephen Smalley 				   sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6565ec27c356SChenbo Feng 				   NULL);
6566ec27c356SChenbo Feng 		break;
6567ec27c356SChenbo Feng 	default:
6568ec27c356SChenbo Feng 		ret = 0;
6569ec27c356SChenbo Feng 		break;
6570ec27c356SChenbo Feng 	}
6571ec27c356SChenbo Feng 
6572ec27c356SChenbo Feng 	return ret;
6573ec27c356SChenbo Feng }
6574ec27c356SChenbo Feng 
6575ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode)
6576ec27c356SChenbo Feng {
6577ec27c356SChenbo Feng 	u32 av = 0;
6578ec27c356SChenbo Feng 
6579ec27c356SChenbo Feng 	if (fmode & FMODE_READ)
6580ec27c356SChenbo Feng 		av |= BPF__MAP_READ;
6581ec27c356SChenbo Feng 	if (fmode & FMODE_WRITE)
6582ec27c356SChenbo Feng 		av |= BPF__MAP_WRITE;
6583ec27c356SChenbo Feng 	return av;
6584ec27c356SChenbo Feng }
6585ec27c356SChenbo Feng 
6586f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see
6587f66e448cSChenbo Feng  * if it is a bpf related object. And apply correspinding checks on the bpf
6588f66e448cSChenbo Feng  * object based on the type. The bpf maps and programs, not like other files and
6589f66e448cSChenbo Feng  * socket, are using a shared anonymous inode inside the kernel as their inode.
6590f66e448cSChenbo Feng  * So checking that inode cannot identify if the process have privilege to
6591f66e448cSChenbo Feng  * access the bpf object and that's why we have to add this additional check in
6592f66e448cSChenbo Feng  * selinux_file_receive and selinux_binder_transfer_files.
6593f66e448cSChenbo Feng  */
6594f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid)
6595f66e448cSChenbo Feng {
6596f66e448cSChenbo Feng 	struct bpf_security_struct *bpfsec;
6597f66e448cSChenbo Feng 	struct bpf_prog *prog;
6598f66e448cSChenbo Feng 	struct bpf_map *map;
6599f66e448cSChenbo Feng 	int ret;
6600f66e448cSChenbo Feng 
6601f66e448cSChenbo Feng 	if (file->f_op == &bpf_map_fops) {
6602f66e448cSChenbo Feng 		map = file->private_data;
6603f66e448cSChenbo Feng 		bpfsec = map->security;
66046b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66056b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6606f66e448cSChenbo Feng 				   bpf_map_fmode_to_av(file->f_mode), NULL);
6607f66e448cSChenbo Feng 		if (ret)
6608f66e448cSChenbo Feng 			return ret;
6609f66e448cSChenbo Feng 	} else if (file->f_op == &bpf_prog_fops) {
6610f66e448cSChenbo Feng 		prog = file->private_data;
6611f66e448cSChenbo Feng 		bpfsec = prog->aux->security;
66126b6bc620SStephen Smalley 		ret = avc_has_perm(&selinux_state,
66136b6bc620SStephen Smalley 				   sid, bpfsec->sid, SECCLASS_BPF,
6614f66e448cSChenbo Feng 				   BPF__PROG_RUN, NULL);
6615f66e448cSChenbo Feng 		if (ret)
6616f66e448cSChenbo Feng 			return ret;
6617f66e448cSChenbo Feng 	}
6618f66e448cSChenbo Feng 	return 0;
6619f66e448cSChenbo Feng }
6620f66e448cSChenbo Feng 
6621ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6622ec27c356SChenbo Feng {
6623ec27c356SChenbo Feng 	u32 sid = current_sid();
6624ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6625ec27c356SChenbo Feng 
6626ec27c356SChenbo Feng 	bpfsec = map->security;
66276b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66286b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6629ec27c356SChenbo Feng 			    bpf_map_fmode_to_av(fmode), NULL);
6630ec27c356SChenbo Feng }
6631ec27c356SChenbo Feng 
6632ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog)
6633ec27c356SChenbo Feng {
6634ec27c356SChenbo Feng 	u32 sid = current_sid();
6635ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6636ec27c356SChenbo Feng 
6637ec27c356SChenbo Feng 	bpfsec = prog->aux->security;
66386b6bc620SStephen Smalley 	return avc_has_perm(&selinux_state,
66396b6bc620SStephen Smalley 			    sid, bpfsec->sid, SECCLASS_BPF,
6640ec27c356SChenbo Feng 			    BPF__PROG_RUN, NULL);
6641ec27c356SChenbo Feng }
6642ec27c356SChenbo Feng 
6643ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map)
6644ec27c356SChenbo Feng {
6645ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6646ec27c356SChenbo Feng 
6647ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6648ec27c356SChenbo Feng 	if (!bpfsec)
6649ec27c356SChenbo Feng 		return -ENOMEM;
6650ec27c356SChenbo Feng 
6651ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6652ec27c356SChenbo Feng 	map->security = bpfsec;
6653ec27c356SChenbo Feng 
6654ec27c356SChenbo Feng 	return 0;
6655ec27c356SChenbo Feng }
6656ec27c356SChenbo Feng 
6657ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map)
6658ec27c356SChenbo Feng {
6659ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = map->security;
6660ec27c356SChenbo Feng 
6661ec27c356SChenbo Feng 	map->security = NULL;
6662ec27c356SChenbo Feng 	kfree(bpfsec);
6663ec27c356SChenbo Feng }
6664ec27c356SChenbo Feng 
6665ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6666ec27c356SChenbo Feng {
6667ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec;
6668ec27c356SChenbo Feng 
6669ec27c356SChenbo Feng 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6670ec27c356SChenbo Feng 	if (!bpfsec)
6671ec27c356SChenbo Feng 		return -ENOMEM;
6672ec27c356SChenbo Feng 
6673ec27c356SChenbo Feng 	bpfsec->sid = current_sid();
6674ec27c356SChenbo Feng 	aux->security = bpfsec;
6675ec27c356SChenbo Feng 
6676ec27c356SChenbo Feng 	return 0;
6677ec27c356SChenbo Feng }
6678ec27c356SChenbo Feng 
6679ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6680ec27c356SChenbo Feng {
6681ec27c356SChenbo Feng 	struct bpf_security_struct *bpfsec = aux->security;
6682ec27c356SChenbo Feng 
6683ec27c356SChenbo Feng 	aux->security = NULL;
6684ec27c356SChenbo Feng 	kfree(bpfsec);
6685ec27c356SChenbo Feng }
6686ec27c356SChenbo Feng #endif
6687ec27c356SChenbo Feng 
6688ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6689e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6690e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6691e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6692e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6693076c54c5SAhmed S. Darwish 
6694e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6695e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6696e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
6697e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
6698e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
6699e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
6700e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
6701e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
6702e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
670379af7307SStephen Smalley 
6704e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
67051da177e4SLinus Torvalds 
6706e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6707e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6708e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
67091da177e4SLinus Torvalds 
6710e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6711e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
67125b400239SAl Viro 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
6713204cc0ccSAl Viro 	LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6714e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6715e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6716e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6717e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6718e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
6719e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
6720e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6721e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6722757cbe59SAl Viro 	LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
67231da177e4SLinus Torvalds 
6724e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6725a518b0a5SVivek Goyal 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6726e0007529SEric Paris 
6727e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6728e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6729e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6730e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
6731e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
6732e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6733e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6734e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6735e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6736e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6737e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6738e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6739e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6740e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6741e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6742e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6743e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6744e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6745e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6746e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6747e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6748e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6749e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6750e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6751e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
675256909eb3SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
675319472b69SVivek Goyal 	LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
67541da177e4SLinus Torvalds 
6755e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
6756e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6757e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6758e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6759e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6760e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6761e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6762e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
6763e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6764e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6765e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6766e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
67671da177e4SLinus Torvalds 
6768e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
67691da177e4SLinus Torvalds 
6770a79be238STetsuo Handa 	LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6771e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6772e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, selinux_cred_free),
6773e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6774e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
67753ec30113SMatthew Garrett 	LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6776e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6777e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6778e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6779c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
678061d612eaSJeff Vander Stoep 	LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6781e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6782e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6783e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6784e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6785e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6786e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6787e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6788791ec491SStephen Smalley 	LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6789e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6790e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6791e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6792e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6793e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
6794e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6795788e7dd4SYuichi Nakamura 
6796e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6797e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
67981da177e4SLinus Torvalds 
6799e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6800e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
68011da177e4SLinus Torvalds 
6802e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_alloc_security,
6803e20b043aSCasey Schaufler 			selinux_msg_queue_alloc_security),
6804e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6805e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6806e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6807e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6808e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
68091da177e4SLinus Torvalds 
6810e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6811e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6812e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6813e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6814e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
68151da177e4SLinus Torvalds 
6816e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6817e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6818e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6819e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6820e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
68211da177e4SLinus Torvalds 
6822e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
68231da177e4SLinus Torvalds 
6824e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6825e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
68261da177e4SLinus Torvalds 
6827e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6828e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6829e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6830e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
68316f3be9f5SAndreas Gruenbacher 	LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6832e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6833e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6834e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
68351da177e4SLinus Torvalds 
6836e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6837e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6838dc49c1f9SCatherine Zhang 
6839e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
6840e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
68410b811db2SDavid Herrmann 	LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
6842e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6843e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6844e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6845e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6846e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6847e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6848e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6849e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6850e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6851e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6852e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6853e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6854e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
6855e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
6856e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6857e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6858e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6859e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6860e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6861e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6862d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6863d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6864d452930fSRichard Haines 	LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
6865e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6866e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6867e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6868e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6869e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6870e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6871e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6872e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6873e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6874e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6875e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6876e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6877e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
68783a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
6879cfc4d882SDaniel Jurgens 	LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
6880ab861dfcSDaniel Jurgens 	LSM_HOOK_INIT(ib_endport_manage_subnet,
6881ab861dfcSDaniel Jurgens 		      selinux_ib_endport_manage_subnet),
68823a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
68833a976fa6SDaniel Jurgens 	LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
68843a976fa6SDaniel Jurgens #endif
6885d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
6886e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6887e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6888e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6889e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6890e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6891e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6892e20b043aSCasey Schaufler 			selinux_xfrm_state_alloc_acquire),
6893e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6894e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6895e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6896e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6897e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
6898e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
68991da177e4SLinus Torvalds #endif
6900d720024eSMichael LeMay 
6901d720024eSMichael LeMay #ifdef CONFIG_KEYS
6902e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6903e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
6904e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
6905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6906d720024eSMichael LeMay #endif
69079d57a7f9SAhmed S. Darwish 
69089d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
6909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6912e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
69139d57a7f9SAhmed S. Darwish #endif
6914ec27c356SChenbo Feng 
6915ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
6916ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf, selinux_bpf),
6917ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6918ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6919ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6920ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6921ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6922ec27c356SChenbo Feng 	LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6923ec27c356SChenbo Feng #endif
69241da177e4SLinus Torvalds };
69251da177e4SLinus Torvalds 
69261da177e4SLinus Torvalds static __init int selinux_init(void)
69271da177e4SLinus Torvalds {
6928c103a91eSpeter enderborg 	pr_info("SELinux:  Initializing.\n");
69291da177e4SLinus Torvalds 
6930aa8e712cSStephen Smalley 	memset(&selinux_state, 0, sizeof(selinux_state));
6931e5a5ca96SPaul Moore 	enforcing_set(&selinux_state, selinux_enforcing_boot);
6932aa8e712cSStephen Smalley 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
6933aa8e712cSStephen Smalley 	selinux_ss_init(&selinux_state.ss);
69346b6bc620SStephen Smalley 	selinux_avc_init(&selinux_state.avc);
6935aa8e712cSStephen Smalley 
69361da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
6937d84f4f99SDavid Howells 	cred_init_security();
69381da177e4SLinus Torvalds 
6939fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6940fcaaade1SStephen Smalley 
69417cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
69427cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
694320c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
694463205654SSangwoo 	file_security_cache = kmem_cache_create("selinux_file_security",
694563205654SSangwoo 					    sizeof(struct file_security_struct),
694663205654SSangwoo 					    0, SLAB_PANIC, NULL);
69471da177e4SLinus Torvalds 	avc_init();
69481da177e4SLinus Torvalds 
6949aa8e712cSStephen Smalley 	avtab_cache_init();
6950aa8e712cSStephen Smalley 
6951aa8e712cSStephen Smalley 	ebitmap_cache_init();
6952aa8e712cSStephen Smalley 
6953aa8e712cSStephen Smalley 	hashtab_cache_init();
6954aa8e712cSStephen Smalley 
6955d69dece5SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
69561da177e4SLinus Torvalds 
6957615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6958615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
6959615e51fdSPaul Moore 
69608f408ab6SDaniel Jurgens 	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
69618f408ab6SDaniel Jurgens 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
69628f408ab6SDaniel Jurgens 
6963aa8e712cSStephen Smalley 	if (selinux_enforcing_boot)
6964c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in enforcing mode\n");
6965828dfe1dSEric Paris 	else
6966c103a91eSpeter enderborg 		pr_debug("SELinux:  Starting in permissive mode\n");
6967d720024eSMichael LeMay 
69681da177e4SLinus Torvalds 	return 0;
69691da177e4SLinus Torvalds }
69701da177e4SLinus Torvalds 
6971e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
6972e8c26255SAl Viro {
6973204cc0ccSAl Viro 	selinux_set_mnt_opts(sb, NULL, 0, NULL);
6974e8c26255SAl Viro }
6975e8c26255SAl Viro 
69761da177e4SLinus Torvalds void selinux_complete_init(void)
69771da177e4SLinus Torvalds {
6978c103a91eSpeter enderborg 	pr_debug("SELinux:  Completing initialization.\n");
69791da177e4SLinus Torvalds 
69801da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
6981c103a91eSpeter enderborg 	pr_debug("SELinux:  Setting up existing superblocks.\n");
6982e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
69831da177e4SLinus Torvalds }
69841da177e4SLinus Torvalds 
69851da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
69861da177e4SLinus Torvalds    all processes and objects when they are created. */
69873d6e5f6dSKees Cook DEFINE_LSM(selinux) = {
698807aed2f2SKees Cook 	.name = "selinux",
698914bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
6990c5459b82SKees Cook 	.enabled = &selinux_enabled,
69913d6e5f6dSKees Cook 	.init = selinux_init,
69923d6e5f6dSKees Cook };
69931da177e4SLinus Torvalds 
6994c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
69951da177e4SLinus Torvalds 
6996591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = {
6997effad8dfSPaul Moore 	{
6998effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
69992597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
70006e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
70011da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
7002effad8dfSPaul Moore 	},
7003effad8dfSPaul Moore 	{
7004effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
70052597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7006effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7007effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
7008948bf85cSPaul Moore 	},
7009948bf85cSPaul Moore 	{
7010948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
70112597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
7012948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
7013948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
701425db6beaSJiri Pirko 	},
70151a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6)
7016effad8dfSPaul Moore 	{
7017effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
70182597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
70196e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
70201da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
7021effad8dfSPaul Moore 	},
7022effad8dfSPaul Moore 	{
7023effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
70242597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
7025effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
7026effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
702725db6beaSJiri Pirko 	},
70282917f57bSHuw Davies 	{
70292917f57bSHuw Davies 		.hook =		selinux_ipv6_output,
70302917f57bSHuw Davies 		.pf =		NFPROTO_IPV6,
70312917f57bSHuw Davies 		.hooknum =	NF_INET_LOCAL_OUT,
70322917f57bSHuw Davies 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
70332917f57bSHuw Davies 	},
70341da177e4SLinus Torvalds #endif	/* IPV6 */
703525db6beaSJiri Pirko };
70361da177e4SLinus Torvalds 
70378e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net)
70388e71bf75SFlorian Westphal {
70398e71bf75SFlorian Westphal 	return nf_register_net_hooks(net, selinux_nf_ops,
70408e71bf75SFlorian Westphal 				     ARRAY_SIZE(selinux_nf_ops));
70418e71bf75SFlorian Westphal }
70428e71bf75SFlorian Westphal 
70438e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net)
70448e71bf75SFlorian Westphal {
70458e71bf75SFlorian Westphal 	nf_unregister_net_hooks(net, selinux_nf_ops,
70468e71bf75SFlorian Westphal 				ARRAY_SIZE(selinux_nf_ops));
70478e71bf75SFlorian Westphal }
70488e71bf75SFlorian Westphal 
70498e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = {
70508e71bf75SFlorian Westphal 	.init = selinux_nf_register,
70518e71bf75SFlorian Westphal 	.exit = selinux_nf_unregister,
70528e71bf75SFlorian Westphal };
70538e71bf75SFlorian Westphal 
70541da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
70551da177e4SLinus Torvalds {
705625db6beaSJiri Pirko 	int err;
70571da177e4SLinus Torvalds 
70581da177e4SLinus Torvalds 	if (!selinux_enabled)
705925db6beaSJiri Pirko 		return 0;
70601da177e4SLinus Torvalds 
7061c103a91eSpeter enderborg 	pr_debug("SELinux:  Registering netfilter hooks\n");
70621da177e4SLinus Torvalds 
70638e71bf75SFlorian Westphal 	err = register_pernet_subsys(&selinux_net_ops);
70641da177e4SLinus Torvalds 	if (err)
70658e71bf75SFlorian Westphal 		panic("SELinux: register_pernet_subsys: error %d\n", err);
70661da177e4SLinus Torvalds 
706725db6beaSJiri Pirko 	return 0;
70681da177e4SLinus Torvalds }
70691da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
70701da177e4SLinus Torvalds 
70711da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
70721da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
70731da177e4SLinus Torvalds {
7074c103a91eSpeter enderborg 	pr_debug("SELinux:  Unregistering netfilter hooks\n");
70751da177e4SLinus Torvalds 
70768e71bf75SFlorian Westphal 	unregister_pernet_subsys(&selinux_net_ops);
70771da177e4SLinus Torvalds }
70781da177e4SLinus Torvalds #endif
70791da177e4SLinus Torvalds 
7080c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
70811da177e4SLinus Torvalds 
70821da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
70831da177e4SLinus Torvalds #define selinux_nf_ip_exit()
70841da177e4SLinus Torvalds #endif
70851da177e4SLinus Torvalds 
7086c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
70871da177e4SLinus Torvalds 
70881da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7089aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state)
70901da177e4SLinus Torvalds {
7091aa8e712cSStephen Smalley 	if (state->initialized) {
70921da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
70931da177e4SLinus Torvalds 		return -EINVAL;
70941da177e4SLinus Torvalds 	}
70951da177e4SLinus Torvalds 
7096aa8e712cSStephen Smalley 	if (state->disabled) {
70971da177e4SLinus Torvalds 		/* Only do this once. */
70981da177e4SLinus Torvalds 		return -EINVAL;
70991da177e4SLinus Torvalds 	}
71001da177e4SLinus Torvalds 
7101aa8e712cSStephen Smalley 	state->disabled = 1;
7102aa8e712cSStephen Smalley 
7103c103a91eSpeter enderborg 	pr_info("SELinux:  Disabled at runtime.\n");
71041da177e4SLinus Torvalds 
710530d55280SStephen Smalley 	selinux_enabled = 0;
71061da177e4SLinus Torvalds 
7107b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
71081da177e4SLinus Torvalds 
7109af8ff049SEric Paris 	/* Try to destroy the avc node cache */
7110af8ff049SEric Paris 	avc_disable();
7111af8ff049SEric Paris 
71121da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
71131da177e4SLinus Torvalds 	selinux_nf_ip_exit();
71141da177e4SLinus Torvalds 
71151da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
71161da177e4SLinus Torvalds 	exit_sel_fs();
71171da177e4SLinus Torvalds 
71181da177e4SLinus Torvalds 	return 0;
71191da177e4SLinus Torvalds }
71201da177e4SLinus Torvalds #endif
7121