xref: /openbmc/linux/security/selinux/hooks.c (revision 9e7c8f8c62c1e1cda203b5bfaba4575b141e42e7)
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  *
61da177e4SLinus Torvalds  *  Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
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>
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
221da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
231da177e4SLinus Torvalds  *	as published by the Free Software Foundation.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/init.h>
270b24dcb7SEric Paris #include <linux/kd.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
290d094efeSRoland McGrath #include <linux/tracehook.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
311da177e4SLinus Torvalds #include <linux/sched.h>
323c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
331da177e4SLinus Torvalds #include <linux/xattr.h>
341da177e4SLinus Torvalds #include <linux/capability.h>
351da177e4SLinus Torvalds #include <linux/unistd.h>
361da177e4SLinus Torvalds #include <linux/mm.h>
371da177e4SLinus Torvalds #include <linux/mman.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds #include <linux/pagemap.h>
400b24dcb7SEric Paris #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/swap.h>
421da177e4SLinus Torvalds #include <linux/spinlock.h>
431da177e4SLinus Torvalds #include <linux/syscalls.h>
442a7dba39SEric Paris #include <linux/dcache.h>
451da177e4SLinus Torvalds #include <linux/file.h>
469f3acc31SAl Viro #include <linux/fdtable.h>
471da177e4SLinus Torvalds #include <linux/namei.h>
481da177e4SLinus Torvalds #include <linux/mount.h>
491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
501da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
511da177e4SLinus Torvalds #include <linux/tty.h>
521da177e4SLinus Torvalds #include <net/icmp.h>
53227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
541da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5547180068SPaul Moore #include <net/inet_connection_sock.h>
56220deb96SPaul Moore #include <net/net_namespace.h>
57d621d35eSPaul Moore #include <net/netlabel.h>
58f5269710SEric Paris #include <linux/uaccess.h>
591da177e4SLinus Torvalds #include <asm/ioctls.h>
6060063497SArun Sharma #include <linux/atomic.h>
611da177e4SLinus Torvalds #include <linux/bitops.h>
621da177e4SLinus Torvalds #include <linux/interrupt.h>
631da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6477954983SHong zhi guo #include <net/netlink.h>
651da177e4SLinus Torvalds #include <linux/tcp.h>
661da177e4SLinus Torvalds #include <linux/udp.h>
672ee92d46SJames Morris #include <linux/dccp.h>
681da177e4SLinus Torvalds #include <linux/quota.h>
691da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
701da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
711da177e4SLinus Torvalds #include <linux/parser.h>
721da177e4SLinus Torvalds #include <linux/nfs_mount.h>
731da177e4SLinus Torvalds #include <net/ipv6.h>
741da177e4SLinus Torvalds #include <linux/hugetlb.h>
751da177e4SLinus Torvalds #include <linux/personality.h>
761da177e4SLinus Torvalds #include <linux/audit.h>
776931dfc9SEric Paris #include <linux/string.h>
78877ce7c1SCatherine Zhang #include <linux/selinux.h>
7923970741SEric Paris #include <linux/mutex.h>
80f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8100234592SKees Cook #include <linux/syslog.h>
823486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8344fc7ea0SPaul Gortmaker #include <linux/export.h>
8440401530SAl Viro #include <linux/msg.h>
8540401530SAl Viro #include <linux/shm.h>
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #include "avc.h"
881da177e4SLinus Torvalds #include "objsec.h"
891da177e4SLinus Torvalds #include "netif.h"
90224dfbd8SPaul Moore #include "netnode.h"
913e112172SPaul Moore #include "netport.h"
92d28d1e08STrent Jaeger #include "xfrm.h"
93c60475bfSPaul Moore #include "netlabel.h"
949d57a7f9SAhmed S. Darwish #include "audit.h"
957b98a585SJames Morris #include "avc_ss.h"
961da177e4SLinus Torvalds 
97d621d35eSPaul Moore /* SECMARK reference count */
9856a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
99d621d35eSPaul Moore 
1001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
101828dfe1dSEric Paris int selinux_enforcing;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1041da177e4SLinus Torvalds {
105f5269710SEric Paris 	unsigned long enforcing;
10629707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
107f5269710SEric Paris 		selinux_enforcing = enforcing ? 1 : 0;
1081da177e4SLinus Torvalds 	return 1;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1141da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1171da177e4SLinus Torvalds {
118f5269710SEric Paris 	unsigned long enabled;
11929707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
120f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1211da177e4SLinus Torvalds 	return 1;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
12430d55280SStephen Smalley #else
12530d55280SStephen Smalley int selinux_enabled = 1;
1261da177e4SLinus Torvalds #endif
1271da177e4SLinus Torvalds 
128e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1297cae7e26SJames Morris 
130d621d35eSPaul Moore /**
131d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
132d621d35eSPaul Moore  *
133d621d35eSPaul Moore  * Description:
134d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
135d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
136d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1372be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1382be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
139d621d35eSPaul Moore  *
140d621d35eSPaul Moore  */
141d621d35eSPaul Moore static int selinux_secmark_enabled(void)
142d621d35eSPaul Moore {
1432be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
1442be4d74fSChris PeBenito }
1452be4d74fSChris PeBenito 
1462be4d74fSChris PeBenito /**
1472be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1482be4d74fSChris PeBenito  *
1492be4d74fSChris PeBenito  * Description:
1502be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1512be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1522be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1532be4d74fSChris PeBenito  * is always considered enabled.
1542be4d74fSChris PeBenito  *
1552be4d74fSChris PeBenito  */
1562be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1572be4d74fSChris PeBenito {
1582be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
159d621d35eSPaul Moore }
160d621d35eSPaul Moore 
161615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
162615e51fdSPaul Moore {
163615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
164615e51fdSPaul Moore 		sel_netif_flush();
165615e51fdSPaul Moore 		sel_netnode_flush();
166615e51fdSPaul Moore 		sel_netport_flush();
167615e51fdSPaul Moore 		synchronize_net();
168615e51fdSPaul Moore 	}
169615e51fdSPaul Moore 	return 0;
170615e51fdSPaul Moore }
171615e51fdSPaul Moore 
172d84f4f99SDavid Howells /*
173d84f4f99SDavid Howells  * initialise the security for the init task
174d84f4f99SDavid Howells  */
175d84f4f99SDavid Howells static void cred_init_security(void)
1761da177e4SLinus Torvalds {
1773b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1781da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1791da177e4SLinus Torvalds 
18089d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1811da177e4SLinus Torvalds 	if (!tsec)
182d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1831da177e4SLinus Torvalds 
184d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
185f1752eecSDavid Howells 	cred->security = tsec;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188275bb41eSDavid Howells /*
18988e67f3bSDavid Howells  * get the security ID of a set of credentials
19088e67f3bSDavid Howells  */
19188e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
19288e67f3bSDavid Howells {
19388e67f3bSDavid Howells 	const struct task_security_struct *tsec;
19488e67f3bSDavid Howells 
19588e67f3bSDavid Howells 	tsec = cred->security;
19688e67f3bSDavid Howells 	return tsec->sid;
19788e67f3bSDavid Howells }
19888e67f3bSDavid Howells 
19988e67f3bSDavid Howells /*
2003b11a1deSDavid Howells  * get the objective security ID of a task
201275bb41eSDavid Howells  */
202275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
203275bb41eSDavid Howells {
204275bb41eSDavid Howells 	u32 sid;
205275bb41eSDavid Howells 
206275bb41eSDavid Howells 	rcu_read_lock();
20788e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
208275bb41eSDavid Howells 	rcu_read_unlock();
209275bb41eSDavid Howells 	return sid;
210275bb41eSDavid Howells }
211275bb41eSDavid Howells 
212275bb41eSDavid Howells /*
2133b11a1deSDavid Howells  * get the subjective security ID of the current task
214275bb41eSDavid Howells  */
215275bb41eSDavid Howells static inline u32 current_sid(void)
216275bb41eSDavid Howells {
2175fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
218275bb41eSDavid Howells 
219275bb41eSDavid Howells 	return tsec->sid;
220275bb41eSDavid Howells }
221275bb41eSDavid Howells 
22288e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
22388e67f3bSDavid Howells 
2241da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	struct inode_security_struct *isec;
227275bb41eSDavid Howells 	u32 sid = current_sid();
2281da177e4SLinus Torvalds 
229a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2301da177e4SLinus Torvalds 	if (!isec)
2311da177e4SLinus Torvalds 		return -ENOMEM;
2321da177e4SLinus Torvalds 
23323970741SEric Paris 	mutex_init(&isec->lock);
2341da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2351da177e4SLinus Torvalds 	isec->inode = inode;
2361da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2371da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
238275bb41eSDavid Howells 	isec->task_sid = sid;
2391da177e4SLinus Torvalds 	inode->i_security = isec;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	return 0;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2443dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head)
2453dc91d43SSteven Rostedt {
2463dc91d43SSteven Rostedt 	struct inode_security_struct *isec;
2473dc91d43SSteven Rostedt 
2483dc91d43SSteven Rostedt 	isec = container_of(head, struct inode_security_struct, rcu);
2493dc91d43SSteven Rostedt 	kmem_cache_free(sel_inode_cache, isec);
2503dc91d43SSteven Rostedt }
2513dc91d43SSteven Rostedt 
2521da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2551da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2581da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2591da177e4SLinus Torvalds 		list_del_init(&isec->list);
2601da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2611da177e4SLinus Torvalds 
2623dc91d43SSteven Rostedt 	/*
2633dc91d43SSteven Rostedt 	 * The inode may still be referenced in a path walk and
2643dc91d43SSteven Rostedt 	 * a call to selinux_inode_permission() can be made
2653dc91d43SSteven Rostedt 	 * after inode_free_security() is called. Ideally, the VFS
2663dc91d43SSteven Rostedt 	 * wouldn't do this, but fixing that is a much harder
2673dc91d43SSteven Rostedt 	 * job. For now, simply free the i_security via RCU, and
2683dc91d43SSteven Rostedt 	 * leave the current inode->i_security pointer intact.
2693dc91d43SSteven Rostedt 	 * The inode will be freed after the RCU grace period too.
2703dc91d43SSteven Rostedt 	 */
2713dc91d43SSteven Rostedt 	call_rcu(&isec->rcu, inode_free_rcu);
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct file_security_struct *fsec;
277275bb41eSDavid Howells 	u32 sid = current_sid();
2781da177e4SLinus Torvalds 
27926d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2801da177e4SLinus Torvalds 	if (!fsec)
2811da177e4SLinus Torvalds 		return -ENOMEM;
2821da177e4SLinus Torvalds 
283275bb41eSDavid Howells 	fsec->sid = sid;
284275bb41eSDavid Howells 	fsec->fown_sid = sid;
2851da177e4SLinus Torvalds 	file->f_security = fsec;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void file_free_security(struct file *file)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2931da177e4SLinus Torvalds 	file->f_security = NULL;
2941da177e4SLinus Torvalds 	kfree(fsec);
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
3001da177e4SLinus Torvalds 
30189d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
3021da177e4SLinus Torvalds 	if (!sbsec)
3031da177e4SLinus Torvalds 		return -ENOMEM;
3041da177e4SLinus Torvalds 
305bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
3061da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
3071da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
3081da177e4SLinus Torvalds 	sbsec->sb = sb;
3091da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
3101da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
311c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
3121da177e4SLinus Torvalds 	sb->s_security = sbsec;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	return 0;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3201da177e4SLinus Torvalds 	sb->s_security = NULL;
3211da177e4SLinus Torvalds 	kfree(sbsec);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3251da177e4SLinus Torvalds 
326eb9ae686SDavid Quigley static const char *labeling_behaviors[7] = {
3271da177e4SLinus Torvalds 	"uses xattr",
3281da177e4SLinus Torvalds 	"uses transition SIDs",
3291da177e4SLinus Torvalds 	"uses task SIDs",
3301da177e4SLinus Torvalds 	"uses genfs_contexts",
3311da177e4SLinus Torvalds 	"not configured for labeling",
3321da177e4SLinus Torvalds 	"uses mountpoint labeling",
333eb9ae686SDavid Quigley 	"uses native labeling",
3341da177e4SLinus Torvalds };
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds enum {
34431e87930SEric Paris 	Opt_error = -1,
3451da177e4SLinus Torvalds 	Opt_context = 1,
3461da177e4SLinus Torvalds 	Opt_fscontext = 2,
347c9180a57SEric Paris 	Opt_defcontext = 3,
348c9180a57SEric Paris 	Opt_rootcontext = 4,
34911689d47SDavid P. Quigley 	Opt_labelsupport = 5,
350d355987fSEric Paris 	Opt_nextmntopt = 6,
3511da177e4SLinus Torvalds };
3521da177e4SLinus Torvalds 
353d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
354d355987fSEric Paris 
355a447c093SSteven Whitehouse static const match_table_t tokens = {
356832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
357832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
358832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
359832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
36011689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
36131e87930SEric Paris 	{Opt_error, NULL},
3621da177e4SLinus Torvalds };
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3651da177e4SLinus Torvalds 
366c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
367c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
368275bb41eSDavid Howells 			const struct cred *cred)
369c312feb2SEric Paris {
370275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
371c312feb2SEric Paris 	int rc;
372c312feb2SEric Paris 
373c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
374c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
375c312feb2SEric Paris 	if (rc)
376c312feb2SEric Paris 		return rc;
377c312feb2SEric Paris 
378c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
379c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
380c312feb2SEric Paris 	return rc;
381c312feb2SEric Paris }
382c312feb2SEric Paris 
3830808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3840808925eSEric Paris 			struct superblock_security_struct *sbsec,
385275bb41eSDavid Howells 			const struct cred *cred)
3860808925eSEric Paris {
387275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3880808925eSEric Paris 	int rc;
3890808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3900808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3910808925eSEric Paris 	if (rc)
3920808925eSEric Paris 		return rc;
3930808925eSEric Paris 
3940808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3950808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3960808925eSEric Paris 	return rc;
3970808925eSEric Paris }
3980808925eSEric Paris 
399b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
400b43e725dSEric Paris {
401b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
402b43e725dSEric Paris 
403d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
404b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
405d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
406d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
407d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
408d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
409d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
410d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "rootfs");
411b43e725dSEric Paris }
412b43e725dSEric Paris 
413c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4161da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
417c6f493d6SDavid Howells 	struct inode *root_inode = d_backing_inode(root);
4181da177e4SLinus Torvalds 	int rc = 0;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
4211da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
4221da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
4231da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
4241da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
4251da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
426c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
42729b1deb2SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
42829b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
4291da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
4301da177e4SLinus Torvalds 			goto out;
4311da177e4SLinus Torvalds 		}
432c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
4331da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
4341da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
4351da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
43629b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
43729b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
4381da177e4SLinus Torvalds 			else
4391da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
44029b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
44129b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
4421da177e4SLinus Torvalds 			goto out;
4431da177e4SLinus Torvalds 		}
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
446c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
44729b1deb2SLinus Torvalds 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
44829b1deb2SLinus Torvalds 		       sb->s_id, sb->s_type->name);
4491da177e4SLinus Torvalds 
450eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
451b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
45212f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
453ddd29ec6SDavid P. Quigley 
4541da177e4SLinus Torvalds 	/* Initialize the root inode. */
455c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4581da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4591da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4601da177e4SLinus Torvalds 	   populates itself. */
4611da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4621da177e4SLinus Torvalds next_inode:
4631da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4641da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4651da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4661da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4671da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
468923190d3SStephen Smalley 		list_del_init(&isec->list);
4691da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4701da177e4SLinus Torvalds 		inode = igrab(inode);
4711da177e4SLinus Torvalds 		if (inode) {
4721da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4731da177e4SLinus Torvalds 				inode_doinit(inode);
4741da177e4SLinus Torvalds 			iput(inode);
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4771da177e4SLinus Torvalds 		goto next_inode;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4801da177e4SLinus Torvalds out:
481c9180a57SEric Paris 	return rc;
482c9180a57SEric Paris }
483c9180a57SEric Paris 
484c9180a57SEric Paris /*
485c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
486c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
487c9180a57SEric Paris  * mount options, or whatever.
488c9180a57SEric Paris  */
489c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
490e0007529SEric Paris 				struct security_mnt_opts *opts)
491c9180a57SEric Paris {
492c9180a57SEric Paris 	int rc = 0, i;
493c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
494c9180a57SEric Paris 	char *context = NULL;
495c9180a57SEric Paris 	u32 len;
496c9180a57SEric Paris 	char tmp;
497c9180a57SEric Paris 
498e0007529SEric Paris 	security_init_mnt_opts(opts);
499c9180a57SEric Paris 
5000d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
501c9180a57SEric Paris 		return -EINVAL;
502c9180a57SEric Paris 
503c9180a57SEric Paris 	if (!ss_initialized)
504c9180a57SEric Paris 		return -EINVAL;
505c9180a57SEric Paris 
506af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
507af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
508af8e50ccSEric Paris 
5090d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
510c9180a57SEric Paris 	/* count the number of mount options for this sb */
511af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
512c9180a57SEric Paris 		if (tmp & 0x01)
513e0007529SEric Paris 			opts->num_mnt_opts++;
514c9180a57SEric Paris 		tmp >>= 1;
515c9180a57SEric Paris 	}
51611689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
5170b4bdb35SEric Paris 	if (sbsec->flags & SBLABEL_MNT)
51811689d47SDavid P. Quigley 		opts->num_mnt_opts++;
519c9180a57SEric Paris 
520e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
521e0007529SEric Paris 	if (!opts->mnt_opts) {
522c9180a57SEric Paris 		rc = -ENOMEM;
523c9180a57SEric Paris 		goto out_free;
524c9180a57SEric Paris 	}
525c9180a57SEric Paris 
526e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
527e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
528c9180a57SEric Paris 		rc = -ENOMEM;
529c9180a57SEric Paris 		goto out_free;
530c9180a57SEric Paris 	}
531c9180a57SEric Paris 
532c9180a57SEric Paris 	i = 0;
533c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
534c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
535c9180a57SEric Paris 		if (rc)
536c9180a57SEric Paris 			goto out_free;
537e0007529SEric Paris 		opts->mnt_opts[i] = context;
538e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
539c9180a57SEric Paris 	}
540c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
541c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
542c9180a57SEric Paris 		if (rc)
543c9180a57SEric Paris 			goto out_free;
544e0007529SEric Paris 		opts->mnt_opts[i] = context;
545e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
546c9180a57SEric Paris 	}
547c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
548c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
549c9180a57SEric Paris 		if (rc)
550c9180a57SEric Paris 			goto out_free;
551e0007529SEric Paris 		opts->mnt_opts[i] = context;
552e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
553c9180a57SEric Paris 	}
554c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
555c6f493d6SDavid Howells 		struct inode *root = d_backing_inode(sbsec->sb->s_root);
556c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
557c9180a57SEric Paris 
558c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
559c9180a57SEric Paris 		if (rc)
560c9180a57SEric Paris 			goto out_free;
561e0007529SEric Paris 		opts->mnt_opts[i] = context;
562e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
563c9180a57SEric Paris 	}
56412f348b9SEric Paris 	if (sbsec->flags & SBLABEL_MNT) {
56511689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
56612f348b9SEric Paris 		opts->mnt_opts_flags[i++] = SBLABEL_MNT;
56711689d47SDavid P. Quigley 	}
568c9180a57SEric Paris 
569e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
570c9180a57SEric Paris 
571c9180a57SEric Paris 	return 0;
572c9180a57SEric Paris 
573c9180a57SEric Paris out_free:
574e0007529SEric Paris 	security_free_mnt_opts(opts);
575c9180a57SEric Paris 	return rc;
576c9180a57SEric Paris }
577c9180a57SEric Paris 
578c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
579c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
580c9180a57SEric Paris {
5810d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5820d90a7ecSDavid P. Quigley 
583c9180a57SEric Paris 	/* check if the old mount command had the same options */
5840d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
585c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
586c9180a57SEric Paris 		    (old_sid != new_sid))
587c9180a57SEric Paris 			return 1;
588c9180a57SEric Paris 
589c9180a57SEric Paris 	/* check if we were passed the same options twice,
590c9180a57SEric Paris 	 * aka someone passed context=a,context=b
591c9180a57SEric Paris 	 */
5920d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5930d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
594c9180a57SEric Paris 			return 1;
595c9180a57SEric Paris 	return 0;
596c9180a57SEric Paris }
597e0007529SEric Paris 
598c9180a57SEric Paris /*
599c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
600c9180a57SEric Paris  * labeling information.
601c9180a57SEric Paris  */
602e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
603649f6e77SDavid Quigley 				struct security_mnt_opts *opts,
604649f6e77SDavid Quigley 				unsigned long kern_flags,
605649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
606c9180a57SEric Paris {
607275bb41eSDavid Howells 	const struct cred *cred = current_cred();
608c9180a57SEric Paris 	int rc = 0, i;
609c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
61029b1deb2SLinus Torvalds 	const char *name = sb->s_type->name;
611c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(sbsec->sb->s_root);
612089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
613c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
614c9180a57SEric Paris 	u32 defcontext_sid = 0;
615e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
616e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
617e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
618c9180a57SEric Paris 
619c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
620c9180a57SEric Paris 
621c9180a57SEric Paris 	if (!ss_initialized) {
622c9180a57SEric Paris 		if (!num_opts) {
623c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
624c9180a57SEric Paris 			   after the initial policy is loaded and the security
625c9180a57SEric Paris 			   server is ready to handle calls. */
626c9180a57SEric Paris 			goto out;
627c9180a57SEric Paris 		}
628c9180a57SEric Paris 		rc = -EINVAL;
629744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
630744ba35eSEric Paris 			"before the security server is initialized\n");
631c9180a57SEric Paris 		goto out;
632c9180a57SEric Paris 	}
633649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
634649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
635649f6e77SDavid Quigley 		 * place the results is not allowed */
636649f6e77SDavid Quigley 		rc = -EINVAL;
637649f6e77SDavid Quigley 		goto out;
638649f6e77SDavid Quigley 	}
639c9180a57SEric Paris 
640c9180a57SEric Paris 	/*
641e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
642e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
643e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
644e0007529SEric Paris 	 * we need to skip the double mount verification.
645e0007529SEric Paris 	 *
646e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
647e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
648e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
649e0007529SEric Paris 	 * will be used for both mounts)
650e0007529SEric Paris 	 */
6510d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
652e0007529SEric Paris 	    && (num_opts == 0))
653e0007529SEric Paris 		goto out;
654e0007529SEric Paris 
655e0007529SEric Paris 	/*
656c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
657c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
658c9180a57SEric Paris 	 * than once with different security options.
659c9180a57SEric Paris 	 */
660c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
661c9180a57SEric Paris 		u32 sid;
66211689d47SDavid P. Quigley 
66312f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
66411689d47SDavid P. Quigley 			continue;
665c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
66652a4c640SNikolay Aleksandrov 					     strlen(mount_options[i]), &sid, GFP_KERNEL);
667c9180a57SEric Paris 		if (rc) {
668c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
66929b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
67029b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, name, rc);
671c9180a57SEric Paris 			goto out;
672c9180a57SEric Paris 		}
673c9180a57SEric Paris 		switch (flags[i]) {
674c9180a57SEric Paris 		case FSCONTEXT_MNT:
675c9180a57SEric Paris 			fscontext_sid = sid;
676c9180a57SEric Paris 
677c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
678c9180a57SEric Paris 					fscontext_sid))
679c9180a57SEric Paris 				goto out_double_mount;
680c9180a57SEric Paris 
681c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
682c9180a57SEric Paris 			break;
683c9180a57SEric Paris 		case CONTEXT_MNT:
684c9180a57SEric Paris 			context_sid = sid;
685c9180a57SEric Paris 
686c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
687c9180a57SEric Paris 					context_sid))
688c9180a57SEric Paris 				goto out_double_mount;
689c9180a57SEric Paris 
690c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
691c9180a57SEric Paris 			break;
692c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
693c9180a57SEric Paris 			rootcontext_sid = sid;
694c9180a57SEric Paris 
695c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
696c9180a57SEric Paris 					rootcontext_sid))
697c9180a57SEric Paris 				goto out_double_mount;
698c9180a57SEric Paris 
699c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
700c9180a57SEric Paris 
701c9180a57SEric Paris 			break;
702c9180a57SEric Paris 		case DEFCONTEXT_MNT:
703c9180a57SEric Paris 			defcontext_sid = sid;
704c9180a57SEric Paris 
705c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
706c9180a57SEric Paris 					defcontext_sid))
707c9180a57SEric Paris 				goto out_double_mount;
708c9180a57SEric Paris 
709c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
710c9180a57SEric Paris 
711c9180a57SEric Paris 			break;
712c9180a57SEric Paris 		default:
713c9180a57SEric Paris 			rc = -EINVAL;
714c9180a57SEric Paris 			goto out;
715c9180a57SEric Paris 		}
716c9180a57SEric Paris 	}
717c9180a57SEric Paris 
7180d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
719c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
7200d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
721c9180a57SEric Paris 			goto out_double_mount;
722c9180a57SEric Paris 		rc = 0;
723c9180a57SEric Paris 		goto out;
724c9180a57SEric Paris 	}
725c9180a57SEric Paris 
726089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
7270d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
728c9180a57SEric Paris 
729eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
730eb9ae686SDavid Quigley 		/*
731eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
732eb9ae686SDavid Quigley 		 * filesystem type.
733eb9ae686SDavid Quigley 		 */
734a64c54cfSEric Paris 		rc = security_fs_use(sb);
735c9180a57SEric Paris 		if (rc) {
736eb9ae686SDavid Quigley 			printk(KERN_WARNING
737eb9ae686SDavid Quigley 				"%s: security_fs_use(%s) returned %d\n",
738089be43eSJames Morris 					__func__, sb->s_type->name, rc);
739c9180a57SEric Paris 			goto out;
740c9180a57SEric Paris 		}
741eb9ae686SDavid Quigley 	}
742c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
743c9180a57SEric Paris 	if (fscontext_sid) {
744275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
745c9180a57SEric Paris 		if (rc)
746c9180a57SEric Paris 			goto out;
747c9180a57SEric Paris 
748c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
749c9180a57SEric Paris 	}
750c9180a57SEric Paris 
751c9180a57SEric Paris 	/*
752c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
753c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
754c9180a57SEric Paris 	 * the superblock context if not already set.
755c9180a57SEric Paris 	 */
756eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
757eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
758eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
759eb9ae686SDavid Quigley 	}
760eb9ae686SDavid Quigley 
761c9180a57SEric Paris 	if (context_sid) {
762c9180a57SEric Paris 		if (!fscontext_sid) {
763275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
764275bb41eSDavid Howells 							  cred);
765c9180a57SEric Paris 			if (rc)
766c9180a57SEric Paris 				goto out;
767c9180a57SEric Paris 			sbsec->sid = context_sid;
768c9180a57SEric Paris 		} else {
769275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
770275bb41eSDavid Howells 							     cred);
771c9180a57SEric Paris 			if (rc)
772c9180a57SEric Paris 				goto out;
773c9180a57SEric Paris 		}
774c9180a57SEric Paris 		if (!rootcontext_sid)
775c9180a57SEric Paris 			rootcontext_sid = context_sid;
776c9180a57SEric Paris 
777c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
778c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
779c9180a57SEric Paris 	}
780c9180a57SEric Paris 
781c9180a57SEric Paris 	if (rootcontext_sid) {
782275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
783275bb41eSDavid Howells 						     cred);
784c9180a57SEric Paris 		if (rc)
785c9180a57SEric Paris 			goto out;
786c9180a57SEric Paris 
787c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
788c9180a57SEric Paris 		root_isec->initialized = 1;
789c9180a57SEric Paris 	}
790c9180a57SEric Paris 
791c9180a57SEric Paris 	if (defcontext_sid) {
792eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
793eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
794c9180a57SEric Paris 			rc = -EINVAL;
795c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
796c9180a57SEric Paris 			       "invalid for this filesystem type\n");
797c9180a57SEric Paris 			goto out;
798c9180a57SEric Paris 		}
799c9180a57SEric Paris 
800c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
801c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
802275bb41eSDavid Howells 							     sbsec, cred);
803c9180a57SEric Paris 			if (rc)
804c9180a57SEric Paris 				goto out;
805c9180a57SEric Paris 		}
806c9180a57SEric Paris 
807c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
808c9180a57SEric Paris 	}
809c9180a57SEric Paris 
810c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
811c9180a57SEric Paris out:
812bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8131da177e4SLinus Torvalds 	return rc;
814c9180a57SEric Paris out_double_mount:
815c9180a57SEric Paris 	rc = -EINVAL;
816c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
81729b1deb2SLinus Torvalds 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
818c9180a57SEric Paris 	goto out;
819c9180a57SEric Paris }
820c9180a57SEric Paris 
821094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
822094f7b69SJeff Layton 				    const struct super_block *newsb)
823094f7b69SJeff Layton {
824094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
825094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
826094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
827094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
828094f7b69SJeff Layton 
829094f7b69SJeff Layton 	if (oldflags != newflags)
830094f7b69SJeff Layton 		goto mismatch;
831094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
832094f7b69SJeff Layton 		goto mismatch;
833094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
834094f7b69SJeff Layton 		goto mismatch;
835094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
836094f7b69SJeff Layton 		goto mismatch;
837094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
838c6f493d6SDavid Howells 		struct inode_security_struct *oldroot = d_backing_inode(oldsb->s_root)->i_security;
839c6f493d6SDavid Howells 		struct inode_security_struct *newroot = d_backing_inode(newsb->s_root)->i_security;
840094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
841094f7b69SJeff Layton 			goto mismatch;
842094f7b69SJeff Layton 	}
843094f7b69SJeff Layton 	return 0;
844094f7b69SJeff Layton mismatch:
845094f7b69SJeff Layton 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
846094f7b69SJeff Layton 			    "different security settings for (dev %s, "
847094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
848094f7b69SJeff Layton 	return -EBUSY;
849094f7b69SJeff Layton }
850094f7b69SJeff Layton 
851094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
852c9180a57SEric Paris 					struct super_block *newsb)
853c9180a57SEric Paris {
854c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
855c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
856c9180a57SEric Paris 
857c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
858c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
859c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
860c9180a57SEric Paris 
8610f5e6420SEric Paris 	/*
8620f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
863e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
8640f5e6420SEric Paris 	 */
865e8c26255SAl Viro 	if (!ss_initialized)
866094f7b69SJeff Layton 		return 0;
867c9180a57SEric Paris 
868c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8690d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
870c9180a57SEric Paris 
871094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
8720d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
873094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
8745a552617SEric Paris 
875c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
876c9180a57SEric Paris 
877c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
878c9180a57SEric Paris 
879c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
880c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
881c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
882c9180a57SEric Paris 
883c9180a57SEric Paris 	if (set_context) {
884c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
885c9180a57SEric Paris 
886c9180a57SEric Paris 		if (!set_fscontext)
887c9180a57SEric Paris 			newsbsec->sid = sid;
888c9180a57SEric Paris 		if (!set_rootcontext) {
889c6f493d6SDavid Howells 			struct inode *newinode = d_backing_inode(newsb->s_root);
890c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
891c9180a57SEric Paris 			newisec->sid = sid;
892c9180a57SEric Paris 		}
893c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
894c9180a57SEric Paris 	}
895c9180a57SEric Paris 	if (set_rootcontext) {
896c6f493d6SDavid Howells 		const struct inode *oldinode = d_backing_inode(oldsb->s_root);
897c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
898c6f493d6SDavid Howells 		struct inode *newinode = d_backing_inode(newsb->s_root);
899c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
900c9180a57SEric Paris 
901c9180a57SEric Paris 		newisec->sid = oldisec->sid;
902c9180a57SEric Paris 	}
903c9180a57SEric Paris 
904c9180a57SEric Paris 	sb_finish_set_opts(newsb);
905c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
906094f7b69SJeff Layton 	return 0;
907c9180a57SEric Paris }
908c9180a57SEric Paris 
9092e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
9102e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
911c9180a57SEric Paris {
912e0007529SEric Paris 	char *p;
913c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
914c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
915e0007529SEric Paris 	int rc, num_mnt_opts = 0;
916c9180a57SEric Paris 
917e0007529SEric Paris 	opts->num_mnt_opts = 0;
918c9180a57SEric Paris 
919c9180a57SEric Paris 	/* Standard string-based options. */
920c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
921c9180a57SEric Paris 		int token;
922c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
923c9180a57SEric Paris 
924c9180a57SEric Paris 		if (!*p)
925c9180a57SEric Paris 			continue;
926c9180a57SEric Paris 
927c9180a57SEric Paris 		token = match_token(p, tokens, args);
928c9180a57SEric Paris 
929c9180a57SEric Paris 		switch (token) {
930c9180a57SEric Paris 		case Opt_context:
931c9180a57SEric Paris 			if (context || defcontext) {
932c9180a57SEric Paris 				rc = -EINVAL;
933c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
934c9180a57SEric Paris 				goto out_err;
935c9180a57SEric Paris 			}
936c9180a57SEric Paris 			context = match_strdup(&args[0]);
937c9180a57SEric Paris 			if (!context) {
938c9180a57SEric Paris 				rc = -ENOMEM;
939c9180a57SEric Paris 				goto out_err;
940c9180a57SEric Paris 			}
941c9180a57SEric Paris 			break;
942c9180a57SEric Paris 
943c9180a57SEric Paris 		case Opt_fscontext:
944c9180a57SEric Paris 			if (fscontext) {
945c9180a57SEric Paris 				rc = -EINVAL;
946c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
947c9180a57SEric Paris 				goto out_err;
948c9180a57SEric Paris 			}
949c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
950c9180a57SEric Paris 			if (!fscontext) {
951c9180a57SEric Paris 				rc = -ENOMEM;
952c9180a57SEric Paris 				goto out_err;
953c9180a57SEric Paris 			}
954c9180a57SEric Paris 			break;
955c9180a57SEric Paris 
956c9180a57SEric Paris 		case Opt_rootcontext:
957c9180a57SEric Paris 			if (rootcontext) {
958c9180a57SEric Paris 				rc = -EINVAL;
959c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
960c9180a57SEric Paris 				goto out_err;
961c9180a57SEric Paris 			}
962c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
963c9180a57SEric Paris 			if (!rootcontext) {
964c9180a57SEric Paris 				rc = -ENOMEM;
965c9180a57SEric Paris 				goto out_err;
966c9180a57SEric Paris 			}
967c9180a57SEric Paris 			break;
968c9180a57SEric Paris 
969c9180a57SEric Paris 		case Opt_defcontext:
970c9180a57SEric Paris 			if (context || defcontext) {
971c9180a57SEric Paris 				rc = -EINVAL;
972c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
973c9180a57SEric Paris 				goto out_err;
974c9180a57SEric Paris 			}
975c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
976c9180a57SEric Paris 			if (!defcontext) {
977c9180a57SEric Paris 				rc = -ENOMEM;
978c9180a57SEric Paris 				goto out_err;
979c9180a57SEric Paris 			}
980c9180a57SEric Paris 			break;
98111689d47SDavid P. Quigley 		case Opt_labelsupport:
98211689d47SDavid P. Quigley 			break;
983c9180a57SEric Paris 		default:
984c9180a57SEric Paris 			rc = -EINVAL;
985c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
986c9180a57SEric Paris 			goto out_err;
987c9180a57SEric Paris 
988c9180a57SEric Paris 		}
989c9180a57SEric Paris 	}
990c9180a57SEric Paris 
991e0007529SEric Paris 	rc = -ENOMEM;
992e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
993e0007529SEric Paris 	if (!opts->mnt_opts)
994e0007529SEric Paris 		goto out_err;
995e0007529SEric Paris 
996e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
997e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
998e0007529SEric Paris 		kfree(opts->mnt_opts);
999e0007529SEric Paris 		goto out_err;
1000c9180a57SEric Paris 	}
1001c9180a57SEric Paris 
1002e0007529SEric Paris 	if (fscontext) {
1003e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
1004e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1005e0007529SEric Paris 	}
1006e0007529SEric Paris 	if (context) {
1007e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
1008e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1009e0007529SEric Paris 	}
1010e0007529SEric Paris 	if (rootcontext) {
1011e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
1012e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1013e0007529SEric Paris 	}
1014e0007529SEric Paris 	if (defcontext) {
1015e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
1016e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1017e0007529SEric Paris 	}
1018e0007529SEric Paris 
1019e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
1020e0007529SEric Paris 	return 0;
1021e0007529SEric Paris 
1022c9180a57SEric Paris out_err:
1023c9180a57SEric Paris 	kfree(context);
1024c9180a57SEric Paris 	kfree(defcontext);
1025c9180a57SEric Paris 	kfree(fscontext);
1026c9180a57SEric Paris 	kfree(rootcontext);
1027c9180a57SEric Paris 	return rc;
10281da177e4SLinus Torvalds }
1029e0007529SEric Paris /*
1030e0007529SEric Paris  * string mount options parsing and call set the sbsec
1031e0007529SEric Paris  */
1032e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
1033e0007529SEric Paris {
1034e0007529SEric Paris 	int rc = 0;
1035e0007529SEric Paris 	char *options = data;
1036e0007529SEric Paris 	struct security_mnt_opts opts;
1037e0007529SEric Paris 
1038e0007529SEric Paris 	security_init_mnt_opts(&opts);
1039e0007529SEric Paris 
1040e0007529SEric Paris 	if (!data)
1041e0007529SEric Paris 		goto out;
1042e0007529SEric Paris 
1043e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1044e0007529SEric Paris 
1045e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1046e0007529SEric Paris 	if (rc)
1047e0007529SEric Paris 		goto out_err;
1048e0007529SEric Paris 
1049e0007529SEric Paris out:
1050649f6e77SDavid Quigley 	rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1051e0007529SEric Paris 
1052e0007529SEric Paris out_err:
1053e0007529SEric Paris 	security_free_mnt_opts(&opts);
1054e0007529SEric Paris 	return rc;
1055e0007529SEric Paris }
10561da177e4SLinus Torvalds 
10573583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
10583583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
10592069f457SEric Paris {
10602069f457SEric Paris 	int i;
10612069f457SEric Paris 	char *prefix;
10622069f457SEric Paris 
10632069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
106411689d47SDavid P. Quigley 		char *has_comma;
106511689d47SDavid P. Quigley 
106611689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
106711689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
106811689d47SDavid P. Quigley 		else
106911689d47SDavid P. Quigley 			has_comma = NULL;
10702069f457SEric Paris 
10712069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
10722069f457SEric Paris 		case CONTEXT_MNT:
10732069f457SEric Paris 			prefix = CONTEXT_STR;
10742069f457SEric Paris 			break;
10752069f457SEric Paris 		case FSCONTEXT_MNT:
10762069f457SEric Paris 			prefix = FSCONTEXT_STR;
10772069f457SEric Paris 			break;
10782069f457SEric Paris 		case ROOTCONTEXT_MNT:
10792069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
10802069f457SEric Paris 			break;
10812069f457SEric Paris 		case DEFCONTEXT_MNT:
10822069f457SEric Paris 			prefix = DEFCONTEXT_STR;
10832069f457SEric Paris 			break;
108412f348b9SEric Paris 		case SBLABEL_MNT:
108511689d47SDavid P. Quigley 			seq_putc(m, ',');
108611689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
108711689d47SDavid P. Quigley 			continue;
10882069f457SEric Paris 		default:
10892069f457SEric Paris 			BUG();
1090a35c6c83SEric Paris 			return;
10912069f457SEric Paris 		};
10922069f457SEric Paris 		/* we need a comma before each option */
10932069f457SEric Paris 		seq_putc(m, ',');
10942069f457SEric Paris 		seq_puts(m, prefix);
10952069f457SEric Paris 		if (has_comma)
10962069f457SEric Paris 			seq_putc(m, '\"');
10972069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
10982069f457SEric Paris 		if (has_comma)
10992069f457SEric Paris 			seq_putc(m, '\"');
11002069f457SEric Paris 	}
11012069f457SEric Paris }
11022069f457SEric Paris 
11032069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
11042069f457SEric Paris {
11052069f457SEric Paris 	struct security_mnt_opts opts;
11062069f457SEric Paris 	int rc;
11072069f457SEric Paris 
11082069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1109383795c2SEric Paris 	if (rc) {
1110383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1111383795c2SEric Paris 		if (rc == -EINVAL)
1112383795c2SEric Paris 			rc = 0;
11132069f457SEric Paris 		return rc;
1114383795c2SEric Paris 	}
11152069f457SEric Paris 
11162069f457SEric Paris 	selinux_write_opts(m, &opts);
11172069f457SEric Paris 
11182069f457SEric Paris 	security_free_mnt_opts(&opts);
11192069f457SEric Paris 
11202069f457SEric Paris 	return rc;
11212069f457SEric Paris }
11222069f457SEric Paris 
11231da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11241da177e4SLinus Torvalds {
11251da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11261da177e4SLinus Torvalds 	case S_IFSOCK:
11271da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11281da177e4SLinus Torvalds 	case S_IFLNK:
11291da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11301da177e4SLinus Torvalds 	case S_IFREG:
11311da177e4SLinus Torvalds 		return SECCLASS_FILE;
11321da177e4SLinus Torvalds 	case S_IFBLK:
11331da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11341da177e4SLinus Torvalds 	case S_IFDIR:
11351da177e4SLinus Torvalds 		return SECCLASS_DIR;
11361da177e4SLinus Torvalds 	case S_IFCHR:
11371da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11381da177e4SLinus Torvalds 	case S_IFIFO:
11391da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	return SECCLASS_FILE;
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
114613402580SJames Morris static inline int default_protocol_stream(int protocol)
114713402580SJames Morris {
114813402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
114913402580SJames Morris }
115013402580SJames Morris 
115113402580SJames Morris static inline int default_protocol_dgram(int protocol)
115213402580SJames Morris {
115313402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
115413402580SJames Morris }
115513402580SJames Morris 
11561da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	switch (family) {
11591da177e4SLinus Torvalds 	case PF_UNIX:
11601da177e4SLinus Torvalds 		switch (type) {
11611da177e4SLinus Torvalds 		case SOCK_STREAM:
11621da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11631da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11641da177e4SLinus Torvalds 		case SOCK_DGRAM:
11651da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11661da177e4SLinus Torvalds 		}
11671da177e4SLinus Torvalds 		break;
11681da177e4SLinus Torvalds 	case PF_INET:
11691da177e4SLinus Torvalds 	case PF_INET6:
11701da177e4SLinus Torvalds 		switch (type) {
11711da177e4SLinus Torvalds 		case SOCK_STREAM:
117213402580SJames Morris 			if (default_protocol_stream(protocol))
11731da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
117413402580SJames Morris 			else
117513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11761da177e4SLinus Torvalds 		case SOCK_DGRAM:
117713402580SJames Morris 			if (default_protocol_dgram(protocol))
11781da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
117913402580SJames Morris 			else
118013402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11812ee92d46SJames Morris 		case SOCK_DCCP:
11822ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
118313402580SJames Morris 		default:
11841da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11851da177e4SLinus Torvalds 		}
11861da177e4SLinus Torvalds 		break;
11871da177e4SLinus Torvalds 	case PF_NETLINK:
11881da177e4SLinus Torvalds 		switch (protocol) {
11891da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11901da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11911da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
11921da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
11937f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11941da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11951da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11961da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11971da177e4SLinus Torvalds 		case NETLINK_XFRM:
11981da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11991da177e4SLinus Torvalds 		case NETLINK_SELINUX:
12001da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
12011da177e4SLinus Torvalds 		case NETLINK_AUDIT:
12021da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
12031da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
12041da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
12051da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12061da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12070c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12080c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12091da177e4SLinus Torvalds 		default:
12101da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12111da177e4SLinus Torvalds 		}
12121da177e4SLinus Torvalds 	case PF_PACKET:
12131da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12141da177e4SLinus Torvalds 	case PF_KEY:
12151da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12163e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12173e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12181da177e4SLinus Torvalds 	}
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12248e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12251da177e4SLinus Torvalds 				u16 tclass,
12261da177e4SLinus Torvalds 				u32 *sid)
12271da177e4SLinus Torvalds {
12288e6c9693SLucian Adrian Grijincu 	int rc;
12298e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
12321da177e4SLinus Torvalds 	if (!buffer)
12331da177e4SLinus Torvalds 		return -ENOMEM;
12341da177e4SLinus Torvalds 
12358e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
12368e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
12378e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
12388e6c9693SLucian Adrian Grijincu 	else {
12398e6c9693SLucian Adrian Grijincu 		/* each process gets a /proc/PID/ entry. Strip off the
12408e6c9693SLucian Adrian Grijincu 		 * PID part to get a valid selinux labeling.
12418e6c9693SLucian Adrian Grijincu 		 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
12428e6c9693SLucian Adrian Grijincu 		while (path[1] >= '0' && path[1] <= '9') {
12438e6c9693SLucian Adrian Grijincu 			path[1] = '/';
12448e6c9693SLucian Adrian Grijincu 			path++;
12451da177e4SLinus Torvalds 		}
12461da177e4SLinus Torvalds 		rc = security_genfs_sid("proc", path, tclass, sid);
12478e6c9693SLucian Adrian Grijincu 	}
12481da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
12491da177e4SLinus Torvalds 	return rc;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds #else
12528e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12531da177e4SLinus Torvalds 				u16 tclass,
12541da177e4SLinus Torvalds 				u32 *sid)
12551da177e4SLinus Torvalds {
12561da177e4SLinus Torvalds 	return -EINVAL;
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds #endif
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
12611da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
12641da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12651da177e4SLinus Torvalds 	u32 sid;
12661da177e4SLinus Torvalds 	struct dentry *dentry;
12671da177e4SLinus Torvalds #define INITCONTEXTLEN 255
12681da177e4SLinus Torvalds 	char *context = NULL;
12691da177e4SLinus Torvalds 	unsigned len = 0;
12701da177e4SLinus Torvalds 	int rc = 0;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	if (isec->initialized)
12731da177e4SLinus Torvalds 		goto out;
12741da177e4SLinus Torvalds 
127523970741SEric Paris 	mutex_lock(&isec->lock);
12761da177e4SLinus Torvalds 	if (isec->initialized)
127723970741SEric Paris 		goto out_unlock;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
12800d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
12811da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
12821da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
12831da177e4SLinus Torvalds 		   server is ready to handle calls. */
12841da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
12851da177e4SLinus Torvalds 		if (list_empty(&isec->list))
12861da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
12871da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
128823970741SEric Paris 		goto out_unlock;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1292eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1293eb9ae686SDavid Quigley 		break;
12941da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
12951da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
12961da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
12971da177e4SLinus Torvalds 			break;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
13011da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
13021da177e4SLinus Torvalds 		if (opt_dentry) {
13031da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
13041da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
13051da177e4SLinus Torvalds 		} else {
13061da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
13071da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
13081da177e4SLinus Torvalds 		}
13091da177e4SLinus Torvalds 		if (!dentry) {
1310df7f54c0SEric Paris 			/*
1311df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1312df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1313df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1314df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1315df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1316df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1317df7f54c0SEric Paris 			 * be used again by userspace.
1318df7f54c0SEric Paris 			 */
131923970741SEric Paris 			goto out_unlock;
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
13234cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
13241da177e4SLinus Torvalds 		if (!context) {
13251da177e4SLinus Torvalds 			rc = -ENOMEM;
13261da177e4SLinus Torvalds 			dput(dentry);
132723970741SEric Paris 			goto out_unlock;
13281da177e4SLinus Torvalds 		}
13294cb912f1SEric Paris 		context[len] = '\0';
13301da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13311da177e4SLinus Torvalds 					   context, len);
13321da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1333314dabb8SJames Morris 			kfree(context);
1334314dabb8SJames Morris 
13351da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
13361da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13371da177e4SLinus Torvalds 						   NULL, 0);
13381da177e4SLinus Torvalds 			if (rc < 0) {
13391da177e4SLinus Torvalds 				dput(dentry);
134023970741SEric Paris 				goto out_unlock;
13411da177e4SLinus Torvalds 			}
13421da177e4SLinus Torvalds 			len = rc;
13434cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
13441da177e4SLinus Torvalds 			if (!context) {
13451da177e4SLinus Torvalds 				rc = -ENOMEM;
13461da177e4SLinus Torvalds 				dput(dentry);
134723970741SEric Paris 				goto out_unlock;
13481da177e4SLinus Torvalds 			}
13494cb912f1SEric Paris 			context[len] = '\0';
13501da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
13511da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
13521da177e4SLinus Torvalds 						   context, len);
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 		dput(dentry);
13551da177e4SLinus Torvalds 		if (rc < 0) {
13561da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1357744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1358dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
13591da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
13601da177e4SLinus Torvalds 				kfree(context);
136123970741SEric Paris 				goto out_unlock;
13621da177e4SLinus Torvalds 			}
13631da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
13641da177e4SLinus Torvalds 			sid = sbsec->def_sid;
13651da177e4SLinus Torvalds 			rc = 0;
13661da177e4SLinus Torvalds 		} else {
1367f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1368869ab514SStephen Smalley 							     sbsec->def_sid,
1369869ab514SStephen Smalley 							     GFP_NOFS);
13701da177e4SLinus Torvalds 			if (rc) {
13714ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
13724ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
13734ba0a8adSEric Paris 
13744ba0a8adSEric Paris 				if (rc == -EINVAL) {
13754ba0a8adSEric Paris 					if (printk_ratelimit())
13764ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
13774ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
13784ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
13794ba0a8adSEric Paris 				} else {
1380744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
13811da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
13824ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
13834ba0a8adSEric Paris 				}
13841da177e4SLinus Torvalds 				kfree(context);
13851da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
13861da177e4SLinus Torvalds 				rc = 0;
13871da177e4SLinus Torvalds 				break;
13881da177e4SLinus Torvalds 			}
13891da177e4SLinus Torvalds 		}
13901da177e4SLinus Torvalds 		kfree(context);
13911da177e4SLinus Torvalds 		isec->sid = sid;
13921da177e4SLinus Torvalds 		break;
13931da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
13941da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
13951da177e4SLinus Torvalds 		break;
13961da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
13971da177e4SLinus Torvalds 		/* Default to the fs SID. */
13981da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
14011da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
1402652bb9b0SEric Paris 		rc = security_transition_sid(isec->task_sid, sbsec->sid,
1403652bb9b0SEric Paris 					     isec->sclass, NULL, &sid);
14041da177e4SLinus Torvalds 		if (rc)
140523970741SEric Paris 			goto out_unlock;
14061da177e4SLinus Torvalds 		isec->sid = sid;
14071da177e4SLinus Torvalds 		break;
1408c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1409c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1410c312feb2SEric Paris 		break;
14111da177e4SLinus Torvalds 	default:
1412c312feb2SEric Paris 		/* Default to the fs superblock SID. */
14131da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
14141da177e4SLinus Torvalds 
14150d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
1416f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1417f64410ecSPaul Moore 			 * procfs inodes */
1418f64410ecSPaul Moore 			if (opt_dentry)
1419f64410ecSPaul Moore 				/* Called from d_instantiate or
1420f64410ecSPaul Moore 				 * d_splice_alias. */
1421f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1422f64410ecSPaul Moore 			else
1423f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1424f64410ecSPaul Moore 				 * find a dentry. */
1425f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1426f64410ecSPaul Moore 			/*
1427f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1428f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1429f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1430f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1431f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1432f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1433f64410ecSPaul Moore 			 * could be used again by userspace.
1434f64410ecSPaul Moore 			 */
1435f64410ecSPaul Moore 			if (!dentry)
1436f64410ecSPaul Moore 				goto out_unlock;
14371da177e4SLinus Torvalds 			isec->sclass = inode_mode_to_security_class(inode->i_mode);
1438f64410ecSPaul Moore 			rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
1439f64410ecSPaul Moore 			dput(dentry);
14401da177e4SLinus Torvalds 			if (rc)
144123970741SEric Paris 				goto out_unlock;
14421da177e4SLinus Torvalds 			isec->sid = sid;
14431da177e4SLinus Torvalds 		}
14441da177e4SLinus Torvalds 		break;
14451da177e4SLinus Torvalds 	}
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	isec->initialized = 1;
14481da177e4SLinus Torvalds 
144923970741SEric Paris out_unlock:
145023970741SEric Paris 	mutex_unlock(&isec->lock);
14511da177e4SLinus Torvalds out:
14521da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
14531da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
14541da177e4SLinus Torvalds 	return rc;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
14581da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	u32 perm = 0;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	switch (sig) {
14631da177e4SLinus Torvalds 	case SIGCHLD:
14641da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
14651da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
14661da177e4SLinus Torvalds 		break;
14671da177e4SLinus Torvalds 	case SIGKILL:
14681da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14691da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
14701da177e4SLinus Torvalds 		break;
14711da177e4SLinus Torvalds 	case SIGSTOP:
14721da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14731da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
14741da177e4SLinus Torvalds 		break;
14751da177e4SLinus Torvalds 	default:
14761da177e4SLinus Torvalds 		/* All other signals. */
14771da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
14781da177e4SLinus Torvalds 		break;
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	return perm;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
1484275bb41eSDavid Howells /*
1485d84f4f99SDavid Howells  * Check permission between a pair of credentials
1486d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1487d84f4f99SDavid Howells  */
1488d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1489d84f4f99SDavid Howells 			 const struct cred *target,
1490d84f4f99SDavid Howells 			 u32 perms)
1491d84f4f99SDavid Howells {
1492d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1493d84f4f99SDavid Howells 
1494d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1495d84f4f99SDavid Howells }
1496d84f4f99SDavid Howells 
1497d84f4f99SDavid Howells /*
149888e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1499275bb41eSDavid Howells  * fork check, ptrace check, etc.
1500275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
15013b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1502275bb41eSDavid Howells  */
1503275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1504275bb41eSDavid Howells 			 const struct task_struct *tsk2,
15051da177e4SLinus Torvalds 			 u32 perms)
15061da177e4SLinus Torvalds {
1507275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1508275bb41eSDavid Howells 	u32 sid1, sid2;
15091da177e4SLinus Torvalds 
1510275bb41eSDavid Howells 	rcu_read_lock();
1511275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1512275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1513275bb41eSDavid Howells 	rcu_read_unlock();
1514275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
15173b11a1deSDavid Howells /*
15183b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
15193b11a1deSDavid Howells  * fork check, ptrace check, etc.
15203b11a1deSDavid Howells  * current is the actor and tsk2 is the target
15213b11a1deSDavid Howells  * - this uses current's subjective creds
15223b11a1deSDavid Howells  */
15233b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
15243b11a1deSDavid Howells 			    u32 perms)
15253b11a1deSDavid Howells {
15263b11a1deSDavid Howells 	u32 sid, tsid;
15273b11a1deSDavid Howells 
15283b11a1deSDavid Howells 	sid = current_sid();
15293b11a1deSDavid Howells 	tsid = task_sid(tsk);
15303b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
15313b11a1deSDavid Howells }
15323b11a1deSDavid Howells 
1533b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1534b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1535b68e418cSStephen Smalley #endif
1536b68e418cSStephen Smalley 
15371da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
15386a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
153906112163SEric Paris 			       int cap, int audit)
15401da177e4SLinus Torvalds {
15412bf49690SThomas Liu 	struct common_audit_data ad;
154206112163SEric Paris 	struct av_decision avd;
1543b68e418cSStephen Smalley 	u16 sclass;
15443699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1545b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
154606112163SEric Paris 	int rc;
15471da177e4SLinus Torvalds 
154850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
15491da177e4SLinus Torvalds 	ad.u.cap = cap;
15501da177e4SLinus Torvalds 
1551b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1552b68e418cSStephen Smalley 	case 0:
1553b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1554b68e418cSStephen Smalley 		break;
1555b68e418cSStephen Smalley 	case 1:
1556b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1557b68e418cSStephen Smalley 		break;
1558b68e418cSStephen Smalley 	default:
1559b68e418cSStephen Smalley 		printk(KERN_ERR
1560b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1561b68e418cSStephen Smalley 		BUG();
1562a35c6c83SEric Paris 		return -EINVAL;
1563b68e418cSStephen Smalley 	}
156406112163SEric Paris 
1565275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
15669ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
1567ab354062SLinus Torvalds 		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
15689ade0cf4SEric Paris 		if (rc2)
15699ade0cf4SEric Paris 			return rc2;
15709ade0cf4SEric Paris 	}
157106112163SEric Paris 	return rc;
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
15751da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
15761da177e4SLinus Torvalds 			   u32 perms)
15771da177e4SLinus Torvalds {
1578275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
15791da177e4SLinus Torvalds 
1580275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
15811da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
15851da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
15861da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
158788e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
15881da177e4SLinus Torvalds 			  struct inode *inode,
15891da177e4SLinus Torvalds 			  u32 perms,
159019e49834SLinus Torvalds 			  struct common_audit_data *adp)
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1593275bb41eSDavid Howells 	u32 sid;
15941da177e4SLinus Torvalds 
1595e0e81739SDavid Howells 	validate_creds(cred);
1596e0e81739SDavid Howells 
1597bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1598bbaca6c2SStephen Smalley 		return 0;
1599bbaca6c2SStephen Smalley 
160088e67f3bSDavid Howells 	sid = cred_sid(cred);
16011da177e4SLinus Torvalds 	isec = inode->i_security;
16021da177e4SLinus Torvalds 
160319e49834SLinus Torvalds 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16071da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16081da177e4SLinus Torvalds    pathname if needed. */
160988e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16101da177e4SLinus Torvalds 				  struct dentry *dentry,
16111da177e4SLinus Torvalds 				  u32 av)
16121da177e4SLinus Torvalds {
1613c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
16142bf49690SThomas Liu 	struct common_audit_data ad;
161588e67f3bSDavid Howells 
161650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16172875fa00SEric Paris 	ad.u.dentry = dentry;
161819e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16192875fa00SEric Paris }
16202875fa00SEric Paris 
16212875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16222875fa00SEric Paris    the path to help the auditing code to more easily generate the
16232875fa00SEric Paris    pathname if needed. */
16242875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
16253f7036a0SAl Viro 				const struct path *path,
16262875fa00SEric Paris 				u32 av)
16272875fa00SEric Paris {
1628c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
16292875fa00SEric Paris 	struct common_audit_data ad;
16302875fa00SEric Paris 
163150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
16322875fa00SEric Paris 	ad.u.path = *path;
163319e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
163613f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
163713f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
163813f8e981SDavid Howells 				     struct file *file,
163913f8e981SDavid Howells 				     u32 av)
164013f8e981SDavid Howells {
164113f8e981SDavid Howells 	struct common_audit_data ad;
164213f8e981SDavid Howells 
164313f8e981SDavid Howells 	ad.type = LSM_AUDIT_DATA_PATH;
164413f8e981SDavid Howells 	ad.u.path = file->f_path;
164519e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
164613f8e981SDavid Howells }
164713f8e981SDavid Howells 
16481da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
16491da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
16501da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
16511da177e4SLinus Torvalds    check a particular permission to the file.
16521da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
16531da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
16541da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
16551da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
165688e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
16571da177e4SLinus Torvalds 			 struct file *file,
16581da177e4SLinus Torvalds 			 u32 av)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1661496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16622bf49690SThomas Liu 	struct common_audit_data ad;
166388e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
16641da177e4SLinus Torvalds 	int rc;
16651da177e4SLinus Torvalds 
166650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
1667f48b7399SEric Paris 	ad.u.path = file->f_path;
16681da177e4SLinus Torvalds 
1669275bb41eSDavid Howells 	if (sid != fsec->sid) {
1670275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
16711da177e4SLinus Torvalds 				  SECCLASS_FD,
16721da177e4SLinus Torvalds 				  FD__USE,
16731da177e4SLinus Torvalds 				  &ad);
16741da177e4SLinus Torvalds 		if (rc)
167588e67f3bSDavid Howells 			goto out;
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
167988e67f3bSDavid Howells 	rc = 0;
16801da177e4SLinus Torvalds 	if (av)
168119e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
16821da177e4SLinus Torvalds 
168388e67f3bSDavid Howells out:
168488e67f3bSDavid Howells 	return rc;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds /* Check whether a task can create a file. */
16881da177e4SLinus Torvalds static int may_create(struct inode *dir,
16891da177e4SLinus Torvalds 		      struct dentry *dentry,
16901da177e4SLinus Torvalds 		      u16 tclass)
16911da177e4SLinus Torvalds {
16925fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
16931da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
16941da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1695275bb41eSDavid Howells 	u32 sid, newsid;
16962bf49690SThomas Liu 	struct common_audit_data ad;
16971da177e4SLinus Torvalds 	int rc;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	dsec = dir->i_security;
17001da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
17011da177e4SLinus Torvalds 
1702275bb41eSDavid Howells 	sid = tsec->sid;
1703275bb41eSDavid Howells 	newsid = tsec->create_sid;
1704275bb41eSDavid Howells 
170550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1706a269434dSEric Paris 	ad.u.dentry = dentry;
17071da177e4SLinus Torvalds 
1708275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
17091da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
17101da177e4SLinus Torvalds 			  &ad);
17111da177e4SLinus Torvalds 	if (rc)
17121da177e4SLinus Torvalds 		return rc;
17131da177e4SLinus Torvalds 
171412f348b9SEric Paris 	if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
1715cb1e922fSEric Paris 		rc = security_transition_sid(sid, dsec->sid, tclass,
1716cb1e922fSEric Paris 					     &dentry->d_name, &newsid);
17171da177e4SLinus Torvalds 		if (rc)
17181da177e4SLinus Torvalds 			return rc;
17191da177e4SLinus Torvalds 	}
17201da177e4SLinus Torvalds 
1721275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
17221da177e4SLinus Torvalds 	if (rc)
17231da177e4SLinus Torvalds 		return rc;
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
17261da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
17271da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds 
17304eb582cfSMichael LeMay /* Check whether a task can create a key. */
17314eb582cfSMichael LeMay static int may_create_key(u32 ksid,
17324eb582cfSMichael LeMay 			  struct task_struct *ctx)
17334eb582cfSMichael LeMay {
1734275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
17354eb582cfSMichael LeMay 
1736275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
17374eb582cfSMichael LeMay }
17384eb582cfSMichael LeMay 
17391da177e4SLinus Torvalds #define MAY_LINK	0
17401da177e4SLinus Torvalds #define MAY_UNLINK	1
17411da177e4SLinus Torvalds #define MAY_RMDIR	2
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
17441da177e4SLinus Torvalds static int may_link(struct inode *dir,
17451da177e4SLinus Torvalds 		    struct dentry *dentry,
17461da177e4SLinus Torvalds 		    int kind)
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds {
17491da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
17502bf49690SThomas Liu 	struct common_audit_data ad;
1751275bb41eSDavid Howells 	u32 sid = current_sid();
17521da177e4SLinus Torvalds 	u32 av;
17531da177e4SLinus Torvalds 	int rc;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	dsec = dir->i_security;
1756c6f493d6SDavid Howells 	isec = d_backing_inode(dentry)->i_security;
17571da177e4SLinus Torvalds 
175850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1759a269434dSEric Paris 	ad.u.dentry = dentry;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	av = DIR__SEARCH;
17621da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1763275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
17641da177e4SLinus Torvalds 	if (rc)
17651da177e4SLinus Torvalds 		return rc;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	switch (kind) {
17681da177e4SLinus Torvalds 	case MAY_LINK:
17691da177e4SLinus Torvalds 		av = FILE__LINK;
17701da177e4SLinus Torvalds 		break;
17711da177e4SLinus Torvalds 	case MAY_UNLINK:
17721da177e4SLinus Torvalds 		av = FILE__UNLINK;
17731da177e4SLinus Torvalds 		break;
17741da177e4SLinus Torvalds 	case MAY_RMDIR:
17751da177e4SLinus Torvalds 		av = DIR__RMDIR;
17761da177e4SLinus Torvalds 		break;
17771da177e4SLinus Torvalds 	default:
1778744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1779744ba35eSEric Paris 			__func__, kind);
17801da177e4SLinus Torvalds 		return 0;
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
1783275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
17841da177e4SLinus Torvalds 	return rc;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
17881da177e4SLinus Torvalds 			     struct dentry *old_dentry,
17891da177e4SLinus Torvalds 			     struct inode *new_dir,
17901da177e4SLinus Torvalds 			     struct dentry *new_dentry)
17911da177e4SLinus Torvalds {
17921da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
17932bf49690SThomas Liu 	struct common_audit_data ad;
1794275bb41eSDavid Howells 	u32 sid = current_sid();
17951da177e4SLinus Torvalds 	u32 av;
17961da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
17971da177e4SLinus Torvalds 	int rc;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
1800c6f493d6SDavid Howells 	old_isec = d_backing_inode(old_dentry)->i_security;
1801e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
18021da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
18031da177e4SLinus Torvalds 
180450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18051da177e4SLinus Torvalds 
1806a269434dSEric Paris 	ad.u.dentry = old_dentry;
1807275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
18081da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
18091da177e4SLinus Torvalds 	if (rc)
18101da177e4SLinus Torvalds 		return rc;
1811275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
18121da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
18131da177e4SLinus Torvalds 	if (rc)
18141da177e4SLinus Torvalds 		return rc;
18151da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1816275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
18171da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
18181da177e4SLinus Torvalds 		if (rc)
18191da177e4SLinus Torvalds 			return rc;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 
1822a269434dSEric Paris 	ad.u.dentry = new_dentry;
18231da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
18242c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
18251da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1826275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
18271da177e4SLinus Torvalds 	if (rc)
18281da177e4SLinus Torvalds 		return rc;
18292c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
1830c6f493d6SDavid Howells 		new_isec = d_backing_inode(new_dentry)->i_security;
1831e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
1832275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
18331da177e4SLinus Torvalds 				  new_isec->sclass,
18341da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
18351da177e4SLinus Torvalds 		if (rc)
18361da177e4SLinus Torvalds 			return rc;
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	return 0;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
184388e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
18441da177e4SLinus Torvalds 			       struct super_block *sb,
18451da177e4SLinus Torvalds 			       u32 perms,
18462bf49690SThomas Liu 			       struct common_audit_data *ad)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
184988e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	sbsec = sb->s_security;
1852275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
18561da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
18571da177e4SLinus Torvalds {
18581da177e4SLinus Torvalds 	u32 av = 0;
18591da177e4SLinus Torvalds 
1860dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
18611da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18621da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
18631da177e4SLinus Torvalds 		if (mask & MAY_READ)
18641da177e4SLinus Torvalds 			av |= FILE__READ;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
18671da177e4SLinus Torvalds 			av |= FILE__APPEND;
18681da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
18691da177e4SLinus Torvalds 			av |= FILE__WRITE;
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	} else {
18721da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18731da177e4SLinus Torvalds 			av |= DIR__SEARCH;
18741da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
18751da177e4SLinus Torvalds 			av |= DIR__WRITE;
18761da177e4SLinus Torvalds 		if (mask & MAY_READ)
18771da177e4SLinus Torvalds 			av |= DIR__READ;
18781da177e4SLinus Torvalds 	}
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	return av;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
18841da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
18851da177e4SLinus Torvalds {
18861da177e4SLinus Torvalds 	u32 av = 0;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
18891da177e4SLinus Torvalds 		av |= FILE__READ;
18901da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
18911da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
18921da177e4SLinus Torvalds 			av |= FILE__APPEND;
18931da177e4SLinus Torvalds 		else
18941da177e4SLinus Torvalds 			av |= FILE__WRITE;
18951da177e4SLinus Torvalds 	}
18960794c66dSStephen Smalley 	if (!av) {
18970794c66dSStephen Smalley 		/*
18980794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
18990794c66dSStephen Smalley 		 */
19000794c66dSStephen Smalley 		av = FILE__IOCTL;
19010794c66dSStephen Smalley 	}
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	return av;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
19068b6a5a37SEric Paris /*
19078b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
19088b6a5a37SEric Paris  * open permission.
19098b6a5a37SEric Paris  */
19108b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
19118b6a5a37SEric Paris {
19128b6a5a37SEric Paris 	u32 av = file_to_av(file);
19138b6a5a37SEric Paris 
191449b7b8deSEric Paris 	if (selinux_policycap_openperm)
19158b6a5a37SEric Paris 		av |= FILE__OPEN;
191649b7b8deSEric Paris 
19178b6a5a37SEric Paris 	return av;
19188b6a5a37SEric Paris }
19198b6a5a37SEric Paris 
19201da177e4SLinus Torvalds /* Hook functions begin here. */
19211da177e4SLinus Torvalds 
192279af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
192379af7307SStephen Smalley {
192479af7307SStephen Smalley 	u32 mysid = current_sid();
192579af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
192679af7307SStephen Smalley 
192779af7307SStephen Smalley 	return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER,
192879af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
192979af7307SStephen Smalley }
193079af7307SStephen Smalley 
193179af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
193279af7307SStephen Smalley 				      struct task_struct *to)
193379af7307SStephen Smalley {
193479af7307SStephen Smalley 	u32 mysid = current_sid();
193579af7307SStephen Smalley 	u32 fromsid = task_sid(from);
193679af7307SStephen Smalley 	u32 tosid = task_sid(to);
193779af7307SStephen Smalley 	int rc;
193879af7307SStephen Smalley 
193979af7307SStephen Smalley 	if (mysid != fromsid) {
194079af7307SStephen Smalley 		rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
194179af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
194279af7307SStephen Smalley 		if (rc)
194379af7307SStephen Smalley 			return rc;
194479af7307SStephen Smalley 	}
194579af7307SStephen Smalley 
194679af7307SStephen Smalley 	return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
194779af7307SStephen Smalley 			    NULL);
194879af7307SStephen Smalley }
194979af7307SStephen Smalley 
195079af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
195179af7307SStephen Smalley 					  struct task_struct *to)
195279af7307SStephen Smalley {
195379af7307SStephen Smalley 	u32 fromsid = task_sid(from);
195479af7307SStephen Smalley 	u32 tosid = task_sid(to);
195579af7307SStephen Smalley 
195679af7307SStephen Smalley 	return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
195779af7307SStephen Smalley 			    NULL);
195879af7307SStephen Smalley }
195979af7307SStephen Smalley 
196079af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
196179af7307SStephen Smalley 					struct task_struct *to,
196279af7307SStephen Smalley 					struct file *file)
196379af7307SStephen Smalley {
196479af7307SStephen Smalley 	u32 sid = task_sid(to);
196579af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
1966c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(file->f_path.dentry);
196779af7307SStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
196879af7307SStephen Smalley 	struct common_audit_data ad;
196979af7307SStephen Smalley 	int rc;
197079af7307SStephen Smalley 
197179af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
197279af7307SStephen Smalley 	ad.u.path = file->f_path;
197379af7307SStephen Smalley 
197479af7307SStephen Smalley 	if (sid != fsec->sid) {
197579af7307SStephen Smalley 		rc = avc_has_perm(sid, fsec->sid,
197679af7307SStephen Smalley 				  SECCLASS_FD,
197779af7307SStephen Smalley 				  FD__USE,
197879af7307SStephen Smalley 				  &ad);
197979af7307SStephen Smalley 		if (rc)
198079af7307SStephen Smalley 			return rc;
198179af7307SStephen Smalley 	}
198279af7307SStephen Smalley 
198379af7307SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
198479af7307SStephen Smalley 		return 0;
198579af7307SStephen Smalley 
198679af7307SStephen Smalley 	return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
198779af7307SStephen Smalley 			    &ad);
198879af7307SStephen Smalley }
198979af7307SStephen Smalley 
19909e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
1991006ebb40SStephen Smalley 				     unsigned int mode)
19921da177e4SLinus Torvalds {
199369f594a3SEric Paris 	if (mode & PTRACE_MODE_READ) {
1994275bb41eSDavid Howells 		u32 sid = current_sid();
1995275bb41eSDavid Howells 		u32 csid = task_sid(child);
1996275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1997006ebb40SStephen Smalley 	}
1998006ebb40SStephen Smalley 
19993b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
20005cd9c58fSDavid Howells }
20015cd9c58fSDavid Howells 
20025cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
20035cd9c58fSDavid Howells {
20045cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
20081da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
20091da177e4SLinus Torvalds {
2010b1d9e6b0SCasey Schaufler 	return current_has_perm(target, PROCESS__GETCAP);
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
2013d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2014d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
201515a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
201615a2460eSDavid Howells 			  const kernel_cap_t *permitted)
20171da177e4SLinus Torvalds {
2018d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20215626d3e8SJames Morris /*
20225626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
20235626d3e8SJames Morris  * which was removed).
20245626d3e8SJames Morris  *
20255626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
20265626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
20275626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
20285626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
20295626d3e8SJames Morris  */
20305626d3e8SJames Morris 
20316a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
20326a9de491SEric Paris 			   int cap, int audit)
20331da177e4SLinus Torvalds {
20346a9de491SEric Paris 	return cred_has_capability(cred, cap, audit);
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
20381da177e4SLinus Torvalds {
203988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
20401da177e4SLinus Torvalds 	int rc = 0;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	if (!sb)
20431da177e4SLinus Torvalds 		return 0;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	switch (cmds) {
20461da177e4SLinus Torvalds 	case Q_SYNC:
20471da177e4SLinus Torvalds 	case Q_QUOTAON:
20481da177e4SLinus Torvalds 	case Q_QUOTAOFF:
20491da177e4SLinus Torvalds 	case Q_SETINFO:
20501da177e4SLinus Torvalds 	case Q_SETQUOTA:
205188e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
20521da177e4SLinus Torvalds 		break;
20531da177e4SLinus Torvalds 	case Q_GETFMT:
20541da177e4SLinus Torvalds 	case Q_GETINFO:
20551da177e4SLinus Torvalds 	case Q_GETQUOTA:
205688e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
20571da177e4SLinus Torvalds 		break;
20581da177e4SLinus Torvalds 	default:
20591da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
20601da177e4SLinus Torvalds 		break;
20611da177e4SLinus Torvalds 	}
20621da177e4SLinus Torvalds 	return rc;
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
20661da177e4SLinus Torvalds {
206788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
206888e67f3bSDavid Howells 
20692875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
207212b3052cSEric Paris static int selinux_syslog(int type)
20731da177e4SLinus Torvalds {
20741da177e4SLinus Torvalds 	int rc;
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	switch (type) {
2077d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2078d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
20791da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
20801da177e4SLinus Torvalds 		break;
2081d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2082d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2083d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2084d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
20851da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
20861da177e4SLinus Torvalds 		break;
2087d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLOSE:	/* Close log */
2088d78ca3cdSKees Cook 	case SYSLOG_ACTION_OPEN:	/* Open log */
2089d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ:	/* Read from log */
2090d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_CLEAR:	/* Read/clear last kernel messages */
2091d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLEAR:	/* Clear ring buffer */
20921da177e4SLinus Torvalds 	default:
20931da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
20941da177e4SLinus Torvalds 		break;
20951da177e4SLinus Torvalds 	}
20961da177e4SLinus Torvalds 	return rc;
20971da177e4SLinus Torvalds }
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds /*
21001da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
21011da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
21021da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
21031da177e4SLinus Torvalds  *
21041da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
21051da177e4SLinus Torvalds  * processes that allocate mappings.
21061da177e4SLinus Torvalds  */
210734b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
21081da177e4SLinus Torvalds {
21091da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
21101da177e4SLinus Torvalds 
2111b1d9e6b0SCasey Schaufler 	rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
21123699c53cSDavid Howells 					SECURITY_CAP_NOAUDIT);
21131da177e4SLinus Torvalds 	if (rc == 0)
21141da177e4SLinus Torvalds 		cap_sys_admin = 1;
21151da177e4SLinus Torvalds 
2116b1d9e6b0SCasey Schaufler 	return cap_sys_admin;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds /* binprm security operations */
21201da177e4SLinus Torvalds 
21217b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
21227b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
21237b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
21247b0d0b40SStephen Smalley {
21257b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
21267b0d0b40SStephen Smalley 	int nosuid = (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID);
21277b0d0b40SStephen Smalley 	int rc;
21287b0d0b40SStephen Smalley 
21297b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
21307b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
21317b0d0b40SStephen Smalley 
21327b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
21337b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
21347b0d0b40SStephen Smalley 
21357b0d0b40SStephen Smalley 	/*
21367b0d0b40SStephen Smalley 	 * The only transitions we permit under NNP or nosuid
21377b0d0b40SStephen Smalley 	 * are transitions to bounded SIDs, i.e. SIDs that are
21387b0d0b40SStephen Smalley 	 * guaranteed to only be allowed a subset of the permissions
21397b0d0b40SStephen Smalley 	 * of the current SID.
21407b0d0b40SStephen Smalley 	 */
21417b0d0b40SStephen Smalley 	rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
21427b0d0b40SStephen Smalley 	if (rc) {
21437b0d0b40SStephen Smalley 		/*
21447b0d0b40SStephen Smalley 		 * On failure, preserve the errno values for NNP vs nosuid.
21457b0d0b40SStephen Smalley 		 * NNP:  Operation not permitted for caller.
21467b0d0b40SStephen Smalley 		 * nosuid:  Permission denied to file.
21477b0d0b40SStephen Smalley 		 */
21487b0d0b40SStephen Smalley 		if (nnp)
21497b0d0b40SStephen Smalley 			return -EPERM;
21507b0d0b40SStephen Smalley 		else
21517b0d0b40SStephen Smalley 			return -EACCES;
21527b0d0b40SStephen Smalley 	}
21537b0d0b40SStephen Smalley 	return 0;
21547b0d0b40SStephen Smalley }
21557b0d0b40SStephen Smalley 
2156a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
21571da177e4SLinus Torvalds {
2158a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2159a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
21601da177e4SLinus Torvalds 	struct inode_security_struct *isec;
21612bf49690SThomas Liu 	struct common_audit_data ad;
2162496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
21631da177e4SLinus Torvalds 	int rc;
21641da177e4SLinus Torvalds 
2165a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2166a6f76f23SDavid Howells 	 * the script interpreter */
2167a6f76f23SDavid Howells 	if (bprm->cred_prepared)
21681da177e4SLinus Torvalds 		return 0;
21691da177e4SLinus Torvalds 
2170a6f76f23SDavid Howells 	old_tsec = current_security();
2171a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
21721da177e4SLinus Torvalds 	isec = inode->i_security;
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 	/* Default to the current task SID. */
2175a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2176a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
21771da177e4SLinus Torvalds 
217828eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2179a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2180a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2181a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
21821da177e4SLinus Torvalds 
2183a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2184a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
21851da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2186a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2187259e5e6cSAndy Lutomirski 
21887b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
21897b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
21907b0d0b40SStephen Smalley 		if (rc)
21917b0d0b40SStephen Smalley 			return rc;
21921da177e4SLinus Torvalds 	} else {
21931da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2194a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2195652bb9b0SEric Paris 					     SECCLASS_PROCESS, NULL,
2196652bb9b0SEric Paris 					     &new_tsec->sid);
21971da177e4SLinus Torvalds 		if (rc)
21981da177e4SLinus Torvalds 			return rc;
21997b0d0b40SStephen Smalley 
22007b0d0b40SStephen Smalley 		/*
22017b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
22027b0d0b40SStephen Smalley 		 * transition.
22037b0d0b40SStephen Smalley 		 */
22047b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
22057b0d0b40SStephen Smalley 		if (rc)
22067b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
22071da177e4SLinus Torvalds 	}
22081da177e4SLinus Torvalds 
220950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
2210f48b7399SEric Paris 	ad.u.path = bprm->file->f_path;
22111da177e4SLinus Torvalds 
2212a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2213a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
22141da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
22151da177e4SLinus Torvalds 		if (rc)
22161da177e4SLinus Torvalds 			return rc;
22171da177e4SLinus Torvalds 	} else {
22181da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2219a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
22201da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
22211da177e4SLinus Torvalds 		if (rc)
22221da177e4SLinus Torvalds 			return rc;
22231da177e4SLinus Torvalds 
2224a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
22251da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
22261da177e4SLinus Torvalds 		if (rc)
22271da177e4SLinus Torvalds 			return rc;
22281da177e4SLinus Torvalds 
2229a6f76f23SDavid Howells 		/* Check for shared state */
2230a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2231a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2232a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2233a6f76f23SDavid Howells 					  NULL);
2234a6f76f23SDavid Howells 			if (rc)
2235a6f76f23SDavid Howells 				return -EPERM;
22361da177e4SLinus Torvalds 		}
22371da177e4SLinus Torvalds 
2238a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2239a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2240a6f76f23SDavid Howells 		if (bprm->unsafe &
2241a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2242a6f76f23SDavid Howells 			struct task_struct *tracer;
2243a6f76f23SDavid Howells 			struct task_security_struct *sec;
2244a6f76f23SDavid Howells 			u32 ptsid = 0;
2245a6f76f23SDavid Howells 
2246a6f76f23SDavid Howells 			rcu_read_lock();
224706d98473STejun Heo 			tracer = ptrace_parent(current);
2248a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2249a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2250a6f76f23SDavid Howells 				ptsid = sec->sid;
2251a6f76f23SDavid Howells 			}
2252a6f76f23SDavid Howells 			rcu_read_unlock();
2253a6f76f23SDavid Howells 
2254a6f76f23SDavid Howells 			if (ptsid != 0) {
2255a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2256a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2257a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2258a6f76f23SDavid Howells 				if (rc)
2259a6f76f23SDavid Howells 					return -EPERM;
2260a6f76f23SDavid Howells 			}
2261a6f76f23SDavid Howells 		}
2262a6f76f23SDavid Howells 
2263a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2264a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2265a6f76f23SDavid Howells 	}
2266a6f76f23SDavid Howells 
22671da177e4SLinus Torvalds 	return 0;
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
22711da177e4SLinus Torvalds {
22725fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
2273275bb41eSDavid Howells 	u32 sid, osid;
22741da177e4SLinus Torvalds 	int atsecure = 0;
22751da177e4SLinus Torvalds 
2276275bb41eSDavid Howells 	sid = tsec->sid;
2277275bb41eSDavid Howells 	osid = tsec->osid;
2278275bb41eSDavid Howells 
2279275bb41eSDavid Howells 	if (osid != sid) {
22801da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
22811da177e4SLinus Torvalds 		   the noatsecure permission is granted between
22821da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2283275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
22841da177e4SLinus Torvalds 					SECCLASS_PROCESS,
22851da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
22861da177e4SLinus Torvalds 	}
22871da177e4SLinus Torvalds 
2288b1d9e6b0SCasey Schaufler 	return !!atsecure;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
2291c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2292c3c073f8SAl Viro {
2293c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2294c3c073f8SAl Viro }
2295c3c073f8SAl Viro 
22961da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2297745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2298745ca247SDavid Howells 					    struct files_struct *files)
22991da177e4SLinus Torvalds {
23001da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2301b20c8122SStephen Smalley 	struct tty_struct *tty;
230224ec839cSPeter Zijlstra 	int drop_tty = 0;
2303c3c073f8SAl Viro 	unsigned n;
23041da177e4SLinus Torvalds 
230524ec839cSPeter Zijlstra 	tty = get_current_tty();
23061da177e4SLinus Torvalds 	if (tty) {
2307ee2ffa0dSNick Piggin 		spin_lock(&tty_files_lock);
230837dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2309d996b62aSNick Piggin 			struct tty_file_private *file_priv;
231037dd0bd0SEric Paris 
23111da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
231213f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
231313f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
231413f8e981SDavid Howells 			   open file may belong to another process and we are
231513f8e981SDavid Howells 			   only interested in the inode-based check here. */
2316d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2317d996b62aSNick Piggin 						struct tty_file_private, list);
2318d996b62aSNick Piggin 			file = file_priv->file;
231913f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
232024ec839cSPeter Zijlstra 				drop_tty = 1;
23211da177e4SLinus Torvalds 		}
2322ee2ffa0dSNick Piggin 		spin_unlock(&tty_files_lock);
2323452a00d2SAlan Cox 		tty_kref_put(tty);
23241da177e4SLinus Torvalds 	}
232598a27ba4SEric W. Biederman 	/* Reset controlling tty. */
232698a27ba4SEric W. Biederman 	if (drop_tty)
232798a27ba4SEric W. Biederman 		no_tty();
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2330c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2331c3c073f8SAl Viro 	if (!n) /* none found? */
2332c3c073f8SAl Viro 		return;
23331da177e4SLinus Torvalds 
2334c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
233545525b26SAl Viro 	if (IS_ERR(devnull))
233645525b26SAl Viro 		devnull = NULL;
2337c3c073f8SAl Viro 	/* replace all the matching ones with this */
2338c3c073f8SAl Viro 	do {
233945525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2340c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
234145525b26SAl Viro 	if (devnull)
2342c3c073f8SAl Viro 		fput(devnull);
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds /*
2346a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
23471da177e4SLinus Torvalds  */
2348a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
23491da177e4SLinus Torvalds {
2350a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23511da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
23521da177e4SLinus Torvalds 	int rc, i;
23531da177e4SLinus Torvalds 
2354a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2355a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
23561da177e4SLinus Torvalds 		return;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2359a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
23601da177e4SLinus Torvalds 
2361a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2362a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2363a6f76f23SDavid Howells 
2364a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2365a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2366a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2367a6f76f23SDavid Howells 	 *
2368a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2369a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2370a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2371a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2372a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2373a6f76f23SDavid Howells 	 */
2374a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2375a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2376a6f76f23SDavid Howells 	if (rc) {
2377eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2378eb2d55a3SOleg Nesterov 		task_lock(current);
2379a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2380a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2381a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2382a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2383a6f76f23SDavid Howells 		}
2384eb2d55a3SOleg Nesterov 		task_unlock(current);
2385eb2d55a3SOleg Nesterov 		update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2386a6f76f23SDavid Howells 	}
2387a6f76f23SDavid Howells }
2388a6f76f23SDavid Howells 
2389a6f76f23SDavid Howells /*
2390a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2391a6f76f23SDavid Howells  * due to exec
2392a6f76f23SDavid Howells  */
2393a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2394a6f76f23SDavid Howells {
2395a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2396a6f76f23SDavid Howells 	struct itimerval itimer;
2397a6f76f23SDavid Howells 	u32 osid, sid;
2398a6f76f23SDavid Howells 	int rc, i;
2399a6f76f23SDavid Howells 
2400a6f76f23SDavid Howells 	osid = tsec->osid;
2401a6f76f23SDavid Howells 	sid = tsec->sid;
2402a6f76f23SDavid Howells 
2403a6f76f23SDavid Howells 	if (sid == osid)
2404a6f76f23SDavid Howells 		return;
2405a6f76f23SDavid Howells 
2406a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2407a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2408a6f76f23SDavid Howells 	 * flush and unblock signals.
2409a6f76f23SDavid Howells 	 *
2410a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2411a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2412a6f76f23SDavid Howells 	 */
2413a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
24141da177e4SLinus Torvalds 	if (rc) {
24151da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
24161da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
24171da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
24181da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
2419*9e7c8f8cSOleg Nesterov 		if (!fatal_signal_pending(current)) {
2420*9e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->pending);
2421*9e7c8f8cSOleg Nesterov 			flush_sigqueue(&current->signal->shared_pending);
24221da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
24231da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
2424*9e7c8f8cSOleg Nesterov 			recalc_sigpending();
24253bcac026SDavid Howells 		}
24261da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
24271da177e4SLinus Torvalds 	}
24281da177e4SLinus Torvalds 
2429a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2430a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2431ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
24320b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2433ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds /* superblock security operations */
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
24391da177e4SLinus Torvalds {
24401da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
24411da177e4SLinus Torvalds }
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
24441da177e4SLinus Torvalds {
24451da177e4SLinus Torvalds 	superblock_free_security(sb);
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
24491da177e4SLinus Torvalds {
24501da177e4SLinus Torvalds 	if (plen > olen)
24511da177e4SLinus Torvalds 		return 0;
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
24571da177e4SLinus Torvalds {
2458832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2459832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2460832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
246111689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
246211689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	if (!*first) {
24681da177e4SLinus Torvalds 		**to = ',';
24691da177e4SLinus Torvalds 		*to += 1;
24703528a953SCory Olmo 	} else
24711da177e4SLinus Torvalds 		*first = 0;
24721da177e4SLinus Torvalds 	memcpy(*to, from, len);
24731da177e4SLinus Torvalds 	*to += len;
24741da177e4SLinus Torvalds }
24751da177e4SLinus Torvalds 
24763528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
24773528a953SCory Olmo 				       int len)
24783528a953SCory Olmo {
24793528a953SCory Olmo 	int current_size = 0;
24803528a953SCory Olmo 
24813528a953SCory Olmo 	if (!*first) {
24823528a953SCory Olmo 		**to = '|';
24833528a953SCory Olmo 		*to += 1;
2484828dfe1dSEric Paris 	} else
24853528a953SCory Olmo 		*first = 0;
24863528a953SCory Olmo 
24873528a953SCory Olmo 	while (current_size < len) {
24883528a953SCory Olmo 		if (*from != '"') {
24893528a953SCory Olmo 			**to = *from;
24903528a953SCory Olmo 			*to += 1;
24913528a953SCory Olmo 		}
24923528a953SCory Olmo 		from += 1;
24933528a953SCory Olmo 		current_size += 1;
24943528a953SCory Olmo 	}
24953528a953SCory Olmo }
24963528a953SCory Olmo 
2497e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
24981da177e4SLinus Torvalds {
24991da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
25001da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
25011da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
25023528a953SCory Olmo 	int open_quote = 0;
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	in_curr = orig;
25051da177e4SLinus Torvalds 	sec_curr = copy;
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
25081da177e4SLinus Torvalds 	if (!nosec) {
25091da177e4SLinus Torvalds 		rc = -ENOMEM;
25101da177e4SLinus Torvalds 		goto out;
25111da177e4SLinus Torvalds 	}
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	nosec_save = nosec;
25141da177e4SLinus Torvalds 	fnosec = fsec = 1;
25151da177e4SLinus Torvalds 	in_save = in_end = orig;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	do {
25183528a953SCory Olmo 		if (*in_end == '"')
25193528a953SCory Olmo 			open_quote = !open_quote;
25203528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
25213528a953SCory Olmo 				*in_end == '\0') {
25221da177e4SLinus Torvalds 			int len = in_end - in_curr;
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
25253528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
25261da177e4SLinus Torvalds 			else
25271da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 			in_curr = in_end + 1;
25301da177e4SLinus Torvalds 		}
25311da177e4SLinus Torvalds 	} while (*in_end++);
25321da177e4SLinus Torvalds 
25336931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2534da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
25351da177e4SLinus Torvalds out:
25361da177e4SLinus Torvalds 	return rc;
25371da177e4SLinus Torvalds }
25381da177e4SLinus Torvalds 
2539026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2540026eb167SEric Paris {
2541026eb167SEric Paris 	int rc, i, *flags;
2542026eb167SEric Paris 	struct security_mnt_opts opts;
2543026eb167SEric Paris 	char *secdata, **mount_options;
2544026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2545026eb167SEric Paris 
2546026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2547026eb167SEric Paris 		return 0;
2548026eb167SEric Paris 
2549026eb167SEric Paris 	if (!data)
2550026eb167SEric Paris 		return 0;
2551026eb167SEric Paris 
2552026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2553026eb167SEric Paris 		return 0;
2554026eb167SEric Paris 
2555026eb167SEric Paris 	security_init_mnt_opts(&opts);
2556026eb167SEric Paris 	secdata = alloc_secdata();
2557026eb167SEric Paris 	if (!secdata)
2558026eb167SEric Paris 		return -ENOMEM;
2559026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2560026eb167SEric Paris 	if (rc)
2561026eb167SEric Paris 		goto out_free_secdata;
2562026eb167SEric Paris 
2563026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2564026eb167SEric Paris 	if (rc)
2565026eb167SEric Paris 		goto out_free_secdata;
2566026eb167SEric Paris 
2567026eb167SEric Paris 	mount_options = opts.mnt_opts;
2568026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2569026eb167SEric Paris 
2570026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2571026eb167SEric Paris 		u32 sid;
2572026eb167SEric Paris 		size_t len;
2573026eb167SEric Paris 
257412f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
2575026eb167SEric Paris 			continue;
2576026eb167SEric Paris 		len = strlen(mount_options[i]);
257752a4c640SNikolay Aleksandrov 		rc = security_context_to_sid(mount_options[i], len, &sid,
257852a4c640SNikolay Aleksandrov 					     GFP_KERNEL);
2579026eb167SEric Paris 		if (rc) {
2580026eb167SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
258129b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
258229b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2583026eb167SEric Paris 			goto out_free_opts;
2584026eb167SEric Paris 		}
2585026eb167SEric Paris 		rc = -EINVAL;
2586026eb167SEric Paris 		switch (flags[i]) {
2587026eb167SEric Paris 		case FSCONTEXT_MNT:
2588026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2589026eb167SEric Paris 				goto out_bad_option;
2590026eb167SEric Paris 			break;
2591026eb167SEric Paris 		case CONTEXT_MNT:
2592026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2593026eb167SEric Paris 				goto out_bad_option;
2594026eb167SEric Paris 			break;
2595026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2596026eb167SEric Paris 			struct inode_security_struct *root_isec;
2597c6f493d6SDavid Howells 			root_isec = d_backing_inode(sb->s_root)->i_security;
2598026eb167SEric Paris 
2599026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2600026eb167SEric Paris 				goto out_bad_option;
2601026eb167SEric Paris 			break;
2602026eb167SEric Paris 		}
2603026eb167SEric Paris 		case DEFCONTEXT_MNT:
2604026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2605026eb167SEric Paris 				goto out_bad_option;
2606026eb167SEric Paris 			break;
2607026eb167SEric Paris 		default:
2608026eb167SEric Paris 			goto out_free_opts;
2609026eb167SEric Paris 		}
2610026eb167SEric Paris 	}
2611026eb167SEric Paris 
2612026eb167SEric Paris 	rc = 0;
2613026eb167SEric Paris out_free_opts:
2614026eb167SEric Paris 	security_free_mnt_opts(&opts);
2615026eb167SEric Paris out_free_secdata:
2616026eb167SEric Paris 	free_secdata(secdata);
2617026eb167SEric Paris 	return rc;
2618026eb167SEric Paris out_bad_option:
2619026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
262029b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
262129b1deb2SLinus Torvalds 	       sb->s_type->name);
2622026eb167SEric Paris 	goto out_free_opts;
2623026eb167SEric Paris }
2624026eb167SEric Paris 
262512204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
26261da177e4SLinus Torvalds {
262788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26282bf49690SThomas Liu 	struct common_audit_data ad;
26291da177e4SLinus Torvalds 	int rc;
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
26321da177e4SLinus Torvalds 	if (rc)
26331da177e4SLinus Torvalds 		return rc;
26341da177e4SLinus Torvalds 
263574192246SJames Morris 	/* Allow all mounts performed by the kernel */
263674192246SJames Morris 	if (flags & MS_KERNMOUNT)
263774192246SJames Morris 		return 0;
263874192246SJames Morris 
263950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2640a269434dSEric Paris 	ad.u.dentry = sb->s_root;
264188e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds 
2644726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
26451da177e4SLinus Torvalds {
264688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26472bf49690SThomas Liu 	struct common_audit_data ad;
26481da177e4SLinus Torvalds 
264950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2650a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
265188e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
2654808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
2655b5266eb4SAl Viro 			 struct path *path,
2656808d4e3cSAl Viro 			 const char *type,
26571da177e4SLinus Torvalds 			 unsigned long flags,
26581da177e4SLinus Torvalds 			 void *data)
26591da177e4SLinus Torvalds {
266088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2663d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
26641da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
26651da177e4SLinus Torvalds 	else
26662875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
26701da177e4SLinus Torvalds {
267188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26721da177e4SLinus Torvalds 
267388e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
26741da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds /* inode security operations */
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
26801da177e4SLinus Torvalds {
26811da177e4SLinus Torvalds 	return inode_alloc_security(inode);
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	inode_free_security(inode);
26871da177e4SLinus Torvalds }
26881da177e4SLinus Torvalds 
2689d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2690d47be3dfSDavid Quigley 					struct qstr *name, void **ctx,
2691d47be3dfSDavid Quigley 					u32 *ctxlen)
2692d47be3dfSDavid Quigley {
2693d47be3dfSDavid Quigley 	const struct cred *cred = current_cred();
2694d47be3dfSDavid Quigley 	struct task_security_struct *tsec;
2695d47be3dfSDavid Quigley 	struct inode_security_struct *dsec;
2696d47be3dfSDavid Quigley 	struct superblock_security_struct *sbsec;
2697c6f493d6SDavid Howells 	struct inode *dir = d_backing_inode(dentry->d_parent);
2698d47be3dfSDavid Quigley 	u32 newsid;
2699d47be3dfSDavid Quigley 	int rc;
2700d47be3dfSDavid Quigley 
2701d47be3dfSDavid Quigley 	tsec = cred->security;
2702d47be3dfSDavid Quigley 	dsec = dir->i_security;
2703d47be3dfSDavid Quigley 	sbsec = dir->i_sb->s_security;
2704d47be3dfSDavid Quigley 
2705d47be3dfSDavid Quigley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2706d47be3dfSDavid Quigley 		newsid = tsec->create_sid;
2707d47be3dfSDavid Quigley 	} else {
2708d47be3dfSDavid Quigley 		rc = security_transition_sid(tsec->sid, dsec->sid,
2709d47be3dfSDavid Quigley 					     inode_mode_to_security_class(mode),
2710d47be3dfSDavid Quigley 					     name,
2711d47be3dfSDavid Quigley 					     &newsid);
2712d47be3dfSDavid Quigley 		if (rc) {
2713d47be3dfSDavid Quigley 			printk(KERN_WARNING
2714d47be3dfSDavid Quigley 				"%s: security_transition_sid failed, rc=%d\n",
2715d47be3dfSDavid Quigley 			       __func__, -rc);
2716d47be3dfSDavid Quigley 			return rc;
2717d47be3dfSDavid Quigley 		}
2718d47be3dfSDavid Quigley 	}
2719d47be3dfSDavid Quigley 
2720d47be3dfSDavid Quigley 	return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2721d47be3dfSDavid Quigley }
2722d47be3dfSDavid Quigley 
27235e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
27249548906bSTetsuo Handa 				       const struct qstr *qstr,
27259548906bSTetsuo Handa 				       const char **name,
27262a7dba39SEric Paris 				       void **value, size_t *len)
27275e41ff9eSStephen Smalley {
27285fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
27295e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
27305e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2731275bb41eSDavid Howells 	u32 sid, newsid, clen;
27325e41ff9eSStephen Smalley 	int rc;
27339548906bSTetsuo Handa 	char *context;
27345e41ff9eSStephen Smalley 
27355e41ff9eSStephen Smalley 	dsec = dir->i_security;
27365e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
27375e41ff9eSStephen Smalley 
2738275bb41eSDavid Howells 	sid = tsec->sid;
27395e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2740275bb41eSDavid Howells 
2741415103f9SEric Paris 	if ((sbsec->flags & SE_SBINITIALIZED) &&
2742415103f9SEric Paris 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2743415103f9SEric Paris 		newsid = sbsec->mntpoint_sid;
274412f348b9SEric Paris 	else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
2745275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
27465e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
2747652bb9b0SEric Paris 					     qstr, &newsid);
27485e41ff9eSStephen Smalley 		if (rc) {
27495e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
27505e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
27515e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2752dd6f953aSHarvey Harrison 			       __func__,
27535e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
27545e41ff9eSStephen Smalley 			return rc;
27555e41ff9eSStephen Smalley 		}
27565e41ff9eSStephen Smalley 	}
27575e41ff9eSStephen Smalley 
2758296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
27590d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2760296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2761296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2762296fddf7SEric Paris 		isec->sid = newsid;
2763296fddf7SEric Paris 		isec->initialized = 1;
2764296fddf7SEric Paris 	}
27655e41ff9eSStephen Smalley 
276612f348b9SEric Paris 	if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
276725a74f3bSStephen Smalley 		return -EOPNOTSUPP;
276825a74f3bSStephen Smalley 
27699548906bSTetsuo Handa 	if (name)
27709548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
27715e41ff9eSStephen Smalley 
2772570bc1c2SStephen Smalley 	if (value && len) {
277312b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
27749548906bSTetsuo Handa 		if (rc)
27755e41ff9eSStephen Smalley 			return rc;
27765e41ff9eSStephen Smalley 		*value = context;
2777570bc1c2SStephen Smalley 		*len = clen;
2778570bc1c2SStephen Smalley 	}
27795e41ff9eSStephen Smalley 
27805e41ff9eSStephen Smalley 	return 0;
27815e41ff9eSStephen Smalley }
27825e41ff9eSStephen Smalley 
27834acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
27841da177e4SLinus Torvalds {
27851da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
27891da177e4SLinus Torvalds {
27901da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
27911da177e4SLinus Torvalds }
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
27941da177e4SLinus Torvalds {
27951da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds 
280318bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
28041da177e4SLinus Torvalds {
28051da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28131a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
28141da177e4SLinus Torvalds {
28151da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
28161da177e4SLinus Torvalds }
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
28191da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
28201da177e4SLinus Torvalds {
28211da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
28251da177e4SLinus Torvalds {
282688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
282788e67f3bSDavid Howells 
28282875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
28291da177e4SLinus Torvalds }
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
28321da177e4SLinus Torvalds {
283388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
28341da177e4SLinus Torvalds 
28352875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
2838d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2839d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2840626b9740SStephen Smalley 					   int result,
2841d4cf970dSEric Paris 					   unsigned flags)
2842d4cf970dSEric Paris {
2843d4cf970dSEric Paris 	struct common_audit_data ad;
2844d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2845d4cf970dSEric Paris 	int rc;
2846d4cf970dSEric Paris 
284750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2848d4cf970dSEric Paris 	ad.u.inode = inode;
2849d4cf970dSEric Paris 
2850d4cf970dSEric Paris 	rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2851626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
2852d4cf970dSEric Paris 	if (rc)
2853d4cf970dSEric Paris 		return rc;
2854d4cf970dSEric Paris 	return 0;
2855d4cf970dSEric Paris }
2856d4cf970dSEric Paris 
2857e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
28581da177e4SLinus Torvalds {
285988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2860b782e0a6SEric Paris 	u32 perms;
2861b782e0a6SEric Paris 	bool from_access;
2862cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
28632e334057SEric Paris 	struct inode_security_struct *isec;
28642e334057SEric Paris 	u32 sid;
28652e334057SEric Paris 	struct av_decision avd;
28662e334057SEric Paris 	int rc, rc2;
28672e334057SEric Paris 	u32 audited, denied;
28681da177e4SLinus Torvalds 
2869b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2870d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2871d09ca739SEric Paris 
28721da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2873b782e0a6SEric Paris 	if (!mask)
28741da177e4SLinus Torvalds 		return 0;
28751da177e4SLinus Torvalds 
28762e334057SEric Paris 	validate_creds(cred);
2877b782e0a6SEric Paris 
28782e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
28792e334057SEric Paris 		return 0;
2880b782e0a6SEric Paris 
2881b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2882b782e0a6SEric Paris 
28832e334057SEric Paris 	sid = cred_sid(cred);
28842e334057SEric Paris 	isec = inode->i_security;
28852e334057SEric Paris 
28862e334057SEric Paris 	rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
28872e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
28882e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
28892e334057SEric Paris 				     &denied);
28902e334057SEric Paris 	if (likely(!audited))
28912e334057SEric Paris 		return rc;
28922e334057SEric Paris 
2893626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
28942e334057SEric Paris 	if (rc2)
28952e334057SEric Paris 		return rc2;
28962e334057SEric Paris 	return rc;
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
29001da177e4SLinus Torvalds {
290188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2902bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
290395dbf739SEric Paris 	__u32 av = FILE__WRITE;
29041da177e4SLinus Torvalds 
2905bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2906bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
2907bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2908bc6a6008SAmerigo Wang 			      ATTR_FORCE);
2909bc6a6008SAmerigo Wang 		if (!ia_valid)
29101da177e4SLinus Torvalds 			return 0;
2911bc6a6008SAmerigo Wang 	}
29121da177e4SLinus Torvalds 
2913bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2914bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
29152875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
29161da177e4SLinus Torvalds 
29173d2195c3SEric Paris 	if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
291895dbf739SEric Paris 		av |= FILE__OPEN;
291995dbf739SEric Paris 
292095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29233f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
29241da177e4SLinus Torvalds {
29253f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds 
29288f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2929b5376771SSerge E. Hallyn {
293088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
293188e67f3bSDavid Howells 
2932b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2933b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2934b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2935b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2936b5376771SSerge E. Hallyn 				return -EPERM;
2937b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2938b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2939b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2940b5376771SSerge E. Hallyn 			return -EPERM;
2941b5376771SSerge E. Hallyn 		}
2942b5376771SSerge E. Hallyn 	}
2943b5376771SSerge E. Hallyn 
2944b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2945b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
29462875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__SETATTR);
2947b5376771SSerge E. Hallyn }
2948b5376771SSerge E. Hallyn 
29498f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
29508f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
29511da177e4SLinus Torvalds {
2952c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
29531da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29541da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
29552bf49690SThomas Liu 	struct common_audit_data ad;
2956275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
29571da177e4SLinus Torvalds 	int rc = 0;
29581da177e4SLinus Torvalds 
2959b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2960b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
296312f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
29641da177e4SLinus Torvalds 		return -EOPNOTSUPP;
29651da177e4SLinus Torvalds 
29662e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
29671da177e4SLinus Torvalds 		return -EPERM;
29681da177e4SLinus Torvalds 
296950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2970a269434dSEric Paris 	ad.u.dentry = dentry;
29711da177e4SLinus Torvalds 
2972275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
29731da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
29741da177e4SLinus Torvalds 	if (rc)
29751da177e4SLinus Torvalds 		return rc;
29761da177e4SLinus Torvalds 
297752a4c640SNikolay Aleksandrov 	rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
297812b29f34SStephen Smalley 	if (rc == -EINVAL) {
2979d6ea83ecSEric Paris 		if (!capable(CAP_MAC_ADMIN)) {
2980d6ea83ecSEric Paris 			struct audit_buffer *ab;
2981d6ea83ecSEric Paris 			size_t audit_size;
2982d6ea83ecSEric Paris 			const char *str;
2983d6ea83ecSEric Paris 
2984d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
2985d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
2986e3fea3f7SAl Viro 			if (value) {
2987d6ea83ecSEric Paris 				str = value;
2988d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
2989d6ea83ecSEric Paris 					audit_size = size - 1;
2990d6ea83ecSEric Paris 				else
2991d6ea83ecSEric Paris 					audit_size = size;
2992e3fea3f7SAl Viro 			} else {
2993e3fea3f7SAl Viro 				str = "";
2994e3fea3f7SAl Viro 				audit_size = 0;
2995e3fea3f7SAl Viro 			}
2996d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2997d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
2998d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
2999d6ea83ecSEric Paris 			audit_log_end(ab);
3000d6ea83ecSEric Paris 
300112b29f34SStephen Smalley 			return rc;
3002d6ea83ecSEric Paris 		}
300312b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
300412b29f34SStephen Smalley 	}
30051da177e4SLinus Torvalds 	if (rc)
30061da177e4SLinus Torvalds 		return rc;
30071da177e4SLinus Torvalds 
3008275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
30091da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
30101da177e4SLinus Torvalds 	if (rc)
30111da177e4SLinus Torvalds 		return rc;
30121da177e4SLinus Torvalds 
3013275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
30141da177e4SLinus Torvalds 					  isec->sclass);
30151da177e4SLinus Torvalds 	if (rc)
30161da177e4SLinus Torvalds 		return rc;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 	return avc_has_perm(newsid,
30191da177e4SLinus Torvalds 			    sbsec->sid,
30201da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
30211da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
30221da177e4SLinus Torvalds 			    &ad);
30231da177e4SLinus Torvalds }
30241da177e4SLinus Torvalds 
30258f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
30268f0cfa52SDavid Howells 					const void *value, size_t size,
30278f0cfa52SDavid Howells 					int flags)
30281da177e4SLinus Torvalds {
3029c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
30301da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
30311da177e4SLinus Torvalds 	u32 newsid;
30321da177e4SLinus Torvalds 	int rc;
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
30351da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
30361da177e4SLinus Torvalds 		return;
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 
303912b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
30401da177e4SLinus Torvalds 	if (rc) {
304112b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
304212b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
304312b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
30441da177e4SLinus Torvalds 		return;
30451da177e4SLinus Torvalds 	}
30461da177e4SLinus Torvalds 
3047aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
30481da177e4SLinus Torvalds 	isec->sid = newsid;
3049aa9c2669SDavid Quigley 	isec->initialized = 1;
3050aa9c2669SDavid Quigley 
30511da177e4SLinus Torvalds 	return;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30548f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
30551da177e4SLinus Torvalds {
305688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
305788e67f3bSDavid Howells 
30582875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
30621da177e4SLinus Torvalds {
306388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
306488e67f3bSDavid Howells 
30652875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
30688f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
30691da177e4SLinus Torvalds {
3070b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
3071b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
30741da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
30751da177e4SLinus Torvalds 	return -EACCES;
30761da177e4SLinus Torvalds }
30771da177e4SLinus Torvalds 
3078d381d8a9SJames Morris /*
3079abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3080d381d8a9SJames Morris  *
3081d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3082d381d8a9SJames Morris  */
308342492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
30841da177e4SLinus Torvalds {
308542492594SDavid P. Quigley 	u32 size;
308642492594SDavid P. Quigley 	int error;
308742492594SDavid P. Quigley 	char *context = NULL;
30881da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
30891da177e4SLinus Torvalds 
30908c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
30918c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
30921da177e4SLinus Torvalds 
3093abc69bb6SStephen Smalley 	/*
3094abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3095abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3096abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3097abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3098abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3099abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3100abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3101abc69bb6SStephen Smalley 	 */
3102b1d9e6b0SCasey Schaufler 	error = cap_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3103b1d9e6b0SCasey Schaufler 			    SECURITY_CAP_NOAUDIT);
3104b1d9e6b0SCasey Schaufler 	if (!error)
3105b1d9e6b0SCasey Schaufler 		error = cred_has_capability(current_cred(), CAP_MAC_ADMIN,
31063699c53cSDavid Howells 					    SECURITY_CAP_NOAUDIT);
3107abc69bb6SStephen Smalley 	if (!error)
3108abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
3109abc69bb6SStephen Smalley 						      &size);
3110abc69bb6SStephen Smalley 	else
311142492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
311242492594SDavid P. Quigley 	if (error)
311342492594SDavid P. Quigley 		return error;
311442492594SDavid P. Quigley 	error = size;
311542492594SDavid P. Quigley 	if (alloc) {
311642492594SDavid P. Quigley 		*buffer = context;
311742492594SDavid P. Quigley 		goto out_nofree;
311842492594SDavid P. Quigley 	}
311942492594SDavid P. Quigley 	kfree(context);
312042492594SDavid P. Quigley out_nofree:
312142492594SDavid P. Quigley 	return error;
31221da177e4SLinus Torvalds }
31231da177e4SLinus Torvalds 
31241da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
31251da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
31261da177e4SLinus Torvalds {
31271da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
31281da177e4SLinus Torvalds 	u32 newsid;
31291da177e4SLinus Torvalds 	int rc;
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
31321da177e4SLinus Torvalds 		return -EOPNOTSUPP;
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 	if (!value || !size)
31351da177e4SLinus Torvalds 		return -EACCES;
31361da177e4SLinus Torvalds 
313752a4c640SNikolay Aleksandrov 	rc = security_context_to_sid((void *)value, size, &newsid, GFP_KERNEL);
31381da177e4SLinus Torvalds 	if (rc)
31391da177e4SLinus Torvalds 		return rc;
31401da177e4SLinus Torvalds 
3141aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
31421da177e4SLinus Torvalds 	isec->sid = newsid;
3143ddd29ec6SDavid P. Quigley 	isec->initialized = 1;
31441da177e4SLinus Torvalds 	return 0;
31451da177e4SLinus Torvalds }
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
31481da177e4SLinus Torvalds {
31491da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
31501da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
31511da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
31521da177e4SLinus Torvalds 	return len;
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
3155713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3156713a04aeSAhmed S. Darwish {
3157713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
3158713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3159713a04aeSAhmed S. Darwish }
3160713a04aeSAhmed S. Darwish 
31611da177e4SLinus Torvalds /* file security operations */
31621da177e4SLinus Torvalds 
3163788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
31641da177e4SLinus Torvalds {
316588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3166496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
31671da177e4SLinus Torvalds 
31681da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
31691da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
31701da177e4SLinus Torvalds 		mask |= MAY_APPEND;
31711da177e4SLinus Torvalds 
3172389fb800SPaul Moore 	return file_has_perm(cred, file,
31731da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
31741da177e4SLinus Torvalds }
31751da177e4SLinus Torvalds 
3176788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3177788e7dd4SYuichi Nakamura {
3178496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
317920dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
318020dda18bSStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
318120dda18bSStephen Smalley 	u32 sid = current_sid();
318220dda18bSStephen Smalley 
3183389fb800SPaul Moore 	if (!mask)
3184788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3185788e7dd4SYuichi Nakamura 		return 0;
3186788e7dd4SYuichi Nakamura 
318720dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
318820dda18bSStephen Smalley 	    fsec->pseqno == avc_policy_seqno())
318983d49856SEric Paris 		/* No change since file_open check. */
319020dda18bSStephen Smalley 		return 0;
319120dda18bSStephen Smalley 
3192788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3193788e7dd4SYuichi Nakamura }
3194788e7dd4SYuichi Nakamura 
31951da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
31961da177e4SLinus Torvalds {
31971da177e4SLinus Torvalds 	return file_alloc_security(file);
31981da177e4SLinus Torvalds }
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
32011da177e4SLinus Torvalds {
32021da177e4SLinus Torvalds 	file_free_security(file);
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
32061da177e4SLinus Torvalds 			      unsigned long arg)
32071da177e4SLinus Torvalds {
320888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
32090b24dcb7SEric Paris 	int error = 0;
32101da177e4SLinus Torvalds 
32110b24dcb7SEric Paris 	switch (cmd) {
32120b24dcb7SEric Paris 	case FIONREAD:
32130b24dcb7SEric Paris 	/* fall through */
32140b24dcb7SEric Paris 	case FIBMAP:
32150b24dcb7SEric Paris 	/* fall through */
32160b24dcb7SEric Paris 	case FIGETBSZ:
32170b24dcb7SEric Paris 	/* fall through */
32182f99c369SAl Viro 	case FS_IOC_GETFLAGS:
32190b24dcb7SEric Paris 	/* fall through */
32202f99c369SAl Viro 	case FS_IOC_GETVERSION:
32210b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
32220b24dcb7SEric Paris 		break;
32231da177e4SLinus Torvalds 
32242f99c369SAl Viro 	case FS_IOC_SETFLAGS:
32250b24dcb7SEric Paris 	/* fall through */
32262f99c369SAl Viro 	case FS_IOC_SETVERSION:
32270b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
32280b24dcb7SEric Paris 		break;
32290b24dcb7SEric Paris 
32300b24dcb7SEric Paris 	/* sys_ioctl() checks */
32310b24dcb7SEric Paris 	case FIONBIO:
32320b24dcb7SEric Paris 	/* fall through */
32330b24dcb7SEric Paris 	case FIOASYNC:
32340b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
32350b24dcb7SEric Paris 		break;
32360b24dcb7SEric Paris 
32370b24dcb7SEric Paris 	case KDSKBENT:
32380b24dcb7SEric Paris 	case KDSKBSENT:
32396a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
32400b24dcb7SEric Paris 					    SECURITY_CAP_AUDIT);
32410b24dcb7SEric Paris 		break;
32420b24dcb7SEric Paris 
32430b24dcb7SEric Paris 	/* default case assumes that the command will go
32440b24dcb7SEric Paris 	 * to the file's ioctl() function.
32450b24dcb7SEric Paris 	 */
32460b24dcb7SEric Paris 	default:
32470b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__IOCTL);
32480b24dcb7SEric Paris 	}
32490b24dcb7SEric Paris 	return error;
32501da177e4SLinus Torvalds }
32511da177e4SLinus Torvalds 
3252fcaaade1SStephen Smalley static int default_noexec;
3253fcaaade1SStephen Smalley 
32541da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
32551da177e4SLinus Torvalds {
325688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3257d84f4f99SDavid Howells 	int rc = 0;
325888e67f3bSDavid Howells 
3259fcaaade1SStephen Smalley 	if (default_noexec &&
3260fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
32611da177e4SLinus Torvalds 		/*
32621da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
32631da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
32641da177e4SLinus Torvalds 		 * This has an additional check.
32651da177e4SLinus Torvalds 		 */
3266d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
32671da177e4SLinus Torvalds 		if (rc)
3268d84f4f99SDavid Howells 			goto error;
32691da177e4SLinus Torvalds 	}
32701da177e4SLinus Torvalds 
32711da177e4SLinus Torvalds 	if (file) {
32721da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
32731da177e4SLinus Torvalds 		u32 av = FILE__READ;
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
32761da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
32771da177e4SLinus Torvalds 			av |= FILE__WRITE;
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
32801da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
32811da177e4SLinus Torvalds 
328288e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
32831da177e4SLinus Torvalds 	}
3284d84f4f99SDavid Howells 
3285d84f4f99SDavid Howells error:
3286d84f4f99SDavid Howells 	return rc;
32871da177e4SLinus Torvalds }
32881da177e4SLinus Torvalds 
3289e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
32901da177e4SLinus Torvalds {
3291b1d9e6b0SCasey Schaufler 	int rc = 0;
329298883bfdSPaul Moore 
329398883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
329498883bfdSPaul Moore 		u32 sid = current_sid();
329598883bfdSPaul Moore 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
329698883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
329798883bfdSPaul Moore 	}
329898883bfdSPaul Moore 
329998883bfdSPaul Moore 	return rc;
3300e5467859SAl Viro }
33011da177e4SLinus Torvalds 
3302e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3303e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3304e5467859SAl Viro {
33051da177e4SLinus Torvalds 	if (selinux_checkreqprot)
33061da177e4SLinus Torvalds 		prot = reqprot;
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
33091da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
33101da177e4SLinus Torvalds }
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
33131da177e4SLinus Torvalds 				 unsigned long reqprot,
33141da177e4SLinus Torvalds 				 unsigned long prot)
33151da177e4SLinus Torvalds {
331688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 	if (selinux_checkreqprot)
33191da177e4SLinus Torvalds 		prot = reqprot;
33201da177e4SLinus Torvalds 
3321fcaaade1SStephen Smalley 	if (default_noexec &&
3322fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3323d541bbeeSJames Morris 		int rc = 0;
3324db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3325db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3326d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3327db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
33286b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
33296b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
33303b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3331db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3332db4c9641SStephen Smalley 			/*
3333db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3334db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3335db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3336db4c9641SStephen Smalley 			 * modified content.  This typically should only
3337db4c9641SStephen Smalley 			 * occur for text relocations.
3338db4c9641SStephen Smalley 			 */
3339d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3340db4c9641SStephen Smalley 		}
33416b992197SLorenzo Hernandez García-Hierro 		if (rc)
33426b992197SLorenzo Hernandez García-Hierro 			return rc;
33436b992197SLorenzo Hernandez García-Hierro 	}
33441da177e4SLinus Torvalds 
33451da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
33461da177e4SLinus Torvalds }
33471da177e4SLinus Torvalds 
33481da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
33491da177e4SLinus Torvalds {
335088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
335188e67f3bSDavid Howells 
335288e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
33531da177e4SLinus Torvalds }
33541da177e4SLinus Torvalds 
33551da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
33561da177e4SLinus Torvalds 			      unsigned long arg)
33571da177e4SLinus Torvalds {
335888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
33591da177e4SLinus Torvalds 	int err = 0;
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	switch (cmd) {
33621da177e4SLinus Torvalds 	case F_SETFL:
33631da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
336488e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
33651da177e4SLinus Torvalds 			break;
33661da177e4SLinus Torvalds 		}
33671da177e4SLinus Torvalds 		/* fall through */
33681da177e4SLinus Torvalds 	case F_SETOWN:
33691da177e4SLinus Torvalds 	case F_SETSIG:
33701da177e4SLinus Torvalds 	case F_GETFL:
33711da177e4SLinus Torvalds 	case F_GETOWN:
33721da177e4SLinus Torvalds 	case F_GETSIG:
33731d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
33741da177e4SLinus Torvalds 		/* Just check FD__USE permission */
337588e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
33761da177e4SLinus Torvalds 		break;
33771da177e4SLinus Torvalds 	case F_GETLK:
33781da177e4SLinus Torvalds 	case F_SETLK:
33791da177e4SLinus Torvalds 	case F_SETLKW:
33800d3f7a2dSJeff Layton 	case F_OFD_GETLK:
33810d3f7a2dSJeff Layton 	case F_OFD_SETLK:
33820d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
33831da177e4SLinus Torvalds #if BITS_PER_LONG == 32
33841da177e4SLinus Torvalds 	case F_GETLK64:
33851da177e4SLinus Torvalds 	case F_SETLK64:
33861da177e4SLinus Torvalds 	case F_SETLKW64:
33871da177e4SLinus Torvalds #endif
338888e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
33891da177e4SLinus Torvalds 		break;
33901da177e4SLinus Torvalds 	}
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	return err;
33931da177e4SLinus Torvalds }
33941da177e4SLinus Torvalds 
3395e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
33961da177e4SLinus Torvalds {
33971da177e4SLinus Torvalds 	struct file_security_struct *fsec;
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 	fsec = file->f_security;
3400275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
34011da177e4SLinus Torvalds }
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
34041da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
34051da177e4SLinus Torvalds {
34061da177e4SLinus Torvalds 	struct file *file;
340765c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
34081da177e4SLinus Torvalds 	u32 perm;
34091da177e4SLinus Torvalds 	struct file_security_struct *fsec;
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3412b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	fsec = file->f_security;
34151da177e4SLinus Torvalds 
34161da177e4SLinus Torvalds 	if (!signum)
34171da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
34181da177e4SLinus Torvalds 	else
34191da177e4SLinus Torvalds 		perm = signal_to_av(signum);
34201da177e4SLinus Torvalds 
3421275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
34221da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
34231da177e4SLinus Torvalds }
34241da177e4SLinus Torvalds 
34251da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
34261da177e4SLinus Torvalds {
342788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
342888e67f3bSDavid Howells 
342988e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
34301da177e4SLinus Torvalds }
34311da177e4SLinus Torvalds 
343283d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3433788e7dd4SYuichi Nakamura {
3434788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3435788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3436d84f4f99SDavid Howells 
3437788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3438496ad9aaSAl Viro 	isec = file_inode(file)->i_security;
3439788e7dd4SYuichi Nakamura 	/*
3440788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3441788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3442788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3443788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3444788e7dd4SYuichi Nakamura 	 * struct as its SID.
3445788e7dd4SYuichi Nakamura 	 */
3446788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3447788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3448788e7dd4SYuichi Nakamura 	/*
3449788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3450788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3451788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3452788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3453788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3454788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3455788e7dd4SYuichi Nakamura 	 */
345613f8e981SDavid Howells 	return file_path_has_perm(cred, file, open_file_to_av(file));
3457788e7dd4SYuichi Nakamura }
3458788e7dd4SYuichi Nakamura 
34591da177e4SLinus Torvalds /* task security operations */
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
34621da177e4SLinus Torvalds {
34633b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
34641da177e4SLinus Torvalds }
34651da177e4SLinus Torvalds 
3466f1752eecSDavid Howells /*
3467ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3468ee18d64cSDavid Howells  */
3469ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3470ee18d64cSDavid Howells {
3471ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3472ee18d64cSDavid Howells 
3473ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3474ee18d64cSDavid Howells 	if (!tsec)
3475ee18d64cSDavid Howells 		return -ENOMEM;
3476ee18d64cSDavid Howells 
3477ee18d64cSDavid Howells 	cred->security = tsec;
3478ee18d64cSDavid Howells 	return 0;
3479ee18d64cSDavid Howells }
3480ee18d64cSDavid Howells 
3481ee18d64cSDavid Howells /*
3482f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3483f1752eecSDavid Howells  */
3484f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
34851da177e4SLinus Torvalds {
3486f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3487e0e81739SDavid Howells 
34882edeaa34STetsuo Handa 	/*
34892edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
34902edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
34912edeaa34STetsuo Handa 	 */
34922edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3493e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3494f1752eecSDavid Howells 	kfree(tsec);
34951da177e4SLinus Torvalds }
34961da177e4SLinus Torvalds 
3497d84f4f99SDavid Howells /*
3498d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3499d84f4f99SDavid Howells  */
3500d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3501d84f4f99SDavid Howells 				gfp_t gfp)
3502d84f4f99SDavid Howells {
3503d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3504d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3505d84f4f99SDavid Howells 
3506d84f4f99SDavid Howells 	old_tsec = old->security;
3507d84f4f99SDavid Howells 
3508d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3509d84f4f99SDavid Howells 	if (!tsec)
3510d84f4f99SDavid Howells 		return -ENOMEM;
3511d84f4f99SDavid Howells 
3512d84f4f99SDavid Howells 	new->security = tsec;
3513d84f4f99SDavid Howells 	return 0;
3514d84f4f99SDavid Howells }
3515d84f4f99SDavid Howells 
3516d84f4f99SDavid Howells /*
3517ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3518ee18d64cSDavid Howells  */
3519ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3520ee18d64cSDavid Howells {
3521ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3522ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3523ee18d64cSDavid Howells 
3524ee18d64cSDavid Howells 	*tsec = *old_tsec;
3525ee18d64cSDavid Howells }
3526ee18d64cSDavid Howells 
3527ee18d64cSDavid Howells /*
35283a3b7ce9SDavid Howells  * set the security data for a kernel service
35293a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
35303a3b7ce9SDavid Howells  */
35313a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
35323a3b7ce9SDavid Howells {
35333a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
35343a3b7ce9SDavid Howells 	u32 sid = current_sid();
35353a3b7ce9SDavid Howells 	int ret;
35363a3b7ce9SDavid Howells 
35373a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
35383a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
35393a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
35403a3b7ce9SDavid Howells 			   NULL);
35413a3b7ce9SDavid Howells 	if (ret == 0) {
35423a3b7ce9SDavid Howells 		tsec->sid = secid;
35433a3b7ce9SDavid Howells 		tsec->create_sid = 0;
35443a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
35453a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
35463a3b7ce9SDavid Howells 	}
35473a3b7ce9SDavid Howells 	return ret;
35483a3b7ce9SDavid Howells }
35493a3b7ce9SDavid Howells 
35503a3b7ce9SDavid Howells /*
35513a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
35523a3b7ce9SDavid Howells  * objective context of the specified inode
35533a3b7ce9SDavid Howells  */
35543a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
35553a3b7ce9SDavid Howells {
35563a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
35573a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
35583a3b7ce9SDavid Howells 	u32 sid = current_sid();
35593a3b7ce9SDavid Howells 	int ret;
35603a3b7ce9SDavid Howells 
35613a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
35623a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
35633a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
35643a3b7ce9SDavid Howells 			   NULL);
35653a3b7ce9SDavid Howells 
35663a3b7ce9SDavid Howells 	if (ret == 0)
35673a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3568ef57471aSDavid Howells 	return ret;
35693a3b7ce9SDavid Howells }
35703a3b7ce9SDavid Howells 
3571dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
357225354c4fSEric Paris {
3573dd8dbf2eSEric Paris 	u32 sid;
3574dd8dbf2eSEric Paris 	struct common_audit_data ad;
3575dd8dbf2eSEric Paris 
3576dd8dbf2eSEric Paris 	sid = task_sid(current);
3577dd8dbf2eSEric Paris 
357850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3579dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3580dd8dbf2eSEric Paris 
3581dd8dbf2eSEric Paris 	return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3582dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
358325354c4fSEric Paris }
358425354c4fSEric Paris 
35851da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
35861da177e4SLinus Torvalds {
35873b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
35881da177e4SLinus Torvalds }
35891da177e4SLinus Torvalds 
35901da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
35911da177e4SLinus Torvalds {
35923b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
35931da177e4SLinus Torvalds }
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
35961da177e4SLinus Torvalds {
35973b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
35981da177e4SLinus Torvalds }
35991da177e4SLinus Torvalds 
3600f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3601f9008e4cSDavid Quigley {
3602275bb41eSDavid Howells 	*secid = task_sid(p);
3603f9008e4cSDavid Quigley }
3604f9008e4cSDavid Quigley 
36051da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
36061da177e4SLinus Torvalds {
36073b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
36081da177e4SLinus Torvalds }
36091da177e4SLinus Torvalds 
361003e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
361103e68060SJames Morris {
36123b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
361303e68060SJames Morris }
361403e68060SJames Morris 
3615a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3616a1836a42SDavid Quigley {
36173b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3618a1836a42SDavid Quigley }
3619a1836a42SDavid Quigley 
36208fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
36218fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
36221da177e4SLinus Torvalds {
36238fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
36241da177e4SLinus Torvalds 
36251da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
36261da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
36271da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3628d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
36291da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
36308fd00b4dSJiri Slaby 		return current_has_perm(p, PROCESS__SETRLIMIT);
36311da177e4SLinus Torvalds 
36321da177e4SLinus Torvalds 	return 0;
36331da177e4SLinus Torvalds }
36341da177e4SLinus Torvalds 
3635b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
36361da177e4SLinus Torvalds {
36373b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
36381da177e4SLinus Torvalds }
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
36411da177e4SLinus Torvalds {
36423b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
36431da177e4SLinus Torvalds }
36441da177e4SLinus Torvalds 
364535601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
364635601547SDavid Quigley {
36473b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
364835601547SDavid Quigley }
364935601547SDavid Quigley 
3650f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3651f9008e4cSDavid Quigley 				int sig, u32 secid)
36521da177e4SLinus Torvalds {
36531da177e4SLinus Torvalds 	u32 perm;
36541da177e4SLinus Torvalds 	int rc;
36551da177e4SLinus Torvalds 
36561da177e4SLinus Torvalds 	if (!sig)
36571da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
36581da177e4SLinus Torvalds 	else
36591da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3660f9008e4cSDavid Quigley 	if (secid)
3661275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3662275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3663f9008e4cSDavid Quigley 	else
36643b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3665f9008e4cSDavid Quigley 	return rc;
36661da177e4SLinus Torvalds }
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
36691da177e4SLinus Torvalds {
36708a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
36711da177e4SLinus Torvalds }
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
36741da177e4SLinus Torvalds 				  struct inode *inode)
36751da177e4SLinus Torvalds {
36761da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3677275bb41eSDavid Howells 	u32 sid = task_sid(p);
36781da177e4SLinus Torvalds 
3679275bb41eSDavid Howells 	isec->sid = sid;
36801da177e4SLinus Torvalds 	isec->initialized = 1;
36811da177e4SLinus Torvalds }
36821da177e4SLinus Torvalds 
36831da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
368467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
36852bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
36861da177e4SLinus Torvalds {
36871da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
36881da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
36891da177e4SLinus Torvalds 
3690bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
36911da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
36921da177e4SLinus Torvalds 	if (ih == NULL)
36931da177e4SLinus Torvalds 		goto out;
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
36961da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
36971da177e4SLinus Torvalds 		goto out;
36981da177e4SLinus Torvalds 
369948c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
370048c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
37011da177e4SLinus Torvalds 	ret = 0;
37021da177e4SLinus Torvalds 
370367f83cbfSVenkat Yekkirala 	if (proto)
370467f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
370567f83cbfSVenkat Yekkirala 
37061da177e4SLinus Torvalds 	switch (ih->protocol) {
37071da177e4SLinus Torvalds 	case IPPROTO_TCP: {
37081da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
37111da177e4SLinus Torvalds 			break;
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds 		offset += ihlen;
37141da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
37151da177e4SLinus Torvalds 		if (th == NULL)
37161da177e4SLinus Torvalds 			break;
37171da177e4SLinus Torvalds 
371848c62af6SEric Paris 		ad->u.net->sport = th->source;
371948c62af6SEric Paris 		ad->u.net->dport = th->dest;
37201da177e4SLinus Torvalds 		break;
37211da177e4SLinus Torvalds 	}
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds 	case IPPROTO_UDP: {
37241da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
37271da177e4SLinus Torvalds 			break;
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds 		offset += ihlen;
37301da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
37311da177e4SLinus Torvalds 		if (uh == NULL)
37321da177e4SLinus Torvalds 			break;
37331da177e4SLinus Torvalds 
373448c62af6SEric Paris 		ad->u.net->sport = uh->source;
373548c62af6SEric Paris 		ad->u.net->dport = uh->dest;
37361da177e4SLinus Torvalds 		break;
37371da177e4SLinus Torvalds 	}
37381da177e4SLinus Torvalds 
37392ee92d46SJames Morris 	case IPPROTO_DCCP: {
37402ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
37412ee92d46SJames Morris 
37422ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
37432ee92d46SJames Morris 			break;
37442ee92d46SJames Morris 
37452ee92d46SJames Morris 		offset += ihlen;
37462ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
37472ee92d46SJames Morris 		if (dh == NULL)
37482ee92d46SJames Morris 			break;
37492ee92d46SJames Morris 
375048c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
375148c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
37522ee92d46SJames Morris 		break;
37532ee92d46SJames Morris 	}
37542ee92d46SJames Morris 
37551da177e4SLinus Torvalds 	default:
37561da177e4SLinus Torvalds 		break;
37571da177e4SLinus Torvalds 	}
37581da177e4SLinus Torvalds out:
37591da177e4SLinus Torvalds 	return ret;
37601da177e4SLinus Torvalds }
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
376567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
37662bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
37671da177e4SLinus Torvalds {
37681da177e4SLinus Torvalds 	u8 nexthdr;
37691da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
37701da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
377175f2811cSJesse Gross 	__be16 frag_off;
37721da177e4SLinus Torvalds 
3773bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
37741da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
37751da177e4SLinus Torvalds 	if (ip6 == NULL)
37761da177e4SLinus Torvalds 		goto out;
37771da177e4SLinus Torvalds 
377848c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
377948c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
37801da177e4SLinus Torvalds 	ret = 0;
37811da177e4SLinus Torvalds 
37821da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
37831da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
378475f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
37851da177e4SLinus Torvalds 	if (offset < 0)
37861da177e4SLinus Torvalds 		goto out;
37871da177e4SLinus Torvalds 
378867f83cbfSVenkat Yekkirala 	if (proto)
378967f83cbfSVenkat Yekkirala 		*proto = nexthdr;
379067f83cbfSVenkat Yekkirala 
37911da177e4SLinus Torvalds 	switch (nexthdr) {
37921da177e4SLinus Torvalds 	case IPPROTO_TCP: {
37931da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
37941da177e4SLinus Torvalds 
37951da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
37961da177e4SLinus Torvalds 		if (th == NULL)
37971da177e4SLinus Torvalds 			break;
37981da177e4SLinus Torvalds 
379948c62af6SEric Paris 		ad->u.net->sport = th->source;
380048c62af6SEric Paris 		ad->u.net->dport = th->dest;
38011da177e4SLinus Torvalds 		break;
38021da177e4SLinus Torvalds 	}
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	case IPPROTO_UDP: {
38051da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
38081da177e4SLinus Torvalds 		if (uh == NULL)
38091da177e4SLinus Torvalds 			break;
38101da177e4SLinus Torvalds 
381148c62af6SEric Paris 		ad->u.net->sport = uh->source;
381248c62af6SEric Paris 		ad->u.net->dport = uh->dest;
38131da177e4SLinus Torvalds 		break;
38141da177e4SLinus Torvalds 	}
38151da177e4SLinus Torvalds 
38162ee92d46SJames Morris 	case IPPROTO_DCCP: {
38172ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
38182ee92d46SJames Morris 
38192ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
38202ee92d46SJames Morris 		if (dh == NULL)
38212ee92d46SJames Morris 			break;
38222ee92d46SJames Morris 
382348c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
382448c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
38252ee92d46SJames Morris 		break;
38262ee92d46SJames Morris 	}
38272ee92d46SJames Morris 
38281da177e4SLinus Torvalds 	/* includes fragments */
38291da177e4SLinus Torvalds 	default:
38301da177e4SLinus Torvalds 		break;
38311da177e4SLinus Torvalds 	}
38321da177e4SLinus Torvalds out:
38331da177e4SLinus Torvalds 	return ret;
38341da177e4SLinus Torvalds }
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds #endif /* IPV6 */
38371da177e4SLinus Torvalds 
38382bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3839cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
38401da177e4SLinus Torvalds {
3841cf9481e2SDavid Howells 	char *addrp;
3842cf9481e2SDavid Howells 	int ret;
38431da177e4SLinus Torvalds 
384448c62af6SEric Paris 	switch (ad->u.net->family) {
38451da177e4SLinus Torvalds 	case PF_INET:
384667f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3847cf9481e2SDavid Howells 		if (ret)
3848cf9481e2SDavid Howells 			goto parse_error;
384948c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
385048c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
3851cf9481e2SDavid Howells 		goto okay;
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
38541da177e4SLinus Torvalds 	case PF_INET6:
385567f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3856cf9481e2SDavid Howells 		if (ret)
3857cf9481e2SDavid Howells 			goto parse_error;
385848c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
385948c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
3860cf9481e2SDavid Howells 		goto okay;
38611da177e4SLinus Torvalds #endif	/* IPV6 */
38621da177e4SLinus Torvalds 	default:
3863cf9481e2SDavid Howells 		addrp = NULL;
3864cf9481e2SDavid Howells 		goto okay;
38651da177e4SLinus Torvalds 	}
38661da177e4SLinus Torvalds 
3867cf9481e2SDavid Howells parse_error:
386871f1cb05SPaul Moore 	printk(KERN_WARNING
386971f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
387071f1cb05SPaul Moore 	       " unable to parse packet\n");
38711da177e4SLinus Torvalds 	return ret;
3872cf9481e2SDavid Howells 
3873cf9481e2SDavid Howells okay:
3874cf9481e2SDavid Howells 	if (_addrp)
3875cf9481e2SDavid Howells 		*_addrp = addrp;
3876cf9481e2SDavid Howells 	return 0;
38771da177e4SLinus Torvalds }
38781da177e4SLinus Torvalds 
38794f6a993fSPaul Moore /**
3880220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
38814f6a993fSPaul Moore  * @skb: the packet
388275e22910SPaul Moore  * @family: protocol family
3883220deb96SPaul Moore  * @sid: the packet's peer label SID
38844f6a993fSPaul Moore  *
38854f6a993fSPaul Moore  * Description:
3886220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3887220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3888220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3889220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3890220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3891220deb96SPaul Moore  * peer labels.
38924f6a993fSPaul Moore  *
38934f6a993fSPaul Moore  */
3894220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
38954f6a993fSPaul Moore {
389671f1cb05SPaul Moore 	int err;
38974f6a993fSPaul Moore 	u32 xfrm_sid;
38984f6a993fSPaul Moore 	u32 nlbl_sid;
3899220deb96SPaul Moore 	u32 nlbl_type;
39004f6a993fSPaul Moore 
3901817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
3902bed4d7efSPaul Moore 	if (unlikely(err))
3903bed4d7efSPaul Moore 		return -EACCES;
3904bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3905bed4d7efSPaul Moore 	if (unlikely(err))
3906bed4d7efSPaul Moore 		return -EACCES;
3907220deb96SPaul Moore 
390871f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
390971f1cb05SPaul Moore 	if (unlikely(err)) {
391071f1cb05SPaul Moore 		printk(KERN_WARNING
391171f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
391271f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3913220deb96SPaul Moore 		return -EACCES;
391471f1cb05SPaul Moore 	}
3915220deb96SPaul Moore 
3916220deb96SPaul Moore 	return 0;
39174f6a993fSPaul Moore }
39184f6a993fSPaul Moore 
3919446b8024SPaul Moore /**
3920446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
3921446b8024SPaul Moore  * @sk_sid: the parent socket's SID
3922446b8024SPaul Moore  * @skb_sid: the packet's SID
3923446b8024SPaul Moore  * @conn_sid: the resulting connection SID
3924446b8024SPaul Moore  *
3925446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
3926446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
3927446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
3928446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
3929446b8024SPaul Moore  *
3930446b8024SPaul Moore  */
3931446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3932446b8024SPaul Moore {
3933446b8024SPaul Moore 	int err = 0;
3934446b8024SPaul Moore 
3935446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
3936446b8024SPaul Moore 		err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3937446b8024SPaul Moore 	else
3938446b8024SPaul Moore 		*conn_sid = sk_sid;
3939446b8024SPaul Moore 
3940446b8024SPaul Moore 	return err;
3941446b8024SPaul Moore }
3942446b8024SPaul Moore 
39431da177e4SLinus Torvalds /* socket security operations */
3944d4f2d978SPaul Moore 
39452ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
39462ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
3947d4f2d978SPaul Moore {
39482ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
39492ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
39502ad18bdfSHarry Ciao 		return 0;
39512ad18bdfSHarry Ciao 	}
39522ad18bdfSHarry Ciao 
39532ad18bdfSHarry Ciao 	return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
39542ad18bdfSHarry Ciao 				       socksid);
3955d4f2d978SPaul Moore }
3956d4f2d978SPaul Moore 
3957253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
39581da177e4SLinus Torvalds {
3959253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
39602bf49690SThomas Liu 	struct common_audit_data ad;
396148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
3962253bfae6SPaul Moore 	u32 tsid = task_sid(task);
39631da177e4SLinus Torvalds 
3964253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
3965253bfae6SPaul Moore 		return 0;
39661da177e4SLinus Torvalds 
396750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
396848c62af6SEric Paris 	ad.u.net = &net;
396948c62af6SEric Paris 	ad.u.net->sk = sk;
39701da177e4SLinus Torvalds 
3971253bfae6SPaul Moore 	return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
39721da177e4SLinus Torvalds }
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
39751da177e4SLinus Torvalds 				 int protocol, int kern)
39761da177e4SLinus Torvalds {
39775fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3978d4f2d978SPaul Moore 	u32 newsid;
3979275bb41eSDavid Howells 	u16 secclass;
39802ad18bdfSHarry Ciao 	int rc;
39811da177e4SLinus Torvalds 
39821da177e4SLinus Torvalds 	if (kern)
3983d4f2d978SPaul Moore 		return 0;
39841da177e4SLinus Torvalds 
3985275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
39862ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
39872ad18bdfSHarry Ciao 	if (rc)
39882ad18bdfSHarry Ciao 		return rc;
39892ad18bdfSHarry Ciao 
3990d4f2d978SPaul Moore 	return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
39911da177e4SLinus Torvalds }
39921da177e4SLinus Torvalds 
39937420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
39941da177e4SLinus Torvalds 				      int type, int protocol, int kern)
39951da177e4SLinus Torvalds {
39965fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3997d4f2d978SPaul Moore 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3998892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
3999275bb41eSDavid Howells 	int err = 0;
4000275bb41eSDavid Howells 
40012ad18bdfSHarry Ciao 	isec->sclass = socket_type_to_security_class(family, type, protocol);
40022ad18bdfSHarry Ciao 
4003275bb41eSDavid Howells 	if (kern)
4004275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
40052ad18bdfSHarry Ciao 	else {
40062ad18bdfSHarry Ciao 		err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
40072ad18bdfSHarry Ciao 		if (err)
40082ad18bdfSHarry Ciao 			return err;
40092ad18bdfSHarry Ciao 	}
4010275bb41eSDavid Howells 
40111da177e4SLinus Torvalds 	isec->initialized = 1;
40121da177e4SLinus Torvalds 
4013892c141eSVenkat Yekkirala 	if (sock->sk) {
4014892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
4015892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
4016220deb96SPaul Moore 		sksec->sclass = isec->sclass;
4017389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4018892c141eSVenkat Yekkirala 	}
4019892c141eSVenkat Yekkirala 
40207420ed23SVenkat Yekkirala 	return err;
40211da177e4SLinus Torvalds }
40221da177e4SLinus Torvalds 
40231da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
40241da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
40251da177e4SLinus Torvalds    permission check between the socket and the port number. */
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
40281da177e4SLinus Torvalds {
4029253bfae6SPaul Moore 	struct sock *sk = sock->sk;
40301da177e4SLinus Torvalds 	u16 family;
40311da177e4SLinus Torvalds 	int err;
40321da177e4SLinus Torvalds 
4033253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__BIND);
40341da177e4SLinus Torvalds 	if (err)
40351da177e4SLinus Torvalds 		goto out;
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 	/*
40381da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
403913402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
404013402580SJames Morris 	 * check the first address now.
40411da177e4SLinus Torvalds 	 */
4042253bfae6SPaul Moore 	family = sk->sk_family;
40431da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
40441da177e4SLinus Torvalds 		char *addrp;
4045253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
40462bf49690SThomas Liu 		struct common_audit_data ad;
404748c62af6SEric Paris 		struct lsm_network_audit net = {0,};
40481da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
40491da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
40501da177e4SLinus Torvalds 		unsigned short snum;
4051e399f982SJames Morris 		u32 sid, node_perm;
40521da177e4SLinus Torvalds 
40531da177e4SLinus Torvalds 		if (family == PF_INET) {
40541da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
40551da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
40561da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
40571da177e4SLinus Torvalds 		} else {
40581da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
40591da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
40601da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
40611da177e4SLinus Torvalds 		}
40621da177e4SLinus Torvalds 
4063227b60f5SStephen Hemminger 		if (snum) {
4064227b60f5SStephen Hemminger 			int low, high;
4065227b60f5SStephen Hemminger 
40660bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4067227b60f5SStephen Hemminger 
4068227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
40693e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
40703e112172SPaul Moore 						      snum, &sid);
40711da177e4SLinus Torvalds 				if (err)
40721da177e4SLinus Torvalds 					goto out;
407350c205f5SEric Paris 				ad.type = LSM_AUDIT_DATA_NET;
407448c62af6SEric Paris 				ad.u.net = &net;
407548c62af6SEric Paris 				ad.u.net->sport = htons(snum);
407648c62af6SEric Paris 				ad.u.net->family = family;
4077253bfae6SPaul Moore 				err = avc_has_perm(sksec->sid, sid,
4078253bfae6SPaul Moore 						   sksec->sclass,
40791da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
40801da177e4SLinus Torvalds 				if (err)
40811da177e4SLinus Torvalds 					goto out;
40821da177e4SLinus Torvalds 			}
4083227b60f5SStephen Hemminger 		}
40841da177e4SLinus Torvalds 
4085253bfae6SPaul Moore 		switch (sksec->sclass) {
408613402580SJames Morris 		case SECCLASS_TCP_SOCKET:
40871da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
40881da177e4SLinus Torvalds 			break;
40891da177e4SLinus Torvalds 
409013402580SJames Morris 		case SECCLASS_UDP_SOCKET:
40911da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
40921da177e4SLinus Torvalds 			break;
40931da177e4SLinus Torvalds 
40942ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
40952ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
40962ee92d46SJames Morris 			break;
40972ee92d46SJames Morris 
40981da177e4SLinus Torvalds 		default:
40991da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
41001da177e4SLinus Torvalds 			break;
41011da177e4SLinus Torvalds 		}
41021da177e4SLinus Torvalds 
4103224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
41041da177e4SLinus Torvalds 		if (err)
41051da177e4SLinus Torvalds 			goto out;
41061da177e4SLinus Torvalds 
410750c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
410848c62af6SEric Paris 		ad.u.net = &net;
410948c62af6SEric Paris 		ad.u.net->sport = htons(snum);
411048c62af6SEric Paris 		ad.u.net->family = family;
41111da177e4SLinus Torvalds 
41121da177e4SLinus Torvalds 		if (family == PF_INET)
411348c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
41141da177e4SLinus Torvalds 		else
411548c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
41161da177e4SLinus Torvalds 
4117253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid,
4118253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
41191da177e4SLinus Torvalds 		if (err)
41201da177e4SLinus Torvalds 			goto out;
41211da177e4SLinus Torvalds 	}
41221da177e4SLinus Torvalds out:
41231da177e4SLinus Torvalds 	return err;
41241da177e4SLinus Torvalds }
41251da177e4SLinus Torvalds 
41261da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
41271da177e4SLinus Torvalds {
4128014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4129253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
41301da177e4SLinus Torvalds 	int err;
41311da177e4SLinus Torvalds 
4132253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__CONNECT);
41331da177e4SLinus Torvalds 	if (err)
41341da177e4SLinus Torvalds 		return err;
41351da177e4SLinus Torvalds 
41361da177e4SLinus Torvalds 	/*
41372ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
41381da177e4SLinus Torvalds 	 */
4139253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4140253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_DCCP_SOCKET) {
41412bf49690SThomas Liu 		struct common_audit_data ad;
414248c62af6SEric Paris 		struct lsm_network_audit net = {0,};
41431da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
41441da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
41451da177e4SLinus Torvalds 		unsigned short snum;
41462ee92d46SJames Morris 		u32 sid, perm;
41471da177e4SLinus Torvalds 
41481da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
41491da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4150911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
41511da177e4SLinus Torvalds 				return -EINVAL;
41521da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
41531da177e4SLinus Torvalds 		} else {
41541da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4155911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
41561da177e4SLinus Torvalds 				return -EINVAL;
41571da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
41581da177e4SLinus Torvalds 		}
41591da177e4SLinus Torvalds 
41603e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
41611da177e4SLinus Torvalds 		if (err)
41621da177e4SLinus Torvalds 			goto out;
41631da177e4SLinus Torvalds 
4164253bfae6SPaul Moore 		perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
41652ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
41662ee92d46SJames Morris 
416750c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
416848c62af6SEric Paris 		ad.u.net = &net;
416948c62af6SEric Paris 		ad.u.net->dport = htons(snum);
417048c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
4171253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
41721da177e4SLinus Torvalds 		if (err)
41731da177e4SLinus Torvalds 			goto out;
41741da177e4SLinus Torvalds 	}
41751da177e4SLinus Torvalds 
4176014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
4177014ab19aSPaul Moore 
41781da177e4SLinus Torvalds out:
41791da177e4SLinus Torvalds 	return err;
41801da177e4SLinus Torvalds }
41811da177e4SLinus Torvalds 
41821da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
41831da177e4SLinus Torvalds {
4184253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
41851da177e4SLinus Torvalds }
41861da177e4SLinus Torvalds 
41871da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
41881da177e4SLinus Torvalds {
41891da177e4SLinus Torvalds 	int err;
41901da177e4SLinus Torvalds 	struct inode_security_struct *isec;
41911da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
41921da177e4SLinus Torvalds 
4193253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
41941da177e4SLinus Torvalds 	if (err)
41951da177e4SLinus Torvalds 		return err;
41961da177e4SLinus Torvalds 
41971da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
41981da177e4SLinus Torvalds 
41991da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
42001da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
42011da177e4SLinus Torvalds 	newisec->sid = isec->sid;
42021da177e4SLinus Torvalds 	newisec->initialized = 1;
42031da177e4SLinus Torvalds 
42041da177e4SLinus Torvalds 	return 0;
42051da177e4SLinus Torvalds }
42061da177e4SLinus Torvalds 
42071da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
42081da177e4SLinus Torvalds 				  int size)
42091da177e4SLinus Torvalds {
4210253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__WRITE);
42111da177e4SLinus Torvalds }
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
42141da177e4SLinus Torvalds 				  int size, int flags)
42151da177e4SLinus Torvalds {
4216253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__READ);
42171da177e4SLinus Torvalds }
42181da177e4SLinus Torvalds 
42191da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
42201da177e4SLinus Torvalds {
4221253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
42221da177e4SLinus Torvalds }
42231da177e4SLinus Torvalds 
42241da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
42251da177e4SLinus Torvalds {
4226253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
42271da177e4SLinus Torvalds }
42281da177e4SLinus Torvalds 
42291da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
42301da177e4SLinus Torvalds {
4231f8687afeSPaul Moore 	int err;
4232f8687afeSPaul Moore 
4233253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4234f8687afeSPaul Moore 	if (err)
4235f8687afeSPaul Moore 		return err;
4236f8687afeSPaul Moore 
4237f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
42381da177e4SLinus Torvalds }
42391da177e4SLinus Torvalds 
42401da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
42411da177e4SLinus Torvalds 				     int optname)
42421da177e4SLinus Torvalds {
4243253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
42441da177e4SLinus Torvalds }
42451da177e4SLinus Torvalds 
42461da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
42471da177e4SLinus Torvalds {
4248253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
42491da177e4SLinus Torvalds }
42501da177e4SLinus Torvalds 
42513610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
42523610cda5SDavid S. Miller 					      struct sock *other,
42531da177e4SLinus Torvalds 					      struct sock *newsk)
42541da177e4SLinus Torvalds {
42553610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
42563610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
42574d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
42582bf49690SThomas Liu 	struct common_audit_data ad;
425948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
42601da177e4SLinus Torvalds 	int err;
42611da177e4SLinus Torvalds 
426250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
426348c62af6SEric Paris 	ad.u.net = &net;
426448c62af6SEric Paris 	ad.u.net->sk = other;
42651da177e4SLinus Torvalds 
42664d1e2451SPaul Moore 	err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
42674d1e2451SPaul Moore 			   sksec_other->sclass,
42681da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
42691da177e4SLinus Torvalds 	if (err)
42701da177e4SLinus Torvalds 		return err;
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 	/* server child socket */
42734d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
42744d1e2451SPaul Moore 	err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
42754d1e2451SPaul Moore 				    &sksec_new->sid);
42764d1e2451SPaul Moore 	if (err)
42774237c75cSVenkat Yekkirala 		return err;
42784d1e2451SPaul Moore 
42794d1e2451SPaul Moore 	/* connecting socket */
42804d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
42814d1e2451SPaul Moore 
42824d1e2451SPaul Moore 	return 0;
42831da177e4SLinus Torvalds }
42841da177e4SLinus Torvalds 
42851da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
42861da177e4SLinus Torvalds 					struct socket *other)
42871da177e4SLinus Torvalds {
4288253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4289253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
42902bf49690SThomas Liu 	struct common_audit_data ad;
429148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
42921da177e4SLinus Torvalds 
429350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
429448c62af6SEric Paris 	ad.u.net = &net;
429548c62af6SEric Paris 	ad.u.net->sk = other->sk;
42961da177e4SLinus Torvalds 
4297253bfae6SPaul Moore 	return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4298253bfae6SPaul Moore 			    &ad);
42991da177e4SLinus Torvalds }
43001da177e4SLinus Torvalds 
4301cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4302cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
43032bf49690SThomas Liu 				    struct common_audit_data *ad)
4304effad8dfSPaul Moore {
4305effad8dfSPaul Moore 	int err;
4306effad8dfSPaul Moore 	u32 if_sid;
4307effad8dfSPaul Moore 	u32 node_sid;
4308effad8dfSPaul Moore 
4309cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4310effad8dfSPaul Moore 	if (err)
4311effad8dfSPaul Moore 		return err;
4312effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4313effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4314effad8dfSPaul Moore 	if (err)
4315effad8dfSPaul Moore 		return err;
4316effad8dfSPaul Moore 
4317effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4318effad8dfSPaul Moore 	if (err)
4319effad8dfSPaul Moore 		return err;
4320effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4321effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4322effad8dfSPaul Moore }
4323effad8dfSPaul Moore 
4324220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4325d8395c87SPaul Moore 				       u16 family)
4326220deb96SPaul Moore {
4327277d342fSPaul Moore 	int err = 0;
4328220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4329220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
43302bf49690SThomas Liu 	struct common_audit_data ad;
433148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4332d8395c87SPaul Moore 	char *addrp;
4333d8395c87SPaul Moore 
433450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
433548c62af6SEric Paris 	ad.u.net = &net;
433648c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
433748c62af6SEric Paris 	ad.u.net->family = family;
4338d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4339d8395c87SPaul Moore 	if (err)
4340d8395c87SPaul Moore 		return err;
4341220deb96SPaul Moore 
434258bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
4343220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4344d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4345220deb96SPaul Moore 		if (err)
4346220deb96SPaul Moore 			return err;
434758bfbb51SPaul Moore 	}
4348220deb96SPaul Moore 
4349d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4350220deb96SPaul Moore 	if (err)
4351220deb96SPaul Moore 		return err;
4352d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4353220deb96SPaul Moore 
43544e5ab4cbSJames Morris 	return err;
43554e5ab4cbSJames Morris }
4356d28d1e08STrent Jaeger 
43574e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
43584e5ab4cbSJames Morris {
4359220deb96SPaul Moore 	int err;
43604237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4361220deb96SPaul Moore 	u16 family = sk->sk_family;
4362220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
43632bf49690SThomas Liu 	struct common_audit_data ad;
436448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4365220deb96SPaul Moore 	char *addrp;
4366d8395c87SPaul Moore 	u8 secmark_active;
4367d8395c87SPaul Moore 	u8 peerlbl_active;
43684e5ab4cbSJames Morris 
43694e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4370220deb96SPaul Moore 		return 0;
43714e5ab4cbSJames Morris 
43724e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
437387fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
43744e5ab4cbSJames Morris 		family = PF_INET;
43754e5ab4cbSJames Morris 
4376d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4377d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4378d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4379d8395c87SPaul Moore 	 * as fast and as clean as possible. */
438058bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4381d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4382d8395c87SPaul Moore 
4383d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
43842be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4385d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4386d8395c87SPaul Moore 		return 0;
4387d8395c87SPaul Moore 
438850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
438948c62af6SEric Paris 	ad.u.net = &net;
439048c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
439148c62af6SEric Paris 	ad.u.net->family = family;
4392224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
43934e5ab4cbSJames Morris 	if (err)
4394220deb96SPaul Moore 		return err;
43954e5ab4cbSJames Morris 
4396d8395c87SPaul Moore 	if (peerlbl_active) {
4397d621d35eSPaul Moore 		u32 peer_sid;
4398220deb96SPaul Moore 
4399220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4400220deb96SPaul Moore 		if (err)
4401220deb96SPaul Moore 			return err;
4402cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4403cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4404dfaebe98SPaul Moore 		if (err) {
4405dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4406effad8dfSPaul Moore 			return err;
4407dfaebe98SPaul Moore 		}
4408d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4409d621d35eSPaul Moore 				   PEER__RECV, &ad);
441046d01d63SChad Hanson 		if (err) {
4411dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
441246d01d63SChad Hanson 			return err;
441346d01d63SChad Hanson 		}
4414d621d35eSPaul Moore 	}
4415d621d35eSPaul Moore 
4416d8395c87SPaul Moore 	if (secmark_active) {
4417effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4418effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4419effad8dfSPaul Moore 		if (err)
4420effad8dfSPaul Moore 			return err;
4421effad8dfSPaul Moore 	}
4422effad8dfSPaul Moore 
4423d621d35eSPaul Moore 	return err;
44241da177e4SLinus Torvalds }
44251da177e4SLinus Torvalds 
44262c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
44271da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
44281da177e4SLinus Torvalds {
44291da177e4SLinus Torvalds 	int err = 0;
44301da177e4SLinus Torvalds 	char *scontext;
44311da177e4SLinus Torvalds 	u32 scontext_len;
4432253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
44333de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
44341da177e4SLinus Torvalds 
4435253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4436253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_TCP_SOCKET)
4437dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4438253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4439253bfae6SPaul Moore 		return -ENOPROTOOPT;
44401da177e4SLinus Torvalds 
44412c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
44421da177e4SLinus Torvalds 	if (err)
4443253bfae6SPaul Moore 		return err;
44441da177e4SLinus Torvalds 
44451da177e4SLinus Torvalds 	if (scontext_len > len) {
44461da177e4SLinus Torvalds 		err = -ERANGE;
44471da177e4SLinus Torvalds 		goto out_len;
44481da177e4SLinus Torvalds 	}
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
44511da177e4SLinus Torvalds 		err = -EFAULT;
44521da177e4SLinus Torvalds 
44531da177e4SLinus Torvalds out_len:
44541da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
44551da177e4SLinus Torvalds 		err = -EFAULT;
44561da177e4SLinus Torvalds 	kfree(scontext);
44571da177e4SLinus Torvalds 	return err;
44581da177e4SLinus Torvalds }
44591da177e4SLinus Torvalds 
4460dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
44612c7946a7SCatherine Zhang {
4462dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
446375e22910SPaul Moore 	u16 family;
4464877ce7c1SCatherine Zhang 
4465aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4466aa862900SPaul Moore 		family = PF_INET;
4467aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4468aa862900SPaul Moore 		family = PF_INET6;
4469aa862900SPaul Moore 	else if (sock)
447075e22910SPaul Moore 		family = sock->sk->sk_family;
447175e22910SPaul Moore 	else
447275e22910SPaul Moore 		goto out;
447375e22910SPaul Moore 
447475e22910SPaul Moore 	if (sock && family == PF_UNIX)
4475713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
44763de4bab5SPaul Moore 	else if (skb)
4477220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
44782c7946a7SCatherine Zhang 
447975e22910SPaul Moore out:
4480dc49c1f9SCatherine Zhang 	*secid = peer_secid;
448175e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
448275e22910SPaul Moore 		return -EINVAL;
448375e22910SPaul Moore 	return 0;
44842c7946a7SCatherine Zhang }
44852c7946a7SCatherine Zhang 
44867d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
44871da177e4SLinus Torvalds {
448884914b7eSPaul Moore 	struct sk_security_struct *sksec;
448984914b7eSPaul Moore 
449084914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
449184914b7eSPaul Moore 	if (!sksec)
449284914b7eSPaul Moore 		return -ENOMEM;
449384914b7eSPaul Moore 
449484914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
449584914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
449684914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
449784914b7eSPaul Moore 	sk->sk_security = sksec;
449884914b7eSPaul Moore 
449984914b7eSPaul Moore 	return 0;
45001da177e4SLinus Torvalds }
45011da177e4SLinus Torvalds 
45021da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
45031da177e4SLinus Torvalds {
450484914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
450584914b7eSPaul Moore 
450684914b7eSPaul Moore 	sk->sk_security = NULL;
450784914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
450884914b7eSPaul Moore 	kfree(sksec);
45091da177e4SLinus Torvalds }
45101da177e4SLinus Torvalds 
4511892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4512892c141eSVenkat Yekkirala {
4513dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4514dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4515892c141eSVenkat Yekkirala 
4516dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4517dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4518dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
451999f59ed0SPaul Moore 
4520dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4521892c141eSVenkat Yekkirala }
4522892c141eSVenkat Yekkirala 
4523beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4524d28d1e08STrent Jaeger {
4525d28d1e08STrent Jaeger 	if (!sk)
4526beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4527892c141eSVenkat Yekkirala 	else {
4528892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4529d28d1e08STrent Jaeger 
4530beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4531892c141eSVenkat Yekkirala 	}
4532d28d1e08STrent Jaeger }
4533d28d1e08STrent Jaeger 
45349a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
45354237c75cSVenkat Yekkirala {
45364237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
45374237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
45384237c75cSVenkat Yekkirala 
45392873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
45402873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
45414237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4542220deb96SPaul Moore 	sksec->sclass = isec->sclass;
45434237c75cSVenkat Yekkirala }
45444237c75cSVenkat Yekkirala 
45459a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
45464237c75cSVenkat Yekkirala 				     struct request_sock *req)
45474237c75cSVenkat Yekkirala {
45484237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
45494237c75cSVenkat Yekkirala 	int err;
45500b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
4551446b8024SPaul Moore 	u32 connsid;
45524237c75cSVenkat Yekkirala 	u32 peersid;
45534237c75cSVenkat Yekkirala 
4554aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4555220deb96SPaul Moore 	if (err)
4556220deb96SPaul Moore 		return err;
4557446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
45584237c75cSVenkat Yekkirala 	if (err)
45594237c75cSVenkat Yekkirala 		return err;
4560446b8024SPaul Moore 	req->secid = connsid;
45616b877699SVenkat Yekkirala 	req->peer_secid = peersid;
4562389fb800SPaul Moore 
4563389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
45644237c75cSVenkat Yekkirala }
45654237c75cSVenkat Yekkirala 
45669a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
45679a673e56SAdrian Bunk 				   const struct request_sock *req)
45684237c75cSVenkat Yekkirala {
45694237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
45704237c75cSVenkat Yekkirala 
45714237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
45726b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
45734237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
45744237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
45754237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
45764237c75cSVenkat Yekkirala 	   time it will have been created and available. */
457799f59ed0SPaul Moore 
45789f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
45799f2ad665SPaul Moore 	 * thread with access to newsksec */
4580389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
45814237c75cSVenkat Yekkirala }
45824237c75cSVenkat Yekkirala 
4583014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
45846b877699SVenkat Yekkirala {
4585aa862900SPaul Moore 	u16 family = sk->sk_family;
45866b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
45876b877699SVenkat Yekkirala 
4588aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4589aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4590aa862900SPaul Moore 		family = PF_INET;
4591aa862900SPaul Moore 
4592aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
45936b877699SVenkat Yekkirala }
45946b877699SVenkat Yekkirala 
45952606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
45962606fd1fSEric Paris {
45972606fd1fSEric Paris 	const struct task_security_struct *__tsec;
45982606fd1fSEric Paris 	u32 tsid;
45992606fd1fSEric Paris 
46002606fd1fSEric Paris 	__tsec = current_security();
46012606fd1fSEric Paris 	tsid = __tsec->sid;
46022606fd1fSEric Paris 
46032606fd1fSEric Paris 	return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
46042606fd1fSEric Paris }
46052606fd1fSEric Paris 
46062606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
46072606fd1fSEric Paris {
46082606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
46092606fd1fSEric Paris }
46102606fd1fSEric Paris 
46112606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
46122606fd1fSEric Paris {
46132606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
46142606fd1fSEric Paris }
46152606fd1fSEric Paris 
46169a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
46179a673e56SAdrian Bunk 				      struct flowi *fl)
46184237c75cSVenkat Yekkirala {
46191d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
46204237c75cSVenkat Yekkirala }
46214237c75cSVenkat Yekkirala 
46225dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
46235dbbaf2dSPaul Moore {
46245dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
46255dbbaf2dSPaul Moore 
46265dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
46275dbbaf2dSPaul Moore 	if (!tunsec)
46285dbbaf2dSPaul Moore 		return -ENOMEM;
46295dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
46305dbbaf2dSPaul Moore 
46315dbbaf2dSPaul Moore 	*security = tunsec;
46325dbbaf2dSPaul Moore 	return 0;
46335dbbaf2dSPaul Moore }
46345dbbaf2dSPaul Moore 
46355dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
46365dbbaf2dSPaul Moore {
46375dbbaf2dSPaul Moore 	kfree(security);
46385dbbaf2dSPaul Moore }
46395dbbaf2dSPaul Moore 
4640ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
4641ed6d76e4SPaul Moore {
4642ed6d76e4SPaul Moore 	u32 sid = current_sid();
4643ed6d76e4SPaul Moore 
4644ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
4645ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
4646ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
4647ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
4648ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
4649ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
4650ed6d76e4SPaul Moore 
4651ed6d76e4SPaul Moore 	return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4652ed6d76e4SPaul Moore 			    NULL);
4653ed6d76e4SPaul Moore }
4654ed6d76e4SPaul Moore 
46555dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
4656ed6d76e4SPaul Moore {
46575dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
46585dbbaf2dSPaul Moore 
46595dbbaf2dSPaul Moore 	return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
46605dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
46615dbbaf2dSPaul Moore }
46625dbbaf2dSPaul Moore 
46635dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
46645dbbaf2dSPaul Moore {
46655dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4666ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4667ed6d76e4SPaul Moore 
4668ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
4669ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
4670ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
4671ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
4672ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
4673ed6d76e4SPaul Moore 	 * protocols were being used */
4674ed6d76e4SPaul Moore 
46755dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
4676ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
46775dbbaf2dSPaul Moore 
46785dbbaf2dSPaul Moore 	return 0;
4679ed6d76e4SPaul Moore }
4680ed6d76e4SPaul Moore 
46815dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
4682ed6d76e4SPaul Moore {
46835dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4684ed6d76e4SPaul Moore 	u32 sid = current_sid();
4685ed6d76e4SPaul Moore 	int err;
4686ed6d76e4SPaul Moore 
46875dbbaf2dSPaul Moore 	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4688ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
4689ed6d76e4SPaul Moore 	if (err)
4690ed6d76e4SPaul Moore 		return err;
4691ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4692ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
4693ed6d76e4SPaul Moore 	if (err)
4694ed6d76e4SPaul Moore 		return err;
46955dbbaf2dSPaul Moore 	tunsec->sid = sid;
4696ed6d76e4SPaul Moore 
4697ed6d76e4SPaul Moore 	return 0;
4698ed6d76e4SPaul Moore }
4699ed6d76e4SPaul Moore 
47001da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
47011da177e4SLinus Torvalds {
47021da177e4SLinus Torvalds 	int err = 0;
47031da177e4SLinus Torvalds 	u32 perm;
47041da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4705253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
47061da177e4SLinus Torvalds 
470777954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
47081da177e4SLinus Torvalds 		err = -EINVAL;
47091da177e4SLinus Torvalds 		goto out;
47101da177e4SLinus Torvalds 	}
4711b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
47121da177e4SLinus Torvalds 
4713253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
47141da177e4SLinus Torvalds 	if (err) {
47151da177e4SLinus Torvalds 		if (err == -EINVAL) {
4716d950f84cSRichard Guy Briggs 			printk(KERN_WARNING
4717d950f84cSRichard Guy Briggs 			       "SELinux: unrecognized netlink message:"
4718cded3fffSMarek Milkovic 			       " protocol=%hu nlmsg_type=%hu sclass=%s\n",
4719cded3fffSMarek Milkovic 			       sk->sk_protocol, nlh->nlmsg_type,
4720cded3fffSMarek Milkovic 			       secclass_map[sksec->sclass - 1].name);
472139c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
47221da177e4SLinus Torvalds 				err = 0;
47231da177e4SLinus Torvalds 		}
47241da177e4SLinus Torvalds 
47251da177e4SLinus Torvalds 		/* Ignore */
47261da177e4SLinus Torvalds 		if (err == -ENOENT)
47271da177e4SLinus Torvalds 			err = 0;
47281da177e4SLinus Torvalds 		goto out;
47291da177e4SLinus Torvalds 	}
47301da177e4SLinus Torvalds 
4731253bfae6SPaul Moore 	err = sock_has_perm(current, sk, perm);
47321da177e4SLinus Torvalds out:
47331da177e4SLinus Torvalds 	return err;
47341da177e4SLinus Torvalds }
47351da177e4SLinus Torvalds 
47361da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
47371da177e4SLinus Torvalds 
4738cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
4739cbe0d6e8SPaul Moore 				       const struct net_device *indev,
4740effad8dfSPaul Moore 				       u16 family)
47411da177e4SLinus Torvalds {
4742dfaebe98SPaul Moore 	int err;
4743effad8dfSPaul Moore 	char *addrp;
4744effad8dfSPaul Moore 	u32 peer_sid;
47452bf49690SThomas Liu 	struct common_audit_data ad;
474648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4747effad8dfSPaul Moore 	u8 secmark_active;
4748948bf85cSPaul Moore 	u8 netlbl_active;
4749effad8dfSPaul Moore 	u8 peerlbl_active;
47504237c75cSVenkat Yekkirala 
4751effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4752effad8dfSPaul Moore 		return NF_ACCEPT;
47534237c75cSVenkat Yekkirala 
4754effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4755948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
47562be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4757effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4758effad8dfSPaul Moore 		return NF_ACCEPT;
47594237c75cSVenkat Yekkirala 
4760d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4761d8395c87SPaul Moore 		return NF_DROP;
4762d8395c87SPaul Moore 
476350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
476448c62af6SEric Paris 	ad.u.net = &net;
4765cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
476648c62af6SEric Paris 	ad.u.net->family = family;
4767effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4768effad8dfSPaul Moore 		return NF_DROP;
47691da177e4SLinus Torvalds 
4770dfaebe98SPaul Moore 	if (peerlbl_active) {
4771cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
4772cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4773dfaebe98SPaul Moore 		if (err) {
4774dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4775effad8dfSPaul Moore 			return NF_DROP;
4776dfaebe98SPaul Moore 		}
4777dfaebe98SPaul Moore 	}
4778effad8dfSPaul Moore 
4779effad8dfSPaul Moore 	if (secmark_active)
4780effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4781effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4782effad8dfSPaul Moore 			return NF_DROP;
4783effad8dfSPaul Moore 
4784948bf85cSPaul Moore 	if (netlbl_active)
4785948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4786948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4787948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4788948bf85cSPaul Moore 		 * protection */
4789948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4790948bf85cSPaul Moore 			return NF_DROP;
4791948bf85cSPaul Moore 
4792effad8dfSPaul Moore 	return NF_ACCEPT;
4793effad8dfSPaul Moore }
4794effad8dfSPaul Moore 
4795795aa6efSPatrick McHardy static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
4796effad8dfSPaul Moore 					 struct sk_buff *skb,
4797238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
4798effad8dfSPaul Moore {
4799238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET);
4800effad8dfSPaul Moore }
4801effad8dfSPaul Moore 
4802effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4803795aa6efSPatrick McHardy static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
4804effad8dfSPaul Moore 					 struct sk_buff *skb,
4805238e54c9SDavid S. Miller 					 const struct nf_hook_state *state)
4806effad8dfSPaul Moore {
4807238e54c9SDavid S. Miller 	return selinux_ip_forward(skb, state->in, PF_INET6);
4808effad8dfSPaul Moore }
4809effad8dfSPaul Moore #endif	/* IPV6 */
4810effad8dfSPaul Moore 
4811948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4812948bf85cSPaul Moore 				      u16 family)
4813948bf85cSPaul Moore {
481447180068SPaul Moore 	struct sock *sk;
4815948bf85cSPaul Moore 	u32 sid;
4816948bf85cSPaul Moore 
4817948bf85cSPaul Moore 	if (!netlbl_enabled())
4818948bf85cSPaul Moore 		return NF_ACCEPT;
4819948bf85cSPaul Moore 
4820948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4821948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4822948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
482347180068SPaul Moore 	sk = skb->sk;
482447180068SPaul Moore 	if (sk) {
482547180068SPaul Moore 		struct sk_security_struct *sksec;
482647180068SPaul Moore 
482747180068SPaul Moore 		if (sk->sk_state == TCP_LISTEN)
482847180068SPaul Moore 			/* if the socket is the listening state then this
482947180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
483047180068SPaul Moore 			 * be labeled based on the connection/request_sock and
483147180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
483247180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
483347180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
483447180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
483547180068SPaul Moore 			 * as any IP option based labeling should be copied
483647180068SPaul Moore 			 * from the initial connection request (in the IP
483747180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
483847180068SPaul Moore 			 * security label in the packet itself this is the
483947180068SPaul Moore 			 * best we can do. */
484047180068SPaul Moore 			return NF_ACCEPT;
484147180068SPaul Moore 
484247180068SPaul Moore 		/* standard practice, label using the parent socket */
484347180068SPaul Moore 		sksec = sk->sk_security;
4844948bf85cSPaul Moore 		sid = sksec->sid;
4845948bf85cSPaul Moore 	} else
4846948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4847948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4848948bf85cSPaul Moore 		return NF_DROP;
4849948bf85cSPaul Moore 
4850948bf85cSPaul Moore 	return NF_ACCEPT;
4851948bf85cSPaul Moore }
4852948bf85cSPaul Moore 
4853795aa6efSPatrick McHardy static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
4854948bf85cSPaul Moore 					struct sk_buff *skb,
4855238e54c9SDavid S. Miller 					const struct nf_hook_state *state)
4856948bf85cSPaul Moore {
4857948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4858948bf85cSPaul Moore }
4859948bf85cSPaul Moore 
4860effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4861effad8dfSPaul Moore 						int ifindex,
4862d8395c87SPaul Moore 						u16 family)
48634e5ab4cbSJames Morris {
4864effad8dfSPaul Moore 	struct sock *sk = skb->sk;
48654237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
48662bf49690SThomas Liu 	struct common_audit_data ad;
486748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4868d8395c87SPaul Moore 	char *addrp;
4869d8395c87SPaul Moore 	u8 proto;
48704e5ab4cbSJames Morris 
4871effad8dfSPaul Moore 	if (sk == NULL)
4872effad8dfSPaul Moore 		return NF_ACCEPT;
48734237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
48744e5ab4cbSJames Morris 
487550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
487648c62af6SEric Paris 	ad.u.net = &net;
487748c62af6SEric Paris 	ad.u.net->netif = ifindex;
487848c62af6SEric Paris 	ad.u.net->family = family;
4879d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4880d8395c87SPaul Moore 		return NF_DROP;
4881d8395c87SPaul Moore 
488258bfbb51SPaul Moore 	if (selinux_secmark_enabled())
4883effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4884d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
48852fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
48861da177e4SLinus Torvalds 
4887d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
48882fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
4889effad8dfSPaul Moore 
4890effad8dfSPaul Moore 	return NF_ACCEPT;
4891effad8dfSPaul Moore }
4892effad8dfSPaul Moore 
4893cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
4894cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
4895effad8dfSPaul Moore 					 u16 family)
4896effad8dfSPaul Moore {
4897effad8dfSPaul Moore 	u32 secmark_perm;
4898effad8dfSPaul Moore 	u32 peer_sid;
4899cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
4900effad8dfSPaul Moore 	struct sock *sk;
49012bf49690SThomas Liu 	struct common_audit_data ad;
490248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4903effad8dfSPaul Moore 	char *addrp;
4904effad8dfSPaul Moore 	u8 secmark_active;
4905effad8dfSPaul Moore 	u8 peerlbl_active;
4906effad8dfSPaul Moore 
4907effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4908effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4909effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4910effad8dfSPaul Moore 	 * as fast and as clean as possible. */
491158bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4912d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4913c0828e50SPaul Moore 
4914effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
49152be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4916effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4917effad8dfSPaul Moore 		return NF_ACCEPT;
4918effad8dfSPaul Moore 
4919effad8dfSPaul Moore 	sk = skb->sk;
4920c0828e50SPaul Moore 
4921effad8dfSPaul Moore #ifdef CONFIG_XFRM
4922effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4923effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4924effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4925effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4926effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4927c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
4928c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4929c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
4930c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
4931c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
4932c0828e50SPaul Moore 	 *       connection. */
4933c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4934c0828e50SPaul Moore 	    !(sk != NULL && sk->sk_state == TCP_LISTEN))
4935effad8dfSPaul Moore 		return NF_ACCEPT;
4936effad8dfSPaul Moore #endif
4937effad8dfSPaul Moore 
4938d8395c87SPaul Moore 	if (sk == NULL) {
4939446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
4940446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
4941446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
4942446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
49434a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
4944d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
4945d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
494604f6d70fSEric Paris 				return NF_DROP;
49474a7ab3dcSSteffen Klassert 		} else {
49484a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
4949d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
49504a7ab3dcSSteffen Klassert 		}
4951446b8024SPaul Moore 	} else if (sk->sk_state == TCP_LISTEN) {
4952446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
4953446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
4954446b8024SPaul Moore 		 * this particular case the correct security label is assigned
4955446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
4956446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
4957446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
4958446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
4959446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
4960446b8024SPaul Moore 		 * for similar problems. */
4961446b8024SPaul Moore 		u32 skb_sid;
4962446b8024SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4963446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4964446b8024SPaul Moore 			return NF_DROP;
4965c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
4966c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
4967c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
4968c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
4969c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
4970c0828e50SPaul Moore 		 * pass the packet. */
4971c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
4972c0828e50SPaul Moore 			switch (family) {
4973c0828e50SPaul Moore 			case PF_INET:
4974c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4975c0828e50SPaul Moore 					return NF_ACCEPT;
4976c0828e50SPaul Moore 				break;
4977c0828e50SPaul Moore 			case PF_INET6:
4978c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4979c0828e50SPaul Moore 					return NF_ACCEPT;
4980a7a91a19SPaul Moore 				break;
4981c0828e50SPaul Moore 			default:
4982c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
4983c0828e50SPaul Moore 			}
4984c0828e50SPaul Moore 		}
4985446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4986446b8024SPaul Moore 			return NF_DROP;
4987446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
4988d8395c87SPaul Moore 	} else {
4989446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
4990446b8024SPaul Moore 		 * associated socket. */
4991effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4992effad8dfSPaul Moore 		peer_sid = sksec->sid;
4993effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4994effad8dfSPaul Moore 	}
4995effad8dfSPaul Moore 
499650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
499748c62af6SEric Paris 	ad.u.net = &net;
499848c62af6SEric Paris 	ad.u.net->netif = ifindex;
499948c62af6SEric Paris 	ad.u.net->family = family;
5000d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
500104f6d70fSEric Paris 		return NF_DROP;
5002d8395c87SPaul Moore 
5003effad8dfSPaul Moore 	if (secmark_active)
5004effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
5005effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
50061f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5007effad8dfSPaul Moore 
5008effad8dfSPaul Moore 	if (peerlbl_active) {
5009effad8dfSPaul Moore 		u32 if_sid;
5010effad8dfSPaul Moore 		u32 node_sid;
5011effad8dfSPaul Moore 
5012cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
501304f6d70fSEric Paris 			return NF_DROP;
5014effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
5015effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
50161f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5017effad8dfSPaul Moore 
5018effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
501904f6d70fSEric Paris 			return NF_DROP;
5020effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
5021effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
50221f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5023effad8dfSPaul Moore 	}
5024effad8dfSPaul Moore 
5025effad8dfSPaul Moore 	return NF_ACCEPT;
5026effad8dfSPaul Moore }
5027effad8dfSPaul Moore 
5028795aa6efSPatrick McHardy static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
5029a224be76SDavid S. Miller 					   struct sk_buff *skb,
5030238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
50311da177e4SLinus Torvalds {
5032238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET);
50331da177e4SLinus Torvalds }
50341da177e4SLinus Torvalds 
50351da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5036795aa6efSPatrick McHardy static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
5037a224be76SDavid S. Miller 					   struct sk_buff *skb,
5038238e54c9SDavid S. Miller 					   const struct nf_hook_state *state)
50391da177e4SLinus Torvalds {
5040238e54c9SDavid S. Miller 	return selinux_ip_postroute(skb, state->out, PF_INET6);
50411da177e4SLinus Torvalds }
50421da177e4SLinus Torvalds #endif	/* IPV6 */
50431da177e4SLinus Torvalds 
50441da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
50451da177e4SLinus Torvalds 
50461da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
50471da177e4SLinus Torvalds {
5048941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
50491da177e4SLinus Torvalds }
50501da177e4SLinus Torvalds 
50511da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
50521da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
50531da177e4SLinus Torvalds 			      u16 sclass)
50541da177e4SLinus Torvalds {
50551da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
5056275bb41eSDavid Howells 	u32 sid;
50571da177e4SLinus Torvalds 
505889d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
50591da177e4SLinus Torvalds 	if (!isec)
50601da177e4SLinus Torvalds 		return -ENOMEM;
50611da177e4SLinus Torvalds 
5062275bb41eSDavid Howells 	sid = task_sid(task);
50631da177e4SLinus Torvalds 	isec->sclass = sclass;
5064275bb41eSDavid Howells 	isec->sid = sid;
50651da177e4SLinus Torvalds 	perm->security = isec;
50661da177e4SLinus Torvalds 
50671da177e4SLinus Torvalds 	return 0;
50681da177e4SLinus Torvalds }
50691da177e4SLinus Torvalds 
50701da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
50711da177e4SLinus Torvalds {
50721da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
50731da177e4SLinus Torvalds 	perm->security = NULL;
50741da177e4SLinus Torvalds 	kfree(isec);
50751da177e4SLinus Torvalds }
50761da177e4SLinus Torvalds 
50771da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
50781da177e4SLinus Torvalds {
50791da177e4SLinus Torvalds 	struct msg_security_struct *msec;
50801da177e4SLinus Torvalds 
508189d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
50821da177e4SLinus Torvalds 	if (!msec)
50831da177e4SLinus Torvalds 		return -ENOMEM;
50841da177e4SLinus Torvalds 
50851da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
50861da177e4SLinus Torvalds 	msg->security = msec;
50871da177e4SLinus Torvalds 
50881da177e4SLinus Torvalds 	return 0;
50891da177e4SLinus Torvalds }
50901da177e4SLinus Torvalds 
50911da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
50921da177e4SLinus Torvalds {
50931da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
50941da177e4SLinus Torvalds 
50951da177e4SLinus Torvalds 	msg->security = NULL;
50961da177e4SLinus Torvalds 	kfree(msec);
50971da177e4SLinus Torvalds }
50981da177e4SLinus Torvalds 
50991da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
51006af963f1SStephen Smalley 			u32 perms)
51011da177e4SLinus Torvalds {
51021da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51032bf49690SThomas Liu 	struct common_audit_data ad;
5104275bb41eSDavid Howells 	u32 sid = current_sid();
51051da177e4SLinus Torvalds 
51061da177e4SLinus Torvalds 	isec = ipc_perms->security;
51071da177e4SLinus Torvalds 
510850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51091da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
51101da177e4SLinus Torvalds 
5111275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
51121da177e4SLinus Torvalds }
51131da177e4SLinus Torvalds 
51141da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
51151da177e4SLinus Torvalds {
51161da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
51171da177e4SLinus Torvalds }
51181da177e4SLinus Torvalds 
51191da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
51201da177e4SLinus Torvalds {
51211da177e4SLinus Torvalds 	msg_msg_free_security(msg);
51221da177e4SLinus Torvalds }
51231da177e4SLinus Torvalds 
51241da177e4SLinus Torvalds /* message queue security operations */
51251da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
51261da177e4SLinus Torvalds {
51271da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51282bf49690SThomas Liu 	struct common_audit_data ad;
5129275bb41eSDavid Howells 	u32 sid = current_sid();
51301da177e4SLinus Torvalds 	int rc;
51311da177e4SLinus Torvalds 
51321da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
51331da177e4SLinus Torvalds 	if (rc)
51341da177e4SLinus Torvalds 		return rc;
51351da177e4SLinus Torvalds 
51361da177e4SLinus Torvalds 	isec = msq->q_perm.security;
51371da177e4SLinus Torvalds 
513850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51391da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
51401da177e4SLinus Torvalds 
5141275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
51421da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
51431da177e4SLinus Torvalds 	if (rc) {
51441da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
51451da177e4SLinus Torvalds 		return rc;
51461da177e4SLinus Torvalds 	}
51471da177e4SLinus Torvalds 	return 0;
51481da177e4SLinus Torvalds }
51491da177e4SLinus Torvalds 
51501da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
51511da177e4SLinus Torvalds {
51521da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
51531da177e4SLinus Torvalds }
51541da177e4SLinus Torvalds 
51551da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
51561da177e4SLinus Torvalds {
51571da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51582bf49690SThomas Liu 	struct common_audit_data ad;
5159275bb41eSDavid Howells 	u32 sid = current_sid();
51601da177e4SLinus Torvalds 
51611da177e4SLinus Torvalds 	isec = msq->q_perm.security;
51621da177e4SLinus Torvalds 
516350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51641da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
51651da177e4SLinus Torvalds 
5166275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
51671da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
51681da177e4SLinus Torvalds }
51691da177e4SLinus Torvalds 
51701da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
51711da177e4SLinus Torvalds {
51721da177e4SLinus Torvalds 	int err;
51731da177e4SLinus Torvalds 	int perms;
51741da177e4SLinus Torvalds 
51751da177e4SLinus Torvalds 	switch (cmd) {
51761da177e4SLinus Torvalds 	case IPC_INFO:
51771da177e4SLinus Torvalds 	case MSG_INFO:
51781da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
51791da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
51801da177e4SLinus Torvalds 	case IPC_STAT:
51811da177e4SLinus Torvalds 	case MSG_STAT:
51821da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
51831da177e4SLinus Torvalds 		break;
51841da177e4SLinus Torvalds 	case IPC_SET:
51851da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
51861da177e4SLinus Torvalds 		break;
51871da177e4SLinus Torvalds 	case IPC_RMID:
51881da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
51891da177e4SLinus Torvalds 		break;
51901da177e4SLinus Torvalds 	default:
51911da177e4SLinus Torvalds 		return 0;
51921da177e4SLinus Torvalds 	}
51931da177e4SLinus Torvalds 
51946af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
51951da177e4SLinus Torvalds 	return err;
51961da177e4SLinus Torvalds }
51971da177e4SLinus Torvalds 
51981da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
51991da177e4SLinus Torvalds {
52001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52011da177e4SLinus Torvalds 	struct msg_security_struct *msec;
52022bf49690SThomas Liu 	struct common_audit_data ad;
5203275bb41eSDavid Howells 	u32 sid = current_sid();
52041da177e4SLinus Torvalds 	int rc;
52051da177e4SLinus Torvalds 
52061da177e4SLinus Torvalds 	isec = msq->q_perm.security;
52071da177e4SLinus Torvalds 	msec = msg->security;
52081da177e4SLinus Torvalds 
52091da177e4SLinus Torvalds 	/*
52101da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
52111da177e4SLinus Torvalds 	 */
52121da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
52131da177e4SLinus Torvalds 		/*
52141da177e4SLinus Torvalds 		 * Compute new sid based on current process and
52151da177e4SLinus Torvalds 		 * message queue this message will be stored in
52161da177e4SLinus Torvalds 		 */
5217275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5218652bb9b0SEric Paris 					     NULL, &msec->sid);
52191da177e4SLinus Torvalds 		if (rc)
52201da177e4SLinus Torvalds 			return rc;
52211da177e4SLinus Torvalds 	}
52221da177e4SLinus Torvalds 
522350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52241da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
52251da177e4SLinus Torvalds 
52261da177e4SLinus Torvalds 	/* Can this process write to the queue? */
5227275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
52281da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
52291da177e4SLinus Torvalds 	if (!rc)
52301da177e4SLinus Torvalds 		/* Can this process send the message */
5231275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5232275bb41eSDavid Howells 				  MSG__SEND, &ad);
52331da177e4SLinus Torvalds 	if (!rc)
52341da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
5235275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5236275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
52371da177e4SLinus Torvalds 
52381da177e4SLinus Torvalds 	return rc;
52391da177e4SLinus Torvalds }
52401da177e4SLinus Torvalds 
52411da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
52421da177e4SLinus Torvalds 				    struct task_struct *target,
52431da177e4SLinus Torvalds 				    long type, int mode)
52441da177e4SLinus Torvalds {
52451da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52461da177e4SLinus Torvalds 	struct msg_security_struct *msec;
52472bf49690SThomas Liu 	struct common_audit_data ad;
5248275bb41eSDavid Howells 	u32 sid = task_sid(target);
52491da177e4SLinus Torvalds 	int rc;
52501da177e4SLinus Torvalds 
52511da177e4SLinus Torvalds 	isec = msq->q_perm.security;
52521da177e4SLinus Torvalds 	msec = msg->security;
52531da177e4SLinus Torvalds 
525450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52551da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
52561da177e4SLinus Torvalds 
5257275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
52581da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
52591da177e4SLinus Torvalds 	if (!rc)
5260275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
52611da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
52621da177e4SLinus Torvalds 	return rc;
52631da177e4SLinus Torvalds }
52641da177e4SLinus Torvalds 
52651da177e4SLinus Torvalds /* Shared Memory security operations */
52661da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
52671da177e4SLinus Torvalds {
52681da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52692bf49690SThomas Liu 	struct common_audit_data ad;
5270275bb41eSDavid Howells 	u32 sid = current_sid();
52711da177e4SLinus Torvalds 	int rc;
52721da177e4SLinus Torvalds 
52731da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
52741da177e4SLinus Torvalds 	if (rc)
52751da177e4SLinus Torvalds 		return rc;
52761da177e4SLinus Torvalds 
52771da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
52781da177e4SLinus Torvalds 
527950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52801da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
52811da177e4SLinus Torvalds 
5282275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
52831da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
52841da177e4SLinus Torvalds 	if (rc) {
52851da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
52861da177e4SLinus Torvalds 		return rc;
52871da177e4SLinus Torvalds 	}
52881da177e4SLinus Torvalds 	return 0;
52891da177e4SLinus Torvalds }
52901da177e4SLinus Torvalds 
52911da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
52921da177e4SLinus Torvalds {
52931da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
52941da177e4SLinus Torvalds }
52951da177e4SLinus Torvalds 
52961da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
52971da177e4SLinus Torvalds {
52981da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52992bf49690SThomas Liu 	struct common_audit_data ad;
5300275bb41eSDavid Howells 	u32 sid = current_sid();
53011da177e4SLinus Torvalds 
53021da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
53031da177e4SLinus Torvalds 
530450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53051da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
53061da177e4SLinus Torvalds 
5307275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
53081da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
53091da177e4SLinus Torvalds }
53101da177e4SLinus Torvalds 
53111da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
53121da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
53131da177e4SLinus Torvalds {
53141da177e4SLinus Torvalds 	int perms;
53151da177e4SLinus Torvalds 	int err;
53161da177e4SLinus Torvalds 
53171da177e4SLinus Torvalds 	switch (cmd) {
53181da177e4SLinus Torvalds 	case IPC_INFO:
53191da177e4SLinus Torvalds 	case SHM_INFO:
53201da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
53211da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
53221da177e4SLinus Torvalds 	case IPC_STAT:
53231da177e4SLinus Torvalds 	case SHM_STAT:
53241da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
53251da177e4SLinus Torvalds 		break;
53261da177e4SLinus Torvalds 	case IPC_SET:
53271da177e4SLinus Torvalds 		perms = SHM__SETATTR;
53281da177e4SLinus Torvalds 		break;
53291da177e4SLinus Torvalds 	case SHM_LOCK:
53301da177e4SLinus Torvalds 	case SHM_UNLOCK:
53311da177e4SLinus Torvalds 		perms = SHM__LOCK;
53321da177e4SLinus Torvalds 		break;
53331da177e4SLinus Torvalds 	case IPC_RMID:
53341da177e4SLinus Torvalds 		perms = SHM__DESTROY;
53351da177e4SLinus Torvalds 		break;
53361da177e4SLinus Torvalds 	default:
53371da177e4SLinus Torvalds 		return 0;
53381da177e4SLinus Torvalds 	}
53391da177e4SLinus Torvalds 
53406af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
53411da177e4SLinus Torvalds 	return err;
53421da177e4SLinus Torvalds }
53431da177e4SLinus Torvalds 
53441da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
53451da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
53461da177e4SLinus Torvalds {
53471da177e4SLinus Torvalds 	u32 perms;
53481da177e4SLinus Torvalds 
53491da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
53501da177e4SLinus Torvalds 		perms = SHM__READ;
53511da177e4SLinus Torvalds 	else
53521da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
53531da177e4SLinus Torvalds 
53546af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
53551da177e4SLinus Torvalds }
53561da177e4SLinus Torvalds 
53571da177e4SLinus Torvalds /* Semaphore security operations */
53581da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
53591da177e4SLinus Torvalds {
53601da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53612bf49690SThomas Liu 	struct common_audit_data ad;
5362275bb41eSDavid Howells 	u32 sid = current_sid();
53631da177e4SLinus Torvalds 	int rc;
53641da177e4SLinus Torvalds 
53651da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
53661da177e4SLinus Torvalds 	if (rc)
53671da177e4SLinus Torvalds 		return rc;
53681da177e4SLinus Torvalds 
53691da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
53701da177e4SLinus Torvalds 
537150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53721da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
53731da177e4SLinus Torvalds 
5374275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
53751da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
53761da177e4SLinus Torvalds 	if (rc) {
53771da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
53781da177e4SLinus Torvalds 		return rc;
53791da177e4SLinus Torvalds 	}
53801da177e4SLinus Torvalds 	return 0;
53811da177e4SLinus Torvalds }
53821da177e4SLinus Torvalds 
53831da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
53841da177e4SLinus Torvalds {
53851da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
53861da177e4SLinus Torvalds }
53871da177e4SLinus Torvalds 
53881da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
53891da177e4SLinus Torvalds {
53901da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53912bf49690SThomas Liu 	struct common_audit_data ad;
5392275bb41eSDavid Howells 	u32 sid = current_sid();
53931da177e4SLinus Torvalds 
53941da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
53951da177e4SLinus Torvalds 
539650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53971da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
53981da177e4SLinus Torvalds 
5399275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
54001da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
54011da177e4SLinus Torvalds }
54021da177e4SLinus Torvalds 
54031da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
54041da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
54051da177e4SLinus Torvalds {
54061da177e4SLinus Torvalds 	int err;
54071da177e4SLinus Torvalds 	u32 perms;
54081da177e4SLinus Torvalds 
54091da177e4SLinus Torvalds 	switch (cmd) {
54101da177e4SLinus Torvalds 	case IPC_INFO:
54111da177e4SLinus Torvalds 	case SEM_INFO:
54121da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
54131da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
54141da177e4SLinus Torvalds 	case GETPID:
54151da177e4SLinus Torvalds 	case GETNCNT:
54161da177e4SLinus Torvalds 	case GETZCNT:
54171da177e4SLinus Torvalds 		perms = SEM__GETATTR;
54181da177e4SLinus Torvalds 		break;
54191da177e4SLinus Torvalds 	case GETVAL:
54201da177e4SLinus Torvalds 	case GETALL:
54211da177e4SLinus Torvalds 		perms = SEM__READ;
54221da177e4SLinus Torvalds 		break;
54231da177e4SLinus Torvalds 	case SETVAL:
54241da177e4SLinus Torvalds 	case SETALL:
54251da177e4SLinus Torvalds 		perms = SEM__WRITE;
54261da177e4SLinus Torvalds 		break;
54271da177e4SLinus Torvalds 	case IPC_RMID:
54281da177e4SLinus Torvalds 		perms = SEM__DESTROY;
54291da177e4SLinus Torvalds 		break;
54301da177e4SLinus Torvalds 	case IPC_SET:
54311da177e4SLinus Torvalds 		perms = SEM__SETATTR;
54321da177e4SLinus Torvalds 		break;
54331da177e4SLinus Torvalds 	case IPC_STAT:
54341da177e4SLinus Torvalds 	case SEM_STAT:
54351da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
54361da177e4SLinus Torvalds 		break;
54371da177e4SLinus Torvalds 	default:
54381da177e4SLinus Torvalds 		return 0;
54391da177e4SLinus Torvalds 	}
54401da177e4SLinus Torvalds 
54416af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
54421da177e4SLinus Torvalds 	return err;
54431da177e4SLinus Torvalds }
54441da177e4SLinus Torvalds 
54451da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
54461da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
54471da177e4SLinus Torvalds {
54481da177e4SLinus Torvalds 	u32 perms;
54491da177e4SLinus Torvalds 
54501da177e4SLinus Torvalds 	if (alter)
54511da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
54521da177e4SLinus Torvalds 	else
54531da177e4SLinus Torvalds 		perms = SEM__READ;
54541da177e4SLinus Torvalds 
54556af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
54561da177e4SLinus Torvalds }
54571da177e4SLinus Torvalds 
54581da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
54591da177e4SLinus Torvalds {
54601da177e4SLinus Torvalds 	u32 av = 0;
54611da177e4SLinus Torvalds 
54621da177e4SLinus Torvalds 	av = 0;
54631da177e4SLinus Torvalds 	if (flag & S_IRUGO)
54641da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
54651da177e4SLinus Torvalds 	if (flag & S_IWUGO)
54661da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
54671da177e4SLinus Torvalds 
54681da177e4SLinus Torvalds 	if (av == 0)
54691da177e4SLinus Torvalds 		return 0;
54701da177e4SLinus Torvalds 
54716af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
54721da177e4SLinus Torvalds }
54731da177e4SLinus Torvalds 
5474713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5475713a04aeSAhmed S. Darwish {
5476713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5477713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5478713a04aeSAhmed S. Darwish }
5479713a04aeSAhmed S. Darwish 
54801da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
54811da177e4SLinus Torvalds {
54821da177e4SLinus Torvalds 	if (inode)
54831da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
54841da177e4SLinus Torvalds }
54851da177e4SLinus Torvalds 
54861da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
548704ff9708SAl Viro 			       char *name, char **value)
54881da177e4SLinus Torvalds {
5489275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
54908c8570fbSDustin Kirkland 	u32 sid;
54911da177e4SLinus Torvalds 	int error;
549204ff9708SAl Viro 	unsigned len;
54931da177e4SLinus Torvalds 
54941da177e4SLinus Torvalds 	if (current != p) {
54953b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
54961da177e4SLinus Torvalds 		if (error)
54971da177e4SLinus Torvalds 			return error;
54981da177e4SLinus Torvalds 	}
54991da177e4SLinus Torvalds 
5500275bb41eSDavid Howells 	rcu_read_lock();
5501275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
55021da177e4SLinus Torvalds 
55031da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5504275bb41eSDavid Howells 		sid = __tsec->sid;
55051da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5506275bb41eSDavid Howells 		sid = __tsec->osid;
55071da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5508275bb41eSDavid Howells 		sid = __tsec->exec_sid;
55091da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5510275bb41eSDavid Howells 		sid = __tsec->create_sid;
55114eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5512275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
551342c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5514275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
55151da177e4SLinus Torvalds 	else
5516275bb41eSDavid Howells 		goto invalid;
5517275bb41eSDavid Howells 	rcu_read_unlock();
55181da177e4SLinus Torvalds 
55191da177e4SLinus Torvalds 	if (!sid)
55201da177e4SLinus Torvalds 		return 0;
55211da177e4SLinus Torvalds 
552204ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
552304ff9708SAl Viro 	if (error)
552404ff9708SAl Viro 		return error;
552504ff9708SAl Viro 	return len;
5526275bb41eSDavid Howells 
5527275bb41eSDavid Howells invalid:
5528275bb41eSDavid Howells 	rcu_read_unlock();
5529275bb41eSDavid Howells 	return -EINVAL;
55301da177e4SLinus Torvalds }
55311da177e4SLinus Torvalds 
55321da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
55331da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
55341da177e4SLinus Torvalds {
55351da177e4SLinus Torvalds 	struct task_security_struct *tsec;
55360356357cSRoland McGrath 	struct task_struct *tracer;
5537d84f4f99SDavid Howells 	struct cred *new;
5538d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
55391da177e4SLinus Torvalds 	int error;
55401da177e4SLinus Torvalds 	char *str = value;
55411da177e4SLinus Torvalds 
55421da177e4SLinus Torvalds 	if (current != p) {
55431da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
55441da177e4SLinus Torvalds 		   security attributes. */
55451da177e4SLinus Torvalds 		return -EACCES;
55461da177e4SLinus Torvalds 	}
55471da177e4SLinus Torvalds 
55481da177e4SLinus Torvalds 	/*
55491da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
55501da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
55511da177e4SLinus Torvalds 	 * above restriction is ever removed.
55521da177e4SLinus Torvalds 	 */
55531da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
55543b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
55551da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
55563b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
55574eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
55583b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
555942c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
55603b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
55611da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
55623b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
55631da177e4SLinus Torvalds 	else
55641da177e4SLinus Torvalds 		error = -EINVAL;
55651da177e4SLinus Torvalds 	if (error)
55661da177e4SLinus Torvalds 		return error;
55671da177e4SLinus Torvalds 
55681da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
55691da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
55701da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
55711da177e4SLinus Torvalds 			str[size-1] = 0;
55721da177e4SLinus Torvalds 			size--;
55731da177e4SLinus Torvalds 		}
557452a4c640SNikolay Aleksandrov 		error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
557512b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
5576d6ea83ecSEric Paris 			if (!capable(CAP_MAC_ADMIN)) {
5577d6ea83ecSEric Paris 				struct audit_buffer *ab;
5578d6ea83ecSEric Paris 				size_t audit_size;
5579d6ea83ecSEric Paris 
5580d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
5581d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
5582d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
5583d6ea83ecSEric Paris 					audit_size = size - 1;
5584d6ea83ecSEric Paris 				else
5585d6ea83ecSEric Paris 					audit_size = size;
5586d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5587d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
5588d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
5589d6ea83ecSEric Paris 				audit_log_end(ab);
5590d6ea83ecSEric Paris 
559112b29f34SStephen Smalley 				return error;
5592d6ea83ecSEric Paris 			}
559312b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
559412b29f34SStephen Smalley 							      &sid);
559512b29f34SStephen Smalley 		}
55961da177e4SLinus Torvalds 		if (error)
55971da177e4SLinus Torvalds 			return error;
55981da177e4SLinus Torvalds 	}
55991da177e4SLinus Torvalds 
5600d84f4f99SDavid Howells 	new = prepare_creds();
5601d84f4f99SDavid Howells 	if (!new)
5602d84f4f99SDavid Howells 		return -ENOMEM;
5603d84f4f99SDavid Howells 
56041da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
56051da177e4SLinus Torvalds 	   performed during the actual operation (execve,
56061da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5607d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
56081da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
56091da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5610d84f4f99SDavid Howells 	tsec = new->security;
5611d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
56121da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5613d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
56141da177e4SLinus Torvalds 		tsec->create_sid = sid;
5615d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
56164eb582cfSMichael LeMay 		error = may_create_key(sid, p);
56174eb582cfSMichael LeMay 		if (error)
5618d84f4f99SDavid Howells 			goto abort_change;
56194eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5620d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
562142c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5622d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5623d84f4f99SDavid Howells 		error = -EINVAL;
56241da177e4SLinus Torvalds 		if (sid == 0)
5625d84f4f99SDavid Howells 			goto abort_change;
5626d9250deaSKaiGai Kohei 
5627d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5628d84f4f99SDavid Howells 		error = -EPERM;
56295bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
5630d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5631d84f4f99SDavid Howells 			if (error)
5632d84f4f99SDavid Howells 				goto abort_change;
56331da177e4SLinus Torvalds 		}
56341da177e4SLinus Torvalds 
56351da177e4SLinus Torvalds 		/* Check permissions for the transition. */
56361da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
56371da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
56381da177e4SLinus Torvalds 		if (error)
5639d84f4f99SDavid Howells 			goto abort_change;
56401da177e4SLinus Torvalds 
56411da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
56421da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5643d84f4f99SDavid Howells 		ptsid = 0;
5644c0c14395SOleg Nesterov 		rcu_read_lock();
564506d98473STejun Heo 		tracer = ptrace_parent(p);
5646d84f4f99SDavid Howells 		if (tracer)
5647d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
5648c0c14395SOleg Nesterov 		rcu_read_unlock();
56491da177e4SLinus Torvalds 
5650d84f4f99SDavid Howells 		if (tracer) {
5651d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5652d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5653d84f4f99SDavid Howells 			if (error)
5654d84f4f99SDavid Howells 				goto abort_change;
5655d84f4f99SDavid Howells 		}
5656d84f4f99SDavid Howells 
5657d84f4f99SDavid Howells 		tsec->sid = sid;
5658d84f4f99SDavid Howells 	} else {
5659d84f4f99SDavid Howells 		error = -EINVAL;
5660d84f4f99SDavid Howells 		goto abort_change;
5661d84f4f99SDavid Howells 	}
5662d84f4f99SDavid Howells 
5663d84f4f99SDavid Howells 	commit_creds(new);
56641da177e4SLinus Torvalds 	return size;
5665d84f4f99SDavid Howells 
5666d84f4f99SDavid Howells abort_change:
5667d84f4f99SDavid Howells 	abort_creds(new);
5668d84f4f99SDavid Howells 	return error;
56691da177e4SLinus Torvalds }
56701da177e4SLinus Torvalds 
5671746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
5672746df9b5SDavid Quigley {
5673746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5674746df9b5SDavid Quigley }
5675746df9b5SDavid Quigley 
5676dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5677dc49c1f9SCatherine Zhang {
5678dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5679dc49c1f9SCatherine Zhang }
5680dc49c1f9SCatherine Zhang 
56817bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
568263cb3449SDavid Howells {
568352a4c640SNikolay Aleksandrov 	return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
568463cb3449SDavid Howells }
568563cb3449SDavid Howells 
5686dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5687dc49c1f9SCatherine Zhang {
5688dc49c1f9SCatherine Zhang 	kfree(secdata);
5689dc49c1f9SCatherine Zhang }
5690dc49c1f9SCatherine Zhang 
56911ee65e37SDavid P. Quigley /*
56921ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
56931ee65e37SDavid P. Quigley  */
56941ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
56951ee65e37SDavid P. Quigley {
56961ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
56971ee65e37SDavid P. Quigley }
56981ee65e37SDavid P. Quigley 
56991ee65e37SDavid P. Quigley /*
57001ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
57011ee65e37SDavid P. Quigley  */
57021ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
57031ee65e37SDavid P. Quigley {
57041ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
57051ee65e37SDavid P. Quigley }
57061ee65e37SDavid P. Quigley 
57071ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
57081ee65e37SDavid P. Quigley {
57091ee65e37SDavid P. Quigley 	int len = 0;
57101ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
57111ee65e37SDavid P. Quigley 						ctx, true);
57121ee65e37SDavid P. Quigley 	if (len < 0)
57131ee65e37SDavid P. Quigley 		return len;
57141ee65e37SDavid P. Quigley 	*ctxlen = len;
57151ee65e37SDavid P. Quigley 	return 0;
57161ee65e37SDavid P. Quigley }
5717d720024eSMichael LeMay #ifdef CONFIG_KEYS
5718d720024eSMichael LeMay 
5719d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
57207e047ef5SDavid Howells 			     unsigned long flags)
5721d720024eSMichael LeMay {
5722d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5723d720024eSMichael LeMay 	struct key_security_struct *ksec;
5724d720024eSMichael LeMay 
5725d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5726d720024eSMichael LeMay 	if (!ksec)
5727d720024eSMichael LeMay 		return -ENOMEM;
5728d720024eSMichael LeMay 
5729d84f4f99SDavid Howells 	tsec = cred->security;
5730d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5731d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
57324eb582cfSMichael LeMay 	else
5733d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5734d720024eSMichael LeMay 
5735275bb41eSDavid Howells 	k->security = ksec;
5736d720024eSMichael LeMay 	return 0;
5737d720024eSMichael LeMay }
5738d720024eSMichael LeMay 
5739d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5740d720024eSMichael LeMay {
5741d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5742d720024eSMichael LeMay 
5743d720024eSMichael LeMay 	k->security = NULL;
5744d720024eSMichael LeMay 	kfree(ksec);
5745d720024eSMichael LeMay }
5746d720024eSMichael LeMay 
5747d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5748d84f4f99SDavid Howells 				  const struct cred *cred,
5749f5895943SDavid Howells 				  unsigned perm)
5750d720024eSMichael LeMay {
5751d720024eSMichael LeMay 	struct key *key;
5752d720024eSMichael LeMay 	struct key_security_struct *ksec;
5753275bb41eSDavid Howells 	u32 sid;
5754d720024eSMichael LeMay 
5755d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5756d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5757d720024eSMichael LeMay 	   appear to be created. */
5758d720024eSMichael LeMay 	if (perm == 0)
5759d720024eSMichael LeMay 		return 0;
5760d720024eSMichael LeMay 
5761d84f4f99SDavid Howells 	sid = cred_sid(cred);
5762275bb41eSDavid Howells 
5763275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5764275bb41eSDavid Howells 	ksec = key->security;
5765275bb41eSDavid Howells 
5766275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5767d720024eSMichael LeMay }
5768d720024eSMichael LeMay 
576970a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
577070a5bb72SDavid Howells {
577170a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
577270a5bb72SDavid Howells 	char *context = NULL;
577370a5bb72SDavid Howells 	unsigned len;
577470a5bb72SDavid Howells 	int rc;
577570a5bb72SDavid Howells 
577670a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
577770a5bb72SDavid Howells 	if (!rc)
577870a5bb72SDavid Howells 		rc = len;
577970a5bb72SDavid Howells 	*_buffer = context;
578070a5bb72SDavid Howells 	return rc;
578170a5bb72SDavid Howells }
578270a5bb72SDavid Howells 
5783d720024eSMichael LeMay #endif
5784d720024eSMichael LeMay 
5785b1d9e6b0SCasey Schaufler static struct security_hook_list selinux_hooks[] = {
5786e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
5787e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
5788e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
5789e20b043aSCasey Schaufler 	LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
579079af7307SStephen Smalley 
5791e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
5792e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
5793e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, selinux_capget),
5794e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capset, selinux_capset),
5795e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, selinux_capable),
5796e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quotactl, selinux_quotactl),
5797e20b043aSCasey Schaufler 	LSM_HOOK_INIT(quota_on, selinux_quota_on),
5798e20b043aSCasey Schaufler 	LSM_HOOK_INIT(syslog, selinux_syslog),
5799e20b043aSCasey Schaufler 	LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
58001da177e4SLinus Torvalds 
5801e20b043aSCasey Schaufler 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
58021da177e4SLinus Torvalds 
5803e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
5804e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
5805e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
5806e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_secureexec, selinux_bprm_secureexec),
58071da177e4SLinus Torvalds 
5808e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
5809e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
5810e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
5811e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
5812e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
5813e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
5814e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
5815e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, selinux_mount),
5816e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, selinux_umount),
5817e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
5818e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
5819e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
5820e0007529SEric Paris 
5821e20b043aSCasey Schaufler 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
58221da177e4SLinus Torvalds 
5823e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
5824e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
5825e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
5826e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_create, selinux_inode_create),
5827e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_link, selinux_inode_link),
5828e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
5829e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
5830e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
5831e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
5832e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
5833e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
5834e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
5835e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
5836e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
5837e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
5838e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
5839e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
5840e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
5841e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
5842e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
5843e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
5844e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
5845e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
5846e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
5847e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
58481da177e4SLinus Torvalds 
5849e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, selinux_file_permission),
5850e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
5851e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
5852e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
5853e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
5854e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
5855e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
5856e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, selinux_file_lock),
5857e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
5858e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
5859e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
5860e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_receive, selinux_file_receive),
58611da177e4SLinus Torvalds 
5862e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, selinux_file_open),
5863788e7dd4SYuichi Nakamura 
5864e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_create, selinux_task_create),
5865e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
5866e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, selinux_cred_free),
5867e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
5868e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
5869e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
5870e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
5871e20b043aSCasey Schaufler 	LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
5872e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
5873e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
5874e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
5875e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
5876e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
5877e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
5878e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
5879e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
5880e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
5881e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
5882e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
5883e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
5884e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_wait, selinux_task_wait),
5885e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
58861da177e4SLinus Torvalds 
5887e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
5888e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
58891da177e4SLinus Torvalds 
5890e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
5891e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
58921da177e4SLinus Torvalds 
5893e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_alloc_security,
5894e20b043aSCasey Schaufler 			selinux_msg_queue_alloc_security),
5895e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
5896e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
5897e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
5898e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
5899e20b043aSCasey Schaufler 	LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
59001da177e4SLinus Torvalds 
5901e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
5902e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
5903e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
5904e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
5905e20b043aSCasey Schaufler 	LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
59061da177e4SLinus Torvalds 
5907e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
5908e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
5909e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
5910e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
5911e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
59121da177e4SLinus Torvalds 
5913e20b043aSCasey Schaufler 	LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
59141da177e4SLinus Torvalds 
5915e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
5916e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
59171da177e4SLinus Torvalds 
5918e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
5919e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
5920e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
5921e20b043aSCasey Schaufler 	LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
5922e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
5923e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
5924e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
5925dc49c1f9SCatherine Zhang 
5926e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
5927e20b043aSCasey Schaufler 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
59281da177e4SLinus Torvalds 
5929e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_create, selinux_socket_create),
5930e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
5931e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
5932e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
5933e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
5934e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
5935e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
5936e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
5937e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
5938e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
5939e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
5940e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
5941e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
5942e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
5943e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_stream,
5944e20b043aSCasey Schaufler 			selinux_socket_getpeersec_stream),
5945e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
5946e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
5947e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
5948e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
5949e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
5950e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
5951e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
5952e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
5953e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
5954e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
5955e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
5956e20b043aSCasey Schaufler 	LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
5957e20b043aSCasey Schaufler 	LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
5958e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
5959e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
5960e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
5961e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
5962e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
5963e20b043aSCasey Schaufler 	LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
5964d28d1e08STrent Jaeger 
5965d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5966e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
5967e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
5968e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
5969e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
5970e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
5971e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_alloc_acquire,
5972e20b043aSCasey Schaufler 			selinux_xfrm_state_alloc_acquire),
5973e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
5974e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
5975e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
5976e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_state_pol_flow_match,
5977e20b043aSCasey Schaufler 			selinux_xfrm_state_pol_flow_match),
5978e20b043aSCasey Schaufler 	LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
59791da177e4SLinus Torvalds #endif
5980d720024eSMichael LeMay 
5981d720024eSMichael LeMay #ifdef CONFIG_KEYS
5982e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
5983e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_free, selinux_key_free),
5984e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_permission, selinux_key_permission),
5985e20b043aSCasey Schaufler 	LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
5986d720024eSMichael LeMay #endif
59879d57a7f9SAhmed S. Darwish 
59889d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
5989e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
5990e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
5991e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
5992e20b043aSCasey Schaufler 	LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
59939d57a7f9SAhmed S. Darwish #endif
59941da177e4SLinus Torvalds };
59951da177e4SLinus Torvalds 
59961da177e4SLinus Torvalds static __init int selinux_init(void)
59971da177e4SLinus Torvalds {
5998b1d9e6b0SCasey Schaufler 	if (!security_module_enable("selinux")) {
5999076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
6000076c54c5SAhmed S. Darwish 		return 0;
6001076c54c5SAhmed S. Darwish 	}
6002076c54c5SAhmed S. Darwish 
60031da177e4SLinus Torvalds 	if (!selinux_enabled) {
60041da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
60051da177e4SLinus Torvalds 		return 0;
60061da177e4SLinus Torvalds 	}
60071da177e4SLinus Torvalds 
60081da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
60091da177e4SLinus Torvalds 
60101da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
6011d84f4f99SDavid Howells 	cred_init_security();
60121da177e4SLinus Torvalds 
6013fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6014fcaaade1SStephen Smalley 
60157cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
60167cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
601720c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
60181da177e4SLinus Torvalds 	avc_init();
60191da177e4SLinus Torvalds 
6020b1d9e6b0SCasey Schaufler 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
60211da177e4SLinus Torvalds 
6022615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6023615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
6024615e51fdSPaul Moore 
6025828dfe1dSEric Paris 	if (selinux_enforcing)
6026fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
6027828dfe1dSEric Paris 	else
6028fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
6029d720024eSMichael LeMay 
60301da177e4SLinus Torvalds 	return 0;
60311da177e4SLinus Torvalds }
60321da177e4SLinus Torvalds 
6033e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
6034e8c26255SAl Viro {
6035e8c26255SAl Viro 	superblock_doinit(sb, NULL);
6036e8c26255SAl Viro }
6037e8c26255SAl Viro 
60381da177e4SLinus Torvalds void selinux_complete_init(void)
60391da177e4SLinus Torvalds {
6040fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
60411da177e4SLinus Torvalds 
60421da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
6043fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
6044e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
60451da177e4SLinus Torvalds }
60461da177e4SLinus Torvalds 
60471da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
60481da177e4SLinus Torvalds    all processes and objects when they are created. */
60491da177e4SLinus Torvalds security_initcall(selinux_init);
60501da177e4SLinus Torvalds 
6051c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
60521da177e4SLinus Torvalds 
605325db6beaSJiri Pirko static struct nf_hook_ops selinux_nf_ops[] = {
6054effad8dfSPaul Moore 	{
6055effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
60561da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
60572597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
60586e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
60591da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
6060effad8dfSPaul Moore 	},
6061effad8dfSPaul Moore 	{
6062effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
6063effad8dfSPaul Moore 		.owner =	THIS_MODULE,
60642597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
6065effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
6066effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
6067948bf85cSPaul Moore 	},
6068948bf85cSPaul Moore 	{
6069948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
6070948bf85cSPaul Moore 		.owner =	THIS_MODULE,
60712597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
6072948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
6073948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
607425db6beaSJiri Pirko 	},
60751da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6076effad8dfSPaul Moore 	{
6077effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
60781da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
60792597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
60806e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
60811da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
6082effad8dfSPaul Moore 	},
6083effad8dfSPaul Moore 	{
6084effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
6085effad8dfSPaul Moore 		.owner =	THIS_MODULE,
60862597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
6087effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
6088effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
608925db6beaSJiri Pirko 	},
60901da177e4SLinus Torvalds #endif	/* IPV6 */
609125db6beaSJiri Pirko };
60921da177e4SLinus Torvalds 
60931da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
60941da177e4SLinus Torvalds {
609525db6beaSJiri Pirko 	int err;
60961da177e4SLinus Torvalds 
60971da177e4SLinus Torvalds 	if (!selinux_enabled)
609825db6beaSJiri Pirko 		return 0;
60991da177e4SLinus Torvalds 
6100fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
61011da177e4SLinus Torvalds 
610225db6beaSJiri Pirko 	err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
61031da177e4SLinus Torvalds 	if (err)
610425db6beaSJiri Pirko 		panic("SELinux: nf_register_hooks: error %d\n", err);
61051da177e4SLinus Torvalds 
610625db6beaSJiri Pirko 	return 0;
61071da177e4SLinus Torvalds }
61081da177e4SLinus Torvalds 
61091da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
61101da177e4SLinus Torvalds 
61111da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
61121da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
61131da177e4SLinus Torvalds {
6114fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
61151da177e4SLinus Torvalds 
611625db6beaSJiri Pirko 	nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
61171da177e4SLinus Torvalds }
61181da177e4SLinus Torvalds #endif
61191da177e4SLinus Torvalds 
6120c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
61211da177e4SLinus Torvalds 
61221da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
61231da177e4SLinus Torvalds #define selinux_nf_ip_exit()
61241da177e4SLinus Torvalds #endif
61251da177e4SLinus Torvalds 
6126c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
61271da177e4SLinus Torvalds 
61281da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6129828dfe1dSEric Paris static int selinux_disabled;
6130828dfe1dSEric Paris 
61311da177e4SLinus Torvalds int selinux_disable(void)
61321da177e4SLinus Torvalds {
61331da177e4SLinus Torvalds 	if (ss_initialized) {
61341da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
61351da177e4SLinus Torvalds 		return -EINVAL;
61361da177e4SLinus Torvalds 	}
61371da177e4SLinus Torvalds 
61381da177e4SLinus Torvalds 	if (selinux_disabled) {
61391da177e4SLinus Torvalds 		/* Only do this once. */
61401da177e4SLinus Torvalds 		return -EINVAL;
61411da177e4SLinus Torvalds 	}
61421da177e4SLinus Torvalds 
61431da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
61441da177e4SLinus Torvalds 
61451da177e4SLinus Torvalds 	selinux_disabled = 1;
614630d55280SStephen Smalley 	selinux_enabled = 0;
61471da177e4SLinus Torvalds 
6148b1d9e6b0SCasey Schaufler 	security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
61491da177e4SLinus Torvalds 
6150af8ff049SEric Paris 	/* Try to destroy the avc node cache */
6151af8ff049SEric Paris 	avc_disable();
6152af8ff049SEric Paris 
61531da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
61541da177e4SLinus Torvalds 	selinux_nf_ip_exit();
61551da177e4SLinus Torvalds 
61561da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
61571da177e4SLinus Torvalds 	exit_sel_fs();
61581da177e4SLinus Torvalds 
61591da177e4SLinus Torvalds 	return 0;
61601da177e4SLinus Torvalds }
61611da177e4SLinus Torvalds #endif
6162