xref: /openbmc/linux/security/selinux/hooks.c (revision 2e33405785d3eaec303c54b4a10afdebf3729da7)
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>
321da177e4SLinus Torvalds #include <linux/security.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 */
55220deb96SPaul Moore #include <net/net_namespace.h>
56d621d35eSPaul Moore #include <net/netlabel.h>
57f5269710SEric Paris #include <linux/uaccess.h>
581da177e4SLinus Torvalds #include <asm/ioctls.h>
5960063497SArun Sharma #include <linux/atomic.h>
601da177e4SLinus Torvalds #include <linux/bitops.h>
611da177e4SLinus Torvalds #include <linux/interrupt.h>
621da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
631da177e4SLinus Torvalds #include <linux/netlink.h>
641da177e4SLinus Torvalds #include <linux/tcp.h>
651da177e4SLinus Torvalds #include <linux/udp.h>
662ee92d46SJames Morris #include <linux/dccp.h>
671da177e4SLinus Torvalds #include <linux/quota.h>
681da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
691da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
701da177e4SLinus Torvalds #include <linux/parser.h>
711da177e4SLinus Torvalds #include <linux/nfs_mount.h>
721da177e4SLinus Torvalds #include <net/ipv6.h>
731da177e4SLinus Torvalds #include <linux/hugetlb.h>
741da177e4SLinus Torvalds #include <linux/personality.h>
751da177e4SLinus Torvalds #include <linux/audit.h>
766931dfc9SEric Paris #include <linux/string.h>
77877ce7c1SCatherine Zhang #include <linux/selinux.h>
7823970741SEric Paris #include <linux/mutex.h>
79f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8000234592SKees Cook #include <linux/syslog.h>
813486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8244fc7ea0SPaul Gortmaker #include <linux/export.h>
8340401530SAl Viro #include <linux/msg.h>
8440401530SAl Viro #include <linux/shm.h>
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #include "avc.h"
871da177e4SLinus Torvalds #include "objsec.h"
881da177e4SLinus Torvalds #include "netif.h"
89224dfbd8SPaul Moore #include "netnode.h"
903e112172SPaul Moore #include "netport.h"
91d28d1e08STrent Jaeger #include "xfrm.h"
92c60475bfSPaul Moore #include "netlabel.h"
939d57a7f9SAhmed S. Darwish #include "audit.h"
947b98a585SJames Morris #include "avc_ss.h"
951da177e4SLinus Torvalds 
9611689d47SDavid P. Quigley #define NUM_SEL_MNT_OPTS 5
97c9180a57SEric Paris 
9820510f2fSJames Morris extern struct security_operations *security_ops;
991da177e4SLinus Torvalds 
100d621d35eSPaul Moore /* SECMARK reference count */
10156a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
102d621d35eSPaul Moore 
1031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
104828dfe1dSEric Paris int selinux_enforcing;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1071da177e4SLinus Torvalds {
108f5269710SEric Paris 	unsigned long enforcing;
109f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enforcing))
110f5269710SEric Paris 		selinux_enforcing = enforcing ? 1 : 0;
1111da177e4SLinus Torvalds 	return 1;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1141da177e4SLinus Torvalds #endif
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1171da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1201da177e4SLinus Torvalds {
121f5269710SEric Paris 	unsigned long enabled;
122f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enabled))
123f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1241da177e4SLinus Torvalds 	return 1;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
12730d55280SStephen Smalley #else
12830d55280SStephen Smalley int selinux_enabled = 1;
1291da177e4SLinus Torvalds #endif
1301da177e4SLinus Torvalds 
131e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1327cae7e26SJames Morris 
133d621d35eSPaul Moore /**
134d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
135d621d35eSPaul Moore  *
136d621d35eSPaul Moore  * Description:
137d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
138d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
139d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
140d621d35eSPaul Moore  * enabled, false (0) if SECMARK is disabled.
141d621d35eSPaul Moore  *
142d621d35eSPaul Moore  */
143d621d35eSPaul Moore static int selinux_secmark_enabled(void)
144d621d35eSPaul Moore {
145d621d35eSPaul Moore 	return (atomic_read(&selinux_secmark_refcount) > 0);
146d621d35eSPaul Moore }
147d621d35eSPaul Moore 
148d84f4f99SDavid Howells /*
149d84f4f99SDavid Howells  * initialise the security for the init task
150d84f4f99SDavid Howells  */
151d84f4f99SDavid Howells static void cred_init_security(void)
1521da177e4SLinus Torvalds {
1533b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1541da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1551da177e4SLinus Torvalds 
15689d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1571da177e4SLinus Torvalds 	if (!tsec)
158d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1591da177e4SLinus Torvalds 
160d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
161f1752eecSDavid Howells 	cred->security = tsec;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
164275bb41eSDavid Howells /*
16588e67f3bSDavid Howells  * get the security ID of a set of credentials
16688e67f3bSDavid Howells  */
16788e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
16888e67f3bSDavid Howells {
16988e67f3bSDavid Howells 	const struct task_security_struct *tsec;
17088e67f3bSDavid Howells 
17188e67f3bSDavid Howells 	tsec = cred->security;
17288e67f3bSDavid Howells 	return tsec->sid;
17388e67f3bSDavid Howells }
17488e67f3bSDavid Howells 
17588e67f3bSDavid Howells /*
1763b11a1deSDavid Howells  * get the objective security ID of a task
177275bb41eSDavid Howells  */
178275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
179275bb41eSDavid Howells {
180275bb41eSDavid Howells 	u32 sid;
181275bb41eSDavid Howells 
182275bb41eSDavid Howells 	rcu_read_lock();
18388e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
184275bb41eSDavid Howells 	rcu_read_unlock();
185275bb41eSDavid Howells 	return sid;
186275bb41eSDavid Howells }
187275bb41eSDavid Howells 
188275bb41eSDavid Howells /*
1893b11a1deSDavid Howells  * get the subjective security ID of the current task
190275bb41eSDavid Howells  */
191275bb41eSDavid Howells static inline u32 current_sid(void)
192275bb41eSDavid Howells {
1935fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
194275bb41eSDavid Howells 
195275bb41eSDavid Howells 	return tsec->sid;
196275bb41eSDavid Howells }
197275bb41eSDavid Howells 
19888e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
19988e67f3bSDavid Howells 
2001da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct inode_security_struct *isec;
203275bb41eSDavid Howells 	u32 sid = current_sid();
2041da177e4SLinus Torvalds 
205a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2061da177e4SLinus Torvalds 	if (!isec)
2071da177e4SLinus Torvalds 		return -ENOMEM;
2081da177e4SLinus Torvalds 
20923970741SEric Paris 	mutex_init(&isec->lock);
2101da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2111da177e4SLinus Torvalds 	isec->inode = inode;
2121da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2131da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
214275bb41eSDavid Howells 	isec->task_sid = sid;
2151da177e4SLinus Torvalds 	inode->i_security = isec;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	return 0;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2231da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2261da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2271da177e4SLinus Torvalds 		list_del_init(&isec->list);
2281da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	inode->i_security = NULL;
2317cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct file_security_struct *fsec;
237275bb41eSDavid Howells 	u32 sid = current_sid();
2381da177e4SLinus Torvalds 
23926d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2401da177e4SLinus Torvalds 	if (!fsec)
2411da177e4SLinus Torvalds 		return -ENOMEM;
2421da177e4SLinus Torvalds 
243275bb41eSDavid Howells 	fsec->sid = sid;
244275bb41eSDavid Howells 	fsec->fown_sid = sid;
2451da177e4SLinus Torvalds 	file->f_security = fsec;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	return 0;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static void file_free_security(struct file *file)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2531da177e4SLinus Torvalds 	file->f_security = NULL;
2541da177e4SLinus Torvalds 	kfree(fsec);
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2601da177e4SLinus Torvalds 
26189d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2621da177e4SLinus Torvalds 	if (!sbsec)
2631da177e4SLinus Torvalds 		return -ENOMEM;
2641da177e4SLinus Torvalds 
265bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2661da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2671da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2681da177e4SLinus Torvalds 	sbsec->sb = sb;
2691da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2701da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
271c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2721da177e4SLinus Torvalds 	sb->s_security = sbsec;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	return 0;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2801da177e4SLinus Torvalds 	sb->s_security = NULL;
2811da177e4SLinus Torvalds 	kfree(sbsec);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
2851da177e4SLinus Torvalds 
286634a539eSStephen Hemminger static const char *labeling_behaviors[6] = {
2871da177e4SLinus Torvalds 	"uses xattr",
2881da177e4SLinus Torvalds 	"uses transition SIDs",
2891da177e4SLinus Torvalds 	"uses task SIDs",
2901da177e4SLinus Torvalds 	"uses genfs_contexts",
2911da177e4SLinus Torvalds 	"not configured for labeling",
2921da177e4SLinus Torvalds 	"uses mountpoint labeling",
2931da177e4SLinus Torvalds };
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds enum {
30331e87930SEric Paris 	Opt_error = -1,
3041da177e4SLinus Torvalds 	Opt_context = 1,
3051da177e4SLinus Torvalds 	Opt_fscontext = 2,
306c9180a57SEric Paris 	Opt_defcontext = 3,
307c9180a57SEric Paris 	Opt_rootcontext = 4,
30811689d47SDavid P. Quigley 	Opt_labelsupport = 5,
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
311a447c093SSteven Whitehouse static const match_table_t tokens = {
312832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
313832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
314832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
315832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
31611689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
31731e87930SEric Paris 	{Opt_error, NULL},
3181da177e4SLinus Torvalds };
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3211da177e4SLinus Torvalds 
322c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
323c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
324275bb41eSDavid Howells 			const struct cred *cred)
325c312feb2SEric Paris {
326275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
327c312feb2SEric Paris 	int rc;
328c312feb2SEric Paris 
329c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
330c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
331c312feb2SEric Paris 	if (rc)
332c312feb2SEric Paris 		return rc;
333c312feb2SEric Paris 
334c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
335c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
336c312feb2SEric Paris 	return rc;
337c312feb2SEric Paris }
338c312feb2SEric Paris 
3390808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3400808925eSEric Paris 			struct superblock_security_struct *sbsec,
341275bb41eSDavid Howells 			const struct cred *cred)
3420808925eSEric Paris {
343275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3440808925eSEric Paris 	int rc;
3450808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3460808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3470808925eSEric Paris 	if (rc)
3480808925eSEric Paris 		return rc;
3490808925eSEric Paris 
3500808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3510808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3520808925eSEric Paris 	return rc;
3530808925eSEric Paris }
3540808925eSEric Paris 
355c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3581da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
359c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
3601da177e4SLinus Torvalds 	int rc = 0;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
3631da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
3641da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
3651da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
3661da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
3671da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
368c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
3691da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
3701da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
3711da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
3721da177e4SLinus Torvalds 			goto out;
3731da177e4SLinus Torvalds 		}
374c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
3751da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
3761da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
3771da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3781da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
3791da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
3801da177e4SLinus Torvalds 			else
3811da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3821da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
3831da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
3841da177e4SLinus Torvalds 			goto out;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
38811689d47SDavid P. Quigley 	sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
3891da177e4SLinus Torvalds 
390c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
391fadcdb45SEric Paris 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
3921da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
393c9180a57SEric Paris 	else
394fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
3951da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
3961da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
3971da177e4SLinus Torvalds 
39811689d47SDavid P. Quigley 	if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
39911689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
40011689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_NONE ||
40111689d47SDavid P. Quigley 	    sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
40211689d47SDavid P. Quigley 		sbsec->flags &= ~SE_SBLABELSUPP;
40311689d47SDavid P. Quigley 
404ddd29ec6SDavid P. Quigley 	/* Special handling for sysfs. Is genfs but also has setxattr handler*/
405ddd29ec6SDavid P. Quigley 	if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
406ddd29ec6SDavid P. Quigley 		sbsec->flags |= SE_SBLABELSUPP;
407ddd29ec6SDavid P. Quigley 
4081da177e4SLinus Torvalds 	/* Initialize the root inode. */
409c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4121da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4131da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4141da177e4SLinus Torvalds 	   populates itself. */
4151da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4161da177e4SLinus Torvalds next_inode:
4171da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4181da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4191da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4201da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4211da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
4221da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4231da177e4SLinus Torvalds 		inode = igrab(inode);
4241da177e4SLinus Torvalds 		if (inode) {
4251da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4261da177e4SLinus Torvalds 				inode_doinit(inode);
4271da177e4SLinus Torvalds 			iput(inode);
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4301da177e4SLinus Torvalds 		list_del_init(&isec->list);
4311da177e4SLinus Torvalds 		goto next_inode;
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4341da177e4SLinus Torvalds out:
435c9180a57SEric Paris 	return rc;
436c9180a57SEric Paris }
437c9180a57SEric Paris 
438c9180a57SEric Paris /*
439c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
440c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
441c9180a57SEric Paris  * mount options, or whatever.
442c9180a57SEric Paris  */
443c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
444e0007529SEric Paris 				struct security_mnt_opts *opts)
445c9180a57SEric Paris {
446c9180a57SEric Paris 	int rc = 0, i;
447c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
448c9180a57SEric Paris 	char *context = NULL;
449c9180a57SEric Paris 	u32 len;
450c9180a57SEric Paris 	char tmp;
451c9180a57SEric Paris 
452e0007529SEric Paris 	security_init_mnt_opts(opts);
453c9180a57SEric Paris 
4540d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
455c9180a57SEric Paris 		return -EINVAL;
456c9180a57SEric Paris 
457c9180a57SEric Paris 	if (!ss_initialized)
458c9180a57SEric Paris 		return -EINVAL;
459c9180a57SEric Paris 
4600d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
461c9180a57SEric Paris 	/* count the number of mount options for this sb */
462c9180a57SEric Paris 	for (i = 0; i < 8; i++) {
463c9180a57SEric Paris 		if (tmp & 0x01)
464e0007529SEric Paris 			opts->num_mnt_opts++;
465c9180a57SEric Paris 		tmp >>= 1;
466c9180a57SEric Paris 	}
46711689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
46811689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP)
46911689d47SDavid P. Quigley 		opts->num_mnt_opts++;
470c9180a57SEric Paris 
471e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
472e0007529SEric Paris 	if (!opts->mnt_opts) {
473c9180a57SEric Paris 		rc = -ENOMEM;
474c9180a57SEric Paris 		goto out_free;
475c9180a57SEric Paris 	}
476c9180a57SEric Paris 
477e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
478e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
479c9180a57SEric Paris 		rc = -ENOMEM;
480c9180a57SEric Paris 		goto out_free;
481c9180a57SEric Paris 	}
482c9180a57SEric Paris 
483c9180a57SEric Paris 	i = 0;
484c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
485c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
486c9180a57SEric Paris 		if (rc)
487c9180a57SEric Paris 			goto out_free;
488e0007529SEric Paris 		opts->mnt_opts[i] = context;
489e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
490c9180a57SEric Paris 	}
491c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
492c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
493c9180a57SEric Paris 		if (rc)
494c9180a57SEric Paris 			goto out_free;
495e0007529SEric Paris 		opts->mnt_opts[i] = context;
496e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
497c9180a57SEric Paris 	}
498c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
499c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
500c9180a57SEric Paris 		if (rc)
501c9180a57SEric Paris 			goto out_free;
502e0007529SEric Paris 		opts->mnt_opts[i] = context;
503e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
504c9180a57SEric Paris 	}
505c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
506c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
507c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
508c9180a57SEric Paris 
509c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
510c9180a57SEric Paris 		if (rc)
511c9180a57SEric Paris 			goto out_free;
512e0007529SEric Paris 		opts->mnt_opts[i] = context;
513e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
514c9180a57SEric Paris 	}
51511689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP) {
51611689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
51711689d47SDavid P. Quigley 		opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
51811689d47SDavid P. Quigley 	}
519c9180a57SEric Paris 
520e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
521c9180a57SEric Paris 
522c9180a57SEric Paris 	return 0;
523c9180a57SEric Paris 
524c9180a57SEric Paris out_free:
525e0007529SEric Paris 	security_free_mnt_opts(opts);
526c9180a57SEric Paris 	return rc;
527c9180a57SEric Paris }
528c9180a57SEric Paris 
529c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
530c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
531c9180a57SEric Paris {
5320d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5330d90a7ecSDavid P. Quigley 
534c9180a57SEric Paris 	/* check if the old mount command had the same options */
5350d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
536c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
537c9180a57SEric Paris 		    (old_sid != new_sid))
538c9180a57SEric Paris 			return 1;
539c9180a57SEric Paris 
540c9180a57SEric Paris 	/* check if we were passed the same options twice,
541c9180a57SEric Paris 	 * aka someone passed context=a,context=b
542c9180a57SEric Paris 	 */
5430d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5440d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
545c9180a57SEric Paris 			return 1;
546c9180a57SEric Paris 	return 0;
547c9180a57SEric Paris }
548e0007529SEric Paris 
549c9180a57SEric Paris /*
550c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
551c9180a57SEric Paris  * labeling information.
552c9180a57SEric Paris  */
553e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
554e0007529SEric Paris 				struct security_mnt_opts *opts)
555c9180a57SEric Paris {
556275bb41eSDavid Howells 	const struct cred *cred = current_cred();
557c9180a57SEric Paris 	int rc = 0, i;
558c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
559c9180a57SEric Paris 	const char *name = sb->s_type->name;
560089be43eSJames Morris 	struct inode *inode = sbsec->sb->s_root->d_inode;
561089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
562c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
563c9180a57SEric Paris 	u32 defcontext_sid = 0;
564e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
565e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
566e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
567c9180a57SEric Paris 
568c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
569c9180a57SEric Paris 
570c9180a57SEric Paris 	if (!ss_initialized) {
571c9180a57SEric Paris 		if (!num_opts) {
572c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
573c9180a57SEric Paris 			   after the initial policy is loaded and the security
574c9180a57SEric Paris 			   server is ready to handle calls. */
575c9180a57SEric Paris 			goto out;
576c9180a57SEric Paris 		}
577c9180a57SEric Paris 		rc = -EINVAL;
578744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
579744ba35eSEric Paris 			"before the security server is initialized\n");
580c9180a57SEric Paris 		goto out;
581c9180a57SEric Paris 	}
582c9180a57SEric Paris 
583c9180a57SEric Paris 	/*
584e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
585e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
586e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
587e0007529SEric Paris 	 * we need to skip the double mount verification.
588e0007529SEric Paris 	 *
589e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
590e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
591e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
592e0007529SEric Paris 	 * will be used for both mounts)
593e0007529SEric Paris 	 */
5940d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
595e0007529SEric Paris 	    && (num_opts == 0))
596e0007529SEric Paris 		goto out;
597e0007529SEric Paris 
598e0007529SEric Paris 	/*
599c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
600c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
601c9180a57SEric Paris 	 * than once with different security options.
602c9180a57SEric Paris 	 */
603c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
604c9180a57SEric Paris 		u32 sid;
60511689d47SDavid P. Quigley 
60611689d47SDavid P. Quigley 		if (flags[i] == SE_SBLABELSUPP)
60711689d47SDavid P. Quigley 			continue;
608c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
609c9180a57SEric Paris 					     strlen(mount_options[i]), &sid);
610c9180a57SEric Paris 		if (rc) {
611c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
612c9180a57SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
613c9180a57SEric Paris 			       mount_options[i], sb->s_id, name, rc);
614c9180a57SEric Paris 			goto out;
615c9180a57SEric Paris 		}
616c9180a57SEric Paris 		switch (flags[i]) {
617c9180a57SEric Paris 		case FSCONTEXT_MNT:
618c9180a57SEric Paris 			fscontext_sid = sid;
619c9180a57SEric Paris 
620c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
621c9180a57SEric Paris 					fscontext_sid))
622c9180a57SEric Paris 				goto out_double_mount;
623c9180a57SEric Paris 
624c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
625c9180a57SEric Paris 			break;
626c9180a57SEric Paris 		case CONTEXT_MNT:
627c9180a57SEric Paris 			context_sid = sid;
628c9180a57SEric Paris 
629c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
630c9180a57SEric Paris 					context_sid))
631c9180a57SEric Paris 				goto out_double_mount;
632c9180a57SEric Paris 
633c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
634c9180a57SEric Paris 			break;
635c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
636c9180a57SEric Paris 			rootcontext_sid = sid;
637c9180a57SEric Paris 
638c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
639c9180a57SEric Paris 					rootcontext_sid))
640c9180a57SEric Paris 				goto out_double_mount;
641c9180a57SEric Paris 
642c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
643c9180a57SEric Paris 
644c9180a57SEric Paris 			break;
645c9180a57SEric Paris 		case DEFCONTEXT_MNT:
646c9180a57SEric Paris 			defcontext_sid = sid;
647c9180a57SEric Paris 
648c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
649c9180a57SEric Paris 					defcontext_sid))
650c9180a57SEric Paris 				goto out_double_mount;
651c9180a57SEric Paris 
652c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
653c9180a57SEric Paris 
654c9180a57SEric Paris 			break;
655c9180a57SEric Paris 		default:
656c9180a57SEric Paris 			rc = -EINVAL;
657c9180a57SEric Paris 			goto out;
658c9180a57SEric Paris 		}
659c9180a57SEric Paris 	}
660c9180a57SEric Paris 
6610d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
662c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
6630d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
664c9180a57SEric Paris 			goto out_double_mount;
665c9180a57SEric Paris 		rc = 0;
666c9180a57SEric Paris 		goto out;
667c9180a57SEric Paris 	}
668c9180a57SEric Paris 
669089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
6700d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
671c9180a57SEric Paris 
672c9180a57SEric Paris 	/* Determine the labeling behavior to use for this filesystem type. */
6730d90a7ecSDavid P. Quigley 	rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
674c9180a57SEric Paris 	if (rc) {
675c9180a57SEric Paris 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
676089be43eSJames Morris 		       __func__, sb->s_type->name, rc);
677c9180a57SEric Paris 		goto out;
678c9180a57SEric Paris 	}
679c9180a57SEric Paris 
680c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
681c9180a57SEric Paris 	if (fscontext_sid) {
682275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
683c9180a57SEric Paris 		if (rc)
684c9180a57SEric Paris 			goto out;
685c9180a57SEric Paris 
686c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
687c9180a57SEric Paris 	}
688c9180a57SEric Paris 
689c9180a57SEric Paris 	/*
690c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
691c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
692c9180a57SEric Paris 	 * the superblock context if not already set.
693c9180a57SEric Paris 	 */
694c9180a57SEric Paris 	if (context_sid) {
695c9180a57SEric Paris 		if (!fscontext_sid) {
696275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
697275bb41eSDavid Howells 							  cred);
698c9180a57SEric Paris 			if (rc)
699c9180a57SEric Paris 				goto out;
700c9180a57SEric Paris 			sbsec->sid = context_sid;
701c9180a57SEric Paris 		} else {
702275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
703275bb41eSDavid Howells 							     cred);
704c9180a57SEric Paris 			if (rc)
705c9180a57SEric Paris 				goto out;
706c9180a57SEric Paris 		}
707c9180a57SEric Paris 		if (!rootcontext_sid)
708c9180a57SEric Paris 			rootcontext_sid = context_sid;
709c9180a57SEric Paris 
710c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
711c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
712c9180a57SEric Paris 	}
713c9180a57SEric Paris 
714c9180a57SEric Paris 	if (rootcontext_sid) {
715275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
716275bb41eSDavid Howells 						     cred);
717c9180a57SEric Paris 		if (rc)
718c9180a57SEric Paris 			goto out;
719c9180a57SEric Paris 
720c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
721c9180a57SEric Paris 		root_isec->initialized = 1;
722c9180a57SEric Paris 	}
723c9180a57SEric Paris 
724c9180a57SEric Paris 	if (defcontext_sid) {
725c9180a57SEric Paris 		if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
726c9180a57SEric Paris 			rc = -EINVAL;
727c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
728c9180a57SEric Paris 			       "invalid for this filesystem type\n");
729c9180a57SEric Paris 			goto out;
730c9180a57SEric Paris 		}
731c9180a57SEric Paris 
732c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
733c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
734275bb41eSDavid Howells 							     sbsec, cred);
735c9180a57SEric Paris 			if (rc)
736c9180a57SEric Paris 				goto out;
737c9180a57SEric Paris 		}
738c9180a57SEric Paris 
739c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
740c9180a57SEric Paris 	}
741c9180a57SEric Paris 
742c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
743c9180a57SEric Paris out:
744bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
7451da177e4SLinus Torvalds 	return rc;
746c9180a57SEric Paris out_double_mount:
747c9180a57SEric Paris 	rc = -EINVAL;
748c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
749c9180a57SEric Paris 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
750c9180a57SEric Paris 	goto out;
751c9180a57SEric Paris }
752c9180a57SEric Paris 
753c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
754c9180a57SEric Paris 					struct super_block *newsb)
755c9180a57SEric Paris {
756c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
757c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
758c9180a57SEric Paris 
759c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
760c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
761c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
762c9180a57SEric Paris 
7630f5e6420SEric Paris 	/*
7640f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
765e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
7660f5e6420SEric Paris 	 */
767e8c26255SAl Viro 	if (!ss_initialized)
7680f5e6420SEric Paris 		return;
769c9180a57SEric Paris 
770c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
7710d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
772c9180a57SEric Paris 
7735a552617SEric Paris 	/* if fs is reusing a sb, just let its options stand... */
7740d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
7755a552617SEric Paris 		return;
7765a552617SEric Paris 
777c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
778c9180a57SEric Paris 
779c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
780c9180a57SEric Paris 
781c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
782c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
783c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
784c9180a57SEric Paris 
785c9180a57SEric Paris 	if (set_context) {
786c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
787c9180a57SEric Paris 
788c9180a57SEric Paris 		if (!set_fscontext)
789c9180a57SEric Paris 			newsbsec->sid = sid;
790c9180a57SEric Paris 		if (!set_rootcontext) {
791c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
792c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
793c9180a57SEric Paris 			newisec->sid = sid;
794c9180a57SEric Paris 		}
795c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
796c9180a57SEric Paris 	}
797c9180a57SEric Paris 	if (set_rootcontext) {
798c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
799c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
800c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
801c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
802c9180a57SEric Paris 
803c9180a57SEric Paris 		newisec->sid = oldisec->sid;
804c9180a57SEric Paris 	}
805c9180a57SEric Paris 
806c9180a57SEric Paris 	sb_finish_set_opts(newsb);
807c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
808c9180a57SEric Paris }
809c9180a57SEric Paris 
8102e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
8112e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
812c9180a57SEric Paris {
813e0007529SEric Paris 	char *p;
814c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
815c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
816e0007529SEric Paris 	int rc, num_mnt_opts = 0;
817c9180a57SEric Paris 
818e0007529SEric Paris 	opts->num_mnt_opts = 0;
819c9180a57SEric Paris 
820c9180a57SEric Paris 	/* Standard string-based options. */
821c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
822c9180a57SEric Paris 		int token;
823c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
824c9180a57SEric Paris 
825c9180a57SEric Paris 		if (!*p)
826c9180a57SEric Paris 			continue;
827c9180a57SEric Paris 
828c9180a57SEric Paris 		token = match_token(p, tokens, args);
829c9180a57SEric Paris 
830c9180a57SEric Paris 		switch (token) {
831c9180a57SEric Paris 		case Opt_context:
832c9180a57SEric Paris 			if (context || defcontext) {
833c9180a57SEric Paris 				rc = -EINVAL;
834c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
835c9180a57SEric Paris 				goto out_err;
836c9180a57SEric Paris 			}
837c9180a57SEric Paris 			context = match_strdup(&args[0]);
838c9180a57SEric Paris 			if (!context) {
839c9180a57SEric Paris 				rc = -ENOMEM;
840c9180a57SEric Paris 				goto out_err;
841c9180a57SEric Paris 			}
842c9180a57SEric Paris 			break;
843c9180a57SEric Paris 
844c9180a57SEric Paris 		case Opt_fscontext:
845c9180a57SEric Paris 			if (fscontext) {
846c9180a57SEric Paris 				rc = -EINVAL;
847c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
848c9180a57SEric Paris 				goto out_err;
849c9180a57SEric Paris 			}
850c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
851c9180a57SEric Paris 			if (!fscontext) {
852c9180a57SEric Paris 				rc = -ENOMEM;
853c9180a57SEric Paris 				goto out_err;
854c9180a57SEric Paris 			}
855c9180a57SEric Paris 			break;
856c9180a57SEric Paris 
857c9180a57SEric Paris 		case Opt_rootcontext:
858c9180a57SEric Paris 			if (rootcontext) {
859c9180a57SEric Paris 				rc = -EINVAL;
860c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
861c9180a57SEric Paris 				goto out_err;
862c9180a57SEric Paris 			}
863c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
864c9180a57SEric Paris 			if (!rootcontext) {
865c9180a57SEric Paris 				rc = -ENOMEM;
866c9180a57SEric Paris 				goto out_err;
867c9180a57SEric Paris 			}
868c9180a57SEric Paris 			break;
869c9180a57SEric Paris 
870c9180a57SEric Paris 		case Opt_defcontext:
871c9180a57SEric Paris 			if (context || defcontext) {
872c9180a57SEric Paris 				rc = -EINVAL;
873c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
874c9180a57SEric Paris 				goto out_err;
875c9180a57SEric Paris 			}
876c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
877c9180a57SEric Paris 			if (!defcontext) {
878c9180a57SEric Paris 				rc = -ENOMEM;
879c9180a57SEric Paris 				goto out_err;
880c9180a57SEric Paris 			}
881c9180a57SEric Paris 			break;
88211689d47SDavid P. Quigley 		case Opt_labelsupport:
88311689d47SDavid P. Quigley 			break;
884c9180a57SEric Paris 		default:
885c9180a57SEric Paris 			rc = -EINVAL;
886c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
887c9180a57SEric Paris 			goto out_err;
888c9180a57SEric Paris 
889c9180a57SEric Paris 		}
890c9180a57SEric Paris 	}
891c9180a57SEric Paris 
892e0007529SEric Paris 	rc = -ENOMEM;
893e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
894e0007529SEric Paris 	if (!opts->mnt_opts)
895e0007529SEric Paris 		goto out_err;
896e0007529SEric Paris 
897e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
898e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
899e0007529SEric Paris 		kfree(opts->mnt_opts);
900e0007529SEric Paris 		goto out_err;
901c9180a57SEric Paris 	}
902c9180a57SEric Paris 
903e0007529SEric Paris 	if (fscontext) {
904e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
905e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
906e0007529SEric Paris 	}
907e0007529SEric Paris 	if (context) {
908e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
909e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
910e0007529SEric Paris 	}
911e0007529SEric Paris 	if (rootcontext) {
912e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
913e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
914e0007529SEric Paris 	}
915e0007529SEric Paris 	if (defcontext) {
916e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
917e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
918e0007529SEric Paris 	}
919e0007529SEric Paris 
920e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
921e0007529SEric Paris 	return 0;
922e0007529SEric Paris 
923c9180a57SEric Paris out_err:
924c9180a57SEric Paris 	kfree(context);
925c9180a57SEric Paris 	kfree(defcontext);
926c9180a57SEric Paris 	kfree(fscontext);
927c9180a57SEric Paris 	kfree(rootcontext);
928c9180a57SEric Paris 	return rc;
9291da177e4SLinus Torvalds }
930e0007529SEric Paris /*
931e0007529SEric Paris  * string mount options parsing and call set the sbsec
932e0007529SEric Paris  */
933e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
934e0007529SEric Paris {
935e0007529SEric Paris 	int rc = 0;
936e0007529SEric Paris 	char *options = data;
937e0007529SEric Paris 	struct security_mnt_opts opts;
938e0007529SEric Paris 
939e0007529SEric Paris 	security_init_mnt_opts(&opts);
940e0007529SEric Paris 
941e0007529SEric Paris 	if (!data)
942e0007529SEric Paris 		goto out;
943e0007529SEric Paris 
944e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
945e0007529SEric Paris 
946e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
947e0007529SEric Paris 	if (rc)
948e0007529SEric Paris 		goto out_err;
949e0007529SEric Paris 
950e0007529SEric Paris out:
951e0007529SEric Paris 	rc = selinux_set_mnt_opts(sb, &opts);
952e0007529SEric Paris 
953e0007529SEric Paris out_err:
954e0007529SEric Paris 	security_free_mnt_opts(&opts);
955e0007529SEric Paris 	return rc;
956e0007529SEric Paris }
9571da177e4SLinus Torvalds 
9583583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
9593583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
9602069f457SEric Paris {
9612069f457SEric Paris 	int i;
9622069f457SEric Paris 	char *prefix;
9632069f457SEric Paris 
9642069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
96511689d47SDavid P. Quigley 		char *has_comma;
96611689d47SDavid P. Quigley 
96711689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
96811689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
96911689d47SDavid P. Quigley 		else
97011689d47SDavid P. Quigley 			has_comma = NULL;
9712069f457SEric Paris 
9722069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
9732069f457SEric Paris 		case CONTEXT_MNT:
9742069f457SEric Paris 			prefix = CONTEXT_STR;
9752069f457SEric Paris 			break;
9762069f457SEric Paris 		case FSCONTEXT_MNT:
9772069f457SEric Paris 			prefix = FSCONTEXT_STR;
9782069f457SEric Paris 			break;
9792069f457SEric Paris 		case ROOTCONTEXT_MNT:
9802069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
9812069f457SEric Paris 			break;
9822069f457SEric Paris 		case DEFCONTEXT_MNT:
9832069f457SEric Paris 			prefix = DEFCONTEXT_STR;
9842069f457SEric Paris 			break;
98511689d47SDavid P. Quigley 		case SE_SBLABELSUPP:
98611689d47SDavid P. Quigley 			seq_putc(m, ',');
98711689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
98811689d47SDavid P. Quigley 			continue;
9892069f457SEric Paris 		default:
9902069f457SEric Paris 			BUG();
991a35c6c83SEric Paris 			return;
9922069f457SEric Paris 		};
9932069f457SEric Paris 		/* we need a comma before each option */
9942069f457SEric Paris 		seq_putc(m, ',');
9952069f457SEric Paris 		seq_puts(m, prefix);
9962069f457SEric Paris 		if (has_comma)
9972069f457SEric Paris 			seq_putc(m, '\"');
9982069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
9992069f457SEric Paris 		if (has_comma)
10002069f457SEric Paris 			seq_putc(m, '\"');
10012069f457SEric Paris 	}
10022069f457SEric Paris }
10032069f457SEric Paris 
10042069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10052069f457SEric Paris {
10062069f457SEric Paris 	struct security_mnt_opts opts;
10072069f457SEric Paris 	int rc;
10082069f457SEric Paris 
10092069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1010383795c2SEric Paris 	if (rc) {
1011383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1012383795c2SEric Paris 		if (rc == -EINVAL)
1013383795c2SEric Paris 			rc = 0;
10142069f457SEric Paris 		return rc;
1015383795c2SEric Paris 	}
10162069f457SEric Paris 
10172069f457SEric Paris 	selinux_write_opts(m, &opts);
10182069f457SEric Paris 
10192069f457SEric Paris 	security_free_mnt_opts(&opts);
10202069f457SEric Paris 
10212069f457SEric Paris 	return rc;
10222069f457SEric Paris }
10232069f457SEric Paris 
10241da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
10271da177e4SLinus Torvalds 	case S_IFSOCK:
10281da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
10291da177e4SLinus Torvalds 	case S_IFLNK:
10301da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
10311da177e4SLinus Torvalds 	case S_IFREG:
10321da177e4SLinus Torvalds 		return SECCLASS_FILE;
10331da177e4SLinus Torvalds 	case S_IFBLK:
10341da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
10351da177e4SLinus Torvalds 	case S_IFDIR:
10361da177e4SLinus Torvalds 		return SECCLASS_DIR;
10371da177e4SLinus Torvalds 	case S_IFCHR:
10381da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
10391da177e4SLinus Torvalds 	case S_IFIFO:
10401da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	return SECCLASS_FILE;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
104713402580SJames Morris static inline int default_protocol_stream(int protocol)
104813402580SJames Morris {
104913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
105013402580SJames Morris }
105113402580SJames Morris 
105213402580SJames Morris static inline int default_protocol_dgram(int protocol)
105313402580SJames Morris {
105413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
105513402580SJames Morris }
105613402580SJames Morris 
10571da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	switch (family) {
10601da177e4SLinus Torvalds 	case PF_UNIX:
10611da177e4SLinus Torvalds 		switch (type) {
10621da177e4SLinus Torvalds 		case SOCK_STREAM:
10631da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
10641da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
10651da177e4SLinus Torvalds 		case SOCK_DGRAM:
10661da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
10671da177e4SLinus Torvalds 		}
10681da177e4SLinus Torvalds 		break;
10691da177e4SLinus Torvalds 	case PF_INET:
10701da177e4SLinus Torvalds 	case PF_INET6:
10711da177e4SLinus Torvalds 		switch (type) {
10721da177e4SLinus Torvalds 		case SOCK_STREAM:
107313402580SJames Morris 			if (default_protocol_stream(protocol))
10741da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
107513402580SJames Morris 			else
107613402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
10771da177e4SLinus Torvalds 		case SOCK_DGRAM:
107813402580SJames Morris 			if (default_protocol_dgram(protocol))
10791da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
108013402580SJames Morris 			else
108113402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
10822ee92d46SJames Morris 		case SOCK_DCCP:
10832ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
108413402580SJames Morris 		default:
10851da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 		break;
10881da177e4SLinus Torvalds 	case PF_NETLINK:
10891da177e4SLinus Torvalds 		switch (protocol) {
10901da177e4SLinus Torvalds 		case NETLINK_ROUTE:
10911da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
10921da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
10931da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
10947f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
10951da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
10961da177e4SLinus Torvalds 		case NETLINK_NFLOG:
10971da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
10981da177e4SLinus Torvalds 		case NETLINK_XFRM:
10991da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11001da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11011da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11021da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11031da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11041da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
11051da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
11061da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
11071da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
11080c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
11090c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
11101da177e4SLinus Torvalds 		default:
11111da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 	case PF_PACKET:
11141da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
11151da177e4SLinus Torvalds 	case PF_KEY:
11161da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
11173e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
11183e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11258e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
11261da177e4SLinus Torvalds 				u16 tclass,
11271da177e4SLinus Torvalds 				u32 *sid)
11281da177e4SLinus Torvalds {
11298e6c9693SLucian Adrian Grijincu 	int rc;
11308e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
11331da177e4SLinus Torvalds 	if (!buffer)
11341da177e4SLinus Torvalds 		return -ENOMEM;
11351da177e4SLinus Torvalds 
11368e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
11378e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
11388e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
11398e6c9693SLucian Adrian Grijincu 	else {
11408e6c9693SLucian Adrian Grijincu 		/* each process gets a /proc/PID/ entry. Strip off the
11418e6c9693SLucian Adrian Grijincu 		 * PID part to get a valid selinux labeling.
11428e6c9693SLucian Adrian Grijincu 		 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
11438e6c9693SLucian Adrian Grijincu 		while (path[1] >= '0' && path[1] <= '9') {
11448e6c9693SLucian Adrian Grijincu 			path[1] = '/';
11458e6c9693SLucian Adrian Grijincu 			path++;
11461da177e4SLinus Torvalds 		}
11471da177e4SLinus Torvalds 		rc = security_genfs_sid("proc", path, tclass, sid);
11488e6c9693SLucian Adrian Grijincu 	}
11491da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
11501da177e4SLinus Torvalds 	return rc;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds #else
11538e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
11541da177e4SLinus Torvalds 				u16 tclass,
11551da177e4SLinus Torvalds 				u32 *sid)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	return -EINVAL;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds #endif
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
11621da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
11651da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
11661da177e4SLinus Torvalds 	u32 sid;
11671da177e4SLinus Torvalds 	struct dentry *dentry;
11681da177e4SLinus Torvalds #define INITCONTEXTLEN 255
11691da177e4SLinus Torvalds 	char *context = NULL;
11701da177e4SLinus Torvalds 	unsigned len = 0;
11711da177e4SLinus Torvalds 	int rc = 0;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	if (isec->initialized)
11741da177e4SLinus Torvalds 		goto out;
11751da177e4SLinus Torvalds 
117623970741SEric Paris 	mutex_lock(&isec->lock);
11771da177e4SLinus Torvalds 	if (isec->initialized)
117823970741SEric Paris 		goto out_unlock;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
11810d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
11821da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
11831da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
11841da177e4SLinus Torvalds 		   server is ready to handle calls. */
11851da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
11861da177e4SLinus Torvalds 		if (list_empty(&isec->list))
11871da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
11881da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
118923970741SEric Paris 		goto out_unlock;
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	switch (sbsec->behavior) {
11931da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
11941da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
11951da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
11961da177e4SLinus Torvalds 			break;
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
12001da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
12011da177e4SLinus Torvalds 		if (opt_dentry) {
12021da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
12031da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
12041da177e4SLinus Torvalds 		} else {
12051da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
12061da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
12071da177e4SLinus Torvalds 		}
12081da177e4SLinus Torvalds 		if (!dentry) {
1209df7f54c0SEric Paris 			/*
1210df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1211df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1212df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1213df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1214df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1215df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1216df7f54c0SEric Paris 			 * be used again by userspace.
1217df7f54c0SEric Paris 			 */
121823970741SEric Paris 			goto out_unlock;
12191da177e4SLinus Torvalds 		}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
12224cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
12231da177e4SLinus Torvalds 		if (!context) {
12241da177e4SLinus Torvalds 			rc = -ENOMEM;
12251da177e4SLinus Torvalds 			dput(dentry);
122623970741SEric Paris 			goto out_unlock;
12271da177e4SLinus Torvalds 		}
12284cb912f1SEric Paris 		context[len] = '\0';
12291da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12301da177e4SLinus Torvalds 					   context, len);
12311da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1232314dabb8SJames Morris 			kfree(context);
1233314dabb8SJames Morris 
12341da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
12351da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12361da177e4SLinus Torvalds 						   NULL, 0);
12371da177e4SLinus Torvalds 			if (rc < 0) {
12381da177e4SLinus Torvalds 				dput(dentry);
123923970741SEric Paris 				goto out_unlock;
12401da177e4SLinus Torvalds 			}
12411da177e4SLinus Torvalds 			len = rc;
12424cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
12431da177e4SLinus Torvalds 			if (!context) {
12441da177e4SLinus Torvalds 				rc = -ENOMEM;
12451da177e4SLinus Torvalds 				dput(dentry);
124623970741SEric Paris 				goto out_unlock;
12471da177e4SLinus Torvalds 			}
12484cb912f1SEric Paris 			context[len] = '\0';
12491da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
12501da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
12511da177e4SLinus Torvalds 						   context, len);
12521da177e4SLinus Torvalds 		}
12531da177e4SLinus Torvalds 		dput(dentry);
12541da177e4SLinus Torvalds 		if (rc < 0) {
12551da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1256744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1257dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
12581da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
12591da177e4SLinus Torvalds 				kfree(context);
126023970741SEric Paris 				goto out_unlock;
12611da177e4SLinus Torvalds 			}
12621da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
12631da177e4SLinus Torvalds 			sid = sbsec->def_sid;
12641da177e4SLinus Torvalds 			rc = 0;
12651da177e4SLinus Torvalds 		} else {
1266f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1267869ab514SStephen Smalley 							     sbsec->def_sid,
1268869ab514SStephen Smalley 							     GFP_NOFS);
12691da177e4SLinus Torvalds 			if (rc) {
12704ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
12714ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
12724ba0a8adSEric Paris 
12734ba0a8adSEric Paris 				if (rc == -EINVAL) {
12744ba0a8adSEric Paris 					if (printk_ratelimit())
12754ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
12764ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
12774ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
12784ba0a8adSEric Paris 				} else {
1279744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
12801da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
12814ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
12824ba0a8adSEric Paris 				}
12831da177e4SLinus Torvalds 				kfree(context);
12841da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
12851da177e4SLinus Torvalds 				rc = 0;
12861da177e4SLinus Torvalds 				break;
12871da177e4SLinus Torvalds 			}
12881da177e4SLinus Torvalds 		}
12891da177e4SLinus Torvalds 		kfree(context);
12901da177e4SLinus Torvalds 		isec->sid = sid;
12911da177e4SLinus Torvalds 		break;
12921da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
12931da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
12941da177e4SLinus Torvalds 		break;
12951da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
12961da177e4SLinus Torvalds 		/* Default to the fs SID. */
12971da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
13001da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
1301652bb9b0SEric Paris 		rc = security_transition_sid(isec->task_sid, sbsec->sid,
1302652bb9b0SEric Paris 					     isec->sclass, NULL, &sid);
13031da177e4SLinus Torvalds 		if (rc)
130423970741SEric Paris 			goto out_unlock;
13051da177e4SLinus Torvalds 		isec->sid = sid;
13061da177e4SLinus Torvalds 		break;
1307c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1308c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1309c312feb2SEric Paris 		break;
13101da177e4SLinus Torvalds 	default:
1311c312feb2SEric Paris 		/* Default to the fs superblock SID. */
13121da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13131da177e4SLinus Torvalds 
13140d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
13158e6c9693SLucian Adrian Grijincu 			if (opt_dentry) {
13161da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
13178e6c9693SLucian Adrian Grijincu 				rc = selinux_proc_get_sid(opt_dentry,
13181da177e4SLinus Torvalds 							  isec->sclass,
13191da177e4SLinus Torvalds 							  &sid);
13201da177e4SLinus Torvalds 				if (rc)
132123970741SEric Paris 					goto out_unlock;
13221da177e4SLinus Torvalds 				isec->sid = sid;
13231da177e4SLinus Torvalds 			}
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 		break;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	isec->initialized = 1;
13291da177e4SLinus Torvalds 
133023970741SEric Paris out_unlock:
133123970741SEric Paris 	mutex_unlock(&isec->lock);
13321da177e4SLinus Torvalds out:
13331da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
13341da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
13351da177e4SLinus Torvalds 	return rc;
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
13391da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
13401da177e4SLinus Torvalds {
13411da177e4SLinus Torvalds 	u32 perm = 0;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	switch (sig) {
13441da177e4SLinus Torvalds 	case SIGCHLD:
13451da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
13461da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
13471da177e4SLinus Torvalds 		break;
13481da177e4SLinus Torvalds 	case SIGKILL:
13491da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
13501da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
13511da177e4SLinus Torvalds 		break;
13521da177e4SLinus Torvalds 	case SIGSTOP:
13531da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
13541da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
13551da177e4SLinus Torvalds 		break;
13561da177e4SLinus Torvalds 	default:
13571da177e4SLinus Torvalds 		/* All other signals. */
13581da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
13591da177e4SLinus Torvalds 		break;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	return perm;
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
1365275bb41eSDavid Howells /*
1366d84f4f99SDavid Howells  * Check permission between a pair of credentials
1367d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1368d84f4f99SDavid Howells  */
1369d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1370d84f4f99SDavid Howells 			 const struct cred *target,
1371d84f4f99SDavid Howells 			 u32 perms)
1372d84f4f99SDavid Howells {
1373d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1374d84f4f99SDavid Howells 
1375d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1376d84f4f99SDavid Howells }
1377d84f4f99SDavid Howells 
1378d84f4f99SDavid Howells /*
137988e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1380275bb41eSDavid Howells  * fork check, ptrace check, etc.
1381275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
13823b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1383275bb41eSDavid Howells  */
1384275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1385275bb41eSDavid Howells 			 const struct task_struct *tsk2,
13861da177e4SLinus Torvalds 			 u32 perms)
13871da177e4SLinus Torvalds {
1388275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1389275bb41eSDavid Howells 	u32 sid1, sid2;
13901da177e4SLinus Torvalds 
1391275bb41eSDavid Howells 	rcu_read_lock();
1392275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1393275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1394275bb41eSDavid Howells 	rcu_read_unlock();
1395275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13983b11a1deSDavid Howells /*
13993b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
14003b11a1deSDavid Howells  * fork check, ptrace check, etc.
14013b11a1deSDavid Howells  * current is the actor and tsk2 is the target
14023b11a1deSDavid Howells  * - this uses current's subjective creds
14033b11a1deSDavid Howells  */
14043b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
14053b11a1deSDavid Howells 			    u32 perms)
14063b11a1deSDavid Howells {
14073b11a1deSDavid Howells 	u32 sid, tsid;
14083b11a1deSDavid Howells 
14093b11a1deSDavid Howells 	sid = current_sid();
14103b11a1deSDavid Howells 	tsid = task_sid(tsk);
14113b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
14123b11a1deSDavid Howells }
14133b11a1deSDavid Howells 
1414b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1415b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1416b68e418cSStephen Smalley #endif
1417b68e418cSStephen Smalley 
14181da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
14196a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
142006112163SEric Paris 			       int cap, int audit)
14211da177e4SLinus Torvalds {
14222bf49690SThomas Liu 	struct common_audit_data ad;
14233b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
142406112163SEric Paris 	struct av_decision avd;
1425b68e418cSStephen Smalley 	u16 sclass;
14263699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1427b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
142806112163SEric Paris 	int rc;
14291da177e4SLinus Torvalds 
14302bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, CAP);
14313b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
14326a9de491SEric Paris 	ad.tsk = current;
14331da177e4SLinus Torvalds 	ad.u.cap = cap;
14341da177e4SLinus Torvalds 
1435b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1436b68e418cSStephen Smalley 	case 0:
1437b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1438b68e418cSStephen Smalley 		break;
1439b68e418cSStephen Smalley 	case 1:
1440b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1441b68e418cSStephen Smalley 		break;
1442b68e418cSStephen Smalley 	default:
1443b68e418cSStephen Smalley 		printk(KERN_ERR
1444b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1445b68e418cSStephen Smalley 		BUG();
1446a35c6c83SEric Paris 		return -EINVAL;
1447b68e418cSStephen Smalley 	}
144806112163SEric Paris 
1449275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
14509ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
14519ade0cf4SEric Paris 		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
14529ade0cf4SEric Paris 		if (rc2)
14539ade0cf4SEric Paris 			return rc2;
14549ade0cf4SEric Paris 	}
145506112163SEric Paris 	return rc;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
14591da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
14601da177e4SLinus Torvalds 			   u32 perms)
14611da177e4SLinus Torvalds {
1462275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
14631da177e4SLinus Torvalds 
1464275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
14651da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
14691da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
14701da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
147188e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
14721da177e4SLinus Torvalds 			  struct inode *inode,
14731da177e4SLinus Torvalds 			  u32 perms,
14749ade0cf4SEric Paris 			  struct common_audit_data *adp,
14759ade0cf4SEric Paris 			  unsigned flags)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1478275bb41eSDavid Howells 	u32 sid;
14791da177e4SLinus Torvalds 
1480e0e81739SDavid Howells 	validate_creds(cred);
1481e0e81739SDavid Howells 
1482bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1483bbaca6c2SStephen Smalley 		return 0;
1484bbaca6c2SStephen Smalley 
148588e67f3bSDavid Howells 	sid = cred_sid(cred);
14861da177e4SLinus Torvalds 	isec = inode->i_security;
14871da177e4SLinus Torvalds 
148895f4efb2SLinus Torvalds 	return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
149195f4efb2SLinus Torvalds static int inode_has_perm_noadp(const struct cred *cred,
149295f4efb2SLinus Torvalds 				struct inode *inode,
149395f4efb2SLinus Torvalds 				u32 perms,
149495f4efb2SLinus Torvalds 				unsigned flags)
149595f4efb2SLinus Torvalds {
149695f4efb2SLinus Torvalds 	struct common_audit_data ad;
14973b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
149895f4efb2SLinus Torvalds 
149995f4efb2SLinus Torvalds 	COMMON_AUDIT_DATA_INIT(&ad, INODE);
150095f4efb2SLinus Torvalds 	ad.u.inode = inode;
15013b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
150295f4efb2SLinus Torvalds 	return inode_has_perm(cred, inode, perms, &ad, flags);
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
15061da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
15071da177e4SLinus Torvalds    pathname if needed. */
150888e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
15091da177e4SLinus Torvalds 				  struct dentry *dentry,
15101da177e4SLinus Torvalds 				  u32 av)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15132bf49690SThomas Liu 	struct common_audit_data ad;
15143b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
151588e67f3bSDavid Howells 
15162875fa00SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
15172875fa00SEric Paris 	ad.u.dentry = dentry;
15183b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
15192875fa00SEric Paris 	return inode_has_perm(cred, inode, av, &ad, 0);
15202875fa00SEric Paris }
15212875fa00SEric Paris 
15222875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
15232875fa00SEric Paris    the path to help the auditing code to more easily generate the
15242875fa00SEric Paris    pathname if needed. */
15252875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
15262875fa00SEric Paris 				struct path *path,
15272875fa00SEric Paris 				u32 av)
15282875fa00SEric Paris {
15292875fa00SEric Paris 	struct inode *inode = path->dentry->d_inode;
15302875fa00SEric Paris 	struct common_audit_data ad;
15313b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
15322875fa00SEric Paris 
1533f48b7399SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, PATH);
15342875fa00SEric Paris 	ad.u.path = *path;
15353b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
15369ade0cf4SEric Paris 	return inode_has_perm(cred, inode, av, &ad, 0);
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
15401da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
15411da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
15421da177e4SLinus Torvalds    check a particular permission to the file.
15431da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
15441da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
15451da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
15461da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
154788e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
15481da177e4SLinus Torvalds 			 struct file *file,
15491da177e4SLinus Torvalds 			 u32 av)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
155244707fdfSJan Blunck 	struct inode *inode = file->f_path.dentry->d_inode;
15532bf49690SThomas Liu 	struct common_audit_data ad;
15543b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
155588e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
15561da177e4SLinus Torvalds 	int rc;
15571da177e4SLinus Torvalds 
1558f48b7399SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, PATH);
1559f48b7399SEric Paris 	ad.u.path = file->f_path;
15603b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
15611da177e4SLinus Torvalds 
1562275bb41eSDavid Howells 	if (sid != fsec->sid) {
1563275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
15641da177e4SLinus Torvalds 				  SECCLASS_FD,
15651da177e4SLinus Torvalds 				  FD__USE,
15661da177e4SLinus Torvalds 				  &ad);
15671da177e4SLinus Torvalds 		if (rc)
156888e67f3bSDavid Howells 			goto out;
15691da177e4SLinus Torvalds 	}
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
157288e67f3bSDavid Howells 	rc = 0;
15731da177e4SLinus Torvalds 	if (av)
15749ade0cf4SEric Paris 		rc = inode_has_perm(cred, inode, av, &ad, 0);
15751da177e4SLinus Torvalds 
157688e67f3bSDavid Howells out:
157788e67f3bSDavid Howells 	return rc;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds /* Check whether a task can create a file. */
15811da177e4SLinus Torvalds static int may_create(struct inode *dir,
15821da177e4SLinus Torvalds 		      struct dentry *dentry,
15831da177e4SLinus Torvalds 		      u16 tclass)
15841da177e4SLinus Torvalds {
15855fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
15861da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
15871da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1588275bb41eSDavid Howells 	u32 sid, newsid;
15892bf49690SThomas Liu 	struct common_audit_data ad;
15903b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
15911da177e4SLinus Torvalds 	int rc;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	dsec = dir->i_security;
15941da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
15951da177e4SLinus Torvalds 
1596275bb41eSDavid Howells 	sid = tsec->sid;
1597275bb41eSDavid Howells 	newsid = tsec->create_sid;
1598275bb41eSDavid Howells 
1599a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1600a269434dSEric Paris 	ad.u.dentry = dentry;
16013b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
16021da177e4SLinus Torvalds 
1603275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
16041da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
16051da177e4SLinus Torvalds 			  &ad);
16061da177e4SLinus Torvalds 	if (rc)
16071da177e4SLinus Torvalds 		return rc;
16081da177e4SLinus Torvalds 
1609cd89596fSDavid P. Quigley 	if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
1610cb1e922fSEric Paris 		rc = security_transition_sid(sid, dsec->sid, tclass,
1611cb1e922fSEric Paris 					     &dentry->d_name, &newsid);
16121da177e4SLinus Torvalds 		if (rc)
16131da177e4SLinus Torvalds 			return rc;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 
1616275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
16171da177e4SLinus Torvalds 	if (rc)
16181da177e4SLinus Torvalds 		return rc;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
16211da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
16221da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
16254eb582cfSMichael LeMay /* Check whether a task can create a key. */
16264eb582cfSMichael LeMay static int may_create_key(u32 ksid,
16274eb582cfSMichael LeMay 			  struct task_struct *ctx)
16284eb582cfSMichael LeMay {
1629275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
16304eb582cfSMichael LeMay 
1631275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
16324eb582cfSMichael LeMay }
16334eb582cfSMichael LeMay 
16341da177e4SLinus Torvalds #define MAY_LINK	0
16351da177e4SLinus Torvalds #define MAY_UNLINK	1
16361da177e4SLinus Torvalds #define MAY_RMDIR	2
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
16391da177e4SLinus Torvalds static int may_link(struct inode *dir,
16401da177e4SLinus Torvalds 		    struct dentry *dentry,
16411da177e4SLinus Torvalds 		    int kind)
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
16452bf49690SThomas Liu 	struct common_audit_data ad;
16463b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
1647275bb41eSDavid Howells 	u32 sid = current_sid();
16481da177e4SLinus Torvalds 	u32 av;
16491da177e4SLinus Torvalds 	int rc;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	dsec = dir->i_security;
16521da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
16531da177e4SLinus Torvalds 
1654a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1655a269434dSEric Paris 	ad.u.dentry = dentry;
16563b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	av = DIR__SEARCH;
16591da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1660275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
16611da177e4SLinus Torvalds 	if (rc)
16621da177e4SLinus Torvalds 		return rc;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	switch (kind) {
16651da177e4SLinus Torvalds 	case MAY_LINK:
16661da177e4SLinus Torvalds 		av = FILE__LINK;
16671da177e4SLinus Torvalds 		break;
16681da177e4SLinus Torvalds 	case MAY_UNLINK:
16691da177e4SLinus Torvalds 		av = FILE__UNLINK;
16701da177e4SLinus Torvalds 		break;
16711da177e4SLinus Torvalds 	case MAY_RMDIR:
16721da177e4SLinus Torvalds 		av = DIR__RMDIR;
16731da177e4SLinus Torvalds 		break;
16741da177e4SLinus Torvalds 	default:
1675744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1676744ba35eSEric Paris 			__func__, kind);
16771da177e4SLinus Torvalds 		return 0;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
1680275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
16811da177e4SLinus Torvalds 	return rc;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
16851da177e4SLinus Torvalds 			     struct dentry *old_dentry,
16861da177e4SLinus Torvalds 			     struct inode *new_dir,
16871da177e4SLinus Torvalds 			     struct dentry *new_dentry)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
16902bf49690SThomas Liu 	struct common_audit_data ad;
16913b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
1692275bb41eSDavid Howells 	u32 sid = current_sid();
16931da177e4SLinus Torvalds 	u32 av;
16941da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
16951da177e4SLinus Torvalds 	int rc;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
16981da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
16991da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
17001da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
17011da177e4SLinus Torvalds 
1702a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
17033b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
17041da177e4SLinus Torvalds 
1705a269434dSEric Paris 	ad.u.dentry = old_dentry;
1706275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
17071da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
17081da177e4SLinus Torvalds 	if (rc)
17091da177e4SLinus Torvalds 		return rc;
1710275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
17111da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
17121da177e4SLinus Torvalds 	if (rc)
17131da177e4SLinus Torvalds 		return rc;
17141da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1715275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
17161da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
17171da177e4SLinus Torvalds 		if (rc)
17181da177e4SLinus Torvalds 			return rc;
17191da177e4SLinus Torvalds 	}
17201da177e4SLinus Torvalds 
1721a269434dSEric Paris 	ad.u.dentry = new_dentry;
17221da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
17231da177e4SLinus Torvalds 	if (new_dentry->d_inode)
17241da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1725275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
17261da177e4SLinus Torvalds 	if (rc)
17271da177e4SLinus Torvalds 		return rc;
17281da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
17291da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
17301da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1731275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
17321da177e4SLinus Torvalds 				  new_isec->sclass,
17331da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
17341da177e4SLinus Torvalds 		if (rc)
17351da177e4SLinus Torvalds 			return rc;
17361da177e4SLinus Torvalds 	}
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
174288e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
17431da177e4SLinus Torvalds 			       struct super_block *sb,
17441da177e4SLinus Torvalds 			       u32 perms,
17452bf49690SThomas Liu 			       struct common_audit_data *ad)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
174888e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	sbsec = sb->s_security;
1751275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
17551da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	u32 av = 0;
17581da177e4SLinus Torvalds 
1759dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
17601da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17611da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
17621da177e4SLinus Torvalds 		if (mask & MAY_READ)
17631da177e4SLinus Torvalds 			av |= FILE__READ;
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
17661da177e4SLinus Torvalds 			av |= FILE__APPEND;
17671da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
17681da177e4SLinus Torvalds 			av |= FILE__WRITE;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	} else {
17711da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17721da177e4SLinus Torvalds 			av |= DIR__SEARCH;
17731da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
17741da177e4SLinus Torvalds 			av |= DIR__WRITE;
17751da177e4SLinus Torvalds 		if (mask & MAY_READ)
17761da177e4SLinus Torvalds 			av |= DIR__READ;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	return av;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
17831da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	u32 av = 0;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
17881da177e4SLinus Torvalds 		av |= FILE__READ;
17891da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
17901da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
17911da177e4SLinus Torvalds 			av |= FILE__APPEND;
17921da177e4SLinus Torvalds 		else
17931da177e4SLinus Torvalds 			av |= FILE__WRITE;
17941da177e4SLinus Torvalds 	}
17950794c66dSStephen Smalley 	if (!av) {
17960794c66dSStephen Smalley 		/*
17970794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
17980794c66dSStephen Smalley 		 */
17990794c66dSStephen Smalley 		av = FILE__IOCTL;
18000794c66dSStephen Smalley 	}
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	return av;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18058b6a5a37SEric Paris /*
18068b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
18078b6a5a37SEric Paris  * open permission.
18088b6a5a37SEric Paris  */
18098b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
18108b6a5a37SEric Paris {
18118b6a5a37SEric Paris 	u32 av = file_to_av(file);
18128b6a5a37SEric Paris 
181349b7b8deSEric Paris 	if (selinux_policycap_openperm)
18148b6a5a37SEric Paris 		av |= FILE__OPEN;
181549b7b8deSEric Paris 
18168b6a5a37SEric Paris 	return av;
18178b6a5a37SEric Paris }
18188b6a5a37SEric Paris 
18191da177e4SLinus Torvalds /* Hook functions begin here. */
18201da177e4SLinus Torvalds 
18219e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
1822006ebb40SStephen Smalley 				     unsigned int mode)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	int rc;
18251da177e4SLinus Torvalds 
18269e48858fSIngo Molnar 	rc = cap_ptrace_access_check(child, mode);
18271da177e4SLinus Torvalds 	if (rc)
18281da177e4SLinus Torvalds 		return rc;
18291da177e4SLinus Torvalds 
183069f594a3SEric Paris 	if (mode & PTRACE_MODE_READ) {
1831275bb41eSDavid Howells 		u32 sid = current_sid();
1832275bb41eSDavid Howells 		u32 csid = task_sid(child);
1833275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1834006ebb40SStephen Smalley 	}
1835006ebb40SStephen Smalley 
18363b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
18375cd9c58fSDavid Howells }
18385cd9c58fSDavid Howells 
18395cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
18405cd9c58fSDavid Howells {
18415cd9c58fSDavid Howells 	int rc;
18425cd9c58fSDavid Howells 
1843200ac532SEric Paris 	rc = cap_ptrace_traceme(parent);
18445cd9c58fSDavid Howells 	if (rc)
18455cd9c58fSDavid Howells 		return rc;
18465cd9c58fSDavid Howells 
18475cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
18481da177e4SLinus Torvalds }
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
18511da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	int error;
18541da177e4SLinus Torvalds 
18553b11a1deSDavid Howells 	error = current_has_perm(target, PROCESS__GETCAP);
18561da177e4SLinus Torvalds 	if (error)
18571da177e4SLinus Torvalds 		return error;
18581da177e4SLinus Torvalds 
1859200ac532SEric Paris 	return cap_capget(target, effective, inheritable, permitted);
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
1862d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
1863d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
186415a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
186515a2460eSDavid Howells 			  const kernel_cap_t *permitted)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	int error;
18681da177e4SLinus Torvalds 
1869200ac532SEric Paris 	error = cap_capset(new, old,
1870d84f4f99SDavid Howells 				      effective, inheritable, permitted);
18711da177e4SLinus Torvalds 	if (error)
18721da177e4SLinus Torvalds 		return error;
18731da177e4SLinus Torvalds 
1874d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18775626d3e8SJames Morris /*
18785626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
18795626d3e8SJames Morris  * which was removed).
18805626d3e8SJames Morris  *
18815626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
18825626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
18835626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
18845626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
18855626d3e8SJames Morris  */
18865626d3e8SJames Morris 
18876a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
18886a9de491SEric Paris 			   int cap, int audit)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int rc;
18911da177e4SLinus Torvalds 
18926a9de491SEric Paris 	rc = cap_capable(cred, ns, cap, audit);
18931da177e4SLinus Torvalds 	if (rc)
18941da177e4SLinus Torvalds 		return rc;
18951da177e4SLinus Torvalds 
18966a9de491SEric Paris 	return cred_has_capability(cred, cap, audit);
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
19001da177e4SLinus Torvalds {
190188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
19021da177e4SLinus Torvalds 	int rc = 0;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	if (!sb)
19051da177e4SLinus Torvalds 		return 0;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	switch (cmds) {
19081da177e4SLinus Torvalds 	case Q_SYNC:
19091da177e4SLinus Torvalds 	case Q_QUOTAON:
19101da177e4SLinus Torvalds 	case Q_QUOTAOFF:
19111da177e4SLinus Torvalds 	case Q_SETINFO:
19121da177e4SLinus Torvalds 	case Q_SETQUOTA:
191388e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
19141da177e4SLinus Torvalds 		break;
19151da177e4SLinus Torvalds 	case Q_GETFMT:
19161da177e4SLinus Torvalds 	case Q_GETINFO:
19171da177e4SLinus Torvalds 	case Q_GETQUOTA:
191888e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
19191da177e4SLinus Torvalds 		break;
19201da177e4SLinus Torvalds 	default:
19211da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
19221da177e4SLinus Torvalds 		break;
19231da177e4SLinus Torvalds 	}
19241da177e4SLinus Torvalds 	return rc;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
19281da177e4SLinus Torvalds {
192988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
193088e67f3bSDavid Howells 
19312875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
193412b3052cSEric Paris static int selinux_syslog(int type)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	int rc;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	switch (type) {
1939d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
1940d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
19411da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
19421da177e4SLinus Torvalds 		break;
1943d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
1944d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
1945d78ca3cdSKees Cook 	/* Set level of messages printed to console */
1946d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
19471da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
19481da177e4SLinus Torvalds 		break;
1949d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLOSE:	/* Close log */
1950d78ca3cdSKees Cook 	case SYSLOG_ACTION_OPEN:	/* Open log */
1951d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ:	/* Read from log */
1952d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_CLEAR:	/* Read/clear last kernel messages */
1953d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLEAR:	/* Clear ring buffer */
19541da177e4SLinus Torvalds 	default:
19551da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
19561da177e4SLinus Torvalds 		break;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 	return rc;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds /*
19621da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
19631da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
19641da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
19651da177e4SLinus Torvalds  *
19661da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
19671da177e4SLinus Torvalds  * processes that allocate mappings.
19681da177e4SLinus Torvalds  */
196934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
19721da177e4SLinus Torvalds 
19736a9de491SEric Paris 	rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
19743699c53cSDavid Howells 			     SECURITY_CAP_NOAUDIT);
19751da177e4SLinus Torvalds 	if (rc == 0)
19761da177e4SLinus Torvalds 		cap_sys_admin = 1;
19771da177e4SLinus Torvalds 
197834b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
19791da177e4SLinus Torvalds }
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds /* binprm security operations */
19821da177e4SLinus Torvalds 
1983a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
19841da177e4SLinus Torvalds {
1985a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
1986a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
19871da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19882bf49690SThomas Liu 	struct common_audit_data ad;
19893b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
1990a6f76f23SDavid Howells 	struct inode *inode = bprm->file->f_path.dentry->d_inode;
19911da177e4SLinus Torvalds 	int rc;
19921da177e4SLinus Torvalds 
1993200ac532SEric Paris 	rc = cap_bprm_set_creds(bprm);
19941da177e4SLinus Torvalds 	if (rc)
19951da177e4SLinus Torvalds 		return rc;
19961da177e4SLinus Torvalds 
1997a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
1998a6f76f23SDavid Howells 	 * the script interpreter */
1999a6f76f23SDavid Howells 	if (bprm->cred_prepared)
20001da177e4SLinus Torvalds 		return 0;
20011da177e4SLinus Torvalds 
2002a6f76f23SDavid Howells 	old_tsec = current_security();
2003a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
20041da177e4SLinus Torvalds 	isec = inode->i_security;
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	/* Default to the current task SID. */
2007a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2008a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
20091da177e4SLinus Torvalds 
201028eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2011a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2012a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2013a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
20141da177e4SLinus Torvalds 
2015a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2016a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
20171da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2018a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
20191da177e4SLinus Torvalds 	} else {
20201da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2021a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2022652bb9b0SEric Paris 					     SECCLASS_PROCESS, NULL,
2023652bb9b0SEric Paris 					     &new_tsec->sid);
20241da177e4SLinus Torvalds 		if (rc)
20251da177e4SLinus Torvalds 			return rc;
20261da177e4SLinus Torvalds 	}
20271da177e4SLinus Torvalds 
2028f48b7399SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, PATH);
20293b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
2030f48b7399SEric Paris 	ad.u.path = bprm->file->f_path;
20311da177e4SLinus Torvalds 
20323d5ff529SJosef Sipek 	if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
2033a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->sid;
20341da177e4SLinus Torvalds 
2035a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2036a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
20371da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
20381da177e4SLinus Torvalds 		if (rc)
20391da177e4SLinus Torvalds 			return rc;
20401da177e4SLinus Torvalds 	} else {
20411da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2042a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
20431da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
20441da177e4SLinus Torvalds 		if (rc)
20451da177e4SLinus Torvalds 			return rc;
20461da177e4SLinus Torvalds 
2047a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
20481da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
20491da177e4SLinus Torvalds 		if (rc)
20501da177e4SLinus Torvalds 			return rc;
20511da177e4SLinus Torvalds 
2052a6f76f23SDavid Howells 		/* Check for shared state */
2053a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2054a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2055a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2056a6f76f23SDavid Howells 					  NULL);
2057a6f76f23SDavid Howells 			if (rc)
2058a6f76f23SDavid Howells 				return -EPERM;
20591da177e4SLinus Torvalds 		}
20601da177e4SLinus Torvalds 
2061a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2062a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2063a6f76f23SDavid Howells 		if (bprm->unsafe &
2064a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2065a6f76f23SDavid Howells 			struct task_struct *tracer;
2066a6f76f23SDavid Howells 			struct task_security_struct *sec;
2067a6f76f23SDavid Howells 			u32 ptsid = 0;
2068a6f76f23SDavid Howells 
2069a6f76f23SDavid Howells 			rcu_read_lock();
207006d98473STejun Heo 			tracer = ptrace_parent(current);
2071a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2072a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2073a6f76f23SDavid Howells 				ptsid = sec->sid;
2074a6f76f23SDavid Howells 			}
2075a6f76f23SDavid Howells 			rcu_read_unlock();
2076a6f76f23SDavid Howells 
2077a6f76f23SDavid Howells 			if (ptsid != 0) {
2078a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2079a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2080a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2081a6f76f23SDavid Howells 				if (rc)
2082a6f76f23SDavid Howells 					return -EPERM;
2083a6f76f23SDavid Howells 			}
2084a6f76f23SDavid Howells 		}
2085a6f76f23SDavid Howells 
2086a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2087a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2088a6f76f23SDavid Howells 	}
2089a6f76f23SDavid Howells 
20901da177e4SLinus Torvalds 	return 0;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
20941da177e4SLinus Torvalds {
20955fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
2096275bb41eSDavid Howells 	u32 sid, osid;
20971da177e4SLinus Torvalds 	int atsecure = 0;
20981da177e4SLinus Torvalds 
2099275bb41eSDavid Howells 	sid = tsec->sid;
2100275bb41eSDavid Howells 	osid = tsec->osid;
2101275bb41eSDavid Howells 
2102275bb41eSDavid Howells 	if (osid != sid) {
21031da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
21041da177e4SLinus Torvalds 		   the noatsecure permission is granted between
21051da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2106275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
21071da177e4SLinus Torvalds 					SECCLASS_PROCESS,
21081da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
21091da177e4SLinus Torvalds 	}
21101da177e4SLinus Torvalds 
2111200ac532SEric Paris 	return (atsecure || cap_bprm_secureexec(bprm));
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2115745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2116745ca247SDavid Howells 					    struct files_struct *files)
21171da177e4SLinus Torvalds {
21182bf49690SThomas Liu 	struct common_audit_data ad;
21193b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
21201da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2121b20c8122SStephen Smalley 	struct tty_struct *tty;
2122badf1662SDipankar Sarma 	struct fdtable *fdt;
21231da177e4SLinus Torvalds 	long j = -1;
212424ec839cSPeter Zijlstra 	int drop_tty = 0;
21251da177e4SLinus Torvalds 
212624ec839cSPeter Zijlstra 	tty = get_current_tty();
21271da177e4SLinus Torvalds 	if (tty) {
2128ee2ffa0dSNick Piggin 		spin_lock(&tty_files_lock);
212937dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2130d996b62aSNick Piggin 			struct tty_file_private *file_priv;
213137dd0bd0SEric Paris 			struct inode *inode;
213237dd0bd0SEric Paris 
21331da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
21341da177e4SLinus Torvalds 			   Use inode_has_perm on the tty inode directly rather
21351da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
21361da177e4SLinus Torvalds 			   file may belong to another process and we are only
21371da177e4SLinus Torvalds 			   interested in the inode-based check here. */
2138d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2139d996b62aSNick Piggin 						struct tty_file_private, list);
2140d996b62aSNick Piggin 			file = file_priv->file;
214137dd0bd0SEric Paris 			inode = file->f_path.dentry->d_inode;
214295f4efb2SLinus Torvalds 			if (inode_has_perm_noadp(cred, inode,
214395f4efb2SLinus Torvalds 					   FILE__READ | FILE__WRITE, 0)) {
214424ec839cSPeter Zijlstra 				drop_tty = 1;
21451da177e4SLinus Torvalds 			}
21461da177e4SLinus Torvalds 		}
2147ee2ffa0dSNick Piggin 		spin_unlock(&tty_files_lock);
2148452a00d2SAlan Cox 		tty_kref_put(tty);
21491da177e4SLinus Torvalds 	}
215098a27ba4SEric W. Biederman 	/* Reset controlling tty. */
215198a27ba4SEric W. Biederman 	if (drop_tty)
215298a27ba4SEric W. Biederman 		no_tty();
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
21551da177e4SLinus Torvalds 
2156f48b7399SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, INODE);
21573b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	spin_lock(&files->file_lock);
21601da177e4SLinus Torvalds 	for (;;) {
21611da177e4SLinus Torvalds 		unsigned long set, i;
21621da177e4SLinus Torvalds 		int fd;
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 		j++;
21651da177e4SLinus Torvalds 		i = j * __NFDBITS;
2166badf1662SDipankar Sarma 		fdt = files_fdtable(files);
2167bbea9f69SVadim Lobanov 		if (i >= fdt->max_fds)
21681da177e4SLinus Torvalds 			break;
21691fd36adcSDavid Howells 		set = fdt->open_fds[j];
21701da177e4SLinus Torvalds 		if (!set)
21711da177e4SLinus Torvalds 			continue;
21721da177e4SLinus Torvalds 		spin_unlock(&files->file_lock);
21731da177e4SLinus Torvalds 		for ( ; set ; i++, set >>= 1) {
21741da177e4SLinus Torvalds 			if (set & 1) {
21751da177e4SLinus Torvalds 				file = fget(i);
21761da177e4SLinus Torvalds 				if (!file)
21771da177e4SLinus Torvalds 					continue;
217888e67f3bSDavid Howells 				if (file_has_perm(cred,
21791da177e4SLinus Torvalds 						  file,
21801da177e4SLinus Torvalds 						  file_to_av(file))) {
21811da177e4SLinus Torvalds 					sys_close(i);
21821da177e4SLinus Torvalds 					fd = get_unused_fd();
21831da177e4SLinus Torvalds 					if (fd != i) {
21841da177e4SLinus Torvalds 						if (fd >= 0)
21851da177e4SLinus Torvalds 							put_unused_fd(fd);
21861da177e4SLinus Torvalds 						fput(file);
21871da177e4SLinus Torvalds 						continue;
21881da177e4SLinus Torvalds 					}
21891da177e4SLinus Torvalds 					if (devnull) {
2190095975daSNick Piggin 						get_file(devnull);
21911da177e4SLinus Torvalds 					} else {
2192745ca247SDavid Howells 						devnull = dentry_open(
2193745ca247SDavid Howells 							dget(selinux_null),
2194745ca247SDavid Howells 							mntget(selinuxfs_mount),
2195745ca247SDavid Howells 							O_RDWR, cred);
2196fc5d81e6SAkinobu Mita 						if (IS_ERR(devnull)) {
2197fc5d81e6SAkinobu Mita 							devnull = NULL;
21981da177e4SLinus Torvalds 							put_unused_fd(fd);
21991da177e4SLinus Torvalds 							fput(file);
22001da177e4SLinus Torvalds 							continue;
22011da177e4SLinus Torvalds 						}
22021da177e4SLinus Torvalds 					}
22031da177e4SLinus Torvalds 					fd_install(fd, devnull);
22041da177e4SLinus Torvalds 				}
22051da177e4SLinus Torvalds 				fput(file);
22061da177e4SLinus Torvalds 			}
22071da177e4SLinus Torvalds 		}
22081da177e4SLinus Torvalds 		spin_lock(&files->file_lock);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	}
22111da177e4SLinus Torvalds 	spin_unlock(&files->file_lock);
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds /*
2215a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
22161da177e4SLinus Torvalds  */
2217a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
22181da177e4SLinus Torvalds {
2219a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
22201da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
22211da177e4SLinus Torvalds 	int rc, i;
22221da177e4SLinus Torvalds 
2223a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2224a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
22251da177e4SLinus Torvalds 		return;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2228a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
22291da177e4SLinus Torvalds 
2230a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2231a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2232a6f76f23SDavid Howells 
2233a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2234a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2235a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2236a6f76f23SDavid Howells 	 *
2237a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2238a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2239a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2240a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2241a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2242a6f76f23SDavid Howells 	 */
2243a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2244a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2245a6f76f23SDavid Howells 	if (rc) {
2246eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2247eb2d55a3SOleg Nesterov 		task_lock(current);
2248a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2249a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2250a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2251a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2252a6f76f23SDavid Howells 		}
2253eb2d55a3SOleg Nesterov 		task_unlock(current);
2254eb2d55a3SOleg Nesterov 		update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2255a6f76f23SDavid Howells 	}
2256a6f76f23SDavid Howells }
2257a6f76f23SDavid Howells 
2258a6f76f23SDavid Howells /*
2259a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2260a6f76f23SDavid Howells  * due to exec
2261a6f76f23SDavid Howells  */
2262a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2263a6f76f23SDavid Howells {
2264a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2265a6f76f23SDavid Howells 	struct itimerval itimer;
2266a6f76f23SDavid Howells 	u32 osid, sid;
2267a6f76f23SDavid Howells 	int rc, i;
2268a6f76f23SDavid Howells 
2269a6f76f23SDavid Howells 	osid = tsec->osid;
2270a6f76f23SDavid Howells 	sid = tsec->sid;
2271a6f76f23SDavid Howells 
2272a6f76f23SDavid Howells 	if (sid == osid)
2273a6f76f23SDavid Howells 		return;
2274a6f76f23SDavid Howells 
2275a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2276a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2277a6f76f23SDavid Howells 	 * flush and unblock signals.
2278a6f76f23SDavid Howells 	 *
2279a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2280a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2281a6f76f23SDavid Howells 	 */
2282a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
22831da177e4SLinus Torvalds 	if (rc) {
22841da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
22851da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
22861da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
22871da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
22883bcac026SDavid Howells 		if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
22893bcac026SDavid Howells 			__flush_signals(current);
22901da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
22911da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
22923bcac026SDavid Howells 		}
22931da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
22941da177e4SLinus Torvalds 	}
22951da177e4SLinus Torvalds 
2296a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2297a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2298ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
22990b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2300ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds /* superblock security operations */
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
23061da177e4SLinus Torvalds {
23071da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
23111da177e4SLinus Torvalds {
23121da177e4SLinus Torvalds 	superblock_free_security(sb);
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	if (plen > olen)
23181da177e4SLinus Torvalds 		return 0;
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
23241da177e4SLinus Torvalds {
2325832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2326832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2327832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
232811689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
232911689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
23331da177e4SLinus Torvalds {
23341da177e4SLinus Torvalds 	if (!*first) {
23351da177e4SLinus Torvalds 		**to = ',';
23361da177e4SLinus Torvalds 		*to += 1;
23373528a953SCory Olmo 	} else
23381da177e4SLinus Torvalds 		*first = 0;
23391da177e4SLinus Torvalds 	memcpy(*to, from, len);
23401da177e4SLinus Torvalds 	*to += len;
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
23433528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
23443528a953SCory Olmo 				       int len)
23453528a953SCory Olmo {
23463528a953SCory Olmo 	int current_size = 0;
23473528a953SCory Olmo 
23483528a953SCory Olmo 	if (!*first) {
23493528a953SCory Olmo 		**to = '|';
23503528a953SCory Olmo 		*to += 1;
2351828dfe1dSEric Paris 	} else
23523528a953SCory Olmo 		*first = 0;
23533528a953SCory Olmo 
23543528a953SCory Olmo 	while (current_size < len) {
23553528a953SCory Olmo 		if (*from != '"') {
23563528a953SCory Olmo 			**to = *from;
23573528a953SCory Olmo 			*to += 1;
23583528a953SCory Olmo 		}
23593528a953SCory Olmo 		from += 1;
23603528a953SCory Olmo 		current_size += 1;
23613528a953SCory Olmo 	}
23623528a953SCory Olmo }
23633528a953SCory Olmo 
2364e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
23671da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
23681da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
23693528a953SCory Olmo 	int open_quote = 0;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	in_curr = orig;
23721da177e4SLinus Torvalds 	sec_curr = copy;
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
23751da177e4SLinus Torvalds 	if (!nosec) {
23761da177e4SLinus Torvalds 		rc = -ENOMEM;
23771da177e4SLinus Torvalds 		goto out;
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	nosec_save = nosec;
23811da177e4SLinus Torvalds 	fnosec = fsec = 1;
23821da177e4SLinus Torvalds 	in_save = in_end = orig;
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	do {
23853528a953SCory Olmo 		if (*in_end == '"')
23863528a953SCory Olmo 			open_quote = !open_quote;
23873528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
23883528a953SCory Olmo 				*in_end == '\0') {
23891da177e4SLinus Torvalds 			int len = in_end - in_curr;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
23923528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
23931da177e4SLinus Torvalds 			else
23941da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 			in_curr = in_end + 1;
23971da177e4SLinus Torvalds 		}
23981da177e4SLinus Torvalds 	} while (*in_end++);
23991da177e4SLinus Torvalds 
24006931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2401da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
24021da177e4SLinus Torvalds out:
24031da177e4SLinus Torvalds 	return rc;
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds 
2406026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2407026eb167SEric Paris {
2408026eb167SEric Paris 	int rc, i, *flags;
2409026eb167SEric Paris 	struct security_mnt_opts opts;
2410026eb167SEric Paris 	char *secdata, **mount_options;
2411026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2412026eb167SEric Paris 
2413026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2414026eb167SEric Paris 		return 0;
2415026eb167SEric Paris 
2416026eb167SEric Paris 	if (!data)
2417026eb167SEric Paris 		return 0;
2418026eb167SEric Paris 
2419026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2420026eb167SEric Paris 		return 0;
2421026eb167SEric Paris 
2422026eb167SEric Paris 	security_init_mnt_opts(&opts);
2423026eb167SEric Paris 	secdata = alloc_secdata();
2424026eb167SEric Paris 	if (!secdata)
2425026eb167SEric Paris 		return -ENOMEM;
2426026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2427026eb167SEric Paris 	if (rc)
2428026eb167SEric Paris 		goto out_free_secdata;
2429026eb167SEric Paris 
2430026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2431026eb167SEric Paris 	if (rc)
2432026eb167SEric Paris 		goto out_free_secdata;
2433026eb167SEric Paris 
2434026eb167SEric Paris 	mount_options = opts.mnt_opts;
2435026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2436026eb167SEric Paris 
2437026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2438026eb167SEric Paris 		u32 sid;
2439026eb167SEric Paris 		size_t len;
2440026eb167SEric Paris 
2441026eb167SEric Paris 		if (flags[i] == SE_SBLABELSUPP)
2442026eb167SEric Paris 			continue;
2443026eb167SEric Paris 		len = strlen(mount_options[i]);
2444026eb167SEric Paris 		rc = security_context_to_sid(mount_options[i], len, &sid);
2445026eb167SEric Paris 		if (rc) {
2446026eb167SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
2447026eb167SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
2448026eb167SEric Paris 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2449026eb167SEric Paris 			goto out_free_opts;
2450026eb167SEric Paris 		}
2451026eb167SEric Paris 		rc = -EINVAL;
2452026eb167SEric Paris 		switch (flags[i]) {
2453026eb167SEric Paris 		case FSCONTEXT_MNT:
2454026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2455026eb167SEric Paris 				goto out_bad_option;
2456026eb167SEric Paris 			break;
2457026eb167SEric Paris 		case CONTEXT_MNT:
2458026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2459026eb167SEric Paris 				goto out_bad_option;
2460026eb167SEric Paris 			break;
2461026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2462026eb167SEric Paris 			struct inode_security_struct *root_isec;
2463026eb167SEric Paris 			root_isec = sb->s_root->d_inode->i_security;
2464026eb167SEric Paris 
2465026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2466026eb167SEric Paris 				goto out_bad_option;
2467026eb167SEric Paris 			break;
2468026eb167SEric Paris 		}
2469026eb167SEric Paris 		case DEFCONTEXT_MNT:
2470026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2471026eb167SEric Paris 				goto out_bad_option;
2472026eb167SEric Paris 			break;
2473026eb167SEric Paris 		default:
2474026eb167SEric Paris 			goto out_free_opts;
2475026eb167SEric Paris 		}
2476026eb167SEric Paris 	}
2477026eb167SEric Paris 
2478026eb167SEric Paris 	rc = 0;
2479026eb167SEric Paris out_free_opts:
2480026eb167SEric Paris 	security_free_mnt_opts(&opts);
2481026eb167SEric Paris out_free_secdata:
2482026eb167SEric Paris 	free_secdata(secdata);
2483026eb167SEric Paris 	return rc;
2484026eb167SEric Paris out_bad_option:
2485026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
2486026eb167SEric Paris 	       "during remount (dev %s, type=%s)\n", sb->s_id,
2487026eb167SEric Paris 	       sb->s_type->name);
2488026eb167SEric Paris 	goto out_free_opts;
2489026eb167SEric Paris }
2490026eb167SEric Paris 
249112204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
24921da177e4SLinus Torvalds {
249388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
24942bf49690SThomas Liu 	struct common_audit_data ad;
24953b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
24961da177e4SLinus Torvalds 	int rc;
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
24991da177e4SLinus Torvalds 	if (rc)
25001da177e4SLinus Torvalds 		return rc;
25011da177e4SLinus Torvalds 
250274192246SJames Morris 	/* Allow all mounts performed by the kernel */
250374192246SJames Morris 	if (flags & MS_KERNMOUNT)
250474192246SJames Morris 		return 0;
250574192246SJames Morris 
2506a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
25073b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
2508a269434dSEric Paris 	ad.u.dentry = sb->s_root;
250988e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
2512726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
25131da177e4SLinus Torvalds {
251488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25152bf49690SThomas Liu 	struct common_audit_data ad;
25163b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
25171da177e4SLinus Torvalds 
2518a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
25193b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
2520a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
252188e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
25221da177e4SLinus Torvalds }
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds static int selinux_mount(char *dev_name,
2525b5266eb4SAl Viro 			 struct path *path,
25261da177e4SLinus Torvalds 			 char *type,
25271da177e4SLinus Torvalds 			 unsigned long flags,
25281da177e4SLinus Torvalds 			 void *data)
25291da177e4SLinus Torvalds {
253088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2533d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
25341da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
25351da177e4SLinus Torvalds 	else
25362875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
25371da177e4SLinus Torvalds }
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
25401da177e4SLinus Torvalds {
254188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25421da177e4SLinus Torvalds 
254388e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
25441da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds /* inode security operations */
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
25501da177e4SLinus Torvalds {
25511da177e4SLinus Torvalds 	return inode_alloc_security(inode);
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
25551da177e4SLinus Torvalds {
25561da177e4SLinus Torvalds 	inode_free_security(inode);
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25595e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
25602a7dba39SEric Paris 				       const struct qstr *qstr, char **name,
25612a7dba39SEric Paris 				       void **value, size_t *len)
25625e41ff9eSStephen Smalley {
25635fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
25645e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
25655e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2566275bb41eSDavid Howells 	u32 sid, newsid, clen;
25675e41ff9eSStephen Smalley 	int rc;
2568570bc1c2SStephen Smalley 	char *namep = NULL, *context;
25695e41ff9eSStephen Smalley 
25705e41ff9eSStephen Smalley 	dsec = dir->i_security;
25715e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
25725e41ff9eSStephen Smalley 
2573275bb41eSDavid Howells 	sid = tsec->sid;
25745e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2575275bb41eSDavid Howells 
2576415103f9SEric Paris 	if ((sbsec->flags & SE_SBINITIALIZED) &&
2577415103f9SEric Paris 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2578415103f9SEric Paris 		newsid = sbsec->mntpoint_sid;
2579415103f9SEric Paris 	else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
2580275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
25815e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
2582652bb9b0SEric Paris 					     qstr, &newsid);
25835e41ff9eSStephen Smalley 		if (rc) {
25845e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
25855e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
25865e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2587dd6f953aSHarvey Harrison 			       __func__,
25885e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
25895e41ff9eSStephen Smalley 			return rc;
25905e41ff9eSStephen Smalley 		}
25915e41ff9eSStephen Smalley 	}
25925e41ff9eSStephen Smalley 
2593296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
25940d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2595296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2596296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2597296fddf7SEric Paris 		isec->sid = newsid;
2598296fddf7SEric Paris 		isec->initialized = 1;
2599296fddf7SEric Paris 	}
26005e41ff9eSStephen Smalley 
2601cd89596fSDavid P. Quigley 	if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
260225a74f3bSStephen Smalley 		return -EOPNOTSUPP;
260325a74f3bSStephen Smalley 
2604570bc1c2SStephen Smalley 	if (name) {
2605a02fe132SJosef Bacik 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
26065e41ff9eSStephen Smalley 		if (!namep)
26075e41ff9eSStephen Smalley 			return -ENOMEM;
26085e41ff9eSStephen Smalley 		*name = namep;
2609570bc1c2SStephen Smalley 	}
26105e41ff9eSStephen Smalley 
2611570bc1c2SStephen Smalley 	if (value && len) {
261212b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
26135e41ff9eSStephen Smalley 		if (rc) {
26145e41ff9eSStephen Smalley 			kfree(namep);
26155e41ff9eSStephen Smalley 			return rc;
26165e41ff9eSStephen Smalley 		}
26175e41ff9eSStephen Smalley 		*value = context;
2618570bc1c2SStephen Smalley 		*len = clen;
2619570bc1c2SStephen Smalley 	}
26205e41ff9eSStephen Smalley 
26215e41ff9eSStephen Smalley 	return 0;
26225e41ff9eSStephen Smalley }
26235e41ff9eSStephen Smalley 
26244acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
26301da177e4SLinus Torvalds {
26311da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
26351da177e4SLinus Torvalds {
26361da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
26401da177e4SLinus Torvalds {
26411da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds 
264418bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
26451da177e4SLinus Torvalds {
26461da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
26471da177e4SLinus Torvalds }
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
26501da177e4SLinus Torvalds {
26511da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
26541a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
26551da177e4SLinus Torvalds {
26561da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
26601da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
26611da177e4SLinus Torvalds {
26621da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
26631da177e4SLinus Torvalds }
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
26661da177e4SLinus Torvalds {
266788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
266888e67f3bSDavid Howells 
26692875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
26731da177e4SLinus Torvalds {
267488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26751da177e4SLinus Torvalds 
26762875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds 
2679e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
26801da177e4SLinus Torvalds {
268188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2682b782e0a6SEric Paris 	struct common_audit_data ad;
26833b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
2684b782e0a6SEric Paris 	u32 perms;
2685b782e0a6SEric Paris 	bool from_access;
2686cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
2687*2e334057SEric Paris 	struct inode_security_struct *isec;
2688*2e334057SEric Paris 	u32 sid;
2689*2e334057SEric Paris 	struct av_decision avd;
2690*2e334057SEric Paris 	int rc, rc2;
2691*2e334057SEric Paris 	u32 audited, denied;
26921da177e4SLinus Torvalds 
2693b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2694d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2695d09ca739SEric Paris 
26961da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2697b782e0a6SEric Paris 	if (!mask)
26981da177e4SLinus Torvalds 		return 0;
26991da177e4SLinus Torvalds 
2700*2e334057SEric Paris 	validate_creds(cred);
2701*2e334057SEric Paris 
2702*2e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
2703*2e334057SEric Paris 		return 0;
2704*2e334057SEric Paris 
2705*2e334057SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2706*2e334057SEric Paris 
2707*2e334057SEric Paris 	sid = cred_sid(cred);
2708*2e334057SEric Paris 	isec = inode->i_security;
2709*2e334057SEric Paris 
2710*2e334057SEric Paris 	rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2711*2e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
2712*2e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
2713*2e334057SEric Paris 				     &denied);
2714*2e334057SEric Paris 	if (likely(!audited))
2715*2e334057SEric Paris 		return rc;
2716*2e334057SEric Paris 
2717f48b7399SEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, INODE);
27183b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
2719f48b7399SEric Paris 	ad.u.inode = inode;
2720b782e0a6SEric Paris 
2721b782e0a6SEric Paris 	if (from_access)
27223b3b0e4fSEric Paris 		ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
2723b782e0a6SEric Paris 
2724*2e334057SEric Paris 	rc2 = slow_avc_audit(sid, isec->sid, isec->sclass, perms,
2725*2e334057SEric Paris 			     audited, denied, &ad, flags);
2726*2e334057SEric Paris 	if (rc2)
2727*2e334057SEric Paris 		return rc2;
2728*2e334057SEric Paris 	return rc;
27291da177e4SLinus Torvalds }
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
27321da177e4SLinus Torvalds {
273388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2734bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
273595dbf739SEric Paris 	__u32 av = FILE__WRITE;
27361da177e4SLinus Torvalds 
2737bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2738bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
2739bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2740bc6a6008SAmerigo Wang 			      ATTR_FORCE);
2741bc6a6008SAmerigo Wang 		if (!ia_valid)
27421da177e4SLinus Torvalds 			return 0;
2743bc6a6008SAmerigo Wang 	}
27441da177e4SLinus Torvalds 
2745bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2746bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
27472875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
27481da177e4SLinus Torvalds 
274995dbf739SEric Paris 	if (ia_valid & ATTR_SIZE)
275095dbf739SEric Paris 		av |= FILE__OPEN;
275195dbf739SEric Paris 
275295dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
27531da177e4SLinus Torvalds }
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
27561da177e4SLinus Torvalds {
275788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27582875fa00SEric Paris 	struct path path;
275988e67f3bSDavid Howells 
27602875fa00SEric Paris 	path.dentry = dentry;
27612875fa00SEric Paris 	path.mnt = mnt;
27622875fa00SEric Paris 
27632875fa00SEric Paris 	return path_has_perm(cred, &path, FILE__GETATTR);
27641da177e4SLinus Torvalds }
27651da177e4SLinus Torvalds 
27668f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2767b5376771SSerge E. Hallyn {
276888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
276988e67f3bSDavid Howells 
2770b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2771b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2772b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2773b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2774b5376771SSerge E. Hallyn 				return -EPERM;
2775b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2776b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2777b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2778b5376771SSerge E. Hallyn 			return -EPERM;
2779b5376771SSerge E. Hallyn 		}
2780b5376771SSerge E. Hallyn 	}
2781b5376771SSerge E. Hallyn 
2782b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2783b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
27842875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__SETATTR);
2785b5376771SSerge E. Hallyn }
2786b5376771SSerge E. Hallyn 
27878f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
27888f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
27891da177e4SLinus Torvalds {
27901da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
27911da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27921da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
27932bf49690SThomas Liu 	struct common_audit_data ad;
27943b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
2795275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
27961da177e4SLinus Torvalds 	int rc = 0;
27971da177e4SLinus Torvalds 
2798b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2799b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
2802cd89596fSDavid P. Quigley 	if (!(sbsec->flags & SE_SBLABELSUPP))
28031da177e4SLinus Torvalds 		return -EOPNOTSUPP;
28041da177e4SLinus Torvalds 
28052e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
28061da177e4SLinus Torvalds 		return -EPERM;
28071da177e4SLinus Torvalds 
2808a269434dSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
28093b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
2810a269434dSEric Paris 	ad.u.dentry = dentry;
28111da177e4SLinus Torvalds 
2812275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
28131da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
28141da177e4SLinus Torvalds 	if (rc)
28151da177e4SLinus Torvalds 		return rc;
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
281812b29f34SStephen Smalley 	if (rc == -EINVAL) {
2819d6ea83ecSEric Paris 		if (!capable(CAP_MAC_ADMIN)) {
2820d6ea83ecSEric Paris 			struct audit_buffer *ab;
2821d6ea83ecSEric Paris 			size_t audit_size;
2822d6ea83ecSEric Paris 			const char *str;
2823d6ea83ecSEric Paris 
2824d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
2825d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
2826d6ea83ecSEric Paris 			str = value;
2827d6ea83ecSEric Paris 			if (str[size - 1] == '\0')
2828d6ea83ecSEric Paris 				audit_size = size - 1;
2829d6ea83ecSEric Paris 			else
2830d6ea83ecSEric Paris 				audit_size = size;
2831d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2832d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
2833d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
2834d6ea83ecSEric Paris 			audit_log_end(ab);
2835d6ea83ecSEric Paris 
283612b29f34SStephen Smalley 			return rc;
2837d6ea83ecSEric Paris 		}
283812b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
283912b29f34SStephen Smalley 	}
28401da177e4SLinus Torvalds 	if (rc)
28411da177e4SLinus Torvalds 		return rc;
28421da177e4SLinus Torvalds 
2843275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
28441da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
28451da177e4SLinus Torvalds 	if (rc)
28461da177e4SLinus Torvalds 		return rc;
28471da177e4SLinus Torvalds 
2848275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
28491da177e4SLinus Torvalds 					  isec->sclass);
28501da177e4SLinus Torvalds 	if (rc)
28511da177e4SLinus Torvalds 		return rc;
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	return avc_has_perm(newsid,
28541da177e4SLinus Torvalds 			    sbsec->sid,
28551da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
28561da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
28571da177e4SLinus Torvalds 			    &ad);
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28608f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
28618f0cfa52SDavid Howells 					const void *value, size_t size,
28628f0cfa52SDavid Howells 					int flags)
28631da177e4SLinus Torvalds {
28641da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28651da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28661da177e4SLinus Torvalds 	u32 newsid;
28671da177e4SLinus Torvalds 	int rc;
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
28701da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
28711da177e4SLinus Torvalds 		return;
28721da177e4SLinus Torvalds 	}
28731da177e4SLinus Torvalds 
287412b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
28751da177e4SLinus Torvalds 	if (rc) {
287612b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
287712b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
287812b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
28791da177e4SLinus Torvalds 		return;
28801da177e4SLinus Torvalds 	}
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	isec->sid = newsid;
28831da177e4SLinus Torvalds 	return;
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
28868f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
28871da177e4SLinus Torvalds {
288888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
288988e67f3bSDavid Howells 
28902875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
28911da177e4SLinus Torvalds }
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
28941da177e4SLinus Torvalds {
289588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
289688e67f3bSDavid Howells 
28972875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
28981da177e4SLinus Torvalds }
28991da177e4SLinus Torvalds 
29008f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
29011da177e4SLinus Torvalds {
2902b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2903b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
29061da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
29071da177e4SLinus Torvalds 	return -EACCES;
29081da177e4SLinus Torvalds }
29091da177e4SLinus Torvalds 
2910d381d8a9SJames Morris /*
2911abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
2912d381d8a9SJames Morris  *
2913d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2914d381d8a9SJames Morris  */
291542492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
29161da177e4SLinus Torvalds {
291742492594SDavid P. Quigley 	u32 size;
291842492594SDavid P. Quigley 	int error;
291942492594SDavid P. Quigley 	char *context = NULL;
29201da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29211da177e4SLinus Torvalds 
29228c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29238c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
29241da177e4SLinus Torvalds 
2925abc69bb6SStephen Smalley 	/*
2926abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
2927abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
2928abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
2929abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
2930abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
2931abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
2932abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
2933abc69bb6SStephen Smalley 	 */
29346a9de491SEric Paris 	error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
29353699c53cSDavid Howells 				SECURITY_CAP_NOAUDIT);
2936abc69bb6SStephen Smalley 	if (!error)
2937abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
2938abc69bb6SStephen Smalley 						      &size);
2939abc69bb6SStephen Smalley 	else
294042492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
294142492594SDavid P. Quigley 	if (error)
294242492594SDavid P. Quigley 		return error;
294342492594SDavid P. Quigley 	error = size;
294442492594SDavid P. Quigley 	if (alloc) {
294542492594SDavid P. Quigley 		*buffer = context;
294642492594SDavid P. Quigley 		goto out_nofree;
294742492594SDavid P. Quigley 	}
294842492594SDavid P. Quigley 	kfree(context);
294942492594SDavid P. Quigley out_nofree:
295042492594SDavid P. Quigley 	return error;
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
29541da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
29551da177e4SLinus Torvalds {
29561da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29571da177e4SLinus Torvalds 	u32 newsid;
29581da177e4SLinus Torvalds 	int rc;
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29611da177e4SLinus Torvalds 		return -EOPNOTSUPP;
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	if (!value || !size)
29641da177e4SLinus Torvalds 		return -EACCES;
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 	rc = security_context_to_sid((void *)value, size, &newsid);
29671da177e4SLinus Torvalds 	if (rc)
29681da177e4SLinus Torvalds 		return rc;
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	isec->sid = newsid;
2971ddd29ec6SDavid P. Quigley 	isec->initialized = 1;
29721da177e4SLinus Torvalds 	return 0;
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
29761da177e4SLinus Torvalds {
29771da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
29781da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
29791da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
29801da177e4SLinus Torvalds 	return len;
29811da177e4SLinus Torvalds }
29821da177e4SLinus Torvalds 
2983713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2984713a04aeSAhmed S. Darwish {
2985713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
2986713a04aeSAhmed S. Darwish 	*secid = isec->sid;
2987713a04aeSAhmed S. Darwish }
2988713a04aeSAhmed S. Darwish 
29891da177e4SLinus Torvalds /* file security operations */
29901da177e4SLinus Torvalds 
2991788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
29921da177e4SLinus Torvalds {
299388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29943d5ff529SJosef Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
29971da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
29981da177e4SLinus Torvalds 		mask |= MAY_APPEND;
29991da177e4SLinus Torvalds 
3000389fb800SPaul Moore 	return file_has_perm(cred, file,
30011da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
30021da177e4SLinus Torvalds }
30031da177e4SLinus Torvalds 
3004788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3005788e7dd4SYuichi Nakamura {
300620dda18bSStephen Smalley 	struct inode *inode = file->f_path.dentry->d_inode;
300720dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
300820dda18bSStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
300920dda18bSStephen Smalley 	u32 sid = current_sid();
301020dda18bSStephen Smalley 
3011389fb800SPaul Moore 	if (!mask)
3012788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3013788e7dd4SYuichi Nakamura 		return 0;
3014788e7dd4SYuichi Nakamura 
301520dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
301620dda18bSStephen Smalley 	    fsec->pseqno == avc_policy_seqno())
301783d49856SEric Paris 		/* No change since file_open check. */
301820dda18bSStephen Smalley 		return 0;
301920dda18bSStephen Smalley 
3020788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3021788e7dd4SYuichi Nakamura }
3022788e7dd4SYuichi Nakamura 
30231da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
30241da177e4SLinus Torvalds {
30251da177e4SLinus Torvalds 	return file_alloc_security(file);
30261da177e4SLinus Torvalds }
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
30291da177e4SLinus Torvalds {
30301da177e4SLinus Torvalds 	file_free_security(file);
30311da177e4SLinus Torvalds }
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
30341da177e4SLinus Torvalds 			      unsigned long arg)
30351da177e4SLinus Torvalds {
303688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
30370b24dcb7SEric Paris 	int error = 0;
30381da177e4SLinus Torvalds 
30390b24dcb7SEric Paris 	switch (cmd) {
30400b24dcb7SEric Paris 	case FIONREAD:
30410b24dcb7SEric Paris 	/* fall through */
30420b24dcb7SEric Paris 	case FIBMAP:
30430b24dcb7SEric Paris 	/* fall through */
30440b24dcb7SEric Paris 	case FIGETBSZ:
30450b24dcb7SEric Paris 	/* fall through */
30462f99c369SAl Viro 	case FS_IOC_GETFLAGS:
30470b24dcb7SEric Paris 	/* fall through */
30482f99c369SAl Viro 	case FS_IOC_GETVERSION:
30490b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
30500b24dcb7SEric Paris 		break;
30511da177e4SLinus Torvalds 
30522f99c369SAl Viro 	case FS_IOC_SETFLAGS:
30530b24dcb7SEric Paris 	/* fall through */
30542f99c369SAl Viro 	case FS_IOC_SETVERSION:
30550b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
30560b24dcb7SEric Paris 		break;
30570b24dcb7SEric Paris 
30580b24dcb7SEric Paris 	/* sys_ioctl() checks */
30590b24dcb7SEric Paris 	case FIONBIO:
30600b24dcb7SEric Paris 	/* fall through */
30610b24dcb7SEric Paris 	case FIOASYNC:
30620b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
30630b24dcb7SEric Paris 		break;
30640b24dcb7SEric Paris 
30650b24dcb7SEric Paris 	case KDSKBENT:
30660b24dcb7SEric Paris 	case KDSKBSENT:
30676a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
30680b24dcb7SEric Paris 					    SECURITY_CAP_AUDIT);
30690b24dcb7SEric Paris 		break;
30700b24dcb7SEric Paris 
30710b24dcb7SEric Paris 	/* default case assumes that the command will go
30720b24dcb7SEric Paris 	 * to the file's ioctl() function.
30730b24dcb7SEric Paris 	 */
30740b24dcb7SEric Paris 	default:
30750b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__IOCTL);
30760b24dcb7SEric Paris 	}
30770b24dcb7SEric Paris 	return error;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
3080fcaaade1SStephen Smalley static int default_noexec;
3081fcaaade1SStephen Smalley 
30821da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
30831da177e4SLinus Torvalds {
308488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3085d84f4f99SDavid Howells 	int rc = 0;
308688e67f3bSDavid Howells 
3087fcaaade1SStephen Smalley 	if (default_noexec &&
3088fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
30891da177e4SLinus Torvalds 		/*
30901da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
30911da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
30921da177e4SLinus Torvalds 		 * This has an additional check.
30931da177e4SLinus Torvalds 		 */
3094d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
30951da177e4SLinus Torvalds 		if (rc)
3096d84f4f99SDavid Howells 			goto error;
30971da177e4SLinus Torvalds 	}
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds 	if (file) {
31001da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
31011da177e4SLinus Torvalds 		u32 av = FILE__READ;
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
31041da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
31051da177e4SLinus Torvalds 			av |= FILE__WRITE;
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
31081da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
31091da177e4SLinus Torvalds 
311088e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
31111da177e4SLinus Torvalds 	}
3112d84f4f99SDavid Howells 
3113d84f4f99SDavid Howells error:
3114d84f4f99SDavid Howells 	return rc;
31151da177e4SLinus Torvalds }
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot,
3118ed032189SEric Paris 			     unsigned long prot, unsigned long flags,
3119ed032189SEric Paris 			     unsigned long addr, unsigned long addr_only)
31201da177e4SLinus Torvalds {
3121ed032189SEric Paris 	int rc = 0;
3122275bb41eSDavid Howells 	u32 sid = current_sid();
31231da177e4SLinus Torvalds 
312484336d1aSEric Paris 	/*
312584336d1aSEric Paris 	 * notice that we are intentionally putting the SELinux check before
312684336d1aSEric Paris 	 * the secondary cap_file_mmap check.  This is such a likely attempt
312784336d1aSEric Paris 	 * at bad behaviour/exploit that we always want to get the AVC, even
312884336d1aSEric Paris 	 * if DAC would have also denied the operation.
312984336d1aSEric Paris 	 */
3130a2551df7SEric Paris 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3131ed032189SEric Paris 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3132ed032189SEric Paris 				  MEMPROTECT__MMAP_ZERO, NULL);
313384336d1aSEric Paris 		if (rc)
313484336d1aSEric Paris 			return rc;
313584336d1aSEric Paris 	}
313684336d1aSEric Paris 
313784336d1aSEric Paris 	/* do DAC check on address space usage */
313884336d1aSEric Paris 	rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
3139ed032189SEric Paris 	if (rc || addr_only)
31401da177e4SLinus Torvalds 		return rc;
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds 	if (selinux_checkreqprot)
31431da177e4SLinus Torvalds 		prot = reqprot;
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
31461da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
31471da177e4SLinus Torvalds }
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
31501da177e4SLinus Torvalds 				 unsigned long reqprot,
31511da177e4SLinus Torvalds 				 unsigned long prot)
31521da177e4SLinus Torvalds {
315388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds 	if (selinux_checkreqprot)
31561da177e4SLinus Torvalds 		prot = reqprot;
31571da177e4SLinus Torvalds 
3158fcaaade1SStephen Smalley 	if (default_noexec &&
3159fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3160d541bbeeSJames Morris 		int rc = 0;
3161db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3162db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3163d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3164db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
31656b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
31666b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
31673b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3168db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3169db4c9641SStephen Smalley 			/*
3170db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3171db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3172db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3173db4c9641SStephen Smalley 			 * modified content.  This typically should only
3174db4c9641SStephen Smalley 			 * occur for text relocations.
3175db4c9641SStephen Smalley 			 */
3176d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3177db4c9641SStephen Smalley 		}
31786b992197SLorenzo Hernandez García-Hierro 		if (rc)
31796b992197SLorenzo Hernandez García-Hierro 			return rc;
31806b992197SLorenzo Hernandez García-Hierro 	}
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
31831da177e4SLinus Torvalds }
31841da177e4SLinus Torvalds 
31851da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
31861da177e4SLinus Torvalds {
318788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
318888e67f3bSDavid Howells 
318988e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
31921da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
31931da177e4SLinus Torvalds 			      unsigned long arg)
31941da177e4SLinus Torvalds {
319588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31961da177e4SLinus Torvalds 	int err = 0;
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 	switch (cmd) {
31991da177e4SLinus Torvalds 	case F_SETFL:
32003d5ff529SJosef Sipek 		if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
32011da177e4SLinus Torvalds 			err = -EINVAL;
32021da177e4SLinus Torvalds 			break;
32031da177e4SLinus Torvalds 		}
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
320688e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
32071da177e4SLinus Torvalds 			break;
32081da177e4SLinus Torvalds 		}
32091da177e4SLinus Torvalds 		/* fall through */
32101da177e4SLinus Torvalds 	case F_SETOWN:
32111da177e4SLinus Torvalds 	case F_SETSIG:
32121da177e4SLinus Torvalds 	case F_GETFL:
32131da177e4SLinus Torvalds 	case F_GETOWN:
32141da177e4SLinus Torvalds 	case F_GETSIG:
32151da177e4SLinus Torvalds 		/* Just check FD__USE permission */
321688e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
32171da177e4SLinus Torvalds 		break;
32181da177e4SLinus Torvalds 	case F_GETLK:
32191da177e4SLinus Torvalds 	case F_SETLK:
32201da177e4SLinus Torvalds 	case F_SETLKW:
32211da177e4SLinus Torvalds #if BITS_PER_LONG == 32
32221da177e4SLinus Torvalds 	case F_GETLK64:
32231da177e4SLinus Torvalds 	case F_SETLK64:
32241da177e4SLinus Torvalds 	case F_SETLKW64:
32251da177e4SLinus Torvalds #endif
32263d5ff529SJosef Sipek 		if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
32271da177e4SLinus Torvalds 			err = -EINVAL;
32281da177e4SLinus Torvalds 			break;
32291da177e4SLinus Torvalds 		}
323088e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
32311da177e4SLinus Torvalds 		break;
32321da177e4SLinus Torvalds 	}
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	return err;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
32381da177e4SLinus Torvalds {
32391da177e4SLinus Torvalds 	struct file_security_struct *fsec;
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds 	fsec = file->f_security;
3242275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	return 0;
32451da177e4SLinus Torvalds }
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
32481da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
32491da177e4SLinus Torvalds {
32501da177e4SLinus Torvalds 	struct file *file;
325165c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
32521da177e4SLinus Torvalds 	u32 perm;
32531da177e4SLinus Torvalds 	struct file_security_struct *fsec;
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3256b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds 	fsec = file->f_security;
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 	if (!signum)
32611da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
32621da177e4SLinus Torvalds 	else
32631da177e4SLinus Torvalds 		perm = signal_to_av(signum);
32641da177e4SLinus Torvalds 
3265275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
32661da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
32701da177e4SLinus Torvalds {
327188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
327288e67f3bSDavid Howells 
327388e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
32741da177e4SLinus Torvalds }
32751da177e4SLinus Torvalds 
327683d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3277788e7dd4SYuichi Nakamura {
3278788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3279788e7dd4SYuichi Nakamura 	struct inode *inode;
3280788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3281d84f4f99SDavid Howells 
3282788e7dd4SYuichi Nakamura 	inode = file->f_path.dentry->d_inode;
3283788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3284788e7dd4SYuichi Nakamura 	isec = inode->i_security;
3285788e7dd4SYuichi Nakamura 	/*
3286788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3287788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3288788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3289788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3290788e7dd4SYuichi Nakamura 	 * struct as its SID.
3291788e7dd4SYuichi Nakamura 	 */
3292788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3293788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3294788e7dd4SYuichi Nakamura 	/*
3295788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3296788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3297788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3298788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3299788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3300788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3301788e7dd4SYuichi Nakamura 	 */
330295f4efb2SLinus Torvalds 	return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
3303788e7dd4SYuichi Nakamura }
3304788e7dd4SYuichi Nakamura 
33051da177e4SLinus Torvalds /* task security operations */
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
33081da177e4SLinus Torvalds {
33093b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
33101da177e4SLinus Torvalds }
33111da177e4SLinus Torvalds 
3312f1752eecSDavid Howells /*
3313ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3314ee18d64cSDavid Howells  */
3315ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3316ee18d64cSDavid Howells {
3317ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3318ee18d64cSDavid Howells 
3319ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3320ee18d64cSDavid Howells 	if (!tsec)
3321ee18d64cSDavid Howells 		return -ENOMEM;
3322ee18d64cSDavid Howells 
3323ee18d64cSDavid Howells 	cred->security = tsec;
3324ee18d64cSDavid Howells 	return 0;
3325ee18d64cSDavid Howells }
3326ee18d64cSDavid Howells 
3327ee18d64cSDavid Howells /*
3328f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3329f1752eecSDavid Howells  */
3330f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
33311da177e4SLinus Torvalds {
3332f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3333e0e81739SDavid Howells 
33342edeaa34STetsuo Handa 	/*
33352edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
33362edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
33372edeaa34STetsuo Handa 	 */
33382edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3339e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3340f1752eecSDavid Howells 	kfree(tsec);
33411da177e4SLinus Torvalds }
33421da177e4SLinus Torvalds 
3343d84f4f99SDavid Howells /*
3344d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3345d84f4f99SDavid Howells  */
3346d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3347d84f4f99SDavid Howells 				gfp_t gfp)
3348d84f4f99SDavid Howells {
3349d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3350d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3351d84f4f99SDavid Howells 
3352d84f4f99SDavid Howells 	old_tsec = old->security;
3353d84f4f99SDavid Howells 
3354d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3355d84f4f99SDavid Howells 	if (!tsec)
3356d84f4f99SDavid Howells 		return -ENOMEM;
3357d84f4f99SDavid Howells 
3358d84f4f99SDavid Howells 	new->security = tsec;
3359d84f4f99SDavid Howells 	return 0;
3360d84f4f99SDavid Howells }
3361d84f4f99SDavid Howells 
3362d84f4f99SDavid Howells /*
3363ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3364ee18d64cSDavid Howells  */
3365ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3366ee18d64cSDavid Howells {
3367ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3368ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3369ee18d64cSDavid Howells 
3370ee18d64cSDavid Howells 	*tsec = *old_tsec;
3371ee18d64cSDavid Howells }
3372ee18d64cSDavid Howells 
3373ee18d64cSDavid Howells /*
33743a3b7ce9SDavid Howells  * set the security data for a kernel service
33753a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
33763a3b7ce9SDavid Howells  */
33773a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
33783a3b7ce9SDavid Howells {
33793a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
33803a3b7ce9SDavid Howells 	u32 sid = current_sid();
33813a3b7ce9SDavid Howells 	int ret;
33823a3b7ce9SDavid Howells 
33833a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
33843a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
33853a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
33863a3b7ce9SDavid Howells 			   NULL);
33873a3b7ce9SDavid Howells 	if (ret == 0) {
33883a3b7ce9SDavid Howells 		tsec->sid = secid;
33893a3b7ce9SDavid Howells 		tsec->create_sid = 0;
33903a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
33913a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
33923a3b7ce9SDavid Howells 	}
33933a3b7ce9SDavid Howells 	return ret;
33943a3b7ce9SDavid Howells }
33953a3b7ce9SDavid Howells 
33963a3b7ce9SDavid Howells /*
33973a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
33983a3b7ce9SDavid Howells  * objective context of the specified inode
33993a3b7ce9SDavid Howells  */
34003a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
34013a3b7ce9SDavid Howells {
34023a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
34033a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
34043a3b7ce9SDavid Howells 	u32 sid = current_sid();
34053a3b7ce9SDavid Howells 	int ret;
34063a3b7ce9SDavid Howells 
34073a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
34083a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
34093a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
34103a3b7ce9SDavid Howells 			   NULL);
34113a3b7ce9SDavid Howells 
34123a3b7ce9SDavid Howells 	if (ret == 0)
34133a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3414ef57471aSDavid Howells 	return ret;
34153a3b7ce9SDavid Howells }
34163a3b7ce9SDavid Howells 
3417dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
341825354c4fSEric Paris {
3419dd8dbf2eSEric Paris 	u32 sid;
3420dd8dbf2eSEric Paris 	struct common_audit_data ad;
34213b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
3422dd8dbf2eSEric Paris 
3423dd8dbf2eSEric Paris 	sid = task_sid(current);
3424dd8dbf2eSEric Paris 
3425dd8dbf2eSEric Paris 	COMMON_AUDIT_DATA_INIT(&ad, KMOD);
34263b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
3427dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3428dd8dbf2eSEric Paris 
3429dd8dbf2eSEric Paris 	return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3430dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
343125354c4fSEric Paris }
343225354c4fSEric Paris 
34331da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
34341da177e4SLinus Torvalds {
34353b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
34361da177e4SLinus Torvalds }
34371da177e4SLinus Torvalds 
34381da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
34391da177e4SLinus Torvalds {
34403b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
34411da177e4SLinus Torvalds }
34421da177e4SLinus Torvalds 
34431da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
34441da177e4SLinus Torvalds {
34453b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
34461da177e4SLinus Torvalds }
34471da177e4SLinus Torvalds 
3448f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3449f9008e4cSDavid Quigley {
3450275bb41eSDavid Howells 	*secid = task_sid(p);
3451f9008e4cSDavid Quigley }
3452f9008e4cSDavid Quigley 
34531da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
34541da177e4SLinus Torvalds {
34551da177e4SLinus Torvalds 	int rc;
34561da177e4SLinus Torvalds 
3457200ac532SEric Paris 	rc = cap_task_setnice(p, nice);
34581da177e4SLinus Torvalds 	if (rc)
34591da177e4SLinus Torvalds 		return rc;
34601da177e4SLinus Torvalds 
34613b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
34621da177e4SLinus Torvalds }
34631da177e4SLinus Torvalds 
346403e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
346503e68060SJames Morris {
3466b5376771SSerge E. Hallyn 	int rc;
3467b5376771SSerge E. Hallyn 
3468200ac532SEric Paris 	rc = cap_task_setioprio(p, ioprio);
3469b5376771SSerge E. Hallyn 	if (rc)
3470b5376771SSerge E. Hallyn 		return rc;
3471b5376771SSerge E. Hallyn 
34723b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
347303e68060SJames Morris }
347403e68060SJames Morris 
3475a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3476a1836a42SDavid Quigley {
34773b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3478a1836a42SDavid Quigley }
3479a1836a42SDavid Quigley 
34808fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
34818fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
34821da177e4SLinus Torvalds {
34838fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
34861da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
34871da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3488d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
34891da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
34908fd00b4dSJiri Slaby 		return current_has_perm(p, PROCESS__SETRLIMIT);
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds 	return 0;
34931da177e4SLinus Torvalds }
34941da177e4SLinus Torvalds 
3495b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
34961da177e4SLinus Torvalds {
3497b5376771SSerge E. Hallyn 	int rc;
3498b5376771SSerge E. Hallyn 
3499b0ae1981SKOSAKI Motohiro 	rc = cap_task_setscheduler(p);
3500b5376771SSerge E. Hallyn 	if (rc)
3501b5376771SSerge E. Hallyn 		return rc;
3502b5376771SSerge E. Hallyn 
35033b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
35041da177e4SLinus Torvalds }
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
35071da177e4SLinus Torvalds {
35083b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
35091da177e4SLinus Torvalds }
35101da177e4SLinus Torvalds 
351135601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
351235601547SDavid Quigley {
35133b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
351435601547SDavid Quigley }
351535601547SDavid Quigley 
3516f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3517f9008e4cSDavid Quigley 				int sig, u32 secid)
35181da177e4SLinus Torvalds {
35191da177e4SLinus Torvalds 	u32 perm;
35201da177e4SLinus Torvalds 	int rc;
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 	if (!sig)
35231da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
35241da177e4SLinus Torvalds 	else
35251da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3526f9008e4cSDavid Quigley 	if (secid)
3527275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3528275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3529f9008e4cSDavid Quigley 	else
35303b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3531f9008e4cSDavid Quigley 	return rc;
35321da177e4SLinus Torvalds }
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
35351da177e4SLinus Torvalds {
35368a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
35371da177e4SLinus Torvalds }
35381da177e4SLinus Torvalds 
35391da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
35401da177e4SLinus Torvalds 				  struct inode *inode)
35411da177e4SLinus Torvalds {
35421da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3543275bb41eSDavid Howells 	u32 sid = task_sid(p);
35441da177e4SLinus Torvalds 
3545275bb41eSDavid Howells 	isec->sid = sid;
35461da177e4SLinus Torvalds 	isec->initialized = 1;
35471da177e4SLinus Torvalds }
35481da177e4SLinus Torvalds 
35491da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
355067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
35512bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
35521da177e4SLinus Torvalds {
35531da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
35541da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
35551da177e4SLinus Torvalds 
3556bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
35571da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
35581da177e4SLinus Torvalds 	if (ih == NULL)
35591da177e4SLinus Torvalds 		goto out;
35601da177e4SLinus Torvalds 
35611da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
35621da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
35631da177e4SLinus Torvalds 		goto out;
35641da177e4SLinus Torvalds 
356548c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
356648c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
35671da177e4SLinus Torvalds 	ret = 0;
35681da177e4SLinus Torvalds 
356967f83cbfSVenkat Yekkirala 	if (proto)
357067f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
357167f83cbfSVenkat Yekkirala 
35721da177e4SLinus Torvalds 	switch (ih->protocol) {
35731da177e4SLinus Torvalds 	case IPPROTO_TCP: {
35741da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
35751da177e4SLinus Torvalds 
35761da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
35771da177e4SLinus Torvalds 			break;
35781da177e4SLinus Torvalds 
35791da177e4SLinus Torvalds 		offset += ihlen;
35801da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
35811da177e4SLinus Torvalds 		if (th == NULL)
35821da177e4SLinus Torvalds 			break;
35831da177e4SLinus Torvalds 
358448c62af6SEric Paris 		ad->u.net->sport = th->source;
358548c62af6SEric Paris 		ad->u.net->dport = th->dest;
35861da177e4SLinus Torvalds 		break;
35871da177e4SLinus Torvalds 	}
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds 	case IPPROTO_UDP: {
35901da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
35931da177e4SLinus Torvalds 			break;
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 		offset += ihlen;
35961da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
35971da177e4SLinus Torvalds 		if (uh == NULL)
35981da177e4SLinus Torvalds 			break;
35991da177e4SLinus Torvalds 
360048c62af6SEric Paris 		ad->u.net->sport = uh->source;
360148c62af6SEric Paris 		ad->u.net->dport = uh->dest;
36021da177e4SLinus Torvalds 		break;
36031da177e4SLinus Torvalds 	}
36041da177e4SLinus Torvalds 
36052ee92d46SJames Morris 	case IPPROTO_DCCP: {
36062ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
36072ee92d46SJames Morris 
36082ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
36092ee92d46SJames Morris 			break;
36102ee92d46SJames Morris 
36112ee92d46SJames Morris 		offset += ihlen;
36122ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
36132ee92d46SJames Morris 		if (dh == NULL)
36142ee92d46SJames Morris 			break;
36152ee92d46SJames Morris 
361648c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
361748c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
36182ee92d46SJames Morris 		break;
36192ee92d46SJames Morris 	}
36202ee92d46SJames Morris 
36211da177e4SLinus Torvalds 	default:
36221da177e4SLinus Torvalds 		break;
36231da177e4SLinus Torvalds 	}
36241da177e4SLinus Torvalds out:
36251da177e4SLinus Torvalds 	return ret;
36261da177e4SLinus Torvalds }
36271da177e4SLinus Torvalds 
36281da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
363167f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
36322bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
36331da177e4SLinus Torvalds {
36341da177e4SLinus Torvalds 	u8 nexthdr;
36351da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
36361da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
363775f2811cSJesse Gross 	__be16 frag_off;
36381da177e4SLinus Torvalds 
3639bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
36401da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
36411da177e4SLinus Torvalds 	if (ip6 == NULL)
36421da177e4SLinus Torvalds 		goto out;
36431da177e4SLinus Torvalds 
364448c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
364548c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
36461da177e4SLinus Torvalds 	ret = 0;
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
36491da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
365075f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
36511da177e4SLinus Torvalds 	if (offset < 0)
36521da177e4SLinus Torvalds 		goto out;
36531da177e4SLinus Torvalds 
365467f83cbfSVenkat Yekkirala 	if (proto)
365567f83cbfSVenkat Yekkirala 		*proto = nexthdr;
365667f83cbfSVenkat Yekkirala 
36571da177e4SLinus Torvalds 	switch (nexthdr) {
36581da177e4SLinus Torvalds 	case IPPROTO_TCP: {
36591da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
36621da177e4SLinus Torvalds 		if (th == NULL)
36631da177e4SLinus Torvalds 			break;
36641da177e4SLinus Torvalds 
366548c62af6SEric Paris 		ad->u.net->sport = th->source;
366648c62af6SEric Paris 		ad->u.net->dport = th->dest;
36671da177e4SLinus Torvalds 		break;
36681da177e4SLinus Torvalds 	}
36691da177e4SLinus Torvalds 
36701da177e4SLinus Torvalds 	case IPPROTO_UDP: {
36711da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
36741da177e4SLinus Torvalds 		if (uh == NULL)
36751da177e4SLinus Torvalds 			break;
36761da177e4SLinus Torvalds 
367748c62af6SEric Paris 		ad->u.net->sport = uh->source;
367848c62af6SEric Paris 		ad->u.net->dport = uh->dest;
36791da177e4SLinus Torvalds 		break;
36801da177e4SLinus Torvalds 	}
36811da177e4SLinus Torvalds 
36822ee92d46SJames Morris 	case IPPROTO_DCCP: {
36832ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
36842ee92d46SJames Morris 
36852ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
36862ee92d46SJames Morris 		if (dh == NULL)
36872ee92d46SJames Morris 			break;
36882ee92d46SJames Morris 
368948c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
369048c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
36912ee92d46SJames Morris 		break;
36922ee92d46SJames Morris 	}
36932ee92d46SJames Morris 
36941da177e4SLinus Torvalds 	/* includes fragments */
36951da177e4SLinus Torvalds 	default:
36961da177e4SLinus Torvalds 		break;
36971da177e4SLinus Torvalds 	}
36981da177e4SLinus Torvalds out:
36991da177e4SLinus Torvalds 	return ret;
37001da177e4SLinus Torvalds }
37011da177e4SLinus Torvalds 
37021da177e4SLinus Torvalds #endif /* IPV6 */
37031da177e4SLinus Torvalds 
37042bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3705cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
37061da177e4SLinus Torvalds {
3707cf9481e2SDavid Howells 	char *addrp;
3708cf9481e2SDavid Howells 	int ret;
37091da177e4SLinus Torvalds 
371048c62af6SEric Paris 	switch (ad->u.net->family) {
37111da177e4SLinus Torvalds 	case PF_INET:
371267f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3713cf9481e2SDavid Howells 		if (ret)
3714cf9481e2SDavid Howells 			goto parse_error;
371548c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
371648c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
3717cf9481e2SDavid Howells 		goto okay;
37181da177e4SLinus Torvalds 
37191da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37201da177e4SLinus Torvalds 	case PF_INET6:
372167f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3722cf9481e2SDavid Howells 		if (ret)
3723cf9481e2SDavid Howells 			goto parse_error;
372448c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
372548c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
3726cf9481e2SDavid Howells 		goto okay;
37271da177e4SLinus Torvalds #endif	/* IPV6 */
37281da177e4SLinus Torvalds 	default:
3729cf9481e2SDavid Howells 		addrp = NULL;
3730cf9481e2SDavid Howells 		goto okay;
37311da177e4SLinus Torvalds 	}
37321da177e4SLinus Torvalds 
3733cf9481e2SDavid Howells parse_error:
373471f1cb05SPaul Moore 	printk(KERN_WARNING
373571f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
373671f1cb05SPaul Moore 	       " unable to parse packet\n");
37371da177e4SLinus Torvalds 	return ret;
3738cf9481e2SDavid Howells 
3739cf9481e2SDavid Howells okay:
3740cf9481e2SDavid Howells 	if (_addrp)
3741cf9481e2SDavid Howells 		*_addrp = addrp;
3742cf9481e2SDavid Howells 	return 0;
37431da177e4SLinus Torvalds }
37441da177e4SLinus Torvalds 
37454f6a993fSPaul Moore /**
3746220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
37474f6a993fSPaul Moore  * @skb: the packet
374875e22910SPaul Moore  * @family: protocol family
3749220deb96SPaul Moore  * @sid: the packet's peer label SID
37504f6a993fSPaul Moore  *
37514f6a993fSPaul Moore  * Description:
3752220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3753220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3754220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3755220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3756220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3757220deb96SPaul Moore  * peer labels.
37584f6a993fSPaul Moore  *
37594f6a993fSPaul Moore  */
3760220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
37614f6a993fSPaul Moore {
376271f1cb05SPaul Moore 	int err;
37634f6a993fSPaul Moore 	u32 xfrm_sid;
37644f6a993fSPaul Moore 	u32 nlbl_sid;
3765220deb96SPaul Moore 	u32 nlbl_type;
37664f6a993fSPaul Moore 
37674f6a993fSPaul Moore 	selinux_skb_xfrm_sid(skb, &xfrm_sid);
37685dbe1eb0SPaul Moore 	selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3769220deb96SPaul Moore 
377071f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
377171f1cb05SPaul Moore 	if (unlikely(err)) {
377271f1cb05SPaul Moore 		printk(KERN_WARNING
377371f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
377471f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3775220deb96SPaul Moore 		return -EACCES;
377671f1cb05SPaul Moore 	}
3777220deb96SPaul Moore 
3778220deb96SPaul Moore 	return 0;
37794f6a993fSPaul Moore }
37804f6a993fSPaul Moore 
37811da177e4SLinus Torvalds /* socket security operations */
3782d4f2d978SPaul Moore 
37832ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
37842ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
3785d4f2d978SPaul Moore {
37862ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
37872ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
37882ad18bdfSHarry Ciao 		return 0;
37892ad18bdfSHarry Ciao 	}
37902ad18bdfSHarry Ciao 
37912ad18bdfSHarry Ciao 	return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
37922ad18bdfSHarry Ciao 				       socksid);
3793d4f2d978SPaul Moore }
3794d4f2d978SPaul Moore 
3795253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
37961da177e4SLinus Torvalds {
3797253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
37982bf49690SThomas Liu 	struct common_audit_data ad;
37993b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
380048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
3801253bfae6SPaul Moore 	u32 tsid = task_sid(task);
38021da177e4SLinus Torvalds 
3803253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
3804253bfae6SPaul Moore 		return 0;
38051da177e4SLinus Torvalds 
38062bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
38073b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
380848c62af6SEric Paris 	ad.u.net = &net;
380948c62af6SEric Paris 	ad.u.net->sk = sk;
38101da177e4SLinus Torvalds 
3811253bfae6SPaul Moore 	return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
38121da177e4SLinus Torvalds }
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
38151da177e4SLinus Torvalds 				 int protocol, int kern)
38161da177e4SLinus Torvalds {
38175fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3818d4f2d978SPaul Moore 	u32 newsid;
3819275bb41eSDavid Howells 	u16 secclass;
38202ad18bdfSHarry Ciao 	int rc;
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	if (kern)
3823d4f2d978SPaul Moore 		return 0;
38241da177e4SLinus Torvalds 
3825275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
38262ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
38272ad18bdfSHarry Ciao 	if (rc)
38282ad18bdfSHarry Ciao 		return rc;
38292ad18bdfSHarry Ciao 
3830d4f2d978SPaul Moore 	return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
38311da177e4SLinus Torvalds }
38321da177e4SLinus Torvalds 
38337420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
38341da177e4SLinus Torvalds 				      int type, int protocol, int kern)
38351da177e4SLinus Torvalds {
38365fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3837d4f2d978SPaul Moore 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3838892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
3839275bb41eSDavid Howells 	int err = 0;
3840275bb41eSDavid Howells 
38412ad18bdfSHarry Ciao 	isec->sclass = socket_type_to_security_class(family, type, protocol);
38422ad18bdfSHarry Ciao 
3843275bb41eSDavid Howells 	if (kern)
3844275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
38452ad18bdfSHarry Ciao 	else {
38462ad18bdfSHarry Ciao 		err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
38472ad18bdfSHarry Ciao 		if (err)
38482ad18bdfSHarry Ciao 			return err;
38492ad18bdfSHarry Ciao 	}
3850275bb41eSDavid Howells 
38511da177e4SLinus Torvalds 	isec->initialized = 1;
38521da177e4SLinus Torvalds 
3853892c141eSVenkat Yekkirala 	if (sock->sk) {
3854892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3855892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
3856220deb96SPaul Moore 		sksec->sclass = isec->sclass;
3857389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
3858892c141eSVenkat Yekkirala 	}
3859892c141eSVenkat Yekkirala 
38607420ed23SVenkat Yekkirala 	return err;
38611da177e4SLinus Torvalds }
38621da177e4SLinus Torvalds 
38631da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
38641da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
38651da177e4SLinus Torvalds    permission check between the socket and the port number. */
38661da177e4SLinus Torvalds 
38671da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
38681da177e4SLinus Torvalds {
3869253bfae6SPaul Moore 	struct sock *sk = sock->sk;
38701da177e4SLinus Torvalds 	u16 family;
38711da177e4SLinus Torvalds 	int err;
38721da177e4SLinus Torvalds 
3873253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__BIND);
38741da177e4SLinus Torvalds 	if (err)
38751da177e4SLinus Torvalds 		goto out;
38761da177e4SLinus Torvalds 
38771da177e4SLinus Torvalds 	/*
38781da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
387913402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
388013402580SJames Morris 	 * check the first address now.
38811da177e4SLinus Torvalds 	 */
3882253bfae6SPaul Moore 	family = sk->sk_family;
38831da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
38841da177e4SLinus Torvalds 		char *addrp;
3885253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
38862bf49690SThomas Liu 		struct common_audit_data ad;
38873b3b0e4fSEric Paris 		struct selinux_audit_data sad = {0,};
388848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
38891da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
38901da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
38911da177e4SLinus Torvalds 		unsigned short snum;
3892e399f982SJames Morris 		u32 sid, node_perm;
38931da177e4SLinus Torvalds 
38941da177e4SLinus Torvalds 		if (family == PF_INET) {
38951da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
38961da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
38971da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
38981da177e4SLinus Torvalds 		} else {
38991da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
39001da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
39011da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
39021da177e4SLinus Torvalds 		}
39031da177e4SLinus Torvalds 
3904227b60f5SStephen Hemminger 		if (snum) {
3905227b60f5SStephen Hemminger 			int low, high;
3906227b60f5SStephen Hemminger 
3907227b60f5SStephen Hemminger 			inet_get_local_port_range(&low, &high);
3908227b60f5SStephen Hemminger 
3909227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
39103e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
39113e112172SPaul Moore 						      snum, &sid);
39121da177e4SLinus Torvalds 				if (err)
39131da177e4SLinus Torvalds 					goto out;
39142bf49690SThomas Liu 				COMMON_AUDIT_DATA_INIT(&ad, NET);
39153b3b0e4fSEric Paris 				ad.selinux_audit_data = &sad;
391648c62af6SEric Paris 				ad.u.net = &net;
391748c62af6SEric Paris 				ad.u.net->sport = htons(snum);
391848c62af6SEric Paris 				ad.u.net->family = family;
3919253bfae6SPaul Moore 				err = avc_has_perm(sksec->sid, sid,
3920253bfae6SPaul Moore 						   sksec->sclass,
39211da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
39221da177e4SLinus Torvalds 				if (err)
39231da177e4SLinus Torvalds 					goto out;
39241da177e4SLinus Torvalds 			}
3925227b60f5SStephen Hemminger 		}
39261da177e4SLinus Torvalds 
3927253bfae6SPaul Moore 		switch (sksec->sclass) {
392813402580SJames Morris 		case SECCLASS_TCP_SOCKET:
39291da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
39301da177e4SLinus Torvalds 			break;
39311da177e4SLinus Torvalds 
393213402580SJames Morris 		case SECCLASS_UDP_SOCKET:
39331da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
39341da177e4SLinus Torvalds 			break;
39351da177e4SLinus Torvalds 
39362ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
39372ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
39382ee92d46SJames Morris 			break;
39392ee92d46SJames Morris 
39401da177e4SLinus Torvalds 		default:
39411da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
39421da177e4SLinus Torvalds 			break;
39431da177e4SLinus Torvalds 		}
39441da177e4SLinus Torvalds 
3945224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
39461da177e4SLinus Torvalds 		if (err)
39471da177e4SLinus Torvalds 			goto out;
39481da177e4SLinus Torvalds 
39492bf49690SThomas Liu 		COMMON_AUDIT_DATA_INIT(&ad, NET);
39503b3b0e4fSEric Paris 		ad.selinux_audit_data = &sad;
395148c62af6SEric Paris 		ad.u.net = &net;
395248c62af6SEric Paris 		ad.u.net->sport = htons(snum);
395348c62af6SEric Paris 		ad.u.net->family = family;
39541da177e4SLinus Torvalds 
39551da177e4SLinus Torvalds 		if (family == PF_INET)
395648c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
39571da177e4SLinus Torvalds 		else
395848c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
39591da177e4SLinus Torvalds 
3960253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid,
3961253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
39621da177e4SLinus Torvalds 		if (err)
39631da177e4SLinus Torvalds 			goto out;
39641da177e4SLinus Torvalds 	}
39651da177e4SLinus Torvalds out:
39661da177e4SLinus Torvalds 	return err;
39671da177e4SLinus Torvalds }
39681da177e4SLinus Torvalds 
39691da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
39701da177e4SLinus Torvalds {
3971014ab19aSPaul Moore 	struct sock *sk = sock->sk;
3972253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
39731da177e4SLinus Torvalds 	int err;
39741da177e4SLinus Torvalds 
3975253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__CONNECT);
39761da177e4SLinus Torvalds 	if (err)
39771da177e4SLinus Torvalds 		return err;
39781da177e4SLinus Torvalds 
39791da177e4SLinus Torvalds 	/*
39802ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
39811da177e4SLinus Torvalds 	 */
3982253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3983253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_DCCP_SOCKET) {
39842bf49690SThomas Liu 		struct common_audit_data ad;
39853b3b0e4fSEric Paris 		struct selinux_audit_data sad = {0,};
398648c62af6SEric Paris 		struct lsm_network_audit net = {0,};
39871da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
39881da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
39891da177e4SLinus Torvalds 		unsigned short snum;
39902ee92d46SJames Morris 		u32 sid, perm;
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
39931da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3994911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
39951da177e4SLinus Torvalds 				return -EINVAL;
39961da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
39971da177e4SLinus Torvalds 		} else {
39981da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3999911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
40001da177e4SLinus Torvalds 				return -EINVAL;
40011da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
40021da177e4SLinus Torvalds 		}
40031da177e4SLinus Torvalds 
40043e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
40051da177e4SLinus Torvalds 		if (err)
40061da177e4SLinus Torvalds 			goto out;
40071da177e4SLinus Torvalds 
4008253bfae6SPaul Moore 		perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
40092ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
40102ee92d46SJames Morris 
40112bf49690SThomas Liu 		COMMON_AUDIT_DATA_INIT(&ad, NET);
40123b3b0e4fSEric Paris 		ad.selinux_audit_data = &sad;
401348c62af6SEric Paris 		ad.u.net = &net;
401448c62af6SEric Paris 		ad.u.net->dport = htons(snum);
401548c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
4016253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
40171da177e4SLinus Torvalds 		if (err)
40181da177e4SLinus Torvalds 			goto out;
40191da177e4SLinus Torvalds 	}
40201da177e4SLinus Torvalds 
4021014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
4022014ab19aSPaul Moore 
40231da177e4SLinus Torvalds out:
40241da177e4SLinus Torvalds 	return err;
40251da177e4SLinus Torvalds }
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
40281da177e4SLinus Torvalds {
4029253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
40301da177e4SLinus Torvalds }
40311da177e4SLinus Torvalds 
40321da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
40331da177e4SLinus Torvalds {
40341da177e4SLinus Torvalds 	int err;
40351da177e4SLinus Torvalds 	struct inode_security_struct *isec;
40361da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
40371da177e4SLinus Torvalds 
4038253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
40391da177e4SLinus Torvalds 	if (err)
40401da177e4SLinus Torvalds 		return err;
40411da177e4SLinus Torvalds 
40421da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
40431da177e4SLinus Torvalds 
40441da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
40451da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
40461da177e4SLinus Torvalds 	newisec->sid = isec->sid;
40471da177e4SLinus Torvalds 	newisec->initialized = 1;
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	return 0;
40501da177e4SLinus Torvalds }
40511da177e4SLinus Torvalds 
40521da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
40531da177e4SLinus Torvalds 				  int size)
40541da177e4SLinus Torvalds {
4055253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__WRITE);
40561da177e4SLinus Torvalds }
40571da177e4SLinus Torvalds 
40581da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
40591da177e4SLinus Torvalds 				  int size, int flags)
40601da177e4SLinus Torvalds {
4061253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__READ);
40621da177e4SLinus Torvalds }
40631da177e4SLinus Torvalds 
40641da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
40651da177e4SLinus Torvalds {
4066253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
40671da177e4SLinus Torvalds }
40681da177e4SLinus Torvalds 
40691da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
40701da177e4SLinus Torvalds {
4071253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
40721da177e4SLinus Torvalds }
40731da177e4SLinus Torvalds 
40741da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
40751da177e4SLinus Torvalds {
4076f8687afeSPaul Moore 	int err;
4077f8687afeSPaul Moore 
4078253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4079f8687afeSPaul Moore 	if (err)
4080f8687afeSPaul Moore 		return err;
4081f8687afeSPaul Moore 
4082f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
40831da177e4SLinus Torvalds }
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
40861da177e4SLinus Torvalds 				     int optname)
40871da177e4SLinus Torvalds {
4088253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
40891da177e4SLinus Torvalds }
40901da177e4SLinus Torvalds 
40911da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
40921da177e4SLinus Torvalds {
4093253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
40941da177e4SLinus Torvalds }
40951da177e4SLinus Torvalds 
40963610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
40973610cda5SDavid S. Miller 					      struct sock *other,
40981da177e4SLinus Torvalds 					      struct sock *newsk)
40991da177e4SLinus Torvalds {
41003610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
41013610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
41024d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
41032bf49690SThomas Liu 	struct common_audit_data ad;
41043b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
410548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
41061da177e4SLinus Torvalds 	int err;
41071da177e4SLinus Torvalds 
41082bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
41093b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
411048c62af6SEric Paris 	ad.u.net = &net;
411148c62af6SEric Paris 	ad.u.net->sk = other;
41121da177e4SLinus Torvalds 
41134d1e2451SPaul Moore 	err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
41144d1e2451SPaul Moore 			   sksec_other->sclass,
41151da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
41161da177e4SLinus Torvalds 	if (err)
41171da177e4SLinus Torvalds 		return err;
41181da177e4SLinus Torvalds 
41191da177e4SLinus Torvalds 	/* server child socket */
41204d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
41214d1e2451SPaul Moore 	err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
41224d1e2451SPaul Moore 				    &sksec_new->sid);
41234d1e2451SPaul Moore 	if (err)
41244237c75cSVenkat Yekkirala 		return err;
41254d1e2451SPaul Moore 
41264d1e2451SPaul Moore 	/* connecting socket */
41274d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
41284d1e2451SPaul Moore 
41294d1e2451SPaul Moore 	return 0;
41301da177e4SLinus Torvalds }
41311da177e4SLinus Torvalds 
41321da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
41331da177e4SLinus Torvalds 					struct socket *other)
41341da177e4SLinus Torvalds {
4135253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4136253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
41372bf49690SThomas Liu 	struct common_audit_data ad;
41383b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
413948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
41401da177e4SLinus Torvalds 
41412bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
41423b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
414348c62af6SEric Paris 	ad.u.net = &net;
414448c62af6SEric Paris 	ad.u.net->sk = other->sk;
41451da177e4SLinus Torvalds 
4146253bfae6SPaul Moore 	return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4147253bfae6SPaul Moore 			    &ad);
41481da177e4SLinus Torvalds }
41491da177e4SLinus Torvalds 
4150effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4151effad8dfSPaul Moore 				    u32 peer_sid,
41522bf49690SThomas Liu 				    struct common_audit_data *ad)
4153effad8dfSPaul Moore {
4154effad8dfSPaul Moore 	int err;
4155effad8dfSPaul Moore 	u32 if_sid;
4156effad8dfSPaul Moore 	u32 node_sid;
4157effad8dfSPaul Moore 
4158effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
4159effad8dfSPaul Moore 	if (err)
4160effad8dfSPaul Moore 		return err;
4161effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4162effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4163effad8dfSPaul Moore 	if (err)
4164effad8dfSPaul Moore 		return err;
4165effad8dfSPaul Moore 
4166effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4167effad8dfSPaul Moore 	if (err)
4168effad8dfSPaul Moore 		return err;
4169effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4170effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4171effad8dfSPaul Moore }
4172effad8dfSPaul Moore 
4173220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4174d8395c87SPaul Moore 				       u16 family)
4175220deb96SPaul Moore {
4176277d342fSPaul Moore 	int err = 0;
4177220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4178220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
41792bf49690SThomas Liu 	struct common_audit_data ad;
41803b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
418148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4182d8395c87SPaul Moore 	char *addrp;
4183d8395c87SPaul Moore 
41842bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
41853b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
418648c62af6SEric Paris 	ad.u.net = &net;
418748c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
418848c62af6SEric Paris 	ad.u.net->family = family;
4189d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4190d8395c87SPaul Moore 	if (err)
4191d8395c87SPaul Moore 		return err;
4192220deb96SPaul Moore 
419358bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
4194220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4195d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4196220deb96SPaul Moore 		if (err)
4197220deb96SPaul Moore 			return err;
419858bfbb51SPaul Moore 	}
4199220deb96SPaul Moore 
4200d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4201220deb96SPaul Moore 	if (err)
4202220deb96SPaul Moore 		return err;
4203d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4204220deb96SPaul Moore 
42054e5ab4cbSJames Morris 	return err;
42064e5ab4cbSJames Morris }
4207d28d1e08STrent Jaeger 
42084e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
42094e5ab4cbSJames Morris {
4210220deb96SPaul Moore 	int err;
42114237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4212220deb96SPaul Moore 	u16 family = sk->sk_family;
4213220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
42142bf49690SThomas Liu 	struct common_audit_data ad;
42153b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
421648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4217220deb96SPaul Moore 	char *addrp;
4218d8395c87SPaul Moore 	u8 secmark_active;
4219d8395c87SPaul Moore 	u8 peerlbl_active;
42204e5ab4cbSJames Morris 
42214e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4222220deb96SPaul Moore 		return 0;
42234e5ab4cbSJames Morris 
42244e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
422587fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
42264e5ab4cbSJames Morris 		family = PF_INET;
42274e5ab4cbSJames Morris 
4228d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4229d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4230d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4231d8395c87SPaul Moore 	 * as fast and as clean as possible. */
423258bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4233d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4234d8395c87SPaul Moore 
4235d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
4236d8395c87SPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4237d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4238d8395c87SPaul Moore 		return 0;
4239d8395c87SPaul Moore 
42402bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
42413b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
424248c62af6SEric Paris 	ad.u.net = &net;
424348c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
424448c62af6SEric Paris 	ad.u.net->family = family;
4245224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
42464e5ab4cbSJames Morris 	if (err)
4247220deb96SPaul Moore 		return err;
42484e5ab4cbSJames Morris 
4249d8395c87SPaul Moore 	if (peerlbl_active) {
4250d621d35eSPaul Moore 		u32 peer_sid;
4251220deb96SPaul Moore 
4252220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4253220deb96SPaul Moore 		if (err)
4254220deb96SPaul Moore 			return err;
42558964be4aSEric Dumazet 		err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
4256effad8dfSPaul Moore 					       peer_sid, &ad);
4257dfaebe98SPaul Moore 		if (err) {
4258dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4259effad8dfSPaul Moore 			return err;
4260dfaebe98SPaul Moore 		}
4261d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4262d621d35eSPaul Moore 				   PEER__RECV, &ad);
4263dfaebe98SPaul Moore 		if (err)
4264dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4265d621d35eSPaul Moore 	}
4266d621d35eSPaul Moore 
4267d8395c87SPaul Moore 	if (secmark_active) {
4268effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4269effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4270effad8dfSPaul Moore 		if (err)
4271effad8dfSPaul Moore 			return err;
4272effad8dfSPaul Moore 	}
4273effad8dfSPaul Moore 
4274d621d35eSPaul Moore 	return err;
42751da177e4SLinus Torvalds }
42761da177e4SLinus Torvalds 
42772c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
42781da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
42791da177e4SLinus Torvalds {
42801da177e4SLinus Torvalds 	int err = 0;
42811da177e4SLinus Torvalds 	char *scontext;
42821da177e4SLinus Torvalds 	u32 scontext_len;
4283253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
42843de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
42851da177e4SLinus Torvalds 
4286253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4287253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_TCP_SOCKET)
4288dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4289253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4290253bfae6SPaul Moore 		return -ENOPROTOOPT;
42911da177e4SLinus Torvalds 
42922c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
42931da177e4SLinus Torvalds 	if (err)
4294253bfae6SPaul Moore 		return err;
42951da177e4SLinus Torvalds 
42961da177e4SLinus Torvalds 	if (scontext_len > len) {
42971da177e4SLinus Torvalds 		err = -ERANGE;
42981da177e4SLinus Torvalds 		goto out_len;
42991da177e4SLinus Torvalds 	}
43001da177e4SLinus Torvalds 
43011da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
43021da177e4SLinus Torvalds 		err = -EFAULT;
43031da177e4SLinus Torvalds 
43041da177e4SLinus Torvalds out_len:
43051da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
43061da177e4SLinus Torvalds 		err = -EFAULT;
43071da177e4SLinus Torvalds 	kfree(scontext);
43081da177e4SLinus Torvalds 	return err;
43091da177e4SLinus Torvalds }
43101da177e4SLinus Torvalds 
4311dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
43122c7946a7SCatherine Zhang {
4313dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
431475e22910SPaul Moore 	u16 family;
4315877ce7c1SCatherine Zhang 
4316aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4317aa862900SPaul Moore 		family = PF_INET;
4318aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4319aa862900SPaul Moore 		family = PF_INET6;
4320aa862900SPaul Moore 	else if (sock)
432175e22910SPaul Moore 		family = sock->sk->sk_family;
432275e22910SPaul Moore 	else
432375e22910SPaul Moore 		goto out;
432475e22910SPaul Moore 
432575e22910SPaul Moore 	if (sock && family == PF_UNIX)
4326713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
43273de4bab5SPaul Moore 	else if (skb)
4328220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
43292c7946a7SCatherine Zhang 
433075e22910SPaul Moore out:
4331dc49c1f9SCatherine Zhang 	*secid = peer_secid;
433275e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
433375e22910SPaul Moore 		return -EINVAL;
433475e22910SPaul Moore 	return 0;
43352c7946a7SCatherine Zhang }
43362c7946a7SCatherine Zhang 
43377d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
43381da177e4SLinus Torvalds {
433984914b7eSPaul Moore 	struct sk_security_struct *sksec;
434084914b7eSPaul Moore 
434184914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
434284914b7eSPaul Moore 	if (!sksec)
434384914b7eSPaul Moore 		return -ENOMEM;
434484914b7eSPaul Moore 
434584914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
434684914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
434784914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
434884914b7eSPaul Moore 	sk->sk_security = sksec;
434984914b7eSPaul Moore 
435084914b7eSPaul Moore 	return 0;
43511da177e4SLinus Torvalds }
43521da177e4SLinus Torvalds 
43531da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
43541da177e4SLinus Torvalds {
435584914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
435684914b7eSPaul Moore 
435784914b7eSPaul Moore 	sk->sk_security = NULL;
435884914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
435984914b7eSPaul Moore 	kfree(sksec);
43601da177e4SLinus Torvalds }
43611da177e4SLinus Torvalds 
4362892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4363892c141eSVenkat Yekkirala {
4364dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4365dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4366892c141eSVenkat Yekkirala 
4367dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4368dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4369dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
437099f59ed0SPaul Moore 
4371dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4372892c141eSVenkat Yekkirala }
4373892c141eSVenkat Yekkirala 
4374beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4375d28d1e08STrent Jaeger {
4376d28d1e08STrent Jaeger 	if (!sk)
4377beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4378892c141eSVenkat Yekkirala 	else {
4379892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4380d28d1e08STrent Jaeger 
4381beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4382892c141eSVenkat Yekkirala 	}
4383d28d1e08STrent Jaeger }
4384d28d1e08STrent Jaeger 
43859a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
43864237c75cSVenkat Yekkirala {
43874237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
43884237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43894237c75cSVenkat Yekkirala 
43902148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
43912148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
43924237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4393220deb96SPaul Moore 	sksec->sclass = isec->sclass;
43944237c75cSVenkat Yekkirala }
43954237c75cSVenkat Yekkirala 
43969a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
43974237c75cSVenkat Yekkirala 				     struct request_sock *req)
43984237c75cSVenkat Yekkirala {
43994237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
44004237c75cSVenkat Yekkirala 	int err;
4401aa862900SPaul Moore 	u16 family = sk->sk_family;
44027420ed23SVenkat Yekkirala 	u32 newsid;
44034237c75cSVenkat Yekkirala 	u32 peersid;
44044237c75cSVenkat Yekkirala 
4405aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4406aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4407aa862900SPaul Moore 		family = PF_INET;
4408aa862900SPaul Moore 
4409aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4410220deb96SPaul Moore 	if (err)
4411220deb96SPaul Moore 		return err;
4412a51c64f1SVenkat Yekkirala 	if (peersid == SECSID_NULL) {
4413a51c64f1SVenkat Yekkirala 		req->secid = sksec->sid;
44143de4bab5SPaul Moore 		req->peer_secid = SECSID_NULL;
4415389fb800SPaul Moore 	} else {
44164237c75cSVenkat Yekkirala 		err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
44174237c75cSVenkat Yekkirala 		if (err)
44184237c75cSVenkat Yekkirala 			return err;
44194237c75cSVenkat Yekkirala 		req->secid = newsid;
44206b877699SVenkat Yekkirala 		req->peer_secid = peersid;
4421389fb800SPaul Moore 	}
4422389fb800SPaul Moore 
4423389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
44244237c75cSVenkat Yekkirala }
44254237c75cSVenkat Yekkirala 
44269a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
44279a673e56SAdrian Bunk 				   const struct request_sock *req)
44284237c75cSVenkat Yekkirala {
44294237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
44304237c75cSVenkat Yekkirala 
44314237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
44326b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
44334237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
44344237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
44354237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
44364237c75cSVenkat Yekkirala 	   time it will have been created and available. */
443799f59ed0SPaul Moore 
44389f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
44399f2ad665SPaul Moore 	 * thread with access to newsksec */
4440389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
44414237c75cSVenkat Yekkirala }
44424237c75cSVenkat Yekkirala 
4443014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
44446b877699SVenkat Yekkirala {
4445aa862900SPaul Moore 	u16 family = sk->sk_family;
44466b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
44476b877699SVenkat Yekkirala 
4448aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4449aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4450aa862900SPaul Moore 		family = PF_INET;
4451aa862900SPaul Moore 
4452aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
44536b877699SVenkat Yekkirala }
44546b877699SVenkat Yekkirala 
44552606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
44562606fd1fSEric Paris {
44572606fd1fSEric Paris 	const struct task_security_struct *__tsec;
44582606fd1fSEric Paris 	u32 tsid;
44592606fd1fSEric Paris 
44602606fd1fSEric Paris 	__tsec = current_security();
44612606fd1fSEric Paris 	tsid = __tsec->sid;
44622606fd1fSEric Paris 
44632606fd1fSEric Paris 	return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
44642606fd1fSEric Paris }
44652606fd1fSEric Paris 
44662606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
44672606fd1fSEric Paris {
44682606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
44692606fd1fSEric Paris }
44702606fd1fSEric Paris 
44712606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
44722606fd1fSEric Paris {
44732606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
44742606fd1fSEric Paris }
44752606fd1fSEric Paris 
44769a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
44779a673e56SAdrian Bunk 				      struct flowi *fl)
44784237c75cSVenkat Yekkirala {
44791d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
44804237c75cSVenkat Yekkirala }
44814237c75cSVenkat Yekkirala 
4482ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
4483ed6d76e4SPaul Moore {
4484ed6d76e4SPaul Moore 	u32 sid = current_sid();
4485ed6d76e4SPaul Moore 
4486ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
4487ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
4488ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
4489ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
4490ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
4491ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
4492ed6d76e4SPaul Moore 
4493ed6d76e4SPaul Moore 	return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4494ed6d76e4SPaul Moore 			    NULL);
4495ed6d76e4SPaul Moore }
4496ed6d76e4SPaul Moore 
4497ed6d76e4SPaul Moore static void selinux_tun_dev_post_create(struct sock *sk)
4498ed6d76e4SPaul Moore {
4499ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4500ed6d76e4SPaul Moore 
4501ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
4502ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
4503ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
4504ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
4505ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
4506ed6d76e4SPaul Moore 	 * protocols were being used */
4507ed6d76e4SPaul Moore 
4508ed6d76e4SPaul Moore 	/* see the comments in selinux_tun_dev_create() about why we don't use
4509ed6d76e4SPaul Moore 	 * the sockcreate SID here */
4510ed6d76e4SPaul Moore 
4511ed6d76e4SPaul Moore 	sksec->sid = current_sid();
4512ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
4513ed6d76e4SPaul Moore }
4514ed6d76e4SPaul Moore 
4515ed6d76e4SPaul Moore static int selinux_tun_dev_attach(struct sock *sk)
4516ed6d76e4SPaul Moore {
4517ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4518ed6d76e4SPaul Moore 	u32 sid = current_sid();
4519ed6d76e4SPaul Moore 	int err;
4520ed6d76e4SPaul Moore 
4521ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4522ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
4523ed6d76e4SPaul Moore 	if (err)
4524ed6d76e4SPaul Moore 		return err;
4525ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4526ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
4527ed6d76e4SPaul Moore 	if (err)
4528ed6d76e4SPaul Moore 		return err;
4529ed6d76e4SPaul Moore 
4530ed6d76e4SPaul Moore 	sksec->sid = sid;
4531ed6d76e4SPaul Moore 
4532ed6d76e4SPaul Moore 	return 0;
4533ed6d76e4SPaul Moore }
4534ed6d76e4SPaul Moore 
45351da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
45361da177e4SLinus Torvalds {
45371da177e4SLinus Torvalds 	int err = 0;
45381da177e4SLinus Torvalds 	u32 perm;
45391da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4540253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45411da177e4SLinus Torvalds 
45421da177e4SLinus Torvalds 	if (skb->len < NLMSG_SPACE(0)) {
45431da177e4SLinus Torvalds 		err = -EINVAL;
45441da177e4SLinus Torvalds 		goto out;
45451da177e4SLinus Torvalds 	}
4546b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
45471da177e4SLinus Torvalds 
4548253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
45491da177e4SLinus Torvalds 	if (err) {
45501da177e4SLinus Torvalds 		if (err == -EINVAL) {
45519ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
45521da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
45531da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
4554253bfae6SPaul Moore 				  nlh->nlmsg_type, sksec->sclass);
455539c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
45561da177e4SLinus Torvalds 				err = 0;
45571da177e4SLinus Torvalds 		}
45581da177e4SLinus Torvalds 
45591da177e4SLinus Torvalds 		/* Ignore */
45601da177e4SLinus Torvalds 		if (err == -ENOENT)
45611da177e4SLinus Torvalds 			err = 0;
45621da177e4SLinus Torvalds 		goto out;
45631da177e4SLinus Torvalds 	}
45641da177e4SLinus Torvalds 
4565253bfae6SPaul Moore 	err = sock_has_perm(current, sk, perm);
45661da177e4SLinus Torvalds out:
45671da177e4SLinus Torvalds 	return err;
45681da177e4SLinus Torvalds }
45691da177e4SLinus Torvalds 
45701da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
45711da177e4SLinus Torvalds 
4572effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4573effad8dfSPaul Moore 				       u16 family)
45741da177e4SLinus Torvalds {
4575dfaebe98SPaul Moore 	int err;
4576effad8dfSPaul Moore 	char *addrp;
4577effad8dfSPaul Moore 	u32 peer_sid;
45782bf49690SThomas Liu 	struct common_audit_data ad;
45793b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
458048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4581effad8dfSPaul Moore 	u8 secmark_active;
4582948bf85cSPaul Moore 	u8 netlbl_active;
4583effad8dfSPaul Moore 	u8 peerlbl_active;
45844237c75cSVenkat Yekkirala 
4585effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4586effad8dfSPaul Moore 		return NF_ACCEPT;
45874237c75cSVenkat Yekkirala 
4588effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4589948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
4590948bf85cSPaul Moore 	peerlbl_active = netlbl_active || selinux_xfrm_enabled();
4591effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4592effad8dfSPaul Moore 		return NF_ACCEPT;
45934237c75cSVenkat Yekkirala 
4594d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4595d8395c87SPaul Moore 		return NF_DROP;
4596d8395c87SPaul Moore 
45972bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
45983b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
459948c62af6SEric Paris 	ad.u.net = &net;
460048c62af6SEric Paris 	ad.u.net->netif = ifindex;
460148c62af6SEric Paris 	ad.u.net->family = family;
4602effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4603effad8dfSPaul Moore 		return NF_DROP;
46041da177e4SLinus Torvalds 
4605dfaebe98SPaul Moore 	if (peerlbl_active) {
4606dfaebe98SPaul Moore 		err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4607dfaebe98SPaul Moore 					       peer_sid, &ad);
4608dfaebe98SPaul Moore 		if (err) {
4609dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4610effad8dfSPaul Moore 			return NF_DROP;
4611dfaebe98SPaul Moore 		}
4612dfaebe98SPaul Moore 	}
4613effad8dfSPaul Moore 
4614effad8dfSPaul Moore 	if (secmark_active)
4615effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4616effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4617effad8dfSPaul Moore 			return NF_DROP;
4618effad8dfSPaul Moore 
4619948bf85cSPaul Moore 	if (netlbl_active)
4620948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4621948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4622948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4623948bf85cSPaul Moore 		 * protection */
4624948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4625948bf85cSPaul Moore 			return NF_DROP;
4626948bf85cSPaul Moore 
4627effad8dfSPaul Moore 	return NF_ACCEPT;
4628effad8dfSPaul Moore }
4629effad8dfSPaul Moore 
4630effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4631effad8dfSPaul Moore 					 struct sk_buff *skb,
4632effad8dfSPaul Moore 					 const struct net_device *in,
4633effad8dfSPaul Moore 					 const struct net_device *out,
4634effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4635effad8dfSPaul Moore {
4636effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET);
4637effad8dfSPaul Moore }
4638effad8dfSPaul Moore 
4639effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4640effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4641effad8dfSPaul Moore 					 struct sk_buff *skb,
4642effad8dfSPaul Moore 					 const struct net_device *in,
4643effad8dfSPaul Moore 					 const struct net_device *out,
4644effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4645effad8dfSPaul Moore {
4646effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4647effad8dfSPaul Moore }
4648effad8dfSPaul Moore #endif	/* IPV6 */
4649effad8dfSPaul Moore 
4650948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4651948bf85cSPaul Moore 				      u16 family)
4652948bf85cSPaul Moore {
4653948bf85cSPaul Moore 	u32 sid;
4654948bf85cSPaul Moore 
4655948bf85cSPaul Moore 	if (!netlbl_enabled())
4656948bf85cSPaul Moore 		return NF_ACCEPT;
4657948bf85cSPaul Moore 
4658948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4659948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4660948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
4661948bf85cSPaul Moore 	if (skb->sk) {
4662948bf85cSPaul Moore 		struct sk_security_struct *sksec = skb->sk->sk_security;
4663948bf85cSPaul Moore 		sid = sksec->sid;
4664948bf85cSPaul Moore 	} else
4665948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4666948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4667948bf85cSPaul Moore 		return NF_DROP;
4668948bf85cSPaul Moore 
4669948bf85cSPaul Moore 	return NF_ACCEPT;
4670948bf85cSPaul Moore }
4671948bf85cSPaul Moore 
4672948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum,
4673948bf85cSPaul Moore 					struct sk_buff *skb,
4674948bf85cSPaul Moore 					const struct net_device *in,
4675948bf85cSPaul Moore 					const struct net_device *out,
4676948bf85cSPaul Moore 					int (*okfn)(struct sk_buff *))
4677948bf85cSPaul Moore {
4678948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4679948bf85cSPaul Moore }
4680948bf85cSPaul Moore 
4681effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4682effad8dfSPaul Moore 						int ifindex,
4683d8395c87SPaul Moore 						u16 family)
46844e5ab4cbSJames Morris {
4685effad8dfSPaul Moore 	struct sock *sk = skb->sk;
46864237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
46872bf49690SThomas Liu 	struct common_audit_data ad;
46883b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
468948c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4690d8395c87SPaul Moore 	char *addrp;
4691d8395c87SPaul Moore 	u8 proto;
46924e5ab4cbSJames Morris 
4693effad8dfSPaul Moore 	if (sk == NULL)
4694effad8dfSPaul Moore 		return NF_ACCEPT;
46954237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
46964e5ab4cbSJames Morris 
46972bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
46983b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
469948c62af6SEric Paris 	ad.u.net = &net;
470048c62af6SEric Paris 	ad.u.net->netif = ifindex;
470148c62af6SEric Paris 	ad.u.net->family = family;
4702d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4703d8395c87SPaul Moore 		return NF_DROP;
4704d8395c87SPaul Moore 
470558bfbb51SPaul Moore 	if (selinux_secmark_enabled())
4706effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4707d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
47082fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
47091da177e4SLinus Torvalds 
4710d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
47112fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
4712effad8dfSPaul Moore 
4713effad8dfSPaul Moore 	return NF_ACCEPT;
4714effad8dfSPaul Moore }
4715effad8dfSPaul Moore 
4716effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4717effad8dfSPaul Moore 					 u16 family)
4718effad8dfSPaul Moore {
4719effad8dfSPaul Moore 	u32 secmark_perm;
4720effad8dfSPaul Moore 	u32 peer_sid;
4721effad8dfSPaul Moore 	struct sock *sk;
47222bf49690SThomas Liu 	struct common_audit_data ad;
47233b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
472448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4725effad8dfSPaul Moore 	char *addrp;
4726effad8dfSPaul Moore 	u8 secmark_active;
4727effad8dfSPaul Moore 	u8 peerlbl_active;
4728effad8dfSPaul Moore 
4729effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4730effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4731effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4732effad8dfSPaul Moore 	 * as fast and as clean as possible. */
473358bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4734d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4735def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
4736effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4737effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4738effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4739effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4740effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4741effad8dfSPaul Moore 	 *       is NULL, in this case go ahead and apply access control. */
4742adf30907SEric Dumazet 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
4743effad8dfSPaul Moore 		return NF_ACCEPT;
4744def8b4faSAlexey Dobriyan #endif
4745effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4746effad8dfSPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4747effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4748effad8dfSPaul Moore 		return NF_ACCEPT;
4749effad8dfSPaul Moore 
4750d8395c87SPaul Moore 	/* if the packet is being forwarded then get the peer label from the
4751d8395c87SPaul Moore 	 * packet itself; otherwise check to see if it is from a local
4752d8395c87SPaul Moore 	 * application or the kernel, if from an application get the peer label
4753d8395c87SPaul Moore 	 * from the sending socket, otherwise use the kernel's sid */
4754effad8dfSPaul Moore 	sk = skb->sk;
4755d8395c87SPaul Moore 	if (sk == NULL) {
47564a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
4757d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
4758d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
475904f6d70fSEric Paris 				return NF_DROP;
47604a7ab3dcSSteffen Klassert 		} else {
47614a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
4762d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
47634a7ab3dcSSteffen Klassert 		}
4764d8395c87SPaul Moore 	} else {
4765effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4766effad8dfSPaul Moore 		peer_sid = sksec->sid;
4767effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4768effad8dfSPaul Moore 	}
4769effad8dfSPaul Moore 
47702bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, NET);
47713b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
477248c62af6SEric Paris 	ad.u.net = &net;
477348c62af6SEric Paris 	ad.u.net->netif = ifindex;
477448c62af6SEric Paris 	ad.u.net->family = family;
4775d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
477604f6d70fSEric Paris 		return NF_DROP;
4777d8395c87SPaul Moore 
4778effad8dfSPaul Moore 	if (secmark_active)
4779effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4780effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
47811f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4782effad8dfSPaul Moore 
4783effad8dfSPaul Moore 	if (peerlbl_active) {
4784effad8dfSPaul Moore 		u32 if_sid;
4785effad8dfSPaul Moore 		u32 node_sid;
4786effad8dfSPaul Moore 
4787effad8dfSPaul Moore 		if (sel_netif_sid(ifindex, &if_sid))
478804f6d70fSEric Paris 			return NF_DROP;
4789effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
4790effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
47911f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4792effad8dfSPaul Moore 
4793effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
479404f6d70fSEric Paris 			return NF_DROP;
4795effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
4796effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
47971f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4798effad8dfSPaul Moore 	}
4799effad8dfSPaul Moore 
4800effad8dfSPaul Moore 	return NF_ACCEPT;
4801effad8dfSPaul Moore }
4802effad8dfSPaul Moore 
4803effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4804a224be76SDavid S. Miller 					   struct sk_buff *skb,
48051da177e4SLinus Torvalds 					   const struct net_device *in,
48061da177e4SLinus Torvalds 					   const struct net_device *out,
48071da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
48081da177e4SLinus Torvalds {
4809effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET);
48101da177e4SLinus Torvalds }
48111da177e4SLinus Torvalds 
48121da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4813effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4814a224be76SDavid S. Miller 					   struct sk_buff *skb,
48151da177e4SLinus Torvalds 					   const struct net_device *in,
48161da177e4SLinus Torvalds 					   const struct net_device *out,
48171da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
48181da177e4SLinus Torvalds {
4819effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
48201da177e4SLinus Torvalds }
48211da177e4SLinus Torvalds #endif	/* IPV6 */
48221da177e4SLinus Torvalds 
48231da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
48241da177e4SLinus Torvalds 
48251da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
48261da177e4SLinus Torvalds {
48271da177e4SLinus Torvalds 	int err;
48281da177e4SLinus Torvalds 
4829200ac532SEric Paris 	err = cap_netlink_send(sk, skb);
48301da177e4SLinus Torvalds 	if (err)
48311da177e4SLinus Torvalds 		return err;
48321da177e4SLinus Torvalds 
4833941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
48341da177e4SLinus Torvalds }
48351da177e4SLinus Torvalds 
48361da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
48371da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
48381da177e4SLinus Torvalds 			      u16 sclass)
48391da177e4SLinus Torvalds {
48401da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
4841275bb41eSDavid Howells 	u32 sid;
48421da177e4SLinus Torvalds 
484389d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
48441da177e4SLinus Torvalds 	if (!isec)
48451da177e4SLinus Torvalds 		return -ENOMEM;
48461da177e4SLinus Torvalds 
4847275bb41eSDavid Howells 	sid = task_sid(task);
48481da177e4SLinus Torvalds 	isec->sclass = sclass;
4849275bb41eSDavid Howells 	isec->sid = sid;
48501da177e4SLinus Torvalds 	perm->security = isec;
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds 	return 0;
48531da177e4SLinus Torvalds }
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
48561da177e4SLinus Torvalds {
48571da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
48581da177e4SLinus Torvalds 	perm->security = NULL;
48591da177e4SLinus Torvalds 	kfree(isec);
48601da177e4SLinus Torvalds }
48611da177e4SLinus Torvalds 
48621da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
48631da177e4SLinus Torvalds {
48641da177e4SLinus Torvalds 	struct msg_security_struct *msec;
48651da177e4SLinus Torvalds 
486689d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
48671da177e4SLinus Torvalds 	if (!msec)
48681da177e4SLinus Torvalds 		return -ENOMEM;
48691da177e4SLinus Torvalds 
48701da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
48711da177e4SLinus Torvalds 	msg->security = msec;
48721da177e4SLinus Torvalds 
48731da177e4SLinus Torvalds 	return 0;
48741da177e4SLinus Torvalds }
48751da177e4SLinus Torvalds 
48761da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
48771da177e4SLinus Torvalds {
48781da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
48791da177e4SLinus Torvalds 
48801da177e4SLinus Torvalds 	msg->security = NULL;
48811da177e4SLinus Torvalds 	kfree(msec);
48821da177e4SLinus Torvalds }
48831da177e4SLinus Torvalds 
48841da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
48856af963f1SStephen Smalley 			u32 perms)
48861da177e4SLinus Torvalds {
48871da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48882bf49690SThomas Liu 	struct common_audit_data ad;
48893b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
4890275bb41eSDavid Howells 	u32 sid = current_sid();
48911da177e4SLinus Torvalds 
48921da177e4SLinus Torvalds 	isec = ipc_perms->security;
48931da177e4SLinus Torvalds 
48942bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
48953b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
48961da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
48971da177e4SLinus Torvalds 
4898275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
48991da177e4SLinus Torvalds }
49001da177e4SLinus Torvalds 
49011da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
49021da177e4SLinus Torvalds {
49031da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
49041da177e4SLinus Torvalds }
49051da177e4SLinus Torvalds 
49061da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
49071da177e4SLinus Torvalds {
49081da177e4SLinus Torvalds 	msg_msg_free_security(msg);
49091da177e4SLinus Torvalds }
49101da177e4SLinus Torvalds 
49111da177e4SLinus Torvalds /* message queue security operations */
49121da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
49131da177e4SLinus Torvalds {
49141da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49152bf49690SThomas Liu 	struct common_audit_data ad;
49163b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
4917275bb41eSDavid Howells 	u32 sid = current_sid();
49181da177e4SLinus Torvalds 	int rc;
49191da177e4SLinus Torvalds 
49201da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
49211da177e4SLinus Torvalds 	if (rc)
49221da177e4SLinus Torvalds 		return rc;
49231da177e4SLinus Torvalds 
49241da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49251da177e4SLinus Torvalds 
49262bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
49273b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
49281da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49291da177e4SLinus Torvalds 
4930275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49311da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
49321da177e4SLinus Torvalds 	if (rc) {
49331da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
49341da177e4SLinus Torvalds 		return rc;
49351da177e4SLinus Torvalds 	}
49361da177e4SLinus Torvalds 	return 0;
49371da177e4SLinus Torvalds }
49381da177e4SLinus Torvalds 
49391da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
49401da177e4SLinus Torvalds {
49411da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
49421da177e4SLinus Torvalds }
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
49451da177e4SLinus Torvalds {
49461da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49472bf49690SThomas Liu 	struct common_audit_data ad;
49483b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
4949275bb41eSDavid Howells 	u32 sid = current_sid();
49501da177e4SLinus Torvalds 
49511da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49521da177e4SLinus Torvalds 
49532bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
49543b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
49551da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49561da177e4SLinus Torvalds 
4957275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49581da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
49591da177e4SLinus Torvalds }
49601da177e4SLinus Torvalds 
49611da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
49621da177e4SLinus Torvalds {
49631da177e4SLinus Torvalds 	int err;
49641da177e4SLinus Torvalds 	int perms;
49651da177e4SLinus Torvalds 
49661da177e4SLinus Torvalds 	switch (cmd) {
49671da177e4SLinus Torvalds 	case IPC_INFO:
49681da177e4SLinus Torvalds 	case MSG_INFO:
49691da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
49701da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
49711da177e4SLinus Torvalds 	case IPC_STAT:
49721da177e4SLinus Torvalds 	case MSG_STAT:
49731da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
49741da177e4SLinus Torvalds 		break;
49751da177e4SLinus Torvalds 	case IPC_SET:
49761da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
49771da177e4SLinus Torvalds 		break;
49781da177e4SLinus Torvalds 	case IPC_RMID:
49791da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
49801da177e4SLinus Torvalds 		break;
49811da177e4SLinus Torvalds 	default:
49821da177e4SLinus Torvalds 		return 0;
49831da177e4SLinus Torvalds 	}
49841da177e4SLinus Torvalds 
49856af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
49861da177e4SLinus Torvalds 	return err;
49871da177e4SLinus Torvalds }
49881da177e4SLinus Torvalds 
49891da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
49901da177e4SLinus Torvalds {
49911da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49921da177e4SLinus Torvalds 	struct msg_security_struct *msec;
49932bf49690SThomas Liu 	struct common_audit_data ad;
49943b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
4995275bb41eSDavid Howells 	u32 sid = current_sid();
49961da177e4SLinus Torvalds 	int rc;
49971da177e4SLinus Torvalds 
49981da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49991da177e4SLinus Torvalds 	msec = msg->security;
50001da177e4SLinus Torvalds 
50011da177e4SLinus Torvalds 	/*
50021da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
50031da177e4SLinus Torvalds 	 */
50041da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
50051da177e4SLinus Torvalds 		/*
50061da177e4SLinus Torvalds 		 * Compute new sid based on current process and
50071da177e4SLinus Torvalds 		 * message queue this message will be stored in
50081da177e4SLinus Torvalds 		 */
5009275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5010652bb9b0SEric Paris 					     NULL, &msec->sid);
50111da177e4SLinus Torvalds 		if (rc)
50121da177e4SLinus Torvalds 			return rc;
50131da177e4SLinus Torvalds 	}
50141da177e4SLinus Torvalds 
50152bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
50163b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
50171da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
50181da177e4SLinus Torvalds 
50191da177e4SLinus Torvalds 	/* Can this process write to the queue? */
5020275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
50211da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
50221da177e4SLinus Torvalds 	if (!rc)
50231da177e4SLinus Torvalds 		/* Can this process send the message */
5024275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5025275bb41eSDavid Howells 				  MSG__SEND, &ad);
50261da177e4SLinus Torvalds 	if (!rc)
50271da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
5028275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5029275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
50301da177e4SLinus Torvalds 
50311da177e4SLinus Torvalds 	return rc;
50321da177e4SLinus Torvalds }
50331da177e4SLinus Torvalds 
50341da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
50351da177e4SLinus Torvalds 				    struct task_struct *target,
50361da177e4SLinus Torvalds 				    long type, int mode)
50371da177e4SLinus Torvalds {
50381da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50391da177e4SLinus Torvalds 	struct msg_security_struct *msec;
50402bf49690SThomas Liu 	struct common_audit_data ad;
50413b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
5042275bb41eSDavid Howells 	u32 sid = task_sid(target);
50431da177e4SLinus Torvalds 	int rc;
50441da177e4SLinus Torvalds 
50451da177e4SLinus Torvalds 	isec = msq->q_perm.security;
50461da177e4SLinus Torvalds 	msec = msg->security;
50471da177e4SLinus Torvalds 
50482bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
50493b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
50501da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
50511da177e4SLinus Torvalds 
5052275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
50531da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
50541da177e4SLinus Torvalds 	if (!rc)
5055275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
50561da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
50571da177e4SLinus Torvalds 	return rc;
50581da177e4SLinus Torvalds }
50591da177e4SLinus Torvalds 
50601da177e4SLinus Torvalds /* Shared Memory security operations */
50611da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
50621da177e4SLinus Torvalds {
50631da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50642bf49690SThomas Liu 	struct common_audit_data ad;
50653b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
5066275bb41eSDavid Howells 	u32 sid = current_sid();
50671da177e4SLinus Torvalds 	int rc;
50681da177e4SLinus Torvalds 
50691da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
50701da177e4SLinus Torvalds 	if (rc)
50711da177e4SLinus Torvalds 		return rc;
50721da177e4SLinus Torvalds 
50731da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
50741da177e4SLinus Torvalds 
50752bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
50763b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
50771da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
50781da177e4SLinus Torvalds 
5079275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
50801da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
50811da177e4SLinus Torvalds 	if (rc) {
50821da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
50831da177e4SLinus Torvalds 		return rc;
50841da177e4SLinus Torvalds 	}
50851da177e4SLinus Torvalds 	return 0;
50861da177e4SLinus Torvalds }
50871da177e4SLinus Torvalds 
50881da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
50891da177e4SLinus Torvalds {
50901da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
50911da177e4SLinus Torvalds }
50921da177e4SLinus Torvalds 
50931da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
50941da177e4SLinus Torvalds {
50951da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50962bf49690SThomas Liu 	struct common_audit_data ad;
50973b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
5098275bb41eSDavid Howells 	u32 sid = current_sid();
50991da177e4SLinus Torvalds 
51001da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
51011da177e4SLinus Torvalds 
51022bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
51033b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
51041da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
51051da177e4SLinus Torvalds 
5106275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
51071da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
51081da177e4SLinus Torvalds }
51091da177e4SLinus Torvalds 
51101da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
51111da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
51121da177e4SLinus Torvalds {
51131da177e4SLinus Torvalds 	int perms;
51141da177e4SLinus Torvalds 	int err;
51151da177e4SLinus Torvalds 
51161da177e4SLinus Torvalds 	switch (cmd) {
51171da177e4SLinus Torvalds 	case IPC_INFO:
51181da177e4SLinus Torvalds 	case SHM_INFO:
51191da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
51201da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
51211da177e4SLinus Torvalds 	case IPC_STAT:
51221da177e4SLinus Torvalds 	case SHM_STAT:
51231da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
51241da177e4SLinus Torvalds 		break;
51251da177e4SLinus Torvalds 	case IPC_SET:
51261da177e4SLinus Torvalds 		perms = SHM__SETATTR;
51271da177e4SLinus Torvalds 		break;
51281da177e4SLinus Torvalds 	case SHM_LOCK:
51291da177e4SLinus Torvalds 	case SHM_UNLOCK:
51301da177e4SLinus Torvalds 		perms = SHM__LOCK;
51311da177e4SLinus Torvalds 		break;
51321da177e4SLinus Torvalds 	case IPC_RMID:
51331da177e4SLinus Torvalds 		perms = SHM__DESTROY;
51341da177e4SLinus Torvalds 		break;
51351da177e4SLinus Torvalds 	default:
51361da177e4SLinus Torvalds 		return 0;
51371da177e4SLinus Torvalds 	}
51381da177e4SLinus Torvalds 
51396af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
51401da177e4SLinus Torvalds 	return err;
51411da177e4SLinus Torvalds }
51421da177e4SLinus Torvalds 
51431da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
51441da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
51451da177e4SLinus Torvalds {
51461da177e4SLinus Torvalds 	u32 perms;
51471da177e4SLinus Torvalds 
51481da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
51491da177e4SLinus Torvalds 		perms = SHM__READ;
51501da177e4SLinus Torvalds 	else
51511da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
51521da177e4SLinus Torvalds 
51536af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
51541da177e4SLinus Torvalds }
51551da177e4SLinus Torvalds 
51561da177e4SLinus Torvalds /* Semaphore security operations */
51571da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
51581da177e4SLinus Torvalds {
51591da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51602bf49690SThomas Liu 	struct common_audit_data ad;
51613b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
5162275bb41eSDavid Howells 	u32 sid = current_sid();
51631da177e4SLinus Torvalds 	int rc;
51641da177e4SLinus Torvalds 
51651da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
51661da177e4SLinus Torvalds 	if (rc)
51671da177e4SLinus Torvalds 		return rc;
51681da177e4SLinus Torvalds 
51691da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
51701da177e4SLinus Torvalds 
51712bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
51723b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
51731da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
51741da177e4SLinus Torvalds 
5175275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
51761da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
51771da177e4SLinus Torvalds 	if (rc) {
51781da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
51791da177e4SLinus Torvalds 		return rc;
51801da177e4SLinus Torvalds 	}
51811da177e4SLinus Torvalds 	return 0;
51821da177e4SLinus Torvalds }
51831da177e4SLinus Torvalds 
51841da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
51851da177e4SLinus Torvalds {
51861da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
51871da177e4SLinus Torvalds }
51881da177e4SLinus Torvalds 
51891da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
51901da177e4SLinus Torvalds {
51911da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51922bf49690SThomas Liu 	struct common_audit_data ad;
51933b3b0e4fSEric Paris 	struct selinux_audit_data sad = {0,};
5194275bb41eSDavid Howells 	u32 sid = current_sid();
51951da177e4SLinus Torvalds 
51961da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
51971da177e4SLinus Torvalds 
51982bf49690SThomas Liu 	COMMON_AUDIT_DATA_INIT(&ad, IPC);
51993b3b0e4fSEric Paris 	ad.selinux_audit_data = &sad;
52001da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
52011da177e4SLinus Torvalds 
5202275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
52031da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
52041da177e4SLinus Torvalds }
52051da177e4SLinus Torvalds 
52061da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
52071da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
52081da177e4SLinus Torvalds {
52091da177e4SLinus Torvalds 	int err;
52101da177e4SLinus Torvalds 	u32 perms;
52111da177e4SLinus Torvalds 
52121da177e4SLinus Torvalds 	switch (cmd) {
52131da177e4SLinus Torvalds 	case IPC_INFO:
52141da177e4SLinus Torvalds 	case SEM_INFO:
52151da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
52161da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
52171da177e4SLinus Torvalds 	case GETPID:
52181da177e4SLinus Torvalds 	case GETNCNT:
52191da177e4SLinus Torvalds 	case GETZCNT:
52201da177e4SLinus Torvalds 		perms = SEM__GETATTR;
52211da177e4SLinus Torvalds 		break;
52221da177e4SLinus Torvalds 	case GETVAL:
52231da177e4SLinus Torvalds 	case GETALL:
52241da177e4SLinus Torvalds 		perms = SEM__READ;
52251da177e4SLinus Torvalds 		break;
52261da177e4SLinus Torvalds 	case SETVAL:
52271da177e4SLinus Torvalds 	case SETALL:
52281da177e4SLinus Torvalds 		perms = SEM__WRITE;
52291da177e4SLinus Torvalds 		break;
52301da177e4SLinus Torvalds 	case IPC_RMID:
52311da177e4SLinus Torvalds 		perms = SEM__DESTROY;
52321da177e4SLinus Torvalds 		break;
52331da177e4SLinus Torvalds 	case IPC_SET:
52341da177e4SLinus Torvalds 		perms = SEM__SETATTR;
52351da177e4SLinus Torvalds 		break;
52361da177e4SLinus Torvalds 	case IPC_STAT:
52371da177e4SLinus Torvalds 	case SEM_STAT:
52381da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
52391da177e4SLinus Torvalds 		break;
52401da177e4SLinus Torvalds 	default:
52411da177e4SLinus Torvalds 		return 0;
52421da177e4SLinus Torvalds 	}
52431da177e4SLinus Torvalds 
52446af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
52451da177e4SLinus Torvalds 	return err;
52461da177e4SLinus Torvalds }
52471da177e4SLinus Torvalds 
52481da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
52491da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
52501da177e4SLinus Torvalds {
52511da177e4SLinus Torvalds 	u32 perms;
52521da177e4SLinus Torvalds 
52531da177e4SLinus Torvalds 	if (alter)
52541da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
52551da177e4SLinus Torvalds 	else
52561da177e4SLinus Torvalds 		perms = SEM__READ;
52571da177e4SLinus Torvalds 
52586af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
52591da177e4SLinus Torvalds }
52601da177e4SLinus Torvalds 
52611da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
52621da177e4SLinus Torvalds {
52631da177e4SLinus Torvalds 	u32 av = 0;
52641da177e4SLinus Torvalds 
52651da177e4SLinus Torvalds 	av = 0;
52661da177e4SLinus Torvalds 	if (flag & S_IRUGO)
52671da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
52681da177e4SLinus Torvalds 	if (flag & S_IWUGO)
52691da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
52701da177e4SLinus Torvalds 
52711da177e4SLinus Torvalds 	if (av == 0)
52721da177e4SLinus Torvalds 		return 0;
52731da177e4SLinus Torvalds 
52746af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
52751da177e4SLinus Torvalds }
52761da177e4SLinus Torvalds 
5277713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5278713a04aeSAhmed S. Darwish {
5279713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5280713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5281713a04aeSAhmed S. Darwish }
5282713a04aeSAhmed S. Darwish 
52831da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
52841da177e4SLinus Torvalds {
52851da177e4SLinus Torvalds 	if (inode)
52861da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
52871da177e4SLinus Torvalds }
52881da177e4SLinus Torvalds 
52891da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
529004ff9708SAl Viro 			       char *name, char **value)
52911da177e4SLinus Torvalds {
5292275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
52938c8570fbSDustin Kirkland 	u32 sid;
52941da177e4SLinus Torvalds 	int error;
529504ff9708SAl Viro 	unsigned len;
52961da177e4SLinus Torvalds 
52971da177e4SLinus Torvalds 	if (current != p) {
52983b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
52991da177e4SLinus Torvalds 		if (error)
53001da177e4SLinus Torvalds 			return error;
53011da177e4SLinus Torvalds 	}
53021da177e4SLinus Torvalds 
5303275bb41eSDavid Howells 	rcu_read_lock();
5304275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
53051da177e4SLinus Torvalds 
53061da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5307275bb41eSDavid Howells 		sid = __tsec->sid;
53081da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5309275bb41eSDavid Howells 		sid = __tsec->osid;
53101da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5311275bb41eSDavid Howells 		sid = __tsec->exec_sid;
53121da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5313275bb41eSDavid Howells 		sid = __tsec->create_sid;
53144eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5315275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
531642c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5317275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
53181da177e4SLinus Torvalds 	else
5319275bb41eSDavid Howells 		goto invalid;
5320275bb41eSDavid Howells 	rcu_read_unlock();
53211da177e4SLinus Torvalds 
53221da177e4SLinus Torvalds 	if (!sid)
53231da177e4SLinus Torvalds 		return 0;
53241da177e4SLinus Torvalds 
532504ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
532604ff9708SAl Viro 	if (error)
532704ff9708SAl Viro 		return error;
532804ff9708SAl Viro 	return len;
5329275bb41eSDavid Howells 
5330275bb41eSDavid Howells invalid:
5331275bb41eSDavid Howells 	rcu_read_unlock();
5332275bb41eSDavid Howells 	return -EINVAL;
53331da177e4SLinus Torvalds }
53341da177e4SLinus Torvalds 
53351da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
53361da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
53371da177e4SLinus Torvalds {
53381da177e4SLinus Torvalds 	struct task_security_struct *tsec;
53390356357cSRoland McGrath 	struct task_struct *tracer;
5340d84f4f99SDavid Howells 	struct cred *new;
5341d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
53421da177e4SLinus Torvalds 	int error;
53431da177e4SLinus Torvalds 	char *str = value;
53441da177e4SLinus Torvalds 
53451da177e4SLinus Torvalds 	if (current != p) {
53461da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
53471da177e4SLinus Torvalds 		   security attributes. */
53481da177e4SLinus Torvalds 		return -EACCES;
53491da177e4SLinus Torvalds 	}
53501da177e4SLinus Torvalds 
53511da177e4SLinus Torvalds 	/*
53521da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
53531da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
53541da177e4SLinus Torvalds 	 * above restriction is ever removed.
53551da177e4SLinus Torvalds 	 */
53561da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
53573b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
53581da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
53593b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
53604eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
53613b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
536242c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
53633b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
53641da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
53653b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
53661da177e4SLinus Torvalds 	else
53671da177e4SLinus Torvalds 		error = -EINVAL;
53681da177e4SLinus Torvalds 	if (error)
53691da177e4SLinus Torvalds 		return error;
53701da177e4SLinus Torvalds 
53711da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
53721da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
53731da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
53741da177e4SLinus Torvalds 			str[size-1] = 0;
53751da177e4SLinus Torvalds 			size--;
53761da177e4SLinus Torvalds 		}
53771da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
537812b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
5379d6ea83ecSEric Paris 			if (!capable(CAP_MAC_ADMIN)) {
5380d6ea83ecSEric Paris 				struct audit_buffer *ab;
5381d6ea83ecSEric Paris 				size_t audit_size;
5382d6ea83ecSEric Paris 
5383d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
5384d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
5385d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
5386d6ea83ecSEric Paris 					audit_size = size - 1;
5387d6ea83ecSEric Paris 				else
5388d6ea83ecSEric Paris 					audit_size = size;
5389d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5390d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
5391d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
5392d6ea83ecSEric Paris 				audit_log_end(ab);
5393d6ea83ecSEric Paris 
539412b29f34SStephen Smalley 				return error;
5395d6ea83ecSEric Paris 			}
539612b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
539712b29f34SStephen Smalley 							      &sid);
539812b29f34SStephen Smalley 		}
53991da177e4SLinus Torvalds 		if (error)
54001da177e4SLinus Torvalds 			return error;
54011da177e4SLinus Torvalds 	}
54021da177e4SLinus Torvalds 
5403d84f4f99SDavid Howells 	new = prepare_creds();
5404d84f4f99SDavid Howells 	if (!new)
5405d84f4f99SDavid Howells 		return -ENOMEM;
5406d84f4f99SDavid Howells 
54071da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
54081da177e4SLinus Torvalds 	   performed during the actual operation (execve,
54091da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5410d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
54111da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
54121da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5413d84f4f99SDavid Howells 	tsec = new->security;
5414d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
54151da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5416d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
54171da177e4SLinus Torvalds 		tsec->create_sid = sid;
5418d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
54194eb582cfSMichael LeMay 		error = may_create_key(sid, p);
54204eb582cfSMichael LeMay 		if (error)
5421d84f4f99SDavid Howells 			goto abort_change;
54224eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5423d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
542442c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5425d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5426d84f4f99SDavid Howells 		error = -EINVAL;
54271da177e4SLinus Torvalds 		if (sid == 0)
5428d84f4f99SDavid Howells 			goto abort_change;
5429d9250deaSKaiGai Kohei 
5430d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5431d84f4f99SDavid Howells 		error = -EPERM;
54325bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
5433d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5434d84f4f99SDavid Howells 			if (error)
5435d84f4f99SDavid Howells 				goto abort_change;
54361da177e4SLinus Torvalds 		}
54371da177e4SLinus Torvalds 
54381da177e4SLinus Torvalds 		/* Check permissions for the transition. */
54391da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
54401da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
54411da177e4SLinus Torvalds 		if (error)
5442d84f4f99SDavid Howells 			goto abort_change;
54431da177e4SLinus Torvalds 
54441da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
54451da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5446d84f4f99SDavid Howells 		ptsid = 0;
54471da177e4SLinus Torvalds 		task_lock(p);
544806d98473STejun Heo 		tracer = ptrace_parent(p);
5449d84f4f99SDavid Howells 		if (tracer)
5450d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
54511da177e4SLinus Torvalds 		task_unlock(p);
54521da177e4SLinus Torvalds 
5453d84f4f99SDavid Howells 		if (tracer) {
5454d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5455d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5456d84f4f99SDavid Howells 			if (error)
5457d84f4f99SDavid Howells 				goto abort_change;
5458d84f4f99SDavid Howells 		}
5459d84f4f99SDavid Howells 
5460d84f4f99SDavid Howells 		tsec->sid = sid;
5461d84f4f99SDavid Howells 	} else {
5462d84f4f99SDavid Howells 		error = -EINVAL;
5463d84f4f99SDavid Howells 		goto abort_change;
5464d84f4f99SDavid Howells 	}
5465d84f4f99SDavid Howells 
5466d84f4f99SDavid Howells 	commit_creds(new);
54671da177e4SLinus Torvalds 	return size;
5468d84f4f99SDavid Howells 
5469d84f4f99SDavid Howells abort_change:
5470d84f4f99SDavid Howells 	abort_creds(new);
5471d84f4f99SDavid Howells 	return error;
54721da177e4SLinus Torvalds }
54731da177e4SLinus Torvalds 
5474dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5475dc49c1f9SCatherine Zhang {
5476dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5477dc49c1f9SCatherine Zhang }
5478dc49c1f9SCatherine Zhang 
54797bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
548063cb3449SDavid Howells {
548163cb3449SDavid Howells 	return security_context_to_sid(secdata, seclen, secid);
548263cb3449SDavid Howells }
548363cb3449SDavid Howells 
5484dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5485dc49c1f9SCatherine Zhang {
5486dc49c1f9SCatherine Zhang 	kfree(secdata);
5487dc49c1f9SCatherine Zhang }
5488dc49c1f9SCatherine Zhang 
54891ee65e37SDavid P. Quigley /*
54901ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
54911ee65e37SDavid P. Quigley  */
54921ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
54931ee65e37SDavid P. Quigley {
54941ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
54951ee65e37SDavid P. Quigley }
54961ee65e37SDavid P. Quigley 
54971ee65e37SDavid P. Quigley /*
54981ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
54991ee65e37SDavid P. Quigley  */
55001ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
55011ee65e37SDavid P. Quigley {
55021ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
55031ee65e37SDavid P. Quigley }
55041ee65e37SDavid P. Quigley 
55051ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
55061ee65e37SDavid P. Quigley {
55071ee65e37SDavid P. Quigley 	int len = 0;
55081ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
55091ee65e37SDavid P. Quigley 						ctx, true);
55101ee65e37SDavid P. Quigley 	if (len < 0)
55111ee65e37SDavid P. Quigley 		return len;
55121ee65e37SDavid P. Quigley 	*ctxlen = len;
55131ee65e37SDavid P. Quigley 	return 0;
55141ee65e37SDavid P. Quigley }
5515d720024eSMichael LeMay #ifdef CONFIG_KEYS
5516d720024eSMichael LeMay 
5517d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
55187e047ef5SDavid Howells 			     unsigned long flags)
5519d720024eSMichael LeMay {
5520d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5521d720024eSMichael LeMay 	struct key_security_struct *ksec;
5522d720024eSMichael LeMay 
5523d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5524d720024eSMichael LeMay 	if (!ksec)
5525d720024eSMichael LeMay 		return -ENOMEM;
5526d720024eSMichael LeMay 
5527d84f4f99SDavid Howells 	tsec = cred->security;
5528d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5529d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
55304eb582cfSMichael LeMay 	else
5531d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5532d720024eSMichael LeMay 
5533275bb41eSDavid Howells 	k->security = ksec;
5534d720024eSMichael LeMay 	return 0;
5535d720024eSMichael LeMay }
5536d720024eSMichael LeMay 
5537d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5538d720024eSMichael LeMay {
5539d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5540d720024eSMichael LeMay 
5541d720024eSMichael LeMay 	k->security = NULL;
5542d720024eSMichael LeMay 	kfree(ksec);
5543d720024eSMichael LeMay }
5544d720024eSMichael LeMay 
5545d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5546d84f4f99SDavid Howells 				  const struct cred *cred,
5547d720024eSMichael LeMay 				  key_perm_t perm)
5548d720024eSMichael LeMay {
5549d720024eSMichael LeMay 	struct key *key;
5550d720024eSMichael LeMay 	struct key_security_struct *ksec;
5551275bb41eSDavid Howells 	u32 sid;
5552d720024eSMichael LeMay 
5553d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5554d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5555d720024eSMichael LeMay 	   appear to be created. */
5556d720024eSMichael LeMay 	if (perm == 0)
5557d720024eSMichael LeMay 		return 0;
5558d720024eSMichael LeMay 
5559d84f4f99SDavid Howells 	sid = cred_sid(cred);
5560275bb41eSDavid Howells 
5561275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5562275bb41eSDavid Howells 	ksec = key->security;
5563275bb41eSDavid Howells 
5564275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5565d720024eSMichael LeMay }
5566d720024eSMichael LeMay 
556770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
556870a5bb72SDavid Howells {
556970a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
557070a5bb72SDavid Howells 	char *context = NULL;
557170a5bb72SDavid Howells 	unsigned len;
557270a5bb72SDavid Howells 	int rc;
557370a5bb72SDavid Howells 
557470a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
557570a5bb72SDavid Howells 	if (!rc)
557670a5bb72SDavid Howells 		rc = len;
557770a5bb72SDavid Howells 	*_buffer = context;
557870a5bb72SDavid Howells 	return rc;
557970a5bb72SDavid Howells }
558070a5bb72SDavid Howells 
5581d720024eSMichael LeMay #endif
5582d720024eSMichael LeMay 
55831da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5584076c54c5SAhmed S. Darwish 	.name =				"selinux",
5585076c54c5SAhmed S. Darwish 
55869e48858fSIngo Molnar 	.ptrace_access_check =		selinux_ptrace_access_check,
55875cd9c58fSDavid Howells 	.ptrace_traceme =		selinux_ptrace_traceme,
55881da177e4SLinus Torvalds 	.capget =			selinux_capget,
5589d84f4f99SDavid Howells 	.capset =			selinux_capset,
55901da177e4SLinus Torvalds 	.capable =			selinux_capable,
55911da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
55921da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
55931da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
55941da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
55951da177e4SLinus Torvalds 
55961da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
55971da177e4SLinus Torvalds 
5598a6f76f23SDavid Howells 	.bprm_set_creds =		selinux_bprm_set_creds,
5599a6f76f23SDavid Howells 	.bprm_committing_creds =	selinux_bprm_committing_creds,
5600a6f76f23SDavid Howells 	.bprm_committed_creds =		selinux_bprm_committed_creds,
56011da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
56021da177e4SLinus Torvalds 
56031da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
56041da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
56051da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
5606026eb167SEric Paris 	.sb_remount =			selinux_sb_remount,
56071da177e4SLinus Torvalds 	.sb_kern_mount =		selinux_sb_kern_mount,
56082069f457SEric Paris 	.sb_show_options =		selinux_sb_show_options,
56091da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
56101da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
56111da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5612c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5613c9180a57SEric Paris 	.sb_clone_mnt_opts =		selinux_sb_clone_mnt_opts,
5614e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5615e0007529SEric Paris 
56161da177e4SLinus Torvalds 
56171da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
56181da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
56195e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
56201da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
56211da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
56221da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
56231da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
56241da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
56251da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
56261da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
56271da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
56281da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
56291da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
56301da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
56311da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
56321da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
56331da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
56341da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
56351da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
56361da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
56371da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
56381da177e4SLinus Torvalds 	.inode_getsecurity =		selinux_inode_getsecurity,
56391da177e4SLinus Torvalds 	.inode_setsecurity =		selinux_inode_setsecurity,
56401da177e4SLinus Torvalds 	.inode_listsecurity =		selinux_inode_listsecurity,
5641713a04aeSAhmed S. Darwish 	.inode_getsecid =		selinux_inode_getsecid,
56421da177e4SLinus Torvalds 
56431da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
56441da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
56451da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
56461da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
56471da177e4SLinus Torvalds 	.file_mmap =			selinux_file_mmap,
56481da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
56491da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
56501da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
56511da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
56521da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
56531da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
56541da177e4SLinus Torvalds 
565583d49856SEric Paris 	.file_open =			selinux_file_open,
5656788e7dd4SYuichi Nakamura 
56571da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
5658ee18d64cSDavid Howells 	.cred_alloc_blank =		selinux_cred_alloc_blank,
5659f1752eecSDavid Howells 	.cred_free =			selinux_cred_free,
5660d84f4f99SDavid Howells 	.cred_prepare =			selinux_cred_prepare,
5661ee18d64cSDavid Howells 	.cred_transfer =		selinux_cred_transfer,
56623a3b7ce9SDavid Howells 	.kernel_act_as =		selinux_kernel_act_as,
56633a3b7ce9SDavid Howells 	.kernel_create_files_as =	selinux_kernel_create_files_as,
566425354c4fSEric Paris 	.kernel_module_request =	selinux_kernel_module_request,
56651da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
56661da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
56671da177e4SLinus Torvalds 	.task_getsid =			selinux_task_getsid,
5668f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
56691da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
567003e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5671a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
56721da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
56731da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
56741da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
567535601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
56761da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
56771da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
56781da177e4SLinus Torvalds 	.task_to_inode =		selinux_task_to_inode,
56791da177e4SLinus Torvalds 
56801da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5681713a04aeSAhmed S. Darwish 	.ipc_getsecid =			selinux_ipc_getsecid,
56821da177e4SLinus Torvalds 
56831da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
56841da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
56851da177e4SLinus Torvalds 
56861da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
56871da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
56881da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
56891da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
56901da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
56911da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
56921da177e4SLinus Torvalds 
56931da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
56941da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
56951da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
56961da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
56971da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
56981da177e4SLinus Torvalds 
56991da177e4SLinus Torvalds 	.sem_alloc_security =		selinux_sem_alloc_security,
57001da177e4SLinus Torvalds 	.sem_free_security =		selinux_sem_free_security,
57011da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
57021da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
57031da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
57041da177e4SLinus Torvalds 
57051da177e4SLinus Torvalds 	.d_instantiate =		selinux_d_instantiate,
57061da177e4SLinus Torvalds 
57071da177e4SLinus Torvalds 	.getprocattr =			selinux_getprocattr,
57081da177e4SLinus Torvalds 	.setprocattr =			selinux_setprocattr,
57091da177e4SLinus Torvalds 
5710dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
571163cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5712dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
57131ee65e37SDavid P. Quigley 	.inode_notifysecctx =		selinux_inode_notifysecctx,
57141ee65e37SDavid P. Quigley 	.inode_setsecctx =		selinux_inode_setsecctx,
57151ee65e37SDavid P. Quigley 	.inode_getsecctx =		selinux_inode_getsecctx,
5716dc49c1f9SCatherine Zhang 
57171da177e4SLinus Torvalds 	.unix_stream_connect =		selinux_socket_unix_stream_connect,
57181da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
57191da177e4SLinus Torvalds 
57201da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
57211da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
57221da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
57231da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
57241da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
57251da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
57261da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
57271da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
57281da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
57291da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
57301da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
57311da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
57321da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
57331da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
57342c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
57352c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
57361da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
57371da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
5738892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
5739beb8d13bSVenkat Yekkirala 	.sk_getsecid =			selinux_sk_getsecid,
57404237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
57414237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
57424237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
57436b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
57442606fd1fSEric Paris 	.secmark_relabel_packet =	selinux_secmark_relabel_packet,
57452606fd1fSEric Paris 	.secmark_refcount_inc =		selinux_secmark_refcount_inc,
57462606fd1fSEric Paris 	.secmark_refcount_dec =		selinux_secmark_refcount_dec,
57474237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
5748ed6d76e4SPaul Moore 	.tun_dev_create =		selinux_tun_dev_create,
5749ed6d76e4SPaul Moore 	.tun_dev_post_create = 		selinux_tun_dev_post_create,
5750ed6d76e4SPaul Moore 	.tun_dev_attach =		selinux_tun_dev_attach,
5751d28d1e08STrent Jaeger 
5752d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5753d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
5754d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
5755d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
5756c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
5757d28d1e08STrent Jaeger 	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
5758d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
5759c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
5760d28d1e08STrent Jaeger 	.xfrm_policy_lookup =		selinux_xfrm_policy_lookup,
5761e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
5762e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
57631da177e4SLinus Torvalds #endif
5764d720024eSMichael LeMay 
5765d720024eSMichael LeMay #ifdef CONFIG_KEYS
5766d720024eSMichael LeMay 	.key_alloc =			selinux_key_alloc,
5767d720024eSMichael LeMay 	.key_free =			selinux_key_free,
5768d720024eSMichael LeMay 	.key_permission =		selinux_key_permission,
576970a5bb72SDavid Howells 	.key_getsecurity =		selinux_key_getsecurity,
5770d720024eSMichael LeMay #endif
57719d57a7f9SAhmed S. Darwish 
57729d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
57739d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
57749d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
57759d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
57769d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
57779d57a7f9SAhmed S. Darwish #endif
57781da177e4SLinus Torvalds };
57791da177e4SLinus Torvalds 
57801da177e4SLinus Torvalds static __init int selinux_init(void)
57811da177e4SLinus Torvalds {
5782076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
5783076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
5784076c54c5SAhmed S. Darwish 		return 0;
5785076c54c5SAhmed S. Darwish 	}
5786076c54c5SAhmed S. Darwish 
57871da177e4SLinus Torvalds 	if (!selinux_enabled) {
57881da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
57891da177e4SLinus Torvalds 		return 0;
57901da177e4SLinus Torvalds 	}
57911da177e4SLinus Torvalds 
57921da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
57931da177e4SLinus Torvalds 
57941da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
5795d84f4f99SDavid Howells 	cred_init_security();
57961da177e4SLinus Torvalds 
5797fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5798fcaaade1SStephen Smalley 
57997cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
58007cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
580120c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
58021da177e4SLinus Torvalds 	avc_init();
58031da177e4SLinus Torvalds 
58041da177e4SLinus Torvalds 	if (register_security(&selinux_ops))
58051da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
58061da177e4SLinus Torvalds 
5807828dfe1dSEric Paris 	if (selinux_enforcing)
5808fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
5809828dfe1dSEric Paris 	else
5810fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
5811d720024eSMichael LeMay 
58121da177e4SLinus Torvalds 	return 0;
58131da177e4SLinus Torvalds }
58141da177e4SLinus Torvalds 
5815e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
5816e8c26255SAl Viro {
5817e8c26255SAl Viro 	superblock_doinit(sb, NULL);
5818e8c26255SAl Viro }
5819e8c26255SAl Viro 
58201da177e4SLinus Torvalds void selinux_complete_init(void)
58211da177e4SLinus Torvalds {
5822fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
58231da177e4SLinus Torvalds 
58241da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
5825fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
5826e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
58271da177e4SLinus Torvalds }
58281da177e4SLinus Torvalds 
58291da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
58301da177e4SLinus Torvalds    all processes and objects when they are created. */
58311da177e4SLinus Torvalds security_initcall(selinux_init);
58321da177e4SLinus Torvalds 
5833c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
58341da177e4SLinus Torvalds 
5835effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = {
5836effad8dfSPaul Moore 	{
5837effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
58381da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
58391da177e4SLinus Torvalds 		.pf =		PF_INET,
58406e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
58411da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
5842effad8dfSPaul Moore 	},
5843effad8dfSPaul Moore 	{
5844effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
5845effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5846effad8dfSPaul Moore 		.pf =		PF_INET,
5847effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5848effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5849948bf85cSPaul Moore 	},
5850948bf85cSPaul Moore 	{
5851948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
5852948bf85cSPaul Moore 		.owner =	THIS_MODULE,
5853948bf85cSPaul Moore 		.pf =		PF_INET,
5854948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
5855948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5856effad8dfSPaul Moore 	}
58571da177e4SLinus Torvalds };
58581da177e4SLinus Torvalds 
58591da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
58601da177e4SLinus Torvalds 
5861effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = {
5862effad8dfSPaul Moore 	{
5863effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
58641da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
58651da177e4SLinus Torvalds 		.pf =		PF_INET6,
58666e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
58671da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
5868effad8dfSPaul Moore 	},
5869effad8dfSPaul Moore 	{
5870effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
5871effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5872effad8dfSPaul Moore 		.pf =		PF_INET6,
5873effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5874effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
5875effad8dfSPaul Moore 	}
58761da177e4SLinus Torvalds };
58771da177e4SLinus Torvalds 
58781da177e4SLinus Torvalds #endif	/* IPV6 */
58791da177e4SLinus Torvalds 
58801da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
58811da177e4SLinus Torvalds {
58821da177e4SLinus Torvalds 	int err = 0;
58831da177e4SLinus Torvalds 
58841da177e4SLinus Torvalds 	if (!selinux_enabled)
58851da177e4SLinus Torvalds 		goto out;
58861da177e4SLinus Torvalds 
5887fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
58881da177e4SLinus Torvalds 
58896c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
58901da177e4SLinus Torvalds 	if (err)
58916c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
58921da177e4SLinus Torvalds 
58931da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
58946c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
58951da177e4SLinus Torvalds 	if (err)
58966c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
58971da177e4SLinus Torvalds #endif	/* IPV6 */
5898d28d1e08STrent Jaeger 
58991da177e4SLinus Torvalds out:
59001da177e4SLinus Torvalds 	return err;
59011da177e4SLinus Torvalds }
59021da177e4SLinus Torvalds 
59031da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
59041da177e4SLinus Torvalds 
59051da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
59061da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
59071da177e4SLinus Torvalds {
5908fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
59091da177e4SLinus Torvalds 
59106c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
59111da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
59126c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
59131da177e4SLinus Torvalds #endif	/* IPV6 */
59141da177e4SLinus Torvalds }
59151da177e4SLinus Torvalds #endif
59161da177e4SLinus Torvalds 
5917c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
59181da177e4SLinus Torvalds 
59191da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
59201da177e4SLinus Torvalds #define selinux_nf_ip_exit()
59211da177e4SLinus Torvalds #endif
59221da177e4SLinus Torvalds 
5923c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
59241da177e4SLinus Torvalds 
59251da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
5926828dfe1dSEric Paris static int selinux_disabled;
5927828dfe1dSEric Paris 
59281da177e4SLinus Torvalds int selinux_disable(void)
59291da177e4SLinus Torvalds {
59301da177e4SLinus Torvalds 	if (ss_initialized) {
59311da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
59321da177e4SLinus Torvalds 		return -EINVAL;
59331da177e4SLinus Torvalds 	}
59341da177e4SLinus Torvalds 
59351da177e4SLinus Torvalds 	if (selinux_disabled) {
59361da177e4SLinus Torvalds 		/* Only do this once. */
59371da177e4SLinus Torvalds 		return -EINVAL;
59381da177e4SLinus Torvalds 	}
59391da177e4SLinus Torvalds 
59401da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
59411da177e4SLinus Torvalds 
59421da177e4SLinus Torvalds 	selinux_disabled = 1;
594330d55280SStephen Smalley 	selinux_enabled = 0;
59441da177e4SLinus Torvalds 
5945189b3b1cSwzt.wzt@gmail.com 	reset_security_ops();
59461da177e4SLinus Torvalds 
5947af8ff049SEric Paris 	/* Try to destroy the avc node cache */
5948af8ff049SEric Paris 	avc_disable();
5949af8ff049SEric Paris 
59501da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
59511da177e4SLinus Torvalds 	selinux_nf_ip_exit();
59521da177e4SLinus Torvalds 
59531da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
59541da177e4SLinus Torvalds 	exit_sel_fs();
59551da177e4SLinus Torvalds 
59561da177e4SLinus Torvalds 	return 0;
59571da177e4SLinus Torvalds }
59581da177e4SLinus Torvalds #endif
5959