xref: /openbmc/linux/security/selinux/hooks.c (revision af8e50cc7d546c508e9091bbbdf3cf8b243bd8cd)
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[] */
54ca10b9e9SEric Dumazet #include <net/sock.h>
551da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
56220deb96SPaul Moore #include <net/net_namespace.h>
57d621d35eSPaul Moore #include <net/netlabel.h>
58f5269710SEric Paris #include <linux/uaccess.h>
591da177e4SLinus Torvalds #include <asm/ioctls.h>
6060063497SArun Sharma #include <linux/atomic.h>
611da177e4SLinus Torvalds #include <linux/bitops.h>
621da177e4SLinus Torvalds #include <linux/interrupt.h>
631da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6477954983SHong zhi guo #include <net/netlink.h>
651da177e4SLinus Torvalds #include <linux/tcp.h>
661da177e4SLinus Torvalds #include <linux/udp.h>
672ee92d46SJames Morris #include <linux/dccp.h>
681da177e4SLinus Torvalds #include <linux/quota.h>
691da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
701da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
711da177e4SLinus Torvalds #include <linux/parser.h>
721da177e4SLinus Torvalds #include <linux/nfs_mount.h>
731da177e4SLinus Torvalds #include <net/ipv6.h>
741da177e4SLinus Torvalds #include <linux/hugetlb.h>
751da177e4SLinus Torvalds #include <linux/personality.h>
761da177e4SLinus Torvalds #include <linux/audit.h>
776931dfc9SEric Paris #include <linux/string.h>
78877ce7c1SCatherine Zhang #include <linux/selinux.h>
7923970741SEric Paris #include <linux/mutex.h>
80f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8100234592SKees Cook #include <linux/syslog.h>
823486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8344fc7ea0SPaul Gortmaker #include <linux/export.h>
8440401530SAl Viro #include <linux/msg.h>
8540401530SAl Viro #include <linux/shm.h>
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #include "avc.h"
881da177e4SLinus Torvalds #include "objsec.h"
891da177e4SLinus Torvalds #include "netif.h"
90224dfbd8SPaul Moore #include "netnode.h"
913e112172SPaul Moore #include "netport.h"
92d28d1e08STrent Jaeger #include "xfrm.h"
93c60475bfSPaul Moore #include "netlabel.h"
949d57a7f9SAhmed S. Darwish #include "audit.h"
957b98a585SJames Morris #include "avc_ss.h"
961da177e4SLinus Torvalds 
9720510f2fSJames Morris extern struct security_operations *security_ops;
981da177e4SLinus Torvalds 
99d621d35eSPaul Moore /* SECMARK reference count */
10056a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
101d621d35eSPaul Moore 
1021da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
103828dfe1dSEric Paris int selinux_enforcing;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1061da177e4SLinus Torvalds {
107f5269710SEric Paris 	unsigned long enforcing;
108f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enforcing))
109f5269710SEric Paris 		selinux_enforcing = enforcing ? 1 : 0;
1101da177e4SLinus Torvalds 	return 1;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1131da177e4SLinus Torvalds #endif
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1161da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1191da177e4SLinus Torvalds {
120f5269710SEric Paris 	unsigned long enabled;
121f5269710SEric Paris 	if (!strict_strtoul(str, 0, &enabled))
122f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1231da177e4SLinus Torvalds 	return 1;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
12630d55280SStephen Smalley #else
12730d55280SStephen Smalley int selinux_enabled = 1;
1281da177e4SLinus Torvalds #endif
1291da177e4SLinus Torvalds 
130e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1317cae7e26SJames Morris 
132d621d35eSPaul Moore /**
133d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
134d621d35eSPaul Moore  *
135d621d35eSPaul Moore  * Description:
136d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
137d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
138d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
139d621d35eSPaul Moore  * enabled, false (0) if SECMARK is disabled.
140d621d35eSPaul Moore  *
141d621d35eSPaul Moore  */
142d621d35eSPaul Moore static int selinux_secmark_enabled(void)
143d621d35eSPaul Moore {
144d621d35eSPaul Moore 	return (atomic_read(&selinux_secmark_refcount) > 0);
145d621d35eSPaul Moore }
146d621d35eSPaul Moore 
147d84f4f99SDavid Howells /*
148d84f4f99SDavid Howells  * initialise the security for the init task
149d84f4f99SDavid Howells  */
150d84f4f99SDavid Howells static void cred_init_security(void)
1511da177e4SLinus Torvalds {
1523b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1531da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1541da177e4SLinus Torvalds 
15589d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1561da177e4SLinus Torvalds 	if (!tsec)
157d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1581da177e4SLinus Torvalds 
159d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
160f1752eecSDavid Howells 	cred->security = tsec;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
163275bb41eSDavid Howells /*
16488e67f3bSDavid Howells  * get the security ID of a set of credentials
16588e67f3bSDavid Howells  */
16688e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
16788e67f3bSDavid Howells {
16888e67f3bSDavid Howells 	const struct task_security_struct *tsec;
16988e67f3bSDavid Howells 
17088e67f3bSDavid Howells 	tsec = cred->security;
17188e67f3bSDavid Howells 	return tsec->sid;
17288e67f3bSDavid Howells }
17388e67f3bSDavid Howells 
17488e67f3bSDavid Howells /*
1753b11a1deSDavid Howells  * get the objective security ID of a task
176275bb41eSDavid Howells  */
177275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
178275bb41eSDavid Howells {
179275bb41eSDavid Howells 	u32 sid;
180275bb41eSDavid Howells 
181275bb41eSDavid Howells 	rcu_read_lock();
18288e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
183275bb41eSDavid Howells 	rcu_read_unlock();
184275bb41eSDavid Howells 	return sid;
185275bb41eSDavid Howells }
186275bb41eSDavid Howells 
187275bb41eSDavid Howells /*
1883b11a1deSDavid Howells  * get the subjective security ID of the current task
189275bb41eSDavid Howells  */
190275bb41eSDavid Howells static inline u32 current_sid(void)
191275bb41eSDavid Howells {
1925fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
193275bb41eSDavid Howells 
194275bb41eSDavid Howells 	return tsec->sid;
195275bb41eSDavid Howells }
196275bb41eSDavid Howells 
19788e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
19888e67f3bSDavid Howells 
1991da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct inode_security_struct *isec;
202275bb41eSDavid Howells 	u32 sid = current_sid();
2031da177e4SLinus Torvalds 
204a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2051da177e4SLinus Torvalds 	if (!isec)
2061da177e4SLinus Torvalds 		return -ENOMEM;
2071da177e4SLinus Torvalds 
20823970741SEric Paris 	mutex_init(&isec->lock);
2091da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2101da177e4SLinus Torvalds 	isec->inode = inode;
2111da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2121da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
213275bb41eSDavid Howells 	isec->task_sid = sid;
2141da177e4SLinus Torvalds 	inode->i_security = isec;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	return 0;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2221da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2251da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2261da177e4SLinus Torvalds 		list_del_init(&isec->list);
2271da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	inode->i_security = NULL;
2307cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	struct file_security_struct *fsec;
236275bb41eSDavid Howells 	u32 sid = current_sid();
2371da177e4SLinus Torvalds 
23826d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2391da177e4SLinus Torvalds 	if (!fsec)
2401da177e4SLinus Torvalds 		return -ENOMEM;
2411da177e4SLinus Torvalds 
242275bb41eSDavid Howells 	fsec->sid = sid;
243275bb41eSDavid Howells 	fsec->fown_sid = sid;
2441da177e4SLinus Torvalds 	file->f_security = fsec;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	return 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds static void file_free_security(struct file *file)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2521da177e4SLinus Torvalds 	file->f_security = NULL;
2531da177e4SLinus Torvalds 	kfree(fsec);
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2591da177e4SLinus Torvalds 
26089d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2611da177e4SLinus Torvalds 	if (!sbsec)
2621da177e4SLinus Torvalds 		return -ENOMEM;
2631da177e4SLinus Torvalds 
264bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2661da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2671da177e4SLinus Torvalds 	sbsec->sb = sb;
2681da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2691da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
270c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2711da177e4SLinus Torvalds 	sb->s_security = sbsec;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	return 0;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2791da177e4SLinus Torvalds 	sb->s_security = NULL;
2801da177e4SLinus Torvalds 	kfree(sbsec);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
2841da177e4SLinus Torvalds 
285634a539eSStephen Hemminger static const char *labeling_behaviors[6] = {
2861da177e4SLinus Torvalds 	"uses xattr",
2871da177e4SLinus Torvalds 	"uses transition SIDs",
2881da177e4SLinus Torvalds 	"uses task SIDs",
2891da177e4SLinus Torvalds 	"uses genfs_contexts",
2901da177e4SLinus Torvalds 	"not configured for labeling",
2911da177e4SLinus Torvalds 	"uses mountpoint labeling",
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds enum {
30231e87930SEric Paris 	Opt_error = -1,
3031da177e4SLinus Torvalds 	Opt_context = 1,
3041da177e4SLinus Torvalds 	Opt_fscontext = 2,
305c9180a57SEric Paris 	Opt_defcontext = 3,
306c9180a57SEric Paris 	Opt_rootcontext = 4,
30711689d47SDavid P. Quigley 	Opt_labelsupport = 5,
308d355987fSEric Paris 	Opt_nextmntopt = 6,
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
311d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
312d355987fSEric Paris 
313a447c093SSteven Whitehouse static const match_table_t tokens = {
314832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
315832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
316832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
317832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
31811689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
31931e87930SEric Paris 	{Opt_error, NULL},
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3231da177e4SLinus Torvalds 
324c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
325c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
326275bb41eSDavid Howells 			const struct cred *cred)
327c312feb2SEric Paris {
328275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
329c312feb2SEric Paris 	int rc;
330c312feb2SEric Paris 
331c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
332c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
333c312feb2SEric Paris 	if (rc)
334c312feb2SEric Paris 		return rc;
335c312feb2SEric Paris 
336c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
337c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
338c312feb2SEric Paris 	return rc;
339c312feb2SEric Paris }
340c312feb2SEric Paris 
3410808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3420808925eSEric Paris 			struct superblock_security_struct *sbsec,
343275bb41eSDavid Howells 			const struct cred *cred)
3440808925eSEric Paris {
345275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3460808925eSEric Paris 	int rc;
3470808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3480808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3490808925eSEric Paris 	if (rc)
3500808925eSEric Paris 		return rc;
3510808925eSEric Paris 
3520808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3530808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3540808925eSEric Paris 	return rc;
3550808925eSEric Paris }
3560808925eSEric Paris 
357c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3601da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
361c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
3621da177e4SLinus Torvalds 	int rc = 0;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
3651da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
3661da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
3671da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
3681da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
3691da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
370c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
3711da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
3721da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
3731da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
3741da177e4SLinus Torvalds 			goto out;
3751da177e4SLinus Torvalds 		}
376c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
3771da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
3781da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
3791da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3801da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
3811da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
3821da177e4SLinus Torvalds 			else
3831da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3841da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
3851da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
3861da177e4SLinus Torvalds 			goto out;
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
39011689d47SDavid P. Quigley 	sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
3911da177e4SLinus Torvalds 
392c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
393fadcdb45SEric Paris 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
3941da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
395c9180a57SEric Paris 	else
396fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
3971da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
3981da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
3991da177e4SLinus Torvalds 
40011689d47SDavid P. Quigley 	if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
40111689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
40211689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_NONE ||
40311689d47SDavid P. Quigley 	    sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
40411689d47SDavid P. Quigley 		sbsec->flags &= ~SE_SBLABELSUPP;
40511689d47SDavid P. Quigley 
406ddd29ec6SDavid P. Quigley 	/* Special handling for sysfs. Is genfs but also has setxattr handler*/
407ddd29ec6SDavid P. Quigley 	if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
408ddd29ec6SDavid P. Quigley 		sbsec->flags |= SE_SBLABELSUPP;
409ddd29ec6SDavid P. Quigley 
4105c73fcebSStephen Smalley 	/*
4115c73fcebSStephen Smalley 	 * Special handling for rootfs. Is genfs but supports
4125c73fcebSStephen Smalley 	 * setting SELinux context on in-core inodes.
4135c73fcebSStephen Smalley 	 */
4145c73fcebSStephen Smalley 	if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
4155c73fcebSStephen Smalley 		sbsec->flags |= SE_SBLABELSUPP;
4165c73fcebSStephen Smalley 
4171da177e4SLinus Torvalds 	/* Initialize the root inode. */
418c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4211da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4221da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4231da177e4SLinus Torvalds 	   populates itself. */
4241da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4251da177e4SLinus Torvalds next_inode:
4261da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4271da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4281da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4291da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4301da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
4311da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4321da177e4SLinus Torvalds 		inode = igrab(inode);
4331da177e4SLinus Torvalds 		if (inode) {
4341da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4351da177e4SLinus Torvalds 				inode_doinit(inode);
4361da177e4SLinus Torvalds 			iput(inode);
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4391da177e4SLinus Torvalds 		list_del_init(&isec->list);
4401da177e4SLinus Torvalds 		goto next_inode;
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4431da177e4SLinus Torvalds out:
444c9180a57SEric Paris 	return rc;
445c9180a57SEric Paris }
446c9180a57SEric Paris 
447c9180a57SEric Paris /*
448c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
449c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
450c9180a57SEric Paris  * mount options, or whatever.
451c9180a57SEric Paris  */
452c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
453e0007529SEric Paris 				struct security_mnt_opts *opts)
454c9180a57SEric Paris {
455c9180a57SEric Paris 	int rc = 0, i;
456c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
457c9180a57SEric Paris 	char *context = NULL;
458c9180a57SEric Paris 	u32 len;
459c9180a57SEric Paris 	char tmp;
460c9180a57SEric Paris 
461e0007529SEric Paris 	security_init_mnt_opts(opts);
462c9180a57SEric Paris 
4630d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
464c9180a57SEric Paris 		return -EINVAL;
465c9180a57SEric Paris 
466c9180a57SEric Paris 	if (!ss_initialized)
467c9180a57SEric Paris 		return -EINVAL;
468c9180a57SEric Paris 
469*af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
470*af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
471*af8e50ccSEric Paris 
4720d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
473c9180a57SEric Paris 	/* count the number of mount options for this sb */
474*af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
475c9180a57SEric Paris 		if (tmp & 0x01)
476e0007529SEric Paris 			opts->num_mnt_opts++;
477c9180a57SEric Paris 		tmp >>= 1;
478c9180a57SEric Paris 	}
47911689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
48011689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP)
48111689d47SDavid P. Quigley 		opts->num_mnt_opts++;
482c9180a57SEric Paris 
483e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
484e0007529SEric Paris 	if (!opts->mnt_opts) {
485c9180a57SEric Paris 		rc = -ENOMEM;
486c9180a57SEric Paris 		goto out_free;
487c9180a57SEric Paris 	}
488c9180a57SEric Paris 
489e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
490e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
491c9180a57SEric Paris 		rc = -ENOMEM;
492c9180a57SEric Paris 		goto out_free;
493c9180a57SEric Paris 	}
494c9180a57SEric Paris 
495c9180a57SEric Paris 	i = 0;
496c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
497c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
498c9180a57SEric Paris 		if (rc)
499c9180a57SEric Paris 			goto out_free;
500e0007529SEric Paris 		opts->mnt_opts[i] = context;
501e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
502c9180a57SEric Paris 	}
503c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
504c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
505c9180a57SEric Paris 		if (rc)
506c9180a57SEric Paris 			goto out_free;
507e0007529SEric Paris 		opts->mnt_opts[i] = context;
508e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
509c9180a57SEric Paris 	}
510c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
511c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
512c9180a57SEric Paris 		if (rc)
513c9180a57SEric Paris 			goto out_free;
514e0007529SEric Paris 		opts->mnt_opts[i] = context;
515e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
516c9180a57SEric Paris 	}
517c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
518c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
519c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
520c9180a57SEric Paris 
521c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
522c9180a57SEric Paris 		if (rc)
523c9180a57SEric Paris 			goto out_free;
524e0007529SEric Paris 		opts->mnt_opts[i] = context;
525e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
526c9180a57SEric Paris 	}
52711689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP) {
52811689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
52911689d47SDavid P. Quigley 		opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
53011689d47SDavid P. Quigley 	}
531c9180a57SEric Paris 
532e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
533c9180a57SEric Paris 
534c9180a57SEric Paris 	return 0;
535c9180a57SEric Paris 
536c9180a57SEric Paris out_free:
537e0007529SEric Paris 	security_free_mnt_opts(opts);
538c9180a57SEric Paris 	return rc;
539c9180a57SEric Paris }
540c9180a57SEric Paris 
541c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
542c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
543c9180a57SEric Paris {
5440d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5450d90a7ecSDavid P. Quigley 
546c9180a57SEric Paris 	/* check if the old mount command had the same options */
5470d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
548c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
549c9180a57SEric Paris 		    (old_sid != new_sid))
550c9180a57SEric Paris 			return 1;
551c9180a57SEric Paris 
552c9180a57SEric Paris 	/* check if we were passed the same options twice,
553c9180a57SEric Paris 	 * aka someone passed context=a,context=b
554c9180a57SEric Paris 	 */
5550d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5560d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
557c9180a57SEric Paris 			return 1;
558c9180a57SEric Paris 	return 0;
559c9180a57SEric Paris }
560e0007529SEric Paris 
561c9180a57SEric Paris /*
562c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
563c9180a57SEric Paris  * labeling information.
564c9180a57SEric Paris  */
565e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
566e0007529SEric Paris 				struct security_mnt_opts *opts)
567c9180a57SEric Paris {
568275bb41eSDavid Howells 	const struct cred *cred = current_cred();
569c9180a57SEric Paris 	int rc = 0, i;
570c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
571c9180a57SEric Paris 	const char *name = sb->s_type->name;
572089be43eSJames Morris 	struct inode *inode = sbsec->sb->s_root->d_inode;
573089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
574c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
575c9180a57SEric Paris 	u32 defcontext_sid = 0;
576e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
577e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
578e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
579c9180a57SEric Paris 
580c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
581c9180a57SEric Paris 
582c9180a57SEric Paris 	if (!ss_initialized) {
583c9180a57SEric Paris 		if (!num_opts) {
584c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
585c9180a57SEric Paris 			   after the initial policy is loaded and the security
586c9180a57SEric Paris 			   server is ready to handle calls. */
587c9180a57SEric Paris 			goto out;
588c9180a57SEric Paris 		}
589c9180a57SEric Paris 		rc = -EINVAL;
590744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
591744ba35eSEric Paris 			"before the security server is initialized\n");
592c9180a57SEric Paris 		goto out;
593c9180a57SEric Paris 	}
594c9180a57SEric Paris 
595c9180a57SEric Paris 	/*
596e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
597e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
598e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
599e0007529SEric Paris 	 * we need to skip the double mount verification.
600e0007529SEric Paris 	 *
601e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
602e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
603e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
604e0007529SEric Paris 	 * will be used for both mounts)
605e0007529SEric Paris 	 */
6060d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
607e0007529SEric Paris 	    && (num_opts == 0))
608e0007529SEric Paris 		goto out;
609e0007529SEric Paris 
610e0007529SEric Paris 	/*
611c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
612c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
613c9180a57SEric Paris 	 * than once with different security options.
614c9180a57SEric Paris 	 */
615c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
616c9180a57SEric Paris 		u32 sid;
61711689d47SDavid P. Quigley 
61811689d47SDavid P. Quigley 		if (flags[i] == SE_SBLABELSUPP)
61911689d47SDavid P. Quigley 			continue;
620c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
621c9180a57SEric Paris 					     strlen(mount_options[i]), &sid);
622c9180a57SEric Paris 		if (rc) {
623c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
624c9180a57SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
625c9180a57SEric Paris 			       mount_options[i], sb->s_id, name, rc);
626c9180a57SEric Paris 			goto out;
627c9180a57SEric Paris 		}
628c9180a57SEric Paris 		switch (flags[i]) {
629c9180a57SEric Paris 		case FSCONTEXT_MNT:
630c9180a57SEric Paris 			fscontext_sid = sid;
631c9180a57SEric Paris 
632c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
633c9180a57SEric Paris 					fscontext_sid))
634c9180a57SEric Paris 				goto out_double_mount;
635c9180a57SEric Paris 
636c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
637c9180a57SEric Paris 			break;
638c9180a57SEric Paris 		case CONTEXT_MNT:
639c9180a57SEric Paris 			context_sid = sid;
640c9180a57SEric Paris 
641c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
642c9180a57SEric Paris 					context_sid))
643c9180a57SEric Paris 				goto out_double_mount;
644c9180a57SEric Paris 
645c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
646c9180a57SEric Paris 			break;
647c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
648c9180a57SEric Paris 			rootcontext_sid = sid;
649c9180a57SEric Paris 
650c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
651c9180a57SEric Paris 					rootcontext_sid))
652c9180a57SEric Paris 				goto out_double_mount;
653c9180a57SEric Paris 
654c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
655c9180a57SEric Paris 
656c9180a57SEric Paris 			break;
657c9180a57SEric Paris 		case DEFCONTEXT_MNT:
658c9180a57SEric Paris 			defcontext_sid = sid;
659c9180a57SEric Paris 
660c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
661c9180a57SEric Paris 					defcontext_sid))
662c9180a57SEric Paris 				goto out_double_mount;
663c9180a57SEric Paris 
664c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
665c9180a57SEric Paris 
666c9180a57SEric Paris 			break;
667c9180a57SEric Paris 		default:
668c9180a57SEric Paris 			rc = -EINVAL;
669c9180a57SEric Paris 			goto out;
670c9180a57SEric Paris 		}
671c9180a57SEric Paris 	}
672c9180a57SEric Paris 
6730d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
674c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
6750d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
676c9180a57SEric Paris 			goto out_double_mount;
677c9180a57SEric Paris 		rc = 0;
678c9180a57SEric Paris 		goto out;
679c9180a57SEric Paris 	}
680c9180a57SEric Paris 
681089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
6820d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
683c9180a57SEric Paris 
684c9180a57SEric Paris 	/* Determine the labeling behavior to use for this filesystem type. */
68540d3d0b8SEric Paris 	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
686c9180a57SEric Paris 	if (rc) {
687c9180a57SEric Paris 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
688089be43eSJames Morris 		       __func__, sb->s_type->name, rc);
689c9180a57SEric Paris 		goto out;
690c9180a57SEric Paris 	}
691c9180a57SEric Paris 
692c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
693c9180a57SEric Paris 	if (fscontext_sid) {
694275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
695c9180a57SEric Paris 		if (rc)
696c9180a57SEric Paris 			goto out;
697c9180a57SEric Paris 
698c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
699c9180a57SEric Paris 	}
700c9180a57SEric Paris 
701c9180a57SEric Paris 	/*
702c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
703c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
704c9180a57SEric Paris 	 * the superblock context if not already set.
705c9180a57SEric Paris 	 */
706c9180a57SEric Paris 	if (context_sid) {
707c9180a57SEric Paris 		if (!fscontext_sid) {
708275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
709275bb41eSDavid Howells 							  cred);
710c9180a57SEric Paris 			if (rc)
711c9180a57SEric Paris 				goto out;
712c9180a57SEric Paris 			sbsec->sid = context_sid;
713c9180a57SEric Paris 		} else {
714275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
715275bb41eSDavid Howells 							     cred);
716c9180a57SEric Paris 			if (rc)
717c9180a57SEric Paris 				goto out;
718c9180a57SEric Paris 		}
719c9180a57SEric Paris 		if (!rootcontext_sid)
720c9180a57SEric Paris 			rootcontext_sid = context_sid;
721c9180a57SEric Paris 
722c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
723c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
724c9180a57SEric Paris 	}
725c9180a57SEric Paris 
726c9180a57SEric Paris 	if (rootcontext_sid) {
727275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
728275bb41eSDavid Howells 						     cred);
729c9180a57SEric Paris 		if (rc)
730c9180a57SEric Paris 			goto out;
731c9180a57SEric Paris 
732c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
733c9180a57SEric Paris 		root_isec->initialized = 1;
734c9180a57SEric Paris 	}
735c9180a57SEric Paris 
736c9180a57SEric Paris 	if (defcontext_sid) {
737c9180a57SEric Paris 		if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
738c9180a57SEric Paris 			rc = -EINVAL;
739c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
740c9180a57SEric Paris 			       "invalid for this filesystem type\n");
741c9180a57SEric Paris 			goto out;
742c9180a57SEric Paris 		}
743c9180a57SEric Paris 
744c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
745c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
746275bb41eSDavid Howells 							     sbsec, cred);
747c9180a57SEric Paris 			if (rc)
748c9180a57SEric Paris 				goto out;
749c9180a57SEric Paris 		}
750c9180a57SEric Paris 
751c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
752c9180a57SEric Paris 	}
753c9180a57SEric Paris 
754c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
755c9180a57SEric Paris out:
756bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
7571da177e4SLinus Torvalds 	return rc;
758c9180a57SEric Paris out_double_mount:
759c9180a57SEric Paris 	rc = -EINVAL;
760c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
761c9180a57SEric Paris 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
762c9180a57SEric Paris 	goto out;
763c9180a57SEric Paris }
764c9180a57SEric Paris 
765094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
766094f7b69SJeff Layton 				    const struct super_block *newsb)
767094f7b69SJeff Layton {
768094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
769094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
770094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
771094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
772094f7b69SJeff Layton 
773094f7b69SJeff Layton 	if (oldflags != newflags)
774094f7b69SJeff Layton 		goto mismatch;
775094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
776094f7b69SJeff Layton 		goto mismatch;
777094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
778094f7b69SJeff Layton 		goto mismatch;
779094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
780094f7b69SJeff Layton 		goto mismatch;
781094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
782094f7b69SJeff Layton 		struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
783094f7b69SJeff Layton 		struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
784094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
785094f7b69SJeff Layton 			goto mismatch;
786094f7b69SJeff Layton 	}
787094f7b69SJeff Layton 	return 0;
788094f7b69SJeff Layton mismatch:
789094f7b69SJeff Layton 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
790094f7b69SJeff Layton 			    "different security settings for (dev %s, "
791094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
792094f7b69SJeff Layton 	return -EBUSY;
793094f7b69SJeff Layton }
794094f7b69SJeff Layton 
795094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
796c9180a57SEric Paris 					struct super_block *newsb)
797c9180a57SEric Paris {
798c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
799c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
800c9180a57SEric Paris 
801c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
802c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
803c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
804c9180a57SEric Paris 
8050f5e6420SEric Paris 	/*
8060f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
807e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
8080f5e6420SEric Paris 	 */
809e8c26255SAl Viro 	if (!ss_initialized)
810094f7b69SJeff Layton 		return 0;
811c9180a57SEric Paris 
812c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8130d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
814c9180a57SEric Paris 
815094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
8160d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
817094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
8185a552617SEric Paris 
819c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
820c9180a57SEric Paris 
821c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
822c9180a57SEric Paris 
823c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
824c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
825c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
826c9180a57SEric Paris 
827c9180a57SEric Paris 	if (set_context) {
828c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
829c9180a57SEric Paris 
830c9180a57SEric Paris 		if (!set_fscontext)
831c9180a57SEric Paris 			newsbsec->sid = sid;
832c9180a57SEric Paris 		if (!set_rootcontext) {
833c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
834c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
835c9180a57SEric Paris 			newisec->sid = sid;
836c9180a57SEric Paris 		}
837c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
838c9180a57SEric Paris 	}
839c9180a57SEric Paris 	if (set_rootcontext) {
840c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
841c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
842c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
843c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
844c9180a57SEric Paris 
845c9180a57SEric Paris 		newisec->sid = oldisec->sid;
846c9180a57SEric Paris 	}
847c9180a57SEric Paris 
848c9180a57SEric Paris 	sb_finish_set_opts(newsb);
849c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
850094f7b69SJeff Layton 	return 0;
851c9180a57SEric Paris }
852c9180a57SEric Paris 
8532e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
8542e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
855c9180a57SEric Paris {
856e0007529SEric Paris 	char *p;
857c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
858c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
859e0007529SEric Paris 	int rc, num_mnt_opts = 0;
860c9180a57SEric Paris 
861e0007529SEric Paris 	opts->num_mnt_opts = 0;
862c9180a57SEric Paris 
863c9180a57SEric Paris 	/* Standard string-based options. */
864c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
865c9180a57SEric Paris 		int token;
866c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
867c9180a57SEric Paris 
868c9180a57SEric Paris 		if (!*p)
869c9180a57SEric Paris 			continue;
870c9180a57SEric Paris 
871c9180a57SEric Paris 		token = match_token(p, tokens, args);
872c9180a57SEric Paris 
873c9180a57SEric Paris 		switch (token) {
874c9180a57SEric Paris 		case Opt_context:
875c9180a57SEric Paris 			if (context || defcontext) {
876c9180a57SEric Paris 				rc = -EINVAL;
877c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
878c9180a57SEric Paris 				goto out_err;
879c9180a57SEric Paris 			}
880c9180a57SEric Paris 			context = match_strdup(&args[0]);
881c9180a57SEric Paris 			if (!context) {
882c9180a57SEric Paris 				rc = -ENOMEM;
883c9180a57SEric Paris 				goto out_err;
884c9180a57SEric Paris 			}
885c9180a57SEric Paris 			break;
886c9180a57SEric Paris 
887c9180a57SEric Paris 		case Opt_fscontext:
888c9180a57SEric Paris 			if (fscontext) {
889c9180a57SEric Paris 				rc = -EINVAL;
890c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
891c9180a57SEric Paris 				goto out_err;
892c9180a57SEric Paris 			}
893c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
894c9180a57SEric Paris 			if (!fscontext) {
895c9180a57SEric Paris 				rc = -ENOMEM;
896c9180a57SEric Paris 				goto out_err;
897c9180a57SEric Paris 			}
898c9180a57SEric Paris 			break;
899c9180a57SEric Paris 
900c9180a57SEric Paris 		case Opt_rootcontext:
901c9180a57SEric Paris 			if (rootcontext) {
902c9180a57SEric Paris 				rc = -EINVAL;
903c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
904c9180a57SEric Paris 				goto out_err;
905c9180a57SEric Paris 			}
906c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
907c9180a57SEric Paris 			if (!rootcontext) {
908c9180a57SEric Paris 				rc = -ENOMEM;
909c9180a57SEric Paris 				goto out_err;
910c9180a57SEric Paris 			}
911c9180a57SEric Paris 			break;
912c9180a57SEric Paris 
913c9180a57SEric Paris 		case Opt_defcontext:
914c9180a57SEric Paris 			if (context || defcontext) {
915c9180a57SEric Paris 				rc = -EINVAL;
916c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
917c9180a57SEric Paris 				goto out_err;
918c9180a57SEric Paris 			}
919c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
920c9180a57SEric Paris 			if (!defcontext) {
921c9180a57SEric Paris 				rc = -ENOMEM;
922c9180a57SEric Paris 				goto out_err;
923c9180a57SEric Paris 			}
924c9180a57SEric Paris 			break;
92511689d47SDavid P. Quigley 		case Opt_labelsupport:
92611689d47SDavid P. Quigley 			break;
927c9180a57SEric Paris 		default:
928c9180a57SEric Paris 			rc = -EINVAL;
929c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
930c9180a57SEric Paris 			goto out_err;
931c9180a57SEric Paris 
932c9180a57SEric Paris 		}
933c9180a57SEric Paris 	}
934c9180a57SEric Paris 
935e0007529SEric Paris 	rc = -ENOMEM;
936e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
937e0007529SEric Paris 	if (!opts->mnt_opts)
938e0007529SEric Paris 		goto out_err;
939e0007529SEric Paris 
940e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
941e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
942e0007529SEric Paris 		kfree(opts->mnt_opts);
943e0007529SEric Paris 		goto out_err;
944c9180a57SEric Paris 	}
945c9180a57SEric Paris 
946e0007529SEric Paris 	if (fscontext) {
947e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
948e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
949e0007529SEric Paris 	}
950e0007529SEric Paris 	if (context) {
951e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
952e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
953e0007529SEric Paris 	}
954e0007529SEric Paris 	if (rootcontext) {
955e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
956e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
957e0007529SEric Paris 	}
958e0007529SEric Paris 	if (defcontext) {
959e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
960e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
961e0007529SEric Paris 	}
962e0007529SEric Paris 
963e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
964e0007529SEric Paris 	return 0;
965e0007529SEric Paris 
966c9180a57SEric Paris out_err:
967c9180a57SEric Paris 	kfree(context);
968c9180a57SEric Paris 	kfree(defcontext);
969c9180a57SEric Paris 	kfree(fscontext);
970c9180a57SEric Paris 	kfree(rootcontext);
971c9180a57SEric Paris 	return rc;
9721da177e4SLinus Torvalds }
973e0007529SEric Paris /*
974e0007529SEric Paris  * string mount options parsing and call set the sbsec
975e0007529SEric Paris  */
976e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
977e0007529SEric Paris {
978e0007529SEric Paris 	int rc = 0;
979e0007529SEric Paris 	char *options = data;
980e0007529SEric Paris 	struct security_mnt_opts opts;
981e0007529SEric Paris 
982e0007529SEric Paris 	security_init_mnt_opts(&opts);
983e0007529SEric Paris 
984e0007529SEric Paris 	if (!data)
985e0007529SEric Paris 		goto out;
986e0007529SEric Paris 
987e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
988e0007529SEric Paris 
989e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
990e0007529SEric Paris 	if (rc)
991e0007529SEric Paris 		goto out_err;
992e0007529SEric Paris 
993e0007529SEric Paris out:
994e0007529SEric Paris 	rc = selinux_set_mnt_opts(sb, &opts);
995e0007529SEric Paris 
996e0007529SEric Paris out_err:
997e0007529SEric Paris 	security_free_mnt_opts(&opts);
998e0007529SEric Paris 	return rc;
999e0007529SEric Paris }
10001da177e4SLinus Torvalds 
10013583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
10023583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
10032069f457SEric Paris {
10042069f457SEric Paris 	int i;
10052069f457SEric Paris 	char *prefix;
10062069f457SEric Paris 
10072069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
100811689d47SDavid P. Quigley 		char *has_comma;
100911689d47SDavid P. Quigley 
101011689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
101111689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
101211689d47SDavid P. Quigley 		else
101311689d47SDavid P. Quigley 			has_comma = NULL;
10142069f457SEric Paris 
10152069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
10162069f457SEric Paris 		case CONTEXT_MNT:
10172069f457SEric Paris 			prefix = CONTEXT_STR;
10182069f457SEric Paris 			break;
10192069f457SEric Paris 		case FSCONTEXT_MNT:
10202069f457SEric Paris 			prefix = FSCONTEXT_STR;
10212069f457SEric Paris 			break;
10222069f457SEric Paris 		case ROOTCONTEXT_MNT:
10232069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
10242069f457SEric Paris 			break;
10252069f457SEric Paris 		case DEFCONTEXT_MNT:
10262069f457SEric Paris 			prefix = DEFCONTEXT_STR;
10272069f457SEric Paris 			break;
102811689d47SDavid P. Quigley 		case SE_SBLABELSUPP:
102911689d47SDavid P. Quigley 			seq_putc(m, ',');
103011689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
103111689d47SDavid P. Quigley 			continue;
10322069f457SEric Paris 		default:
10332069f457SEric Paris 			BUG();
1034a35c6c83SEric Paris 			return;
10352069f457SEric Paris 		};
10362069f457SEric Paris 		/* we need a comma before each option */
10372069f457SEric Paris 		seq_putc(m, ',');
10382069f457SEric Paris 		seq_puts(m, prefix);
10392069f457SEric Paris 		if (has_comma)
10402069f457SEric Paris 			seq_putc(m, '\"');
10412069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
10422069f457SEric Paris 		if (has_comma)
10432069f457SEric Paris 			seq_putc(m, '\"');
10442069f457SEric Paris 	}
10452069f457SEric Paris }
10462069f457SEric Paris 
10472069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10482069f457SEric Paris {
10492069f457SEric Paris 	struct security_mnt_opts opts;
10502069f457SEric Paris 	int rc;
10512069f457SEric Paris 
10522069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1053383795c2SEric Paris 	if (rc) {
1054383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1055383795c2SEric Paris 		if (rc == -EINVAL)
1056383795c2SEric Paris 			rc = 0;
10572069f457SEric Paris 		return rc;
1058383795c2SEric Paris 	}
10592069f457SEric Paris 
10602069f457SEric Paris 	selinux_write_opts(m, &opts);
10612069f457SEric Paris 
10622069f457SEric Paris 	security_free_mnt_opts(&opts);
10632069f457SEric Paris 
10642069f457SEric Paris 	return rc;
10652069f457SEric Paris }
10662069f457SEric Paris 
10671da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
10701da177e4SLinus Torvalds 	case S_IFSOCK:
10711da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
10721da177e4SLinus Torvalds 	case S_IFLNK:
10731da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
10741da177e4SLinus Torvalds 	case S_IFREG:
10751da177e4SLinus Torvalds 		return SECCLASS_FILE;
10761da177e4SLinus Torvalds 	case S_IFBLK:
10771da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
10781da177e4SLinus Torvalds 	case S_IFDIR:
10791da177e4SLinus Torvalds 		return SECCLASS_DIR;
10801da177e4SLinus Torvalds 	case S_IFCHR:
10811da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
10821da177e4SLinus Torvalds 	case S_IFIFO:
10831da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	return SECCLASS_FILE;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
109013402580SJames Morris static inline int default_protocol_stream(int protocol)
109113402580SJames Morris {
109213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
109313402580SJames Morris }
109413402580SJames Morris 
109513402580SJames Morris static inline int default_protocol_dgram(int protocol)
109613402580SJames Morris {
109713402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
109813402580SJames Morris }
109913402580SJames Morris 
11001da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11011da177e4SLinus Torvalds {
11021da177e4SLinus Torvalds 	switch (family) {
11031da177e4SLinus Torvalds 	case PF_UNIX:
11041da177e4SLinus Torvalds 		switch (type) {
11051da177e4SLinus Torvalds 		case SOCK_STREAM:
11061da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11071da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11081da177e4SLinus Torvalds 		case SOCK_DGRAM:
11091da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11101da177e4SLinus Torvalds 		}
11111da177e4SLinus Torvalds 		break;
11121da177e4SLinus Torvalds 	case PF_INET:
11131da177e4SLinus Torvalds 	case PF_INET6:
11141da177e4SLinus Torvalds 		switch (type) {
11151da177e4SLinus Torvalds 		case SOCK_STREAM:
111613402580SJames Morris 			if (default_protocol_stream(protocol))
11171da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
111813402580SJames Morris 			else
111913402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11201da177e4SLinus Torvalds 		case SOCK_DGRAM:
112113402580SJames Morris 			if (default_protocol_dgram(protocol))
11221da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
112313402580SJames Morris 			else
112413402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11252ee92d46SJames Morris 		case SOCK_DCCP:
11262ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
112713402580SJames Morris 		default:
11281da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 		break;
11311da177e4SLinus Torvalds 	case PF_NETLINK:
11321da177e4SLinus Torvalds 		switch (protocol) {
11331da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11341da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11351da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
11361da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
11377f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11381da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11391da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11401da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11411da177e4SLinus Torvalds 		case NETLINK_XFRM:
11421da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11431da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11441da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11451da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11461da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11471da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
11481da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
11491da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
11501da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
11510c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
11520c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
11531da177e4SLinus Torvalds 		default:
11541da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 	case PF_PACKET:
11571da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
11581da177e4SLinus Torvalds 	case PF_KEY:
11591da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
11603e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
11613e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11688e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
11691da177e4SLinus Torvalds 				u16 tclass,
11701da177e4SLinus Torvalds 				u32 *sid)
11711da177e4SLinus Torvalds {
11728e6c9693SLucian Adrian Grijincu 	int rc;
11738e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
11761da177e4SLinus Torvalds 	if (!buffer)
11771da177e4SLinus Torvalds 		return -ENOMEM;
11781da177e4SLinus Torvalds 
11798e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
11808e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
11818e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
11828e6c9693SLucian Adrian Grijincu 	else {
11838e6c9693SLucian Adrian Grijincu 		/* each process gets a /proc/PID/ entry. Strip off the
11848e6c9693SLucian Adrian Grijincu 		 * PID part to get a valid selinux labeling.
11858e6c9693SLucian Adrian Grijincu 		 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
11868e6c9693SLucian Adrian Grijincu 		while (path[1] >= '0' && path[1] <= '9') {
11878e6c9693SLucian Adrian Grijincu 			path[1] = '/';
11888e6c9693SLucian Adrian Grijincu 			path++;
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 		rc = security_genfs_sid("proc", path, tclass, sid);
11918e6c9693SLucian Adrian Grijincu 	}
11921da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
11931da177e4SLinus Torvalds 	return rc;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds #else
11968e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
11971da177e4SLinus Torvalds 				u16 tclass,
11981da177e4SLinus Torvalds 				u32 *sid)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	return -EINVAL;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds #endif
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
12051da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
12061da177e4SLinus Torvalds {
12071da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
12081da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12091da177e4SLinus Torvalds 	u32 sid;
12101da177e4SLinus Torvalds 	struct dentry *dentry;
12111da177e4SLinus Torvalds #define INITCONTEXTLEN 255
12121da177e4SLinus Torvalds 	char *context = NULL;
12131da177e4SLinus Torvalds 	unsigned len = 0;
12141da177e4SLinus Torvalds 	int rc = 0;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	if (isec->initialized)
12171da177e4SLinus Torvalds 		goto out;
12181da177e4SLinus Torvalds 
121923970741SEric Paris 	mutex_lock(&isec->lock);
12201da177e4SLinus Torvalds 	if (isec->initialized)
122123970741SEric Paris 		goto out_unlock;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
12240d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
12251da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
12261da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
12271da177e4SLinus Torvalds 		   server is ready to handle calls. */
12281da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
12291da177e4SLinus Torvalds 		if (list_empty(&isec->list))
12301da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
12311da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
123223970741SEric Paris 		goto out_unlock;
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	switch (sbsec->behavior) {
12361da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
12371da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
12381da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
12391da177e4SLinus Torvalds 			break;
12401da177e4SLinus Torvalds 		}
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
12431da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
12441da177e4SLinus Torvalds 		if (opt_dentry) {
12451da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
12461da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
12471da177e4SLinus Torvalds 		} else {
12481da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
12491da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
12501da177e4SLinus Torvalds 		}
12511da177e4SLinus Torvalds 		if (!dentry) {
1252df7f54c0SEric Paris 			/*
1253df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1254df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1255df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1256df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1257df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1258df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1259df7f54c0SEric Paris 			 * be used again by userspace.
1260df7f54c0SEric Paris 			 */
126123970741SEric Paris 			goto out_unlock;
12621da177e4SLinus Torvalds 		}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
12654cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
12661da177e4SLinus Torvalds 		if (!context) {
12671da177e4SLinus Torvalds 			rc = -ENOMEM;
12681da177e4SLinus Torvalds 			dput(dentry);
126923970741SEric Paris 			goto out_unlock;
12701da177e4SLinus Torvalds 		}
12714cb912f1SEric Paris 		context[len] = '\0';
12721da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12731da177e4SLinus Torvalds 					   context, len);
12741da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1275314dabb8SJames Morris 			kfree(context);
1276314dabb8SJames Morris 
12771da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
12781da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12791da177e4SLinus Torvalds 						   NULL, 0);
12801da177e4SLinus Torvalds 			if (rc < 0) {
12811da177e4SLinus Torvalds 				dput(dentry);
128223970741SEric Paris 				goto out_unlock;
12831da177e4SLinus Torvalds 			}
12841da177e4SLinus Torvalds 			len = rc;
12854cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
12861da177e4SLinus Torvalds 			if (!context) {
12871da177e4SLinus Torvalds 				rc = -ENOMEM;
12881da177e4SLinus Torvalds 				dput(dentry);
128923970741SEric Paris 				goto out_unlock;
12901da177e4SLinus Torvalds 			}
12914cb912f1SEric Paris 			context[len] = '\0';
12921da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
12931da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
12941da177e4SLinus Torvalds 						   context, len);
12951da177e4SLinus Torvalds 		}
12961da177e4SLinus Torvalds 		dput(dentry);
12971da177e4SLinus Torvalds 		if (rc < 0) {
12981da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1299744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1300dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
13011da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
13021da177e4SLinus Torvalds 				kfree(context);
130323970741SEric Paris 				goto out_unlock;
13041da177e4SLinus Torvalds 			}
13051da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
13061da177e4SLinus Torvalds 			sid = sbsec->def_sid;
13071da177e4SLinus Torvalds 			rc = 0;
13081da177e4SLinus Torvalds 		} else {
1309f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1310869ab514SStephen Smalley 							     sbsec->def_sid,
1311869ab514SStephen Smalley 							     GFP_NOFS);
13121da177e4SLinus Torvalds 			if (rc) {
13134ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
13144ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
13154ba0a8adSEric Paris 
13164ba0a8adSEric Paris 				if (rc == -EINVAL) {
13174ba0a8adSEric Paris 					if (printk_ratelimit())
13184ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
13194ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
13204ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
13214ba0a8adSEric Paris 				} else {
1322744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
13231da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
13244ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
13254ba0a8adSEric Paris 				}
13261da177e4SLinus Torvalds 				kfree(context);
13271da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
13281da177e4SLinus Torvalds 				rc = 0;
13291da177e4SLinus Torvalds 				break;
13301da177e4SLinus Torvalds 			}
13311da177e4SLinus Torvalds 		}
13321da177e4SLinus Torvalds 		kfree(context);
13331da177e4SLinus Torvalds 		isec->sid = sid;
13341da177e4SLinus Torvalds 		break;
13351da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
13361da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
13371da177e4SLinus Torvalds 		break;
13381da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
13391da177e4SLinus Torvalds 		/* Default to the fs SID. */
13401da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
13431da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
1344652bb9b0SEric Paris 		rc = security_transition_sid(isec->task_sid, sbsec->sid,
1345652bb9b0SEric Paris 					     isec->sclass, NULL, &sid);
13461da177e4SLinus Torvalds 		if (rc)
134723970741SEric Paris 			goto out_unlock;
13481da177e4SLinus Torvalds 		isec->sid = sid;
13491da177e4SLinus Torvalds 		break;
1350c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1351c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1352c312feb2SEric Paris 		break;
13531da177e4SLinus Torvalds 	default:
1354c312feb2SEric Paris 		/* Default to the fs superblock SID. */
13551da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13561da177e4SLinus Torvalds 
13570d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
13588e6c9693SLucian Adrian Grijincu 			if (opt_dentry) {
13591da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
13608e6c9693SLucian Adrian Grijincu 				rc = selinux_proc_get_sid(opt_dentry,
13611da177e4SLinus Torvalds 							  isec->sclass,
13621da177e4SLinus Torvalds 							  &sid);
13631da177e4SLinus Torvalds 				if (rc)
136423970741SEric Paris 					goto out_unlock;
13651da177e4SLinus Torvalds 				isec->sid = sid;
13661da177e4SLinus Torvalds 			}
13671da177e4SLinus Torvalds 		}
13681da177e4SLinus Torvalds 		break;
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	isec->initialized = 1;
13721da177e4SLinus Torvalds 
137323970741SEric Paris out_unlock:
137423970741SEric Paris 	mutex_unlock(&isec->lock);
13751da177e4SLinus Torvalds out:
13761da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
13771da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
13781da177e4SLinus Torvalds 	return rc;
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
13821da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	u32 perm = 0;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	switch (sig) {
13871da177e4SLinus Torvalds 	case SIGCHLD:
13881da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
13891da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
13901da177e4SLinus Torvalds 		break;
13911da177e4SLinus Torvalds 	case SIGKILL:
13921da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
13931da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
13941da177e4SLinus Torvalds 		break;
13951da177e4SLinus Torvalds 	case SIGSTOP:
13961da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
13971da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
13981da177e4SLinus Torvalds 		break;
13991da177e4SLinus Torvalds 	default:
14001da177e4SLinus Torvalds 		/* All other signals. */
14011da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
14021da177e4SLinus Torvalds 		break;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	return perm;
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
1408275bb41eSDavid Howells /*
1409d84f4f99SDavid Howells  * Check permission between a pair of credentials
1410d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1411d84f4f99SDavid Howells  */
1412d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1413d84f4f99SDavid Howells 			 const struct cred *target,
1414d84f4f99SDavid Howells 			 u32 perms)
1415d84f4f99SDavid Howells {
1416d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1417d84f4f99SDavid Howells 
1418d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1419d84f4f99SDavid Howells }
1420d84f4f99SDavid Howells 
1421d84f4f99SDavid Howells /*
142288e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1423275bb41eSDavid Howells  * fork check, ptrace check, etc.
1424275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
14253b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1426275bb41eSDavid Howells  */
1427275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1428275bb41eSDavid Howells 			 const struct task_struct *tsk2,
14291da177e4SLinus Torvalds 			 u32 perms)
14301da177e4SLinus Torvalds {
1431275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1432275bb41eSDavid Howells 	u32 sid1, sid2;
14331da177e4SLinus Torvalds 
1434275bb41eSDavid Howells 	rcu_read_lock();
1435275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1436275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1437275bb41eSDavid Howells 	rcu_read_unlock();
1438275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14413b11a1deSDavid Howells /*
14423b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
14433b11a1deSDavid Howells  * fork check, ptrace check, etc.
14443b11a1deSDavid Howells  * current is the actor and tsk2 is the target
14453b11a1deSDavid Howells  * - this uses current's subjective creds
14463b11a1deSDavid Howells  */
14473b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
14483b11a1deSDavid Howells 			    u32 perms)
14493b11a1deSDavid Howells {
14503b11a1deSDavid Howells 	u32 sid, tsid;
14513b11a1deSDavid Howells 
14523b11a1deSDavid Howells 	sid = current_sid();
14533b11a1deSDavid Howells 	tsid = task_sid(tsk);
14543b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
14553b11a1deSDavid Howells }
14563b11a1deSDavid Howells 
1457b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1458b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1459b68e418cSStephen Smalley #endif
1460b68e418cSStephen Smalley 
14611da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
14626a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
146306112163SEric Paris 			       int cap, int audit)
14641da177e4SLinus Torvalds {
14652bf49690SThomas Liu 	struct common_audit_data ad;
146606112163SEric Paris 	struct av_decision avd;
1467b68e418cSStephen Smalley 	u16 sclass;
14683699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1469b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
147006112163SEric Paris 	int rc;
14711da177e4SLinus Torvalds 
147250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
14731da177e4SLinus Torvalds 	ad.u.cap = cap;
14741da177e4SLinus Torvalds 
1475b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1476b68e418cSStephen Smalley 	case 0:
1477b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1478b68e418cSStephen Smalley 		break;
1479b68e418cSStephen Smalley 	case 1:
1480b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1481b68e418cSStephen Smalley 		break;
1482b68e418cSStephen Smalley 	default:
1483b68e418cSStephen Smalley 		printk(KERN_ERR
1484b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1485b68e418cSStephen Smalley 		BUG();
1486a35c6c83SEric Paris 		return -EINVAL;
1487b68e418cSStephen Smalley 	}
148806112163SEric Paris 
1489275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
14909ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
14919ade0cf4SEric Paris 		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
14929ade0cf4SEric Paris 		if (rc2)
14939ade0cf4SEric Paris 			return rc2;
14949ade0cf4SEric Paris 	}
149506112163SEric Paris 	return rc;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
14991da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
15001da177e4SLinus Torvalds 			   u32 perms)
15011da177e4SLinus Torvalds {
1502275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
15031da177e4SLinus Torvalds 
1504275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
15051da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
15091da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
15101da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
151188e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
15121da177e4SLinus Torvalds 			  struct inode *inode,
15131da177e4SLinus Torvalds 			  u32 perms,
15149ade0cf4SEric Paris 			  struct common_audit_data *adp,
15159ade0cf4SEric Paris 			  unsigned flags)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1518275bb41eSDavid Howells 	u32 sid;
15191da177e4SLinus Torvalds 
1520e0e81739SDavid Howells 	validate_creds(cred);
1521e0e81739SDavid Howells 
1522bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1523bbaca6c2SStephen Smalley 		return 0;
1524bbaca6c2SStephen Smalley 
152588e67f3bSDavid Howells 	sid = cred_sid(cred);
15261da177e4SLinus Torvalds 	isec = inode->i_security;
15271da177e4SLinus Torvalds 
152895f4efb2SLinus Torvalds 	return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
15321da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
15331da177e4SLinus Torvalds    pathname if needed. */
153488e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
15351da177e4SLinus Torvalds 				  struct dentry *dentry,
15361da177e4SLinus Torvalds 				  u32 av)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15392bf49690SThomas Liu 	struct common_audit_data ad;
154088e67f3bSDavid Howells 
154150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
15422875fa00SEric Paris 	ad.u.dentry = dentry;
15432875fa00SEric Paris 	return inode_has_perm(cred, inode, av, &ad, 0);
15442875fa00SEric Paris }
15452875fa00SEric Paris 
15462875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
15472875fa00SEric Paris    the path to help the auditing code to more easily generate the
15482875fa00SEric Paris    pathname if needed. */
15492875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
15502875fa00SEric Paris 				struct path *path,
15512875fa00SEric Paris 				u32 av)
15522875fa00SEric Paris {
15532875fa00SEric Paris 	struct inode *inode = path->dentry->d_inode;
15542875fa00SEric Paris 	struct common_audit_data ad;
15552875fa00SEric Paris 
155650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
15572875fa00SEric Paris 	ad.u.path = *path;
15589ade0cf4SEric Paris 	return inode_has_perm(cred, inode, av, &ad, 0);
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
15621da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
15631da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
15641da177e4SLinus Torvalds    check a particular permission to the file.
15651da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
15661da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
15671da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
15681da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
156988e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
15701da177e4SLinus Torvalds 			 struct file *file,
15711da177e4SLinus Torvalds 			 u32 av)
15721da177e4SLinus Torvalds {
15731da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1574496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
15752bf49690SThomas Liu 	struct common_audit_data ad;
157688e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
15771da177e4SLinus Torvalds 	int rc;
15781da177e4SLinus Torvalds 
157950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
1580f48b7399SEric Paris 	ad.u.path = file->f_path;
15811da177e4SLinus Torvalds 
1582275bb41eSDavid Howells 	if (sid != fsec->sid) {
1583275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
15841da177e4SLinus Torvalds 				  SECCLASS_FD,
15851da177e4SLinus Torvalds 				  FD__USE,
15861da177e4SLinus Torvalds 				  &ad);
15871da177e4SLinus Torvalds 		if (rc)
158888e67f3bSDavid Howells 			goto out;
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
159288e67f3bSDavid Howells 	rc = 0;
15931da177e4SLinus Torvalds 	if (av)
15949ade0cf4SEric Paris 		rc = inode_has_perm(cred, inode, av, &ad, 0);
15951da177e4SLinus Torvalds 
159688e67f3bSDavid Howells out:
159788e67f3bSDavid Howells 	return rc;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds /* Check whether a task can create a file. */
16011da177e4SLinus Torvalds static int may_create(struct inode *dir,
16021da177e4SLinus Torvalds 		      struct dentry *dentry,
16031da177e4SLinus Torvalds 		      u16 tclass)
16041da177e4SLinus Torvalds {
16055fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
16061da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
16071da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1608275bb41eSDavid Howells 	u32 sid, newsid;
16092bf49690SThomas Liu 	struct common_audit_data ad;
16101da177e4SLinus Torvalds 	int rc;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	dsec = dir->i_security;
16131da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
16141da177e4SLinus Torvalds 
1615275bb41eSDavid Howells 	sid = tsec->sid;
1616275bb41eSDavid Howells 	newsid = tsec->create_sid;
1617275bb41eSDavid Howells 
161850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1619a269434dSEric Paris 	ad.u.dentry = dentry;
16201da177e4SLinus Torvalds 
1621275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
16221da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
16231da177e4SLinus Torvalds 			  &ad);
16241da177e4SLinus Torvalds 	if (rc)
16251da177e4SLinus Torvalds 		return rc;
16261da177e4SLinus Torvalds 
1627cd89596fSDavid P. Quigley 	if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
1628cb1e922fSEric Paris 		rc = security_transition_sid(sid, dsec->sid, tclass,
1629cb1e922fSEric Paris 					     &dentry->d_name, &newsid);
16301da177e4SLinus Torvalds 		if (rc)
16311da177e4SLinus Torvalds 			return rc;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
1634275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
16351da177e4SLinus Torvalds 	if (rc)
16361da177e4SLinus Torvalds 		return rc;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
16391da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
16401da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
16434eb582cfSMichael LeMay /* Check whether a task can create a key. */
16444eb582cfSMichael LeMay static int may_create_key(u32 ksid,
16454eb582cfSMichael LeMay 			  struct task_struct *ctx)
16464eb582cfSMichael LeMay {
1647275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
16484eb582cfSMichael LeMay 
1649275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
16504eb582cfSMichael LeMay }
16514eb582cfSMichael LeMay 
16521da177e4SLinus Torvalds #define MAY_LINK	0
16531da177e4SLinus Torvalds #define MAY_UNLINK	1
16541da177e4SLinus Torvalds #define MAY_RMDIR	2
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
16571da177e4SLinus Torvalds static int may_link(struct inode *dir,
16581da177e4SLinus Torvalds 		    struct dentry *dentry,
16591da177e4SLinus Torvalds 		    int kind)
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds {
16621da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
16632bf49690SThomas Liu 	struct common_audit_data ad;
1664275bb41eSDavid Howells 	u32 sid = current_sid();
16651da177e4SLinus Torvalds 	u32 av;
16661da177e4SLinus Torvalds 	int rc;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	dsec = dir->i_security;
16691da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
16701da177e4SLinus Torvalds 
167150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1672a269434dSEric Paris 	ad.u.dentry = dentry;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	av = DIR__SEARCH;
16751da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1676275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
16771da177e4SLinus Torvalds 	if (rc)
16781da177e4SLinus Torvalds 		return rc;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	switch (kind) {
16811da177e4SLinus Torvalds 	case MAY_LINK:
16821da177e4SLinus Torvalds 		av = FILE__LINK;
16831da177e4SLinus Torvalds 		break;
16841da177e4SLinus Torvalds 	case MAY_UNLINK:
16851da177e4SLinus Torvalds 		av = FILE__UNLINK;
16861da177e4SLinus Torvalds 		break;
16871da177e4SLinus Torvalds 	case MAY_RMDIR:
16881da177e4SLinus Torvalds 		av = DIR__RMDIR;
16891da177e4SLinus Torvalds 		break;
16901da177e4SLinus Torvalds 	default:
1691744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1692744ba35eSEric Paris 			__func__, kind);
16931da177e4SLinus Torvalds 		return 0;
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
1696275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
16971da177e4SLinus Torvalds 	return rc;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
17011da177e4SLinus Torvalds 			     struct dentry *old_dentry,
17021da177e4SLinus Torvalds 			     struct inode *new_dir,
17031da177e4SLinus Torvalds 			     struct dentry *new_dentry)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
17062bf49690SThomas Liu 	struct common_audit_data ad;
1707275bb41eSDavid Howells 	u32 sid = current_sid();
17081da177e4SLinus Torvalds 	u32 av;
17091da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
17101da177e4SLinus Torvalds 	int rc;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
17131da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
17141da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
17151da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
17161da177e4SLinus Torvalds 
171750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
17181da177e4SLinus Torvalds 
1719a269434dSEric Paris 	ad.u.dentry = old_dentry;
1720275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
17211da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
17221da177e4SLinus Torvalds 	if (rc)
17231da177e4SLinus Torvalds 		return rc;
1724275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
17251da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
17261da177e4SLinus Torvalds 	if (rc)
17271da177e4SLinus Torvalds 		return rc;
17281da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1729275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
17301da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
17311da177e4SLinus Torvalds 		if (rc)
17321da177e4SLinus Torvalds 			return rc;
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 
1735a269434dSEric Paris 	ad.u.dentry = new_dentry;
17361da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
17371da177e4SLinus Torvalds 	if (new_dentry->d_inode)
17381da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1739275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
17401da177e4SLinus Torvalds 	if (rc)
17411da177e4SLinus Torvalds 		return rc;
17421da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
17431da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
17441da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1745275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
17461da177e4SLinus Torvalds 				  new_isec->sclass,
17471da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
17481da177e4SLinus Torvalds 		if (rc)
17491da177e4SLinus Torvalds 			return rc;
17501da177e4SLinus Torvalds 	}
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	return 0;
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
175688e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
17571da177e4SLinus Torvalds 			       struct super_block *sb,
17581da177e4SLinus Torvalds 			       u32 perms,
17592bf49690SThomas Liu 			       struct common_audit_data *ad)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
176288e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	sbsec = sb->s_security;
1765275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
17691da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
17701da177e4SLinus Torvalds {
17711da177e4SLinus Torvalds 	u32 av = 0;
17721da177e4SLinus Torvalds 
1773dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
17741da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17751da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
17761da177e4SLinus Torvalds 		if (mask & MAY_READ)
17771da177e4SLinus Torvalds 			av |= FILE__READ;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
17801da177e4SLinus Torvalds 			av |= FILE__APPEND;
17811da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
17821da177e4SLinus Torvalds 			av |= FILE__WRITE;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	} else {
17851da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17861da177e4SLinus Torvalds 			av |= DIR__SEARCH;
17871da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
17881da177e4SLinus Torvalds 			av |= DIR__WRITE;
17891da177e4SLinus Torvalds 		if (mask & MAY_READ)
17901da177e4SLinus Torvalds 			av |= DIR__READ;
17911da177e4SLinus Torvalds 	}
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	return av;
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
17971da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	u32 av = 0;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
18021da177e4SLinus Torvalds 		av |= FILE__READ;
18031da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
18041da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
18051da177e4SLinus Torvalds 			av |= FILE__APPEND;
18061da177e4SLinus Torvalds 		else
18071da177e4SLinus Torvalds 			av |= FILE__WRITE;
18081da177e4SLinus Torvalds 	}
18090794c66dSStephen Smalley 	if (!av) {
18100794c66dSStephen Smalley 		/*
18110794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
18120794c66dSStephen Smalley 		 */
18130794c66dSStephen Smalley 		av = FILE__IOCTL;
18140794c66dSStephen Smalley 	}
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	return av;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18198b6a5a37SEric Paris /*
18208b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
18218b6a5a37SEric Paris  * open permission.
18228b6a5a37SEric Paris  */
18238b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
18248b6a5a37SEric Paris {
18258b6a5a37SEric Paris 	u32 av = file_to_av(file);
18268b6a5a37SEric Paris 
182749b7b8deSEric Paris 	if (selinux_policycap_openperm)
18288b6a5a37SEric Paris 		av |= FILE__OPEN;
182949b7b8deSEric Paris 
18308b6a5a37SEric Paris 	return av;
18318b6a5a37SEric Paris }
18328b6a5a37SEric Paris 
18331da177e4SLinus Torvalds /* Hook functions begin here. */
18341da177e4SLinus Torvalds 
18359e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
1836006ebb40SStephen Smalley 				     unsigned int mode)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	int rc;
18391da177e4SLinus Torvalds 
18409e48858fSIngo Molnar 	rc = cap_ptrace_access_check(child, mode);
18411da177e4SLinus Torvalds 	if (rc)
18421da177e4SLinus Torvalds 		return rc;
18431da177e4SLinus Torvalds 
184469f594a3SEric Paris 	if (mode & PTRACE_MODE_READ) {
1845275bb41eSDavid Howells 		u32 sid = current_sid();
1846275bb41eSDavid Howells 		u32 csid = task_sid(child);
1847275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1848006ebb40SStephen Smalley 	}
1849006ebb40SStephen Smalley 
18503b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
18515cd9c58fSDavid Howells }
18525cd9c58fSDavid Howells 
18535cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
18545cd9c58fSDavid Howells {
18555cd9c58fSDavid Howells 	int rc;
18565cd9c58fSDavid Howells 
1857200ac532SEric Paris 	rc = cap_ptrace_traceme(parent);
18585cd9c58fSDavid Howells 	if (rc)
18595cd9c58fSDavid Howells 		return rc;
18605cd9c58fSDavid Howells 
18615cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
18651da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	int error;
18681da177e4SLinus Torvalds 
18693b11a1deSDavid Howells 	error = current_has_perm(target, PROCESS__GETCAP);
18701da177e4SLinus Torvalds 	if (error)
18711da177e4SLinus Torvalds 		return error;
18721da177e4SLinus Torvalds 
1873200ac532SEric Paris 	return cap_capget(target, effective, inheritable, permitted);
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
1876d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
1877d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
187815a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
187915a2460eSDavid Howells 			  const kernel_cap_t *permitted)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	int error;
18821da177e4SLinus Torvalds 
1883200ac532SEric Paris 	error = cap_capset(new, old,
1884d84f4f99SDavid Howells 				      effective, inheritable, permitted);
18851da177e4SLinus Torvalds 	if (error)
18861da177e4SLinus Torvalds 		return error;
18871da177e4SLinus Torvalds 
1888d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18915626d3e8SJames Morris /*
18925626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
18935626d3e8SJames Morris  * which was removed).
18945626d3e8SJames Morris  *
18955626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
18965626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
18975626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
18985626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
18995626d3e8SJames Morris  */
19005626d3e8SJames Morris 
19016a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
19026a9de491SEric Paris 			   int cap, int audit)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	int rc;
19051da177e4SLinus Torvalds 
19066a9de491SEric Paris 	rc = cap_capable(cred, ns, cap, audit);
19071da177e4SLinus Torvalds 	if (rc)
19081da177e4SLinus Torvalds 		return rc;
19091da177e4SLinus Torvalds 
19106a9de491SEric Paris 	return cred_has_capability(cred, cap, audit);
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
19141da177e4SLinus Torvalds {
191588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
19161da177e4SLinus Torvalds 	int rc = 0;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (!sb)
19191da177e4SLinus Torvalds 		return 0;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	switch (cmds) {
19221da177e4SLinus Torvalds 	case Q_SYNC:
19231da177e4SLinus Torvalds 	case Q_QUOTAON:
19241da177e4SLinus Torvalds 	case Q_QUOTAOFF:
19251da177e4SLinus Torvalds 	case Q_SETINFO:
19261da177e4SLinus Torvalds 	case Q_SETQUOTA:
192788e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
19281da177e4SLinus Torvalds 		break;
19291da177e4SLinus Torvalds 	case Q_GETFMT:
19301da177e4SLinus Torvalds 	case Q_GETINFO:
19311da177e4SLinus Torvalds 	case Q_GETQUOTA:
193288e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
19331da177e4SLinus Torvalds 		break;
19341da177e4SLinus Torvalds 	default:
19351da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
19361da177e4SLinus Torvalds 		break;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 	return rc;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
19421da177e4SLinus Torvalds {
194388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
194488e67f3bSDavid Howells 
19452875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
194812b3052cSEric Paris static int selinux_syslog(int type)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds 	int rc;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	switch (type) {
1953d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
1954d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
19551da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
19561da177e4SLinus Torvalds 		break;
1957d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
1958d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
1959d78ca3cdSKees Cook 	/* Set level of messages printed to console */
1960d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
19611da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
19621da177e4SLinus Torvalds 		break;
1963d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLOSE:	/* Close log */
1964d78ca3cdSKees Cook 	case SYSLOG_ACTION_OPEN:	/* Open log */
1965d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ:	/* Read from log */
1966d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_CLEAR:	/* Read/clear last kernel messages */
1967d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLEAR:	/* Clear ring buffer */
19681da177e4SLinus Torvalds 	default:
19691da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
19701da177e4SLinus Torvalds 		break;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds 	return rc;
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds /*
19761da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
19771da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
19781da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
19791da177e4SLinus Torvalds  *
19801da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
19811da177e4SLinus Torvalds  * processes that allocate mappings.
19821da177e4SLinus Torvalds  */
198334b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
19861da177e4SLinus Torvalds 
19876a9de491SEric Paris 	rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
19883699c53cSDavid Howells 			     SECURITY_CAP_NOAUDIT);
19891da177e4SLinus Torvalds 	if (rc == 0)
19901da177e4SLinus Torvalds 		cap_sys_admin = 1;
19911da177e4SLinus Torvalds 
199234b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds /* binprm security operations */
19961da177e4SLinus Torvalds 
1997a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
19981da177e4SLinus Torvalds {
1999a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2000a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
20011da177e4SLinus Torvalds 	struct inode_security_struct *isec;
20022bf49690SThomas Liu 	struct common_audit_data ad;
2003496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
20041da177e4SLinus Torvalds 	int rc;
20051da177e4SLinus Torvalds 
2006200ac532SEric Paris 	rc = cap_bprm_set_creds(bprm);
20071da177e4SLinus Torvalds 	if (rc)
20081da177e4SLinus Torvalds 		return rc;
20091da177e4SLinus Torvalds 
2010a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2011a6f76f23SDavid Howells 	 * the script interpreter */
2012a6f76f23SDavid Howells 	if (bprm->cred_prepared)
20131da177e4SLinus Torvalds 		return 0;
20141da177e4SLinus Torvalds 
2015a6f76f23SDavid Howells 	old_tsec = current_security();
2016a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
20171da177e4SLinus Torvalds 	isec = inode->i_security;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	/* Default to the current task SID. */
2020a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2021a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
20221da177e4SLinus Torvalds 
202328eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2024a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2025a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2026a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
20271da177e4SLinus Torvalds 
2028a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2029a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
20301da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2031a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2032259e5e6cSAndy Lutomirski 
2033259e5e6cSAndy Lutomirski 		/*
2034259e5e6cSAndy Lutomirski 		 * Minimize confusion: if no_new_privs and a transition is
2035259e5e6cSAndy Lutomirski 		 * explicitly requested, then fail the exec.
2036259e5e6cSAndy Lutomirski 		 */
2037259e5e6cSAndy Lutomirski 		if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2038259e5e6cSAndy Lutomirski 			return -EPERM;
20391da177e4SLinus Torvalds 	} else {
20401da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2041a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2042652bb9b0SEric Paris 					     SECCLASS_PROCESS, NULL,
2043652bb9b0SEric Paris 					     &new_tsec->sid);
20441da177e4SLinus Torvalds 		if (rc)
20451da177e4SLinus Torvalds 			return rc;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 
204850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
2049f48b7399SEric Paris 	ad.u.path = bprm->file->f_path;
20501da177e4SLinus Torvalds 
2051259e5e6cSAndy Lutomirski 	if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2052259e5e6cSAndy Lutomirski 	    (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
2053a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->sid;
20541da177e4SLinus Torvalds 
2055a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2056a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
20571da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
20581da177e4SLinus Torvalds 		if (rc)
20591da177e4SLinus Torvalds 			return rc;
20601da177e4SLinus Torvalds 	} else {
20611da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2062a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
20631da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
20641da177e4SLinus Torvalds 		if (rc)
20651da177e4SLinus Torvalds 			return rc;
20661da177e4SLinus Torvalds 
2067a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
20681da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
20691da177e4SLinus Torvalds 		if (rc)
20701da177e4SLinus Torvalds 			return rc;
20711da177e4SLinus Torvalds 
2072a6f76f23SDavid Howells 		/* Check for shared state */
2073a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2074a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2075a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2076a6f76f23SDavid Howells 					  NULL);
2077a6f76f23SDavid Howells 			if (rc)
2078a6f76f23SDavid Howells 				return -EPERM;
20791da177e4SLinus Torvalds 		}
20801da177e4SLinus Torvalds 
2081a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2082a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2083a6f76f23SDavid Howells 		if (bprm->unsafe &
2084a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2085a6f76f23SDavid Howells 			struct task_struct *tracer;
2086a6f76f23SDavid Howells 			struct task_security_struct *sec;
2087a6f76f23SDavid Howells 			u32 ptsid = 0;
2088a6f76f23SDavid Howells 
2089a6f76f23SDavid Howells 			rcu_read_lock();
209006d98473STejun Heo 			tracer = ptrace_parent(current);
2091a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2092a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2093a6f76f23SDavid Howells 				ptsid = sec->sid;
2094a6f76f23SDavid Howells 			}
2095a6f76f23SDavid Howells 			rcu_read_unlock();
2096a6f76f23SDavid Howells 
2097a6f76f23SDavid Howells 			if (ptsid != 0) {
2098a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2099a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2100a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2101a6f76f23SDavid Howells 				if (rc)
2102a6f76f23SDavid Howells 					return -EPERM;
2103a6f76f23SDavid Howells 			}
2104a6f76f23SDavid Howells 		}
2105a6f76f23SDavid Howells 
2106a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2107a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2108a6f76f23SDavid Howells 	}
2109a6f76f23SDavid Howells 
21101da177e4SLinus Torvalds 	return 0;
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
21141da177e4SLinus Torvalds {
21155fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
2116275bb41eSDavid Howells 	u32 sid, osid;
21171da177e4SLinus Torvalds 	int atsecure = 0;
21181da177e4SLinus Torvalds 
2119275bb41eSDavid Howells 	sid = tsec->sid;
2120275bb41eSDavid Howells 	osid = tsec->osid;
2121275bb41eSDavid Howells 
2122275bb41eSDavid Howells 	if (osid != sid) {
21231da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
21241da177e4SLinus Torvalds 		   the noatsecure permission is granted between
21251da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2126275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
21271da177e4SLinus Torvalds 					SECCLASS_PROCESS,
21281da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
21291da177e4SLinus Torvalds 	}
21301da177e4SLinus Torvalds 
2131200ac532SEric Paris 	return (atsecure || cap_bprm_secureexec(bprm));
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
2134c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2135c3c073f8SAl Viro {
2136c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2137c3c073f8SAl Viro }
2138c3c073f8SAl Viro 
21391da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2140745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2141745ca247SDavid Howells 					    struct files_struct *files)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2144b20c8122SStephen Smalley 	struct tty_struct *tty;
214524ec839cSPeter Zijlstra 	int drop_tty = 0;
2146c3c073f8SAl Viro 	unsigned n;
21471da177e4SLinus Torvalds 
214824ec839cSPeter Zijlstra 	tty = get_current_tty();
21491da177e4SLinus Torvalds 	if (tty) {
2150ee2ffa0dSNick Piggin 		spin_lock(&tty_files_lock);
215137dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2152d996b62aSNick Piggin 			struct tty_file_private *file_priv;
215337dd0bd0SEric Paris 
21541da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
2155602a8dd6SEric Paris 			   Use path_has_perm on the tty path directly rather
21561da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
21571da177e4SLinus Torvalds 			   file may belong to another process and we are only
21581da177e4SLinus Torvalds 			   interested in the inode-based check here. */
2159d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2160d996b62aSNick Piggin 						struct tty_file_private, list);
2161d996b62aSNick Piggin 			file = file_priv->file;
2162602a8dd6SEric Paris 			if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE))
216324ec839cSPeter Zijlstra 				drop_tty = 1;
21641da177e4SLinus Torvalds 		}
2165ee2ffa0dSNick Piggin 		spin_unlock(&tty_files_lock);
2166452a00d2SAlan Cox 		tty_kref_put(tty);
21671da177e4SLinus Torvalds 	}
216898a27ba4SEric W. Biederman 	/* Reset controlling tty. */
216998a27ba4SEric W. Biederman 	if (drop_tty)
217098a27ba4SEric W. Biederman 		no_tty();
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2173c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2174c3c073f8SAl Viro 	if (!n) /* none found? */
2175c3c073f8SAl Viro 		return;
21761da177e4SLinus Torvalds 
2177c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
217845525b26SAl Viro 	if (IS_ERR(devnull))
217945525b26SAl Viro 		devnull = NULL;
2180c3c073f8SAl Viro 	/* replace all the matching ones with this */
2181c3c073f8SAl Viro 	do {
218245525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2183c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
218445525b26SAl Viro 	if (devnull)
2185c3c073f8SAl Viro 		fput(devnull);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds /*
2189a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
21901da177e4SLinus Torvalds  */
2191a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
21921da177e4SLinus Torvalds {
2193a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
21941da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
21951da177e4SLinus Torvalds 	int rc, i;
21961da177e4SLinus Torvalds 
2197a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2198a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
21991da177e4SLinus Torvalds 		return;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2202a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
22031da177e4SLinus Torvalds 
2204a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2205a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2206a6f76f23SDavid Howells 
2207a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2208a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2209a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2210a6f76f23SDavid Howells 	 *
2211a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2212a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2213a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2214a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2215a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2216a6f76f23SDavid Howells 	 */
2217a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2218a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2219a6f76f23SDavid Howells 	if (rc) {
2220eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2221eb2d55a3SOleg Nesterov 		task_lock(current);
2222a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2223a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2224a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2225a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2226a6f76f23SDavid Howells 		}
2227eb2d55a3SOleg Nesterov 		task_unlock(current);
2228eb2d55a3SOleg Nesterov 		update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2229a6f76f23SDavid Howells 	}
2230a6f76f23SDavid Howells }
2231a6f76f23SDavid Howells 
2232a6f76f23SDavid Howells /*
2233a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2234a6f76f23SDavid Howells  * due to exec
2235a6f76f23SDavid Howells  */
2236a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2237a6f76f23SDavid Howells {
2238a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2239a6f76f23SDavid Howells 	struct itimerval itimer;
2240a6f76f23SDavid Howells 	u32 osid, sid;
2241a6f76f23SDavid Howells 	int rc, i;
2242a6f76f23SDavid Howells 
2243a6f76f23SDavid Howells 	osid = tsec->osid;
2244a6f76f23SDavid Howells 	sid = tsec->sid;
2245a6f76f23SDavid Howells 
2246a6f76f23SDavid Howells 	if (sid == osid)
2247a6f76f23SDavid Howells 		return;
2248a6f76f23SDavid Howells 
2249a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2250a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2251a6f76f23SDavid Howells 	 * flush and unblock signals.
2252a6f76f23SDavid Howells 	 *
2253a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2254a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2255a6f76f23SDavid Howells 	 */
2256a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
22571da177e4SLinus Torvalds 	if (rc) {
22581da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
22591da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
22601da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
22611da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
22623bcac026SDavid Howells 		if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
22633bcac026SDavid Howells 			__flush_signals(current);
22641da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
22651da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
22663bcac026SDavid Howells 		}
22671da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
22681da177e4SLinus Torvalds 	}
22691da177e4SLinus Torvalds 
2270a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2271a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2272ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
22730b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2274ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds /* superblock security operations */
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
22801da177e4SLinus Torvalds {
22811da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds 	superblock_free_security(sb);
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
22901da177e4SLinus Torvalds {
22911da177e4SLinus Torvalds 	if (plen > olen)
22921da177e4SLinus Torvalds 		return 0;
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
22981da177e4SLinus Torvalds {
2299832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2300832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2301832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
230211689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
230311689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
23071da177e4SLinus Torvalds {
23081da177e4SLinus Torvalds 	if (!*first) {
23091da177e4SLinus Torvalds 		**to = ',';
23101da177e4SLinus Torvalds 		*to += 1;
23113528a953SCory Olmo 	} else
23121da177e4SLinus Torvalds 		*first = 0;
23131da177e4SLinus Torvalds 	memcpy(*to, from, len);
23141da177e4SLinus Torvalds 	*to += len;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23173528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
23183528a953SCory Olmo 				       int len)
23193528a953SCory Olmo {
23203528a953SCory Olmo 	int current_size = 0;
23213528a953SCory Olmo 
23223528a953SCory Olmo 	if (!*first) {
23233528a953SCory Olmo 		**to = '|';
23243528a953SCory Olmo 		*to += 1;
2325828dfe1dSEric Paris 	} else
23263528a953SCory Olmo 		*first = 0;
23273528a953SCory Olmo 
23283528a953SCory Olmo 	while (current_size < len) {
23293528a953SCory Olmo 		if (*from != '"') {
23303528a953SCory Olmo 			**to = *from;
23313528a953SCory Olmo 			*to += 1;
23323528a953SCory Olmo 		}
23333528a953SCory Olmo 		from += 1;
23343528a953SCory Olmo 		current_size += 1;
23353528a953SCory Olmo 	}
23363528a953SCory Olmo }
23373528a953SCory Olmo 
2338e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
23391da177e4SLinus Torvalds {
23401da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
23411da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
23421da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
23433528a953SCory Olmo 	int open_quote = 0;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	in_curr = orig;
23461da177e4SLinus Torvalds 	sec_curr = copy;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
23491da177e4SLinus Torvalds 	if (!nosec) {
23501da177e4SLinus Torvalds 		rc = -ENOMEM;
23511da177e4SLinus Torvalds 		goto out;
23521da177e4SLinus Torvalds 	}
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	nosec_save = nosec;
23551da177e4SLinus Torvalds 	fnosec = fsec = 1;
23561da177e4SLinus Torvalds 	in_save = in_end = orig;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	do {
23593528a953SCory Olmo 		if (*in_end == '"')
23603528a953SCory Olmo 			open_quote = !open_quote;
23613528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
23623528a953SCory Olmo 				*in_end == '\0') {
23631da177e4SLinus Torvalds 			int len = in_end - in_curr;
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
23663528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
23671da177e4SLinus Torvalds 			else
23681da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 			in_curr = in_end + 1;
23711da177e4SLinus Torvalds 		}
23721da177e4SLinus Torvalds 	} while (*in_end++);
23731da177e4SLinus Torvalds 
23746931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2375da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
23761da177e4SLinus Torvalds out:
23771da177e4SLinus Torvalds 	return rc;
23781da177e4SLinus Torvalds }
23791da177e4SLinus Torvalds 
2380026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2381026eb167SEric Paris {
2382026eb167SEric Paris 	int rc, i, *flags;
2383026eb167SEric Paris 	struct security_mnt_opts opts;
2384026eb167SEric Paris 	char *secdata, **mount_options;
2385026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2386026eb167SEric Paris 
2387026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2388026eb167SEric Paris 		return 0;
2389026eb167SEric Paris 
2390026eb167SEric Paris 	if (!data)
2391026eb167SEric Paris 		return 0;
2392026eb167SEric Paris 
2393026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2394026eb167SEric Paris 		return 0;
2395026eb167SEric Paris 
2396026eb167SEric Paris 	security_init_mnt_opts(&opts);
2397026eb167SEric Paris 	secdata = alloc_secdata();
2398026eb167SEric Paris 	if (!secdata)
2399026eb167SEric Paris 		return -ENOMEM;
2400026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2401026eb167SEric Paris 	if (rc)
2402026eb167SEric Paris 		goto out_free_secdata;
2403026eb167SEric Paris 
2404026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2405026eb167SEric Paris 	if (rc)
2406026eb167SEric Paris 		goto out_free_secdata;
2407026eb167SEric Paris 
2408026eb167SEric Paris 	mount_options = opts.mnt_opts;
2409026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2410026eb167SEric Paris 
2411026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2412026eb167SEric Paris 		u32 sid;
2413026eb167SEric Paris 		size_t len;
2414026eb167SEric Paris 
2415026eb167SEric Paris 		if (flags[i] == SE_SBLABELSUPP)
2416026eb167SEric Paris 			continue;
2417026eb167SEric Paris 		len = strlen(mount_options[i]);
2418026eb167SEric Paris 		rc = security_context_to_sid(mount_options[i], len, &sid);
2419026eb167SEric Paris 		if (rc) {
2420026eb167SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
2421026eb167SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
2422026eb167SEric Paris 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2423026eb167SEric Paris 			goto out_free_opts;
2424026eb167SEric Paris 		}
2425026eb167SEric Paris 		rc = -EINVAL;
2426026eb167SEric Paris 		switch (flags[i]) {
2427026eb167SEric Paris 		case FSCONTEXT_MNT:
2428026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2429026eb167SEric Paris 				goto out_bad_option;
2430026eb167SEric Paris 			break;
2431026eb167SEric Paris 		case CONTEXT_MNT:
2432026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2433026eb167SEric Paris 				goto out_bad_option;
2434026eb167SEric Paris 			break;
2435026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2436026eb167SEric Paris 			struct inode_security_struct *root_isec;
2437026eb167SEric Paris 			root_isec = sb->s_root->d_inode->i_security;
2438026eb167SEric Paris 
2439026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2440026eb167SEric Paris 				goto out_bad_option;
2441026eb167SEric Paris 			break;
2442026eb167SEric Paris 		}
2443026eb167SEric Paris 		case DEFCONTEXT_MNT:
2444026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2445026eb167SEric Paris 				goto out_bad_option;
2446026eb167SEric Paris 			break;
2447026eb167SEric Paris 		default:
2448026eb167SEric Paris 			goto out_free_opts;
2449026eb167SEric Paris 		}
2450026eb167SEric Paris 	}
2451026eb167SEric Paris 
2452026eb167SEric Paris 	rc = 0;
2453026eb167SEric Paris out_free_opts:
2454026eb167SEric Paris 	security_free_mnt_opts(&opts);
2455026eb167SEric Paris out_free_secdata:
2456026eb167SEric Paris 	free_secdata(secdata);
2457026eb167SEric Paris 	return rc;
2458026eb167SEric Paris out_bad_option:
2459026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
2460026eb167SEric Paris 	       "during remount (dev %s, type=%s)\n", sb->s_id,
2461026eb167SEric Paris 	       sb->s_type->name);
2462026eb167SEric Paris 	goto out_free_opts;
2463026eb167SEric Paris }
2464026eb167SEric Paris 
246512204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
24661da177e4SLinus Torvalds {
246788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
24682bf49690SThomas Liu 	struct common_audit_data ad;
24691da177e4SLinus Torvalds 	int rc;
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
24721da177e4SLinus Torvalds 	if (rc)
24731da177e4SLinus Torvalds 		return rc;
24741da177e4SLinus Torvalds 
247574192246SJames Morris 	/* Allow all mounts performed by the kernel */
247674192246SJames Morris 	if (flags & MS_KERNMOUNT)
247774192246SJames Morris 		return 0;
247874192246SJames Morris 
247950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2480a269434dSEric Paris 	ad.u.dentry = sb->s_root;
248188e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
24821da177e4SLinus Torvalds }
24831da177e4SLinus Torvalds 
2484726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
24851da177e4SLinus Torvalds {
248688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
24872bf49690SThomas Liu 	struct common_audit_data ad;
24881da177e4SLinus Torvalds 
248950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2490a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
249188e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
2494808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
2495b5266eb4SAl Viro 			 struct path *path,
2496808d4e3cSAl Viro 			 const char *type,
24971da177e4SLinus Torvalds 			 unsigned long flags,
24981da177e4SLinus Torvalds 			 void *data)
24991da177e4SLinus Torvalds {
250088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2503d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
25041da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
25051da177e4SLinus Torvalds 	else
25062875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
25101da177e4SLinus Torvalds {
251188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25121da177e4SLinus Torvalds 
251388e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
25141da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds /* inode security operations */
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
25201da177e4SLinus Torvalds {
25211da177e4SLinus Torvalds 	return inode_alloc_security(inode);
25221da177e4SLinus Torvalds }
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
25251da177e4SLinus Torvalds {
25261da177e4SLinus Torvalds 	inode_free_security(inode);
25271da177e4SLinus Torvalds }
25281da177e4SLinus Torvalds 
25295e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
25302a7dba39SEric Paris 				       const struct qstr *qstr, char **name,
25312a7dba39SEric Paris 				       void **value, size_t *len)
25325e41ff9eSStephen Smalley {
25335fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
25345e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
25355e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2536275bb41eSDavid Howells 	u32 sid, newsid, clen;
25375e41ff9eSStephen Smalley 	int rc;
2538570bc1c2SStephen Smalley 	char *namep = NULL, *context;
25395e41ff9eSStephen Smalley 
25405e41ff9eSStephen Smalley 	dsec = dir->i_security;
25415e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
25425e41ff9eSStephen Smalley 
2543275bb41eSDavid Howells 	sid = tsec->sid;
25445e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2545275bb41eSDavid Howells 
2546415103f9SEric Paris 	if ((sbsec->flags & SE_SBINITIALIZED) &&
2547415103f9SEric Paris 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2548415103f9SEric Paris 		newsid = sbsec->mntpoint_sid;
2549415103f9SEric Paris 	else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
2550275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
25515e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
2552652bb9b0SEric Paris 					     qstr, &newsid);
25535e41ff9eSStephen Smalley 		if (rc) {
25545e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
25555e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
25565e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2557dd6f953aSHarvey Harrison 			       __func__,
25585e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
25595e41ff9eSStephen Smalley 			return rc;
25605e41ff9eSStephen Smalley 		}
25615e41ff9eSStephen Smalley 	}
25625e41ff9eSStephen Smalley 
2563296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
25640d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2565296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2566296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2567296fddf7SEric Paris 		isec->sid = newsid;
2568296fddf7SEric Paris 		isec->initialized = 1;
2569296fddf7SEric Paris 	}
25705e41ff9eSStephen Smalley 
2571cd89596fSDavid P. Quigley 	if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
257225a74f3bSStephen Smalley 		return -EOPNOTSUPP;
257325a74f3bSStephen Smalley 
2574570bc1c2SStephen Smalley 	if (name) {
2575a02fe132SJosef Bacik 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
25765e41ff9eSStephen Smalley 		if (!namep)
25775e41ff9eSStephen Smalley 			return -ENOMEM;
25785e41ff9eSStephen Smalley 		*name = namep;
2579570bc1c2SStephen Smalley 	}
25805e41ff9eSStephen Smalley 
2581570bc1c2SStephen Smalley 	if (value && len) {
258212b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
25835e41ff9eSStephen Smalley 		if (rc) {
25845e41ff9eSStephen Smalley 			kfree(namep);
25855e41ff9eSStephen Smalley 			return rc;
25865e41ff9eSStephen Smalley 		}
25875e41ff9eSStephen Smalley 		*value = context;
2588570bc1c2SStephen Smalley 		*len = clen;
2589570bc1c2SStephen Smalley 	}
25905e41ff9eSStephen Smalley 
25915e41ff9eSStephen Smalley 	return 0;
25925e41ff9eSStephen Smalley }
25935e41ff9eSStephen Smalley 
25944acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
25951da177e4SLinus Torvalds {
25961da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
26001da177e4SLinus Torvalds {
26011da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
26021da177e4SLinus Torvalds }
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
26051da177e4SLinus Torvalds {
26061da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
26101da177e4SLinus Torvalds {
26111da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
261418bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
26151da177e4SLinus Torvalds {
26161da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
26171da177e4SLinus Torvalds }
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
26201da177e4SLinus Torvalds {
26211da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
26301da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
26311da177e4SLinus Torvalds {
26321da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
26331da177e4SLinus Torvalds }
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
26361da177e4SLinus Torvalds {
263788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
263888e67f3bSDavid Howells 
26392875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
26401da177e4SLinus Torvalds }
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
26431da177e4SLinus Torvalds {
264488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26451da177e4SLinus Torvalds 
26462875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
26471da177e4SLinus Torvalds }
26481da177e4SLinus Torvalds 
2649d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2650d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2651d4cf970dSEric Paris 					   unsigned flags)
2652d4cf970dSEric Paris {
2653d4cf970dSEric Paris 	struct common_audit_data ad;
2654d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2655d4cf970dSEric Paris 	int rc;
2656d4cf970dSEric Paris 
265750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2658d4cf970dSEric Paris 	ad.u.inode = inode;
2659d4cf970dSEric Paris 
2660d4cf970dSEric Paris 	rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2661d4cf970dSEric Paris 			    audited, denied, &ad, flags);
2662d4cf970dSEric Paris 	if (rc)
2663d4cf970dSEric Paris 		return rc;
2664d4cf970dSEric Paris 	return 0;
2665d4cf970dSEric Paris }
2666d4cf970dSEric Paris 
2667e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
26681da177e4SLinus Torvalds {
266988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2670b782e0a6SEric Paris 	u32 perms;
2671b782e0a6SEric Paris 	bool from_access;
2672cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
26732e334057SEric Paris 	struct inode_security_struct *isec;
26742e334057SEric Paris 	u32 sid;
26752e334057SEric Paris 	struct av_decision avd;
26762e334057SEric Paris 	int rc, rc2;
26772e334057SEric Paris 	u32 audited, denied;
26781da177e4SLinus Torvalds 
2679b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2680d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2681d09ca739SEric Paris 
26821da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2683b782e0a6SEric Paris 	if (!mask)
26841da177e4SLinus Torvalds 		return 0;
26851da177e4SLinus Torvalds 
26862e334057SEric Paris 	validate_creds(cred);
2687b782e0a6SEric Paris 
26882e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
26892e334057SEric Paris 		return 0;
2690b782e0a6SEric Paris 
2691b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2692b782e0a6SEric Paris 
26932e334057SEric Paris 	sid = cred_sid(cred);
26942e334057SEric Paris 	isec = inode->i_security;
26952e334057SEric Paris 
26962e334057SEric Paris 	rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
26972e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
26982e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
26992e334057SEric Paris 				     &denied);
27002e334057SEric Paris 	if (likely(!audited))
27012e334057SEric Paris 		return rc;
27022e334057SEric Paris 
2703d4cf970dSEric Paris 	rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
27042e334057SEric Paris 	if (rc2)
27052e334057SEric Paris 		return rc2;
27062e334057SEric Paris 	return rc;
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
27101da177e4SLinus Torvalds {
271188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2712bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
271395dbf739SEric Paris 	__u32 av = FILE__WRITE;
27141da177e4SLinus Torvalds 
2715bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2716bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
2717bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2718bc6a6008SAmerigo Wang 			      ATTR_FORCE);
2719bc6a6008SAmerigo Wang 		if (!ia_valid)
27201da177e4SLinus Torvalds 			return 0;
2721bc6a6008SAmerigo Wang 	}
27221da177e4SLinus Torvalds 
2723bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2724bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
27252875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
27261da177e4SLinus Torvalds 
27273d2195c3SEric Paris 	if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
272895dbf739SEric Paris 		av |= FILE__OPEN;
272995dbf739SEric Paris 
273095dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
27311da177e4SLinus Torvalds }
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
27341da177e4SLinus Torvalds {
273588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27362875fa00SEric Paris 	struct path path;
273788e67f3bSDavid Howells 
27382875fa00SEric Paris 	path.dentry = dentry;
27392875fa00SEric Paris 	path.mnt = mnt;
27402875fa00SEric Paris 
27412875fa00SEric Paris 	return path_has_perm(cred, &path, FILE__GETATTR);
27421da177e4SLinus Torvalds }
27431da177e4SLinus Torvalds 
27448f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2745b5376771SSerge E. Hallyn {
274688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
274788e67f3bSDavid Howells 
2748b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2749b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2750b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2751b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2752b5376771SSerge E. Hallyn 				return -EPERM;
2753b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2754b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2755b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2756b5376771SSerge E. Hallyn 			return -EPERM;
2757b5376771SSerge E. Hallyn 		}
2758b5376771SSerge E. Hallyn 	}
2759b5376771SSerge E. Hallyn 
2760b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2761b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
27622875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__SETATTR);
2763b5376771SSerge E. Hallyn }
2764b5376771SSerge E. Hallyn 
27658f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
27668f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
27671da177e4SLinus Torvalds {
27681da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
27691da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27701da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
27712bf49690SThomas Liu 	struct common_audit_data ad;
2772275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
27731da177e4SLinus Torvalds 	int rc = 0;
27741da177e4SLinus Torvalds 
2775b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2776b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
2779cd89596fSDavid P. Quigley 	if (!(sbsec->flags & SE_SBLABELSUPP))
27801da177e4SLinus Torvalds 		return -EOPNOTSUPP;
27811da177e4SLinus Torvalds 
27822e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
27831da177e4SLinus Torvalds 		return -EPERM;
27841da177e4SLinus Torvalds 
278550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2786a269434dSEric Paris 	ad.u.dentry = dentry;
27871da177e4SLinus Torvalds 
2788275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
27891da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
27901da177e4SLinus Torvalds 	if (rc)
27911da177e4SLinus Torvalds 		return rc;
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
279412b29f34SStephen Smalley 	if (rc == -EINVAL) {
2795d6ea83ecSEric Paris 		if (!capable(CAP_MAC_ADMIN)) {
2796d6ea83ecSEric Paris 			struct audit_buffer *ab;
2797d6ea83ecSEric Paris 			size_t audit_size;
2798d6ea83ecSEric Paris 			const char *str;
2799d6ea83ecSEric Paris 
2800d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
2801d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
2802e3fea3f7SAl Viro 			if (value) {
2803d6ea83ecSEric Paris 				str = value;
2804d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
2805d6ea83ecSEric Paris 					audit_size = size - 1;
2806d6ea83ecSEric Paris 				else
2807d6ea83ecSEric Paris 					audit_size = size;
2808e3fea3f7SAl Viro 			} else {
2809e3fea3f7SAl Viro 				str = "";
2810e3fea3f7SAl Viro 				audit_size = 0;
2811e3fea3f7SAl Viro 			}
2812d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2813d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
2814d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
2815d6ea83ecSEric Paris 			audit_log_end(ab);
2816d6ea83ecSEric Paris 
281712b29f34SStephen Smalley 			return rc;
2818d6ea83ecSEric Paris 		}
281912b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
282012b29f34SStephen Smalley 	}
28211da177e4SLinus Torvalds 	if (rc)
28221da177e4SLinus Torvalds 		return rc;
28231da177e4SLinus Torvalds 
2824275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
28251da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
28261da177e4SLinus Torvalds 	if (rc)
28271da177e4SLinus Torvalds 		return rc;
28281da177e4SLinus Torvalds 
2829275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
28301da177e4SLinus Torvalds 					  isec->sclass);
28311da177e4SLinus Torvalds 	if (rc)
28321da177e4SLinus Torvalds 		return rc;
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds 	return avc_has_perm(newsid,
28351da177e4SLinus Torvalds 			    sbsec->sid,
28361da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
28371da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
28381da177e4SLinus Torvalds 			    &ad);
28391da177e4SLinus Torvalds }
28401da177e4SLinus Torvalds 
28418f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
28428f0cfa52SDavid Howells 					const void *value, size_t size,
28438f0cfa52SDavid Howells 					int flags)
28441da177e4SLinus Torvalds {
28451da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28461da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28471da177e4SLinus Torvalds 	u32 newsid;
28481da177e4SLinus Torvalds 	int rc;
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
28511da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
28521da177e4SLinus Torvalds 		return;
28531da177e4SLinus Torvalds 	}
28541da177e4SLinus Torvalds 
285512b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
28561da177e4SLinus Torvalds 	if (rc) {
285712b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
285812b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
285912b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
28601da177e4SLinus Torvalds 		return;
28611da177e4SLinus Torvalds 	}
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds 	isec->sid = newsid;
28641da177e4SLinus Torvalds 	return;
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
28678f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
28681da177e4SLinus Torvalds {
286988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
287088e67f3bSDavid Howells 
28712875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
28721da177e4SLinus Torvalds }
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
28751da177e4SLinus Torvalds {
287688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
287788e67f3bSDavid Howells 
28782875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28818f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
28821da177e4SLinus Torvalds {
2883b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2884b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
28851da177e4SLinus Torvalds 
28861da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
28871da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
28881da177e4SLinus Torvalds 	return -EACCES;
28891da177e4SLinus Torvalds }
28901da177e4SLinus Torvalds 
2891d381d8a9SJames Morris /*
2892abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
2893d381d8a9SJames Morris  *
2894d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2895d381d8a9SJames Morris  */
289642492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
28971da177e4SLinus Torvalds {
289842492594SDavid P. Quigley 	u32 size;
289942492594SDavid P. Quigley 	int error;
290042492594SDavid P. Quigley 	char *context = NULL;
29011da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29021da177e4SLinus Torvalds 
29038c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29048c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
29051da177e4SLinus Torvalds 
2906abc69bb6SStephen Smalley 	/*
2907abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
2908abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
2909abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
2910abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
2911abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
2912abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
2913abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
2914abc69bb6SStephen Smalley 	 */
29156a9de491SEric Paris 	error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
29163699c53cSDavid Howells 				SECURITY_CAP_NOAUDIT);
2917abc69bb6SStephen Smalley 	if (!error)
2918abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
2919abc69bb6SStephen Smalley 						      &size);
2920abc69bb6SStephen Smalley 	else
292142492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
292242492594SDavid P. Quigley 	if (error)
292342492594SDavid P. Quigley 		return error;
292442492594SDavid P. Quigley 	error = size;
292542492594SDavid P. Quigley 	if (alloc) {
292642492594SDavid P. Quigley 		*buffer = context;
292742492594SDavid P. Quigley 		goto out_nofree;
292842492594SDavid P. Quigley 	}
292942492594SDavid P. Quigley 	kfree(context);
293042492594SDavid P. Quigley out_nofree:
293142492594SDavid P. Quigley 	return error;
29321da177e4SLinus Torvalds }
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
29351da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
29361da177e4SLinus Torvalds {
29371da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29381da177e4SLinus Torvalds 	u32 newsid;
29391da177e4SLinus Torvalds 	int rc;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29421da177e4SLinus Torvalds 		return -EOPNOTSUPP;
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	if (!value || !size)
29451da177e4SLinus Torvalds 		return -EACCES;
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds 	rc = security_context_to_sid((void *)value, size, &newsid);
29481da177e4SLinus Torvalds 	if (rc)
29491da177e4SLinus Torvalds 		return rc;
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 	isec->sid = newsid;
2952ddd29ec6SDavid P. Quigley 	isec->initialized = 1;
29531da177e4SLinus Torvalds 	return 0;
29541da177e4SLinus Torvalds }
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
29571da177e4SLinus Torvalds {
29581da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
29591da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
29601da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
29611da177e4SLinus Torvalds 	return len;
29621da177e4SLinus Torvalds }
29631da177e4SLinus Torvalds 
2964713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2965713a04aeSAhmed S. Darwish {
2966713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
2967713a04aeSAhmed S. Darwish 	*secid = isec->sid;
2968713a04aeSAhmed S. Darwish }
2969713a04aeSAhmed S. Darwish 
29701da177e4SLinus Torvalds /* file security operations */
29711da177e4SLinus Torvalds 
2972788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
29731da177e4SLinus Torvalds {
297488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2975496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
29781da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
29791da177e4SLinus Torvalds 		mask |= MAY_APPEND;
29801da177e4SLinus Torvalds 
2981389fb800SPaul Moore 	return file_has_perm(cred, file,
29821da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
29831da177e4SLinus Torvalds }
29841da177e4SLinus Torvalds 
2985788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
2986788e7dd4SYuichi Nakamura {
2987496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
298820dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
298920dda18bSStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
299020dda18bSStephen Smalley 	u32 sid = current_sid();
299120dda18bSStephen Smalley 
2992389fb800SPaul Moore 	if (!mask)
2993788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
2994788e7dd4SYuichi Nakamura 		return 0;
2995788e7dd4SYuichi Nakamura 
299620dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
299720dda18bSStephen Smalley 	    fsec->pseqno == avc_policy_seqno())
299883d49856SEric Paris 		/* No change since file_open check. */
299920dda18bSStephen Smalley 		return 0;
300020dda18bSStephen Smalley 
3001788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3002788e7dd4SYuichi Nakamura }
3003788e7dd4SYuichi Nakamura 
30041da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	return file_alloc_security(file);
30071da177e4SLinus Torvalds }
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
30101da177e4SLinus Torvalds {
30111da177e4SLinus Torvalds 	file_free_security(file);
30121da177e4SLinus Torvalds }
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
30151da177e4SLinus Torvalds 			      unsigned long arg)
30161da177e4SLinus Torvalds {
301788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
30180b24dcb7SEric Paris 	int error = 0;
30191da177e4SLinus Torvalds 
30200b24dcb7SEric Paris 	switch (cmd) {
30210b24dcb7SEric Paris 	case FIONREAD:
30220b24dcb7SEric Paris 	/* fall through */
30230b24dcb7SEric Paris 	case FIBMAP:
30240b24dcb7SEric Paris 	/* fall through */
30250b24dcb7SEric Paris 	case FIGETBSZ:
30260b24dcb7SEric Paris 	/* fall through */
30272f99c369SAl Viro 	case FS_IOC_GETFLAGS:
30280b24dcb7SEric Paris 	/* fall through */
30292f99c369SAl Viro 	case FS_IOC_GETVERSION:
30300b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
30310b24dcb7SEric Paris 		break;
30321da177e4SLinus Torvalds 
30332f99c369SAl Viro 	case FS_IOC_SETFLAGS:
30340b24dcb7SEric Paris 	/* fall through */
30352f99c369SAl Viro 	case FS_IOC_SETVERSION:
30360b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
30370b24dcb7SEric Paris 		break;
30380b24dcb7SEric Paris 
30390b24dcb7SEric Paris 	/* sys_ioctl() checks */
30400b24dcb7SEric Paris 	case FIONBIO:
30410b24dcb7SEric Paris 	/* fall through */
30420b24dcb7SEric Paris 	case FIOASYNC:
30430b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
30440b24dcb7SEric Paris 		break;
30450b24dcb7SEric Paris 
30460b24dcb7SEric Paris 	case KDSKBENT:
30470b24dcb7SEric Paris 	case KDSKBSENT:
30486a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
30490b24dcb7SEric Paris 					    SECURITY_CAP_AUDIT);
30500b24dcb7SEric Paris 		break;
30510b24dcb7SEric Paris 
30520b24dcb7SEric Paris 	/* default case assumes that the command will go
30530b24dcb7SEric Paris 	 * to the file's ioctl() function.
30540b24dcb7SEric Paris 	 */
30550b24dcb7SEric Paris 	default:
30560b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__IOCTL);
30570b24dcb7SEric Paris 	}
30580b24dcb7SEric Paris 	return error;
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
3061fcaaade1SStephen Smalley static int default_noexec;
3062fcaaade1SStephen Smalley 
30631da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
30641da177e4SLinus Torvalds {
306588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3066d84f4f99SDavid Howells 	int rc = 0;
306788e67f3bSDavid Howells 
3068fcaaade1SStephen Smalley 	if (default_noexec &&
3069fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
30701da177e4SLinus Torvalds 		/*
30711da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
30721da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
30731da177e4SLinus Torvalds 		 * This has an additional check.
30741da177e4SLinus Torvalds 		 */
3075d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
30761da177e4SLinus Torvalds 		if (rc)
3077d84f4f99SDavid Howells 			goto error;
30781da177e4SLinus Torvalds 	}
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	if (file) {
30811da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
30821da177e4SLinus Torvalds 		u32 av = FILE__READ;
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
30851da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
30861da177e4SLinus Torvalds 			av |= FILE__WRITE;
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
30891da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
30901da177e4SLinus Torvalds 
309188e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
30921da177e4SLinus Torvalds 	}
3093d84f4f99SDavid Howells 
3094d84f4f99SDavid Howells error:
3095d84f4f99SDavid Howells 	return rc;
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
3098e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
30991da177e4SLinus Torvalds {
3100ed032189SEric Paris 	int rc = 0;
3101275bb41eSDavid Howells 	u32 sid = current_sid();
31021da177e4SLinus Torvalds 
310384336d1aSEric Paris 	/*
310484336d1aSEric Paris 	 * notice that we are intentionally putting the SELinux check before
310584336d1aSEric Paris 	 * the secondary cap_file_mmap check.  This is such a likely attempt
310684336d1aSEric Paris 	 * at bad behaviour/exploit that we always want to get the AVC, even
310784336d1aSEric Paris 	 * if DAC would have also denied the operation.
310884336d1aSEric Paris 	 */
3109a2551df7SEric Paris 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3110ed032189SEric Paris 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3111ed032189SEric Paris 				  MEMPROTECT__MMAP_ZERO, NULL);
311284336d1aSEric Paris 		if (rc)
311384336d1aSEric Paris 			return rc;
311484336d1aSEric Paris 	}
311584336d1aSEric Paris 
311684336d1aSEric Paris 	/* do DAC check on address space usage */
3117e5467859SAl Viro 	return cap_mmap_addr(addr);
3118e5467859SAl Viro }
31191da177e4SLinus Torvalds 
3120e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3121e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3122e5467859SAl Viro {
31231da177e4SLinus Torvalds 	if (selinux_checkreqprot)
31241da177e4SLinus Torvalds 		prot = reqprot;
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
31271da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
31281da177e4SLinus Torvalds }
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
31311da177e4SLinus Torvalds 				 unsigned long reqprot,
31321da177e4SLinus Torvalds 				 unsigned long prot)
31331da177e4SLinus Torvalds {
313488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	if (selinux_checkreqprot)
31371da177e4SLinus Torvalds 		prot = reqprot;
31381da177e4SLinus Torvalds 
3139fcaaade1SStephen Smalley 	if (default_noexec &&
3140fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3141d541bbeeSJames Morris 		int rc = 0;
3142db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3143db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3144d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3145db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
31466b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
31476b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
31483b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3149db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3150db4c9641SStephen Smalley 			/*
3151db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3152db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3153db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3154db4c9641SStephen Smalley 			 * modified content.  This typically should only
3155db4c9641SStephen Smalley 			 * occur for text relocations.
3156db4c9641SStephen Smalley 			 */
3157d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3158db4c9641SStephen Smalley 		}
31596b992197SLorenzo Hernandez García-Hierro 		if (rc)
31606b992197SLorenzo Hernandez García-Hierro 			return rc;
31616b992197SLorenzo Hernandez García-Hierro 	}
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
31641da177e4SLinus Torvalds }
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
31671da177e4SLinus Torvalds {
316888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
316988e67f3bSDavid Howells 
317088e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
31711da177e4SLinus Torvalds }
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
31741da177e4SLinus Torvalds 			      unsigned long arg)
31751da177e4SLinus Torvalds {
317688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31771da177e4SLinus Torvalds 	int err = 0;
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	switch (cmd) {
31801da177e4SLinus Torvalds 	case F_SETFL:
31811da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
318288e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
31831da177e4SLinus Torvalds 			break;
31841da177e4SLinus Torvalds 		}
31851da177e4SLinus Torvalds 		/* fall through */
31861da177e4SLinus Torvalds 	case F_SETOWN:
31871da177e4SLinus Torvalds 	case F_SETSIG:
31881da177e4SLinus Torvalds 	case F_GETFL:
31891da177e4SLinus Torvalds 	case F_GETOWN:
31901da177e4SLinus Torvalds 	case F_GETSIG:
31911d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
31921da177e4SLinus Torvalds 		/* Just check FD__USE permission */
319388e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
31941da177e4SLinus Torvalds 		break;
31951da177e4SLinus Torvalds 	case F_GETLK:
31961da177e4SLinus Torvalds 	case F_SETLK:
31971da177e4SLinus Torvalds 	case F_SETLKW:
31981da177e4SLinus Torvalds #if BITS_PER_LONG == 32
31991da177e4SLinus Torvalds 	case F_GETLK64:
32001da177e4SLinus Torvalds 	case F_SETLK64:
32011da177e4SLinus Torvalds 	case F_SETLKW64:
32021da177e4SLinus Torvalds #endif
320388e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
32041da177e4SLinus Torvalds 		break;
32051da177e4SLinus Torvalds 	}
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds 	return err;
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
32111da177e4SLinus Torvalds {
32121da177e4SLinus Torvalds 	struct file_security_struct *fsec;
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 	fsec = file->f_security;
3215275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
32161da177e4SLinus Torvalds 
32171da177e4SLinus Torvalds 	return 0;
32181da177e4SLinus Torvalds }
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
32211da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
32221da177e4SLinus Torvalds {
32231da177e4SLinus Torvalds 	struct file *file;
322465c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
32251da177e4SLinus Torvalds 	u32 perm;
32261da177e4SLinus Torvalds 	struct file_security_struct *fsec;
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3229b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	fsec = file->f_security;
32321da177e4SLinus Torvalds 
32331da177e4SLinus Torvalds 	if (!signum)
32341da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
32351da177e4SLinus Torvalds 	else
32361da177e4SLinus Torvalds 		perm = signal_to_av(signum);
32371da177e4SLinus Torvalds 
3238275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
32391da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
32401da177e4SLinus Torvalds }
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
32431da177e4SLinus Torvalds {
324488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
324588e67f3bSDavid Howells 
324688e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
32471da177e4SLinus Torvalds }
32481da177e4SLinus Torvalds 
324983d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3250788e7dd4SYuichi Nakamura {
3251788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3252788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3253d84f4f99SDavid Howells 
3254788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3255496ad9aaSAl Viro 	isec = file_inode(file)->i_security;
3256788e7dd4SYuichi Nakamura 	/*
3257788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3258788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3259788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3260788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3261788e7dd4SYuichi Nakamura 	 * struct as its SID.
3262788e7dd4SYuichi Nakamura 	 */
3263788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3264788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3265788e7dd4SYuichi Nakamura 	/*
3266788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3267788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3268788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3269788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3270788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3271788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3272788e7dd4SYuichi Nakamura 	 */
3273602a8dd6SEric Paris 	return path_has_perm(cred, &file->f_path, open_file_to_av(file));
3274788e7dd4SYuichi Nakamura }
3275788e7dd4SYuichi Nakamura 
32761da177e4SLinus Torvalds /* task security operations */
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
32791da177e4SLinus Torvalds {
32803b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
3283f1752eecSDavid Howells /*
3284ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3285ee18d64cSDavid Howells  */
3286ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3287ee18d64cSDavid Howells {
3288ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3289ee18d64cSDavid Howells 
3290ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3291ee18d64cSDavid Howells 	if (!tsec)
3292ee18d64cSDavid Howells 		return -ENOMEM;
3293ee18d64cSDavid Howells 
3294ee18d64cSDavid Howells 	cred->security = tsec;
3295ee18d64cSDavid Howells 	return 0;
3296ee18d64cSDavid Howells }
3297ee18d64cSDavid Howells 
3298ee18d64cSDavid Howells /*
3299f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3300f1752eecSDavid Howells  */
3301f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
33021da177e4SLinus Torvalds {
3303f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3304e0e81739SDavid Howells 
33052edeaa34STetsuo Handa 	/*
33062edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
33072edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
33082edeaa34STetsuo Handa 	 */
33092edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3310e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3311f1752eecSDavid Howells 	kfree(tsec);
33121da177e4SLinus Torvalds }
33131da177e4SLinus Torvalds 
3314d84f4f99SDavid Howells /*
3315d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3316d84f4f99SDavid Howells  */
3317d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3318d84f4f99SDavid Howells 				gfp_t gfp)
3319d84f4f99SDavid Howells {
3320d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3321d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3322d84f4f99SDavid Howells 
3323d84f4f99SDavid Howells 	old_tsec = old->security;
3324d84f4f99SDavid Howells 
3325d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3326d84f4f99SDavid Howells 	if (!tsec)
3327d84f4f99SDavid Howells 		return -ENOMEM;
3328d84f4f99SDavid Howells 
3329d84f4f99SDavid Howells 	new->security = tsec;
3330d84f4f99SDavid Howells 	return 0;
3331d84f4f99SDavid Howells }
3332d84f4f99SDavid Howells 
3333d84f4f99SDavid Howells /*
3334ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3335ee18d64cSDavid Howells  */
3336ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3337ee18d64cSDavid Howells {
3338ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3339ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3340ee18d64cSDavid Howells 
3341ee18d64cSDavid Howells 	*tsec = *old_tsec;
3342ee18d64cSDavid Howells }
3343ee18d64cSDavid Howells 
3344ee18d64cSDavid Howells /*
33453a3b7ce9SDavid Howells  * set the security data for a kernel service
33463a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
33473a3b7ce9SDavid Howells  */
33483a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
33493a3b7ce9SDavid Howells {
33503a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
33513a3b7ce9SDavid Howells 	u32 sid = current_sid();
33523a3b7ce9SDavid Howells 	int ret;
33533a3b7ce9SDavid Howells 
33543a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
33553a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
33563a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
33573a3b7ce9SDavid Howells 			   NULL);
33583a3b7ce9SDavid Howells 	if (ret == 0) {
33593a3b7ce9SDavid Howells 		tsec->sid = secid;
33603a3b7ce9SDavid Howells 		tsec->create_sid = 0;
33613a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
33623a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
33633a3b7ce9SDavid Howells 	}
33643a3b7ce9SDavid Howells 	return ret;
33653a3b7ce9SDavid Howells }
33663a3b7ce9SDavid Howells 
33673a3b7ce9SDavid Howells /*
33683a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
33693a3b7ce9SDavid Howells  * objective context of the specified inode
33703a3b7ce9SDavid Howells  */
33713a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
33723a3b7ce9SDavid Howells {
33733a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
33743a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
33753a3b7ce9SDavid Howells 	u32 sid = current_sid();
33763a3b7ce9SDavid Howells 	int ret;
33773a3b7ce9SDavid Howells 
33783a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
33793a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
33803a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
33813a3b7ce9SDavid Howells 			   NULL);
33823a3b7ce9SDavid Howells 
33833a3b7ce9SDavid Howells 	if (ret == 0)
33843a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3385ef57471aSDavid Howells 	return ret;
33863a3b7ce9SDavid Howells }
33873a3b7ce9SDavid Howells 
3388dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
338925354c4fSEric Paris {
3390dd8dbf2eSEric Paris 	u32 sid;
3391dd8dbf2eSEric Paris 	struct common_audit_data ad;
3392dd8dbf2eSEric Paris 
3393dd8dbf2eSEric Paris 	sid = task_sid(current);
3394dd8dbf2eSEric Paris 
339550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3396dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3397dd8dbf2eSEric Paris 
3398dd8dbf2eSEric Paris 	return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3399dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
340025354c4fSEric Paris }
340125354c4fSEric Paris 
34021da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
34031da177e4SLinus Torvalds {
34043b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
34051da177e4SLinus Torvalds }
34061da177e4SLinus Torvalds 
34071da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
34081da177e4SLinus Torvalds {
34093b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
34131da177e4SLinus Torvalds {
34143b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
34151da177e4SLinus Torvalds }
34161da177e4SLinus Torvalds 
3417f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3418f9008e4cSDavid Quigley {
3419275bb41eSDavid Howells 	*secid = task_sid(p);
3420f9008e4cSDavid Quigley }
3421f9008e4cSDavid Quigley 
34221da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
34231da177e4SLinus Torvalds {
34241da177e4SLinus Torvalds 	int rc;
34251da177e4SLinus Torvalds 
3426200ac532SEric Paris 	rc = cap_task_setnice(p, nice);
34271da177e4SLinus Torvalds 	if (rc)
34281da177e4SLinus Torvalds 		return rc;
34291da177e4SLinus Torvalds 
34303b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
34311da177e4SLinus Torvalds }
34321da177e4SLinus Torvalds 
343303e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
343403e68060SJames Morris {
3435b5376771SSerge E. Hallyn 	int rc;
3436b5376771SSerge E. Hallyn 
3437200ac532SEric Paris 	rc = cap_task_setioprio(p, ioprio);
3438b5376771SSerge E. Hallyn 	if (rc)
3439b5376771SSerge E. Hallyn 		return rc;
3440b5376771SSerge E. Hallyn 
34413b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
344203e68060SJames Morris }
344303e68060SJames Morris 
3444a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3445a1836a42SDavid Quigley {
34463b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3447a1836a42SDavid Quigley }
3448a1836a42SDavid Quigley 
34498fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
34508fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
34511da177e4SLinus Torvalds {
34528fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
34531da177e4SLinus Torvalds 
34541da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
34551da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
34561da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3457d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
34581da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
34598fd00b4dSJiri Slaby 		return current_has_perm(p, PROCESS__SETRLIMIT);
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds 	return 0;
34621da177e4SLinus Torvalds }
34631da177e4SLinus Torvalds 
3464b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
34651da177e4SLinus Torvalds {
3466b5376771SSerge E. Hallyn 	int rc;
3467b5376771SSerge E. Hallyn 
3468b0ae1981SKOSAKI Motohiro 	rc = cap_task_setscheduler(p);
3469b5376771SSerge E. Hallyn 	if (rc)
3470b5376771SSerge E. Hallyn 		return rc;
3471b5376771SSerge E. Hallyn 
34723b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
34761da177e4SLinus Torvalds {
34773b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
34781da177e4SLinus Torvalds }
34791da177e4SLinus Torvalds 
348035601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
348135601547SDavid Quigley {
34823b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
348335601547SDavid Quigley }
348435601547SDavid Quigley 
3485f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3486f9008e4cSDavid Quigley 				int sig, u32 secid)
34871da177e4SLinus Torvalds {
34881da177e4SLinus Torvalds 	u32 perm;
34891da177e4SLinus Torvalds 	int rc;
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds 	if (!sig)
34921da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
34931da177e4SLinus Torvalds 	else
34941da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3495f9008e4cSDavid Quigley 	if (secid)
3496275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3497275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3498f9008e4cSDavid Quigley 	else
34993b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3500f9008e4cSDavid Quigley 	return rc;
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
35041da177e4SLinus Torvalds {
35058a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
35091da177e4SLinus Torvalds 				  struct inode *inode)
35101da177e4SLinus Torvalds {
35111da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3512275bb41eSDavid Howells 	u32 sid = task_sid(p);
35131da177e4SLinus Torvalds 
3514275bb41eSDavid Howells 	isec->sid = sid;
35151da177e4SLinus Torvalds 	isec->initialized = 1;
35161da177e4SLinus Torvalds }
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
351967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
35202bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
35211da177e4SLinus Torvalds {
35221da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
35231da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
35241da177e4SLinus Torvalds 
3525bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
35261da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
35271da177e4SLinus Torvalds 	if (ih == NULL)
35281da177e4SLinus Torvalds 		goto out;
35291da177e4SLinus Torvalds 
35301da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
35311da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
35321da177e4SLinus Torvalds 		goto out;
35331da177e4SLinus Torvalds 
353448c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
353548c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
35361da177e4SLinus Torvalds 	ret = 0;
35371da177e4SLinus Torvalds 
353867f83cbfSVenkat Yekkirala 	if (proto)
353967f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
354067f83cbfSVenkat Yekkirala 
35411da177e4SLinus Torvalds 	switch (ih->protocol) {
35421da177e4SLinus Torvalds 	case IPPROTO_TCP: {
35431da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
35461da177e4SLinus Torvalds 			break;
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds 		offset += ihlen;
35491da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
35501da177e4SLinus Torvalds 		if (th == NULL)
35511da177e4SLinus Torvalds 			break;
35521da177e4SLinus Torvalds 
355348c62af6SEric Paris 		ad->u.net->sport = th->source;
355448c62af6SEric Paris 		ad->u.net->dport = th->dest;
35551da177e4SLinus Torvalds 		break;
35561da177e4SLinus Torvalds 	}
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 	case IPPROTO_UDP: {
35591da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
35601da177e4SLinus Torvalds 
35611da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
35621da177e4SLinus Torvalds 			break;
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds 		offset += ihlen;
35651da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
35661da177e4SLinus Torvalds 		if (uh == NULL)
35671da177e4SLinus Torvalds 			break;
35681da177e4SLinus Torvalds 
356948c62af6SEric Paris 		ad->u.net->sport = uh->source;
357048c62af6SEric Paris 		ad->u.net->dport = uh->dest;
35711da177e4SLinus Torvalds 		break;
35721da177e4SLinus Torvalds 	}
35731da177e4SLinus Torvalds 
35742ee92d46SJames Morris 	case IPPROTO_DCCP: {
35752ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
35762ee92d46SJames Morris 
35772ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
35782ee92d46SJames Morris 			break;
35792ee92d46SJames Morris 
35802ee92d46SJames Morris 		offset += ihlen;
35812ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
35822ee92d46SJames Morris 		if (dh == NULL)
35832ee92d46SJames Morris 			break;
35842ee92d46SJames Morris 
358548c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
358648c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
35872ee92d46SJames Morris 		break;
35882ee92d46SJames Morris 	}
35892ee92d46SJames Morris 
35901da177e4SLinus Torvalds 	default:
35911da177e4SLinus Torvalds 		break;
35921da177e4SLinus Torvalds 	}
35931da177e4SLinus Torvalds out:
35941da177e4SLinus Torvalds 	return ret;
35951da177e4SLinus Torvalds }
35961da177e4SLinus Torvalds 
35971da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
35981da177e4SLinus Torvalds 
35991da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
360067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
36012bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
36021da177e4SLinus Torvalds {
36031da177e4SLinus Torvalds 	u8 nexthdr;
36041da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
36051da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
360675f2811cSJesse Gross 	__be16 frag_off;
36071da177e4SLinus Torvalds 
3608bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
36091da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
36101da177e4SLinus Torvalds 	if (ip6 == NULL)
36111da177e4SLinus Torvalds 		goto out;
36121da177e4SLinus Torvalds 
361348c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
361448c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
36151da177e4SLinus Torvalds 	ret = 0;
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
36181da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
361975f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
36201da177e4SLinus Torvalds 	if (offset < 0)
36211da177e4SLinus Torvalds 		goto out;
36221da177e4SLinus Torvalds 
362367f83cbfSVenkat Yekkirala 	if (proto)
362467f83cbfSVenkat Yekkirala 		*proto = nexthdr;
362567f83cbfSVenkat Yekkirala 
36261da177e4SLinus Torvalds 	switch (nexthdr) {
36271da177e4SLinus Torvalds 	case IPPROTO_TCP: {
36281da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
36311da177e4SLinus Torvalds 		if (th == NULL)
36321da177e4SLinus Torvalds 			break;
36331da177e4SLinus Torvalds 
363448c62af6SEric Paris 		ad->u.net->sport = th->source;
363548c62af6SEric Paris 		ad->u.net->dport = th->dest;
36361da177e4SLinus Torvalds 		break;
36371da177e4SLinus Torvalds 	}
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 	case IPPROTO_UDP: {
36401da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
36431da177e4SLinus Torvalds 		if (uh == NULL)
36441da177e4SLinus Torvalds 			break;
36451da177e4SLinus Torvalds 
364648c62af6SEric Paris 		ad->u.net->sport = uh->source;
364748c62af6SEric Paris 		ad->u.net->dport = uh->dest;
36481da177e4SLinus Torvalds 		break;
36491da177e4SLinus Torvalds 	}
36501da177e4SLinus Torvalds 
36512ee92d46SJames Morris 	case IPPROTO_DCCP: {
36522ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
36532ee92d46SJames Morris 
36542ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
36552ee92d46SJames Morris 		if (dh == NULL)
36562ee92d46SJames Morris 			break;
36572ee92d46SJames Morris 
365848c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
365948c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
36602ee92d46SJames Morris 		break;
36612ee92d46SJames Morris 	}
36622ee92d46SJames Morris 
36631da177e4SLinus Torvalds 	/* includes fragments */
36641da177e4SLinus Torvalds 	default:
36651da177e4SLinus Torvalds 		break;
36661da177e4SLinus Torvalds 	}
36671da177e4SLinus Torvalds out:
36681da177e4SLinus Torvalds 	return ret;
36691da177e4SLinus Torvalds }
36701da177e4SLinus Torvalds 
36711da177e4SLinus Torvalds #endif /* IPV6 */
36721da177e4SLinus Torvalds 
36732bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3674cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
36751da177e4SLinus Torvalds {
3676cf9481e2SDavid Howells 	char *addrp;
3677cf9481e2SDavid Howells 	int ret;
36781da177e4SLinus Torvalds 
367948c62af6SEric Paris 	switch (ad->u.net->family) {
36801da177e4SLinus Torvalds 	case PF_INET:
368167f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3682cf9481e2SDavid Howells 		if (ret)
3683cf9481e2SDavid Howells 			goto parse_error;
368448c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
368548c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
3686cf9481e2SDavid Howells 		goto okay;
36871da177e4SLinus Torvalds 
36881da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
36891da177e4SLinus Torvalds 	case PF_INET6:
369067f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3691cf9481e2SDavid Howells 		if (ret)
3692cf9481e2SDavid Howells 			goto parse_error;
369348c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
369448c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
3695cf9481e2SDavid Howells 		goto okay;
36961da177e4SLinus Torvalds #endif	/* IPV6 */
36971da177e4SLinus Torvalds 	default:
3698cf9481e2SDavid Howells 		addrp = NULL;
3699cf9481e2SDavid Howells 		goto okay;
37001da177e4SLinus Torvalds 	}
37011da177e4SLinus Torvalds 
3702cf9481e2SDavid Howells parse_error:
370371f1cb05SPaul Moore 	printk(KERN_WARNING
370471f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
370571f1cb05SPaul Moore 	       " unable to parse packet\n");
37061da177e4SLinus Torvalds 	return ret;
3707cf9481e2SDavid Howells 
3708cf9481e2SDavid Howells okay:
3709cf9481e2SDavid Howells 	if (_addrp)
3710cf9481e2SDavid Howells 		*_addrp = addrp;
3711cf9481e2SDavid Howells 	return 0;
37121da177e4SLinus Torvalds }
37131da177e4SLinus Torvalds 
37144f6a993fSPaul Moore /**
3715220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
37164f6a993fSPaul Moore  * @skb: the packet
371775e22910SPaul Moore  * @family: protocol family
3718220deb96SPaul Moore  * @sid: the packet's peer label SID
37194f6a993fSPaul Moore  *
37204f6a993fSPaul Moore  * Description:
3721220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3722220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3723220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3724220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3725220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3726220deb96SPaul Moore  * peer labels.
37274f6a993fSPaul Moore  *
37284f6a993fSPaul Moore  */
3729220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
37304f6a993fSPaul Moore {
373171f1cb05SPaul Moore 	int err;
37324f6a993fSPaul Moore 	u32 xfrm_sid;
37334f6a993fSPaul Moore 	u32 nlbl_sid;
3734220deb96SPaul Moore 	u32 nlbl_type;
37354f6a993fSPaul Moore 
3736bed4d7efSPaul Moore 	err = selinux_skb_xfrm_sid(skb, &xfrm_sid);
3737bed4d7efSPaul Moore 	if (unlikely(err))
3738bed4d7efSPaul Moore 		return -EACCES;
3739bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3740bed4d7efSPaul Moore 	if (unlikely(err))
3741bed4d7efSPaul Moore 		return -EACCES;
3742220deb96SPaul Moore 
374371f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
374471f1cb05SPaul Moore 	if (unlikely(err)) {
374571f1cb05SPaul Moore 		printk(KERN_WARNING
374671f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
374771f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3748220deb96SPaul Moore 		return -EACCES;
374971f1cb05SPaul Moore 	}
3750220deb96SPaul Moore 
3751220deb96SPaul Moore 	return 0;
37524f6a993fSPaul Moore }
37534f6a993fSPaul Moore 
37541da177e4SLinus Torvalds /* socket security operations */
3755d4f2d978SPaul Moore 
37562ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
37572ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
3758d4f2d978SPaul Moore {
37592ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
37602ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
37612ad18bdfSHarry Ciao 		return 0;
37622ad18bdfSHarry Ciao 	}
37632ad18bdfSHarry Ciao 
37642ad18bdfSHarry Ciao 	return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
37652ad18bdfSHarry Ciao 				       socksid);
3766d4f2d978SPaul Moore }
3767d4f2d978SPaul Moore 
3768253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
37691da177e4SLinus Torvalds {
3770253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
37712bf49690SThomas Liu 	struct common_audit_data ad;
377248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
3773253bfae6SPaul Moore 	u32 tsid = task_sid(task);
37741da177e4SLinus Torvalds 
3775253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
3776253bfae6SPaul Moore 		return 0;
37771da177e4SLinus Torvalds 
377850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
377948c62af6SEric Paris 	ad.u.net = &net;
378048c62af6SEric Paris 	ad.u.net->sk = sk;
37811da177e4SLinus Torvalds 
3782253bfae6SPaul Moore 	return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
37831da177e4SLinus Torvalds }
37841da177e4SLinus Torvalds 
37851da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
37861da177e4SLinus Torvalds 				 int protocol, int kern)
37871da177e4SLinus Torvalds {
37885fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3789d4f2d978SPaul Moore 	u32 newsid;
3790275bb41eSDavid Howells 	u16 secclass;
37912ad18bdfSHarry Ciao 	int rc;
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds 	if (kern)
3794d4f2d978SPaul Moore 		return 0;
37951da177e4SLinus Torvalds 
3796275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
37972ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
37982ad18bdfSHarry Ciao 	if (rc)
37992ad18bdfSHarry Ciao 		return rc;
38002ad18bdfSHarry Ciao 
3801d4f2d978SPaul Moore 	return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
38021da177e4SLinus Torvalds }
38031da177e4SLinus Torvalds 
38047420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
38051da177e4SLinus Torvalds 				      int type, int protocol, int kern)
38061da177e4SLinus Torvalds {
38075fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
3808d4f2d978SPaul Moore 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3809892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
3810275bb41eSDavid Howells 	int err = 0;
3811275bb41eSDavid Howells 
38122ad18bdfSHarry Ciao 	isec->sclass = socket_type_to_security_class(family, type, protocol);
38132ad18bdfSHarry Ciao 
3814275bb41eSDavid Howells 	if (kern)
3815275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
38162ad18bdfSHarry Ciao 	else {
38172ad18bdfSHarry Ciao 		err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
38182ad18bdfSHarry Ciao 		if (err)
38192ad18bdfSHarry Ciao 			return err;
38202ad18bdfSHarry Ciao 	}
3821275bb41eSDavid Howells 
38221da177e4SLinus Torvalds 	isec->initialized = 1;
38231da177e4SLinus Torvalds 
3824892c141eSVenkat Yekkirala 	if (sock->sk) {
3825892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3826892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
3827220deb96SPaul Moore 		sksec->sclass = isec->sclass;
3828389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
3829892c141eSVenkat Yekkirala 	}
3830892c141eSVenkat Yekkirala 
38317420ed23SVenkat Yekkirala 	return err;
38321da177e4SLinus Torvalds }
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
38351da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
38361da177e4SLinus Torvalds    permission check between the socket and the port number. */
38371da177e4SLinus Torvalds 
38381da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
38391da177e4SLinus Torvalds {
3840253bfae6SPaul Moore 	struct sock *sk = sock->sk;
38411da177e4SLinus Torvalds 	u16 family;
38421da177e4SLinus Torvalds 	int err;
38431da177e4SLinus Torvalds 
3844253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__BIND);
38451da177e4SLinus Torvalds 	if (err)
38461da177e4SLinus Torvalds 		goto out;
38471da177e4SLinus Torvalds 
38481da177e4SLinus Torvalds 	/*
38491da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
385013402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
385113402580SJames Morris 	 * check the first address now.
38521da177e4SLinus Torvalds 	 */
3853253bfae6SPaul Moore 	family = sk->sk_family;
38541da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
38551da177e4SLinus Torvalds 		char *addrp;
3856253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
38572bf49690SThomas Liu 		struct common_audit_data ad;
385848c62af6SEric Paris 		struct lsm_network_audit net = {0,};
38591da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
38601da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
38611da177e4SLinus Torvalds 		unsigned short snum;
3862e399f982SJames Morris 		u32 sid, node_perm;
38631da177e4SLinus Torvalds 
38641da177e4SLinus Torvalds 		if (family == PF_INET) {
38651da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
38661da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
38671da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
38681da177e4SLinus Torvalds 		} else {
38691da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
38701da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
38711da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
38721da177e4SLinus Torvalds 		}
38731da177e4SLinus Torvalds 
3874227b60f5SStephen Hemminger 		if (snum) {
3875227b60f5SStephen Hemminger 			int low, high;
3876227b60f5SStephen Hemminger 
3877227b60f5SStephen Hemminger 			inet_get_local_port_range(&low, &high);
3878227b60f5SStephen Hemminger 
3879227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
38803e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
38813e112172SPaul Moore 						      snum, &sid);
38821da177e4SLinus Torvalds 				if (err)
38831da177e4SLinus Torvalds 					goto out;
388450c205f5SEric Paris 				ad.type = LSM_AUDIT_DATA_NET;
388548c62af6SEric Paris 				ad.u.net = &net;
388648c62af6SEric Paris 				ad.u.net->sport = htons(snum);
388748c62af6SEric Paris 				ad.u.net->family = family;
3888253bfae6SPaul Moore 				err = avc_has_perm(sksec->sid, sid,
3889253bfae6SPaul Moore 						   sksec->sclass,
38901da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
38911da177e4SLinus Torvalds 				if (err)
38921da177e4SLinus Torvalds 					goto out;
38931da177e4SLinus Torvalds 			}
3894227b60f5SStephen Hemminger 		}
38951da177e4SLinus Torvalds 
3896253bfae6SPaul Moore 		switch (sksec->sclass) {
389713402580SJames Morris 		case SECCLASS_TCP_SOCKET:
38981da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
38991da177e4SLinus Torvalds 			break;
39001da177e4SLinus Torvalds 
390113402580SJames Morris 		case SECCLASS_UDP_SOCKET:
39021da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
39031da177e4SLinus Torvalds 			break;
39041da177e4SLinus Torvalds 
39052ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
39062ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
39072ee92d46SJames Morris 			break;
39082ee92d46SJames Morris 
39091da177e4SLinus Torvalds 		default:
39101da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
39111da177e4SLinus Torvalds 			break;
39121da177e4SLinus Torvalds 		}
39131da177e4SLinus Torvalds 
3914224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
39151da177e4SLinus Torvalds 		if (err)
39161da177e4SLinus Torvalds 			goto out;
39171da177e4SLinus Torvalds 
391850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
391948c62af6SEric Paris 		ad.u.net = &net;
392048c62af6SEric Paris 		ad.u.net->sport = htons(snum);
392148c62af6SEric Paris 		ad.u.net->family = family;
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds 		if (family == PF_INET)
392448c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
39251da177e4SLinus Torvalds 		else
392648c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
39271da177e4SLinus Torvalds 
3928253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid,
3929253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
39301da177e4SLinus Torvalds 		if (err)
39311da177e4SLinus Torvalds 			goto out;
39321da177e4SLinus Torvalds 	}
39331da177e4SLinus Torvalds out:
39341da177e4SLinus Torvalds 	return err;
39351da177e4SLinus Torvalds }
39361da177e4SLinus Torvalds 
39371da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
39381da177e4SLinus Torvalds {
3939014ab19aSPaul Moore 	struct sock *sk = sock->sk;
3940253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
39411da177e4SLinus Torvalds 	int err;
39421da177e4SLinus Torvalds 
3943253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__CONNECT);
39441da177e4SLinus Torvalds 	if (err)
39451da177e4SLinus Torvalds 		return err;
39461da177e4SLinus Torvalds 
39471da177e4SLinus Torvalds 	/*
39482ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
39491da177e4SLinus Torvalds 	 */
3950253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3951253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_DCCP_SOCKET) {
39522bf49690SThomas Liu 		struct common_audit_data ad;
395348c62af6SEric Paris 		struct lsm_network_audit net = {0,};
39541da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
39551da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
39561da177e4SLinus Torvalds 		unsigned short snum;
39572ee92d46SJames Morris 		u32 sid, perm;
39581da177e4SLinus Torvalds 
39591da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
39601da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3961911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
39621da177e4SLinus Torvalds 				return -EINVAL;
39631da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
39641da177e4SLinus Torvalds 		} else {
39651da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3966911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
39671da177e4SLinus Torvalds 				return -EINVAL;
39681da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
39691da177e4SLinus Torvalds 		}
39701da177e4SLinus Torvalds 
39713e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
39721da177e4SLinus Torvalds 		if (err)
39731da177e4SLinus Torvalds 			goto out;
39741da177e4SLinus Torvalds 
3975253bfae6SPaul Moore 		perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
39762ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
39772ee92d46SJames Morris 
397850c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
397948c62af6SEric Paris 		ad.u.net = &net;
398048c62af6SEric Paris 		ad.u.net->dport = htons(snum);
398148c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
3982253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
39831da177e4SLinus Torvalds 		if (err)
39841da177e4SLinus Torvalds 			goto out;
39851da177e4SLinus Torvalds 	}
39861da177e4SLinus Torvalds 
3987014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
3988014ab19aSPaul Moore 
39891da177e4SLinus Torvalds out:
39901da177e4SLinus Torvalds 	return err;
39911da177e4SLinus Torvalds }
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
39941da177e4SLinus Torvalds {
3995253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
39961da177e4SLinus Torvalds }
39971da177e4SLinus Torvalds 
39981da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
39991da177e4SLinus Torvalds {
40001da177e4SLinus Torvalds 	int err;
40011da177e4SLinus Torvalds 	struct inode_security_struct *isec;
40021da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
40031da177e4SLinus Torvalds 
4004253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
40051da177e4SLinus Torvalds 	if (err)
40061da177e4SLinus Torvalds 		return err;
40071da177e4SLinus Torvalds 
40081da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
40091da177e4SLinus Torvalds 
40101da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
40111da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
40121da177e4SLinus Torvalds 	newisec->sid = isec->sid;
40131da177e4SLinus Torvalds 	newisec->initialized = 1;
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds 	return 0;
40161da177e4SLinus Torvalds }
40171da177e4SLinus Torvalds 
40181da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
40191da177e4SLinus Torvalds 				  int size)
40201da177e4SLinus Torvalds {
4021253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__WRITE);
40221da177e4SLinus Torvalds }
40231da177e4SLinus Torvalds 
40241da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
40251da177e4SLinus Torvalds 				  int size, int flags)
40261da177e4SLinus Torvalds {
4027253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__READ);
40281da177e4SLinus Torvalds }
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
40311da177e4SLinus Torvalds {
4032253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
40331da177e4SLinus Torvalds }
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
40361da177e4SLinus Torvalds {
4037253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
40381da177e4SLinus Torvalds }
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
40411da177e4SLinus Torvalds {
4042f8687afeSPaul Moore 	int err;
4043f8687afeSPaul Moore 
4044253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4045f8687afeSPaul Moore 	if (err)
4046f8687afeSPaul Moore 		return err;
4047f8687afeSPaul Moore 
4048f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
40491da177e4SLinus Torvalds }
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
40521da177e4SLinus Torvalds 				     int optname)
40531da177e4SLinus Torvalds {
4054253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
40551da177e4SLinus Torvalds }
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
40581da177e4SLinus Torvalds {
4059253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
40601da177e4SLinus Torvalds }
40611da177e4SLinus Torvalds 
40623610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
40633610cda5SDavid S. Miller 					      struct sock *other,
40641da177e4SLinus Torvalds 					      struct sock *newsk)
40651da177e4SLinus Torvalds {
40663610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
40673610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
40684d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
40692bf49690SThomas Liu 	struct common_audit_data ad;
407048c62af6SEric Paris 	struct lsm_network_audit net = {0,};
40711da177e4SLinus Torvalds 	int err;
40721da177e4SLinus Torvalds 
407350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
407448c62af6SEric Paris 	ad.u.net = &net;
407548c62af6SEric Paris 	ad.u.net->sk = other;
40761da177e4SLinus Torvalds 
40774d1e2451SPaul Moore 	err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
40784d1e2451SPaul Moore 			   sksec_other->sclass,
40791da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
40801da177e4SLinus Torvalds 	if (err)
40811da177e4SLinus Torvalds 		return err;
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds 	/* server child socket */
40844d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
40854d1e2451SPaul Moore 	err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
40864d1e2451SPaul Moore 				    &sksec_new->sid);
40874d1e2451SPaul Moore 	if (err)
40884237c75cSVenkat Yekkirala 		return err;
40894d1e2451SPaul Moore 
40904d1e2451SPaul Moore 	/* connecting socket */
40914d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
40924d1e2451SPaul Moore 
40934d1e2451SPaul Moore 	return 0;
40941da177e4SLinus Torvalds }
40951da177e4SLinus Torvalds 
40961da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
40971da177e4SLinus Torvalds 					struct socket *other)
40981da177e4SLinus Torvalds {
4099253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4100253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
41012bf49690SThomas Liu 	struct common_audit_data ad;
410248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
41031da177e4SLinus Torvalds 
410450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
410548c62af6SEric Paris 	ad.u.net = &net;
410648c62af6SEric Paris 	ad.u.net->sk = other->sk;
41071da177e4SLinus Torvalds 
4108253bfae6SPaul Moore 	return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4109253bfae6SPaul Moore 			    &ad);
41101da177e4SLinus Torvalds }
41111da177e4SLinus Torvalds 
4112effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4113effad8dfSPaul Moore 				    u32 peer_sid,
41142bf49690SThomas Liu 				    struct common_audit_data *ad)
4115effad8dfSPaul Moore {
4116effad8dfSPaul Moore 	int err;
4117effad8dfSPaul Moore 	u32 if_sid;
4118effad8dfSPaul Moore 	u32 node_sid;
4119effad8dfSPaul Moore 
4120effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
4121effad8dfSPaul Moore 	if (err)
4122effad8dfSPaul Moore 		return err;
4123effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4124effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4125effad8dfSPaul Moore 	if (err)
4126effad8dfSPaul Moore 		return err;
4127effad8dfSPaul Moore 
4128effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4129effad8dfSPaul Moore 	if (err)
4130effad8dfSPaul Moore 		return err;
4131effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4132effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4133effad8dfSPaul Moore }
4134effad8dfSPaul Moore 
4135220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4136d8395c87SPaul Moore 				       u16 family)
4137220deb96SPaul Moore {
4138277d342fSPaul Moore 	int err = 0;
4139220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4140220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
41412bf49690SThomas Liu 	struct common_audit_data ad;
414248c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4143d8395c87SPaul Moore 	char *addrp;
4144d8395c87SPaul Moore 
414550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
414648c62af6SEric Paris 	ad.u.net = &net;
414748c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
414848c62af6SEric Paris 	ad.u.net->family = family;
4149d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4150d8395c87SPaul Moore 	if (err)
4151d8395c87SPaul Moore 		return err;
4152220deb96SPaul Moore 
415358bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
4154220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4155d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4156220deb96SPaul Moore 		if (err)
4157220deb96SPaul Moore 			return err;
415858bfbb51SPaul Moore 	}
4159220deb96SPaul Moore 
4160d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4161220deb96SPaul Moore 	if (err)
4162220deb96SPaul Moore 		return err;
4163d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4164220deb96SPaul Moore 
41654e5ab4cbSJames Morris 	return err;
41664e5ab4cbSJames Morris }
4167d28d1e08STrent Jaeger 
41684e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
41694e5ab4cbSJames Morris {
4170220deb96SPaul Moore 	int err;
41714237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4172220deb96SPaul Moore 	u16 family = sk->sk_family;
4173220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
41742bf49690SThomas Liu 	struct common_audit_data ad;
417548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4176220deb96SPaul Moore 	char *addrp;
4177d8395c87SPaul Moore 	u8 secmark_active;
4178d8395c87SPaul Moore 	u8 peerlbl_active;
41794e5ab4cbSJames Morris 
41804e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4181220deb96SPaul Moore 		return 0;
41824e5ab4cbSJames Morris 
41834e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
418487fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
41854e5ab4cbSJames Morris 		family = PF_INET;
41864e5ab4cbSJames Morris 
4187d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4188d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4189d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4190d8395c87SPaul Moore 	 * as fast and as clean as possible. */
419158bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4192d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4193d8395c87SPaul Moore 
4194d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
4195d8395c87SPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4196d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4197d8395c87SPaul Moore 		return 0;
4198d8395c87SPaul Moore 
419950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
420048c62af6SEric Paris 	ad.u.net = &net;
420148c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
420248c62af6SEric Paris 	ad.u.net->family = family;
4203224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
42044e5ab4cbSJames Morris 	if (err)
4205220deb96SPaul Moore 		return err;
42064e5ab4cbSJames Morris 
4207d8395c87SPaul Moore 	if (peerlbl_active) {
4208d621d35eSPaul Moore 		u32 peer_sid;
4209220deb96SPaul Moore 
4210220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4211220deb96SPaul Moore 		if (err)
4212220deb96SPaul Moore 			return err;
42138964be4aSEric Dumazet 		err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
4214effad8dfSPaul Moore 					       peer_sid, &ad);
4215dfaebe98SPaul Moore 		if (err) {
4216dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4217effad8dfSPaul Moore 			return err;
4218dfaebe98SPaul Moore 		}
4219d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4220d621d35eSPaul Moore 				   PEER__RECV, &ad);
4221dfaebe98SPaul Moore 		if (err)
4222dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4223d621d35eSPaul Moore 	}
4224d621d35eSPaul Moore 
4225d8395c87SPaul Moore 	if (secmark_active) {
4226effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4227effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4228effad8dfSPaul Moore 		if (err)
4229effad8dfSPaul Moore 			return err;
4230effad8dfSPaul Moore 	}
4231effad8dfSPaul Moore 
4232d621d35eSPaul Moore 	return err;
42331da177e4SLinus Torvalds }
42341da177e4SLinus Torvalds 
42352c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
42361da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
42371da177e4SLinus Torvalds {
42381da177e4SLinus Torvalds 	int err = 0;
42391da177e4SLinus Torvalds 	char *scontext;
42401da177e4SLinus Torvalds 	u32 scontext_len;
4241253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
42423de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
42431da177e4SLinus Torvalds 
4244253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4245253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_TCP_SOCKET)
4246dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4247253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4248253bfae6SPaul Moore 		return -ENOPROTOOPT;
42491da177e4SLinus Torvalds 
42502c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
42511da177e4SLinus Torvalds 	if (err)
4252253bfae6SPaul Moore 		return err;
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds 	if (scontext_len > len) {
42551da177e4SLinus Torvalds 		err = -ERANGE;
42561da177e4SLinus Torvalds 		goto out_len;
42571da177e4SLinus Torvalds 	}
42581da177e4SLinus Torvalds 
42591da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
42601da177e4SLinus Torvalds 		err = -EFAULT;
42611da177e4SLinus Torvalds 
42621da177e4SLinus Torvalds out_len:
42631da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
42641da177e4SLinus Torvalds 		err = -EFAULT;
42651da177e4SLinus Torvalds 	kfree(scontext);
42661da177e4SLinus Torvalds 	return err;
42671da177e4SLinus Torvalds }
42681da177e4SLinus Torvalds 
4269dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
42702c7946a7SCatherine Zhang {
4271dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
427275e22910SPaul Moore 	u16 family;
4273877ce7c1SCatherine Zhang 
4274aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4275aa862900SPaul Moore 		family = PF_INET;
4276aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4277aa862900SPaul Moore 		family = PF_INET6;
4278aa862900SPaul Moore 	else if (sock)
427975e22910SPaul Moore 		family = sock->sk->sk_family;
428075e22910SPaul Moore 	else
428175e22910SPaul Moore 		goto out;
428275e22910SPaul Moore 
428375e22910SPaul Moore 	if (sock && family == PF_UNIX)
4284713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
42853de4bab5SPaul Moore 	else if (skb)
4286220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
42872c7946a7SCatherine Zhang 
428875e22910SPaul Moore out:
4289dc49c1f9SCatherine Zhang 	*secid = peer_secid;
429075e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
429175e22910SPaul Moore 		return -EINVAL;
429275e22910SPaul Moore 	return 0;
42932c7946a7SCatherine Zhang }
42942c7946a7SCatherine Zhang 
42957d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
42961da177e4SLinus Torvalds {
429784914b7eSPaul Moore 	struct sk_security_struct *sksec;
429884914b7eSPaul Moore 
429984914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
430084914b7eSPaul Moore 	if (!sksec)
430184914b7eSPaul Moore 		return -ENOMEM;
430284914b7eSPaul Moore 
430384914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
430484914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
430584914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
430684914b7eSPaul Moore 	sk->sk_security = sksec;
430784914b7eSPaul Moore 
430884914b7eSPaul Moore 	return 0;
43091da177e4SLinus Torvalds }
43101da177e4SLinus Torvalds 
43111da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
43121da177e4SLinus Torvalds {
431384914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
431484914b7eSPaul Moore 
431584914b7eSPaul Moore 	sk->sk_security = NULL;
431684914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
431784914b7eSPaul Moore 	kfree(sksec);
43181da177e4SLinus Torvalds }
43191da177e4SLinus Torvalds 
4320892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4321892c141eSVenkat Yekkirala {
4322dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4323dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4324892c141eSVenkat Yekkirala 
4325dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4326dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4327dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
432899f59ed0SPaul Moore 
4329dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4330892c141eSVenkat Yekkirala }
4331892c141eSVenkat Yekkirala 
4332beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4333d28d1e08STrent Jaeger {
4334d28d1e08STrent Jaeger 	if (!sk)
4335beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4336892c141eSVenkat Yekkirala 	else {
4337892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4338d28d1e08STrent Jaeger 
4339beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4340892c141eSVenkat Yekkirala 	}
4341d28d1e08STrent Jaeger }
4342d28d1e08STrent Jaeger 
43439a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
43444237c75cSVenkat Yekkirala {
43454237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
43464237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43474237c75cSVenkat Yekkirala 
43482148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
43492148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
43504237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4351220deb96SPaul Moore 	sksec->sclass = isec->sclass;
43524237c75cSVenkat Yekkirala }
43534237c75cSVenkat Yekkirala 
43549a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
43554237c75cSVenkat Yekkirala 				     struct request_sock *req)
43564237c75cSVenkat Yekkirala {
43574237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43584237c75cSVenkat Yekkirala 	int err;
4359aa862900SPaul Moore 	u16 family = sk->sk_family;
43607420ed23SVenkat Yekkirala 	u32 newsid;
43614237c75cSVenkat Yekkirala 	u32 peersid;
43624237c75cSVenkat Yekkirala 
4363aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4364aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4365aa862900SPaul Moore 		family = PF_INET;
4366aa862900SPaul Moore 
4367aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4368220deb96SPaul Moore 	if (err)
4369220deb96SPaul Moore 		return err;
4370a51c64f1SVenkat Yekkirala 	if (peersid == SECSID_NULL) {
4371a51c64f1SVenkat Yekkirala 		req->secid = sksec->sid;
43723de4bab5SPaul Moore 		req->peer_secid = SECSID_NULL;
4373389fb800SPaul Moore 	} else {
43744237c75cSVenkat Yekkirala 		err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
43754237c75cSVenkat Yekkirala 		if (err)
43764237c75cSVenkat Yekkirala 			return err;
43774237c75cSVenkat Yekkirala 		req->secid = newsid;
43786b877699SVenkat Yekkirala 		req->peer_secid = peersid;
4379389fb800SPaul Moore 	}
4380389fb800SPaul Moore 
4381389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
43824237c75cSVenkat Yekkirala }
43834237c75cSVenkat Yekkirala 
43849a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
43859a673e56SAdrian Bunk 				   const struct request_sock *req)
43864237c75cSVenkat Yekkirala {
43874237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
43884237c75cSVenkat Yekkirala 
43894237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
43906b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
43914237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
43924237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
43934237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
43944237c75cSVenkat Yekkirala 	   time it will have been created and available. */
439599f59ed0SPaul Moore 
43969f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
43979f2ad665SPaul Moore 	 * thread with access to newsksec */
4398389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
43994237c75cSVenkat Yekkirala }
44004237c75cSVenkat Yekkirala 
4401014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
44026b877699SVenkat Yekkirala {
4403aa862900SPaul Moore 	u16 family = sk->sk_family;
44046b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
44056b877699SVenkat Yekkirala 
4406aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4407aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4408aa862900SPaul Moore 		family = PF_INET;
4409aa862900SPaul Moore 
4410aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
44116b877699SVenkat Yekkirala }
44126b877699SVenkat Yekkirala 
4413ca10b9e9SEric Dumazet static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4414ca10b9e9SEric Dumazet {
4415ca10b9e9SEric Dumazet 	skb_set_owner_w(skb, sk);
4416ca10b9e9SEric Dumazet }
4417ca10b9e9SEric Dumazet 
44182606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
44192606fd1fSEric Paris {
44202606fd1fSEric Paris 	const struct task_security_struct *__tsec;
44212606fd1fSEric Paris 	u32 tsid;
44222606fd1fSEric Paris 
44232606fd1fSEric Paris 	__tsec = current_security();
44242606fd1fSEric Paris 	tsid = __tsec->sid;
44252606fd1fSEric Paris 
44262606fd1fSEric Paris 	return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
44272606fd1fSEric Paris }
44282606fd1fSEric Paris 
44292606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
44302606fd1fSEric Paris {
44312606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
44322606fd1fSEric Paris }
44332606fd1fSEric Paris 
44342606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
44352606fd1fSEric Paris {
44362606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
44372606fd1fSEric Paris }
44382606fd1fSEric Paris 
44399a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
44409a673e56SAdrian Bunk 				      struct flowi *fl)
44414237c75cSVenkat Yekkirala {
44421d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
44434237c75cSVenkat Yekkirala }
44444237c75cSVenkat Yekkirala 
44455dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
44465dbbaf2dSPaul Moore {
44475dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
44485dbbaf2dSPaul Moore 
44495dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
44505dbbaf2dSPaul Moore 	if (!tunsec)
44515dbbaf2dSPaul Moore 		return -ENOMEM;
44525dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
44535dbbaf2dSPaul Moore 
44545dbbaf2dSPaul Moore 	*security = tunsec;
44555dbbaf2dSPaul Moore 	return 0;
44565dbbaf2dSPaul Moore }
44575dbbaf2dSPaul Moore 
44585dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
44595dbbaf2dSPaul Moore {
44605dbbaf2dSPaul Moore 	kfree(security);
44615dbbaf2dSPaul Moore }
44625dbbaf2dSPaul Moore 
4463ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
4464ed6d76e4SPaul Moore {
4465ed6d76e4SPaul Moore 	u32 sid = current_sid();
4466ed6d76e4SPaul Moore 
4467ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
4468ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
4469ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
4470ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
4471ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
4472ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
4473ed6d76e4SPaul Moore 
4474ed6d76e4SPaul Moore 	return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4475ed6d76e4SPaul Moore 			    NULL);
4476ed6d76e4SPaul Moore }
4477ed6d76e4SPaul Moore 
44785dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
4479ed6d76e4SPaul Moore {
44805dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
44815dbbaf2dSPaul Moore 
44825dbbaf2dSPaul Moore 	return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
44835dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
44845dbbaf2dSPaul Moore }
44855dbbaf2dSPaul Moore 
44865dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
44875dbbaf2dSPaul Moore {
44885dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4489ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4490ed6d76e4SPaul Moore 
4491ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
4492ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
4493ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
4494ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
4495ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
4496ed6d76e4SPaul Moore 	 * protocols were being used */
4497ed6d76e4SPaul Moore 
44985dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
4499ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
45005dbbaf2dSPaul Moore 
45015dbbaf2dSPaul Moore 	return 0;
4502ed6d76e4SPaul Moore }
4503ed6d76e4SPaul Moore 
45045dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
4505ed6d76e4SPaul Moore {
45065dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4507ed6d76e4SPaul Moore 	u32 sid = current_sid();
4508ed6d76e4SPaul Moore 	int err;
4509ed6d76e4SPaul Moore 
45105dbbaf2dSPaul Moore 	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4511ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
4512ed6d76e4SPaul Moore 	if (err)
4513ed6d76e4SPaul Moore 		return err;
4514ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4515ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
4516ed6d76e4SPaul Moore 	if (err)
4517ed6d76e4SPaul Moore 		return err;
45185dbbaf2dSPaul Moore 	tunsec->sid = sid;
4519ed6d76e4SPaul Moore 
4520ed6d76e4SPaul Moore 	return 0;
4521ed6d76e4SPaul Moore }
4522ed6d76e4SPaul Moore 
45231da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
45241da177e4SLinus Torvalds {
45251da177e4SLinus Torvalds 	int err = 0;
45261da177e4SLinus Torvalds 	u32 perm;
45271da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4528253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
45291da177e4SLinus Torvalds 
453077954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
45311da177e4SLinus Torvalds 		err = -EINVAL;
45321da177e4SLinus Torvalds 		goto out;
45331da177e4SLinus Torvalds 	}
4534b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
45351da177e4SLinus Torvalds 
4536253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
45371da177e4SLinus Torvalds 	if (err) {
45381da177e4SLinus Torvalds 		if (err == -EINVAL) {
45399ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
45401da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
45411da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
4542253bfae6SPaul Moore 				  nlh->nlmsg_type, sksec->sclass);
454339c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
45441da177e4SLinus Torvalds 				err = 0;
45451da177e4SLinus Torvalds 		}
45461da177e4SLinus Torvalds 
45471da177e4SLinus Torvalds 		/* Ignore */
45481da177e4SLinus Torvalds 		if (err == -ENOENT)
45491da177e4SLinus Torvalds 			err = 0;
45501da177e4SLinus Torvalds 		goto out;
45511da177e4SLinus Torvalds 	}
45521da177e4SLinus Torvalds 
4553253bfae6SPaul Moore 	err = sock_has_perm(current, sk, perm);
45541da177e4SLinus Torvalds out:
45551da177e4SLinus Torvalds 	return err;
45561da177e4SLinus Torvalds }
45571da177e4SLinus Torvalds 
45581da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
45591da177e4SLinus Torvalds 
4560effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4561effad8dfSPaul Moore 				       u16 family)
45621da177e4SLinus Torvalds {
4563dfaebe98SPaul Moore 	int err;
4564effad8dfSPaul Moore 	char *addrp;
4565effad8dfSPaul Moore 	u32 peer_sid;
45662bf49690SThomas Liu 	struct common_audit_data ad;
456748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4568effad8dfSPaul Moore 	u8 secmark_active;
4569948bf85cSPaul Moore 	u8 netlbl_active;
4570effad8dfSPaul Moore 	u8 peerlbl_active;
45714237c75cSVenkat Yekkirala 
4572effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4573effad8dfSPaul Moore 		return NF_ACCEPT;
45744237c75cSVenkat Yekkirala 
4575effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4576948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
4577948bf85cSPaul Moore 	peerlbl_active = netlbl_active || selinux_xfrm_enabled();
4578effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4579effad8dfSPaul Moore 		return NF_ACCEPT;
45804237c75cSVenkat Yekkirala 
4581d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4582d8395c87SPaul Moore 		return NF_DROP;
4583d8395c87SPaul Moore 
458450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
458548c62af6SEric Paris 	ad.u.net = &net;
458648c62af6SEric Paris 	ad.u.net->netif = ifindex;
458748c62af6SEric Paris 	ad.u.net->family = family;
4588effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4589effad8dfSPaul Moore 		return NF_DROP;
45901da177e4SLinus Torvalds 
4591dfaebe98SPaul Moore 	if (peerlbl_active) {
4592dfaebe98SPaul Moore 		err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4593dfaebe98SPaul Moore 					       peer_sid, &ad);
4594dfaebe98SPaul Moore 		if (err) {
4595dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4596effad8dfSPaul Moore 			return NF_DROP;
4597dfaebe98SPaul Moore 		}
4598dfaebe98SPaul Moore 	}
4599effad8dfSPaul Moore 
4600effad8dfSPaul Moore 	if (secmark_active)
4601effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4602effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4603effad8dfSPaul Moore 			return NF_DROP;
4604effad8dfSPaul Moore 
4605948bf85cSPaul Moore 	if (netlbl_active)
4606948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4607948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4608948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4609948bf85cSPaul Moore 		 * protection */
4610948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4611948bf85cSPaul Moore 			return NF_DROP;
4612948bf85cSPaul Moore 
4613effad8dfSPaul Moore 	return NF_ACCEPT;
4614effad8dfSPaul Moore }
4615effad8dfSPaul Moore 
4616effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4617effad8dfSPaul Moore 					 struct sk_buff *skb,
4618effad8dfSPaul Moore 					 const struct net_device *in,
4619effad8dfSPaul Moore 					 const struct net_device *out,
4620effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4621effad8dfSPaul Moore {
4622effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET);
4623effad8dfSPaul Moore }
4624effad8dfSPaul Moore 
4625effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4626effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4627effad8dfSPaul Moore 					 struct sk_buff *skb,
4628effad8dfSPaul Moore 					 const struct net_device *in,
4629effad8dfSPaul Moore 					 const struct net_device *out,
4630effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4631effad8dfSPaul Moore {
4632effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4633effad8dfSPaul Moore }
4634effad8dfSPaul Moore #endif	/* IPV6 */
4635effad8dfSPaul Moore 
4636948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4637948bf85cSPaul Moore 				      u16 family)
4638948bf85cSPaul Moore {
4639948bf85cSPaul Moore 	u32 sid;
4640948bf85cSPaul Moore 
4641948bf85cSPaul Moore 	if (!netlbl_enabled())
4642948bf85cSPaul Moore 		return NF_ACCEPT;
4643948bf85cSPaul Moore 
4644948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4645948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4646948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
4647948bf85cSPaul Moore 	if (skb->sk) {
4648948bf85cSPaul Moore 		struct sk_security_struct *sksec = skb->sk->sk_security;
4649948bf85cSPaul Moore 		sid = sksec->sid;
4650948bf85cSPaul Moore 	} else
4651948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4652948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4653948bf85cSPaul Moore 		return NF_DROP;
4654948bf85cSPaul Moore 
4655948bf85cSPaul Moore 	return NF_ACCEPT;
4656948bf85cSPaul Moore }
4657948bf85cSPaul Moore 
4658948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum,
4659948bf85cSPaul Moore 					struct sk_buff *skb,
4660948bf85cSPaul Moore 					const struct net_device *in,
4661948bf85cSPaul Moore 					const struct net_device *out,
4662948bf85cSPaul Moore 					int (*okfn)(struct sk_buff *))
4663948bf85cSPaul Moore {
4664948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4665948bf85cSPaul Moore }
4666948bf85cSPaul Moore 
4667effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4668effad8dfSPaul Moore 						int ifindex,
4669d8395c87SPaul Moore 						u16 family)
46704e5ab4cbSJames Morris {
4671effad8dfSPaul Moore 	struct sock *sk = skb->sk;
46724237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
46732bf49690SThomas Liu 	struct common_audit_data ad;
467448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4675d8395c87SPaul Moore 	char *addrp;
4676d8395c87SPaul Moore 	u8 proto;
46774e5ab4cbSJames Morris 
4678effad8dfSPaul Moore 	if (sk == NULL)
4679effad8dfSPaul Moore 		return NF_ACCEPT;
46804237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
46814e5ab4cbSJames Morris 
468250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
468348c62af6SEric Paris 	ad.u.net = &net;
468448c62af6SEric Paris 	ad.u.net->netif = ifindex;
468548c62af6SEric Paris 	ad.u.net->family = family;
4686d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4687d8395c87SPaul Moore 		return NF_DROP;
4688d8395c87SPaul Moore 
468958bfbb51SPaul Moore 	if (selinux_secmark_enabled())
4690effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4691d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
46922fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
46931da177e4SLinus Torvalds 
4694d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
46952fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
4696effad8dfSPaul Moore 
4697effad8dfSPaul Moore 	return NF_ACCEPT;
4698effad8dfSPaul Moore }
4699effad8dfSPaul Moore 
4700effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4701effad8dfSPaul Moore 					 u16 family)
4702effad8dfSPaul Moore {
4703effad8dfSPaul Moore 	u32 secmark_perm;
4704effad8dfSPaul Moore 	u32 peer_sid;
4705effad8dfSPaul Moore 	struct sock *sk;
47062bf49690SThomas Liu 	struct common_audit_data ad;
470748c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4708effad8dfSPaul Moore 	char *addrp;
4709effad8dfSPaul Moore 	u8 secmark_active;
4710effad8dfSPaul Moore 	u8 peerlbl_active;
4711effad8dfSPaul Moore 
4712effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4713effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4714effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4715effad8dfSPaul Moore 	 * as fast and as clean as possible. */
471658bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4717d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4718def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
4719effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4720effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4721effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4722effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4723effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4724effad8dfSPaul Moore 	 *       is NULL, in this case go ahead and apply access control. */
4725adf30907SEric Dumazet 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
4726effad8dfSPaul Moore 		return NF_ACCEPT;
4727def8b4faSAlexey Dobriyan #endif
4728effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4729effad8dfSPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4730effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4731effad8dfSPaul Moore 		return NF_ACCEPT;
4732effad8dfSPaul Moore 
4733d8395c87SPaul Moore 	/* if the packet is being forwarded then get the peer label from the
4734d8395c87SPaul Moore 	 * packet itself; otherwise check to see if it is from a local
4735d8395c87SPaul Moore 	 * application or the kernel, if from an application get the peer label
4736d8395c87SPaul Moore 	 * from the sending socket, otherwise use the kernel's sid */
4737effad8dfSPaul Moore 	sk = skb->sk;
4738d8395c87SPaul Moore 	if (sk == NULL) {
47394a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
4740d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
4741d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
474204f6d70fSEric Paris 				return NF_DROP;
47434a7ab3dcSSteffen Klassert 		} else {
47444a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
4745d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
47464a7ab3dcSSteffen Klassert 		}
4747d8395c87SPaul Moore 	} else {
4748effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4749effad8dfSPaul Moore 		peer_sid = sksec->sid;
4750effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4751effad8dfSPaul Moore 	}
4752effad8dfSPaul Moore 
475350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
475448c62af6SEric Paris 	ad.u.net = &net;
475548c62af6SEric Paris 	ad.u.net->netif = ifindex;
475648c62af6SEric Paris 	ad.u.net->family = family;
4757d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
475804f6d70fSEric Paris 		return NF_DROP;
4759d8395c87SPaul Moore 
4760effad8dfSPaul Moore 	if (secmark_active)
4761effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4762effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
47631f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4764effad8dfSPaul Moore 
4765effad8dfSPaul Moore 	if (peerlbl_active) {
4766effad8dfSPaul Moore 		u32 if_sid;
4767effad8dfSPaul Moore 		u32 node_sid;
4768effad8dfSPaul Moore 
4769effad8dfSPaul Moore 		if (sel_netif_sid(ifindex, &if_sid))
477004f6d70fSEric Paris 			return NF_DROP;
4771effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
4772effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
47731f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4774effad8dfSPaul Moore 
4775effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
477604f6d70fSEric Paris 			return NF_DROP;
4777effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
4778effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
47791f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
4780effad8dfSPaul Moore 	}
4781effad8dfSPaul Moore 
4782effad8dfSPaul Moore 	return NF_ACCEPT;
4783effad8dfSPaul Moore }
4784effad8dfSPaul Moore 
4785effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4786a224be76SDavid S. Miller 					   struct sk_buff *skb,
47871da177e4SLinus Torvalds 					   const struct net_device *in,
47881da177e4SLinus Torvalds 					   const struct net_device *out,
47891da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
47901da177e4SLinus Torvalds {
4791effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET);
47921da177e4SLinus Torvalds }
47931da177e4SLinus Torvalds 
47941da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4795effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4796a224be76SDavid S. Miller 					   struct sk_buff *skb,
47971da177e4SLinus Torvalds 					   const struct net_device *in,
47981da177e4SLinus Torvalds 					   const struct net_device *out,
47991da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
48001da177e4SLinus Torvalds {
4801effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
48021da177e4SLinus Torvalds }
48031da177e4SLinus Torvalds #endif	/* IPV6 */
48041da177e4SLinus Torvalds 
48051da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
48061da177e4SLinus Torvalds 
48071da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
48081da177e4SLinus Torvalds {
48091da177e4SLinus Torvalds 	int err;
48101da177e4SLinus Torvalds 
4811200ac532SEric Paris 	err = cap_netlink_send(sk, skb);
48121da177e4SLinus Torvalds 	if (err)
48131da177e4SLinus Torvalds 		return err;
48141da177e4SLinus Torvalds 
4815941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
48161da177e4SLinus Torvalds }
48171da177e4SLinus Torvalds 
48181da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
48191da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
48201da177e4SLinus Torvalds 			      u16 sclass)
48211da177e4SLinus Torvalds {
48221da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
4823275bb41eSDavid Howells 	u32 sid;
48241da177e4SLinus Torvalds 
482589d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
48261da177e4SLinus Torvalds 	if (!isec)
48271da177e4SLinus Torvalds 		return -ENOMEM;
48281da177e4SLinus Torvalds 
4829275bb41eSDavid Howells 	sid = task_sid(task);
48301da177e4SLinus Torvalds 	isec->sclass = sclass;
4831275bb41eSDavid Howells 	isec->sid = sid;
48321da177e4SLinus Torvalds 	perm->security = isec;
48331da177e4SLinus Torvalds 
48341da177e4SLinus Torvalds 	return 0;
48351da177e4SLinus Torvalds }
48361da177e4SLinus Torvalds 
48371da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
48381da177e4SLinus Torvalds {
48391da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
48401da177e4SLinus Torvalds 	perm->security = NULL;
48411da177e4SLinus Torvalds 	kfree(isec);
48421da177e4SLinus Torvalds }
48431da177e4SLinus Torvalds 
48441da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
48451da177e4SLinus Torvalds {
48461da177e4SLinus Torvalds 	struct msg_security_struct *msec;
48471da177e4SLinus Torvalds 
484889d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
48491da177e4SLinus Torvalds 	if (!msec)
48501da177e4SLinus Torvalds 		return -ENOMEM;
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
48531da177e4SLinus Torvalds 	msg->security = msec;
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds 	return 0;
48561da177e4SLinus Torvalds }
48571da177e4SLinus Torvalds 
48581da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
48591da177e4SLinus Torvalds {
48601da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
48611da177e4SLinus Torvalds 
48621da177e4SLinus Torvalds 	msg->security = NULL;
48631da177e4SLinus Torvalds 	kfree(msec);
48641da177e4SLinus Torvalds }
48651da177e4SLinus Torvalds 
48661da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
48676af963f1SStephen Smalley 			u32 perms)
48681da177e4SLinus Torvalds {
48691da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48702bf49690SThomas Liu 	struct common_audit_data ad;
4871275bb41eSDavid Howells 	u32 sid = current_sid();
48721da177e4SLinus Torvalds 
48731da177e4SLinus Torvalds 	isec = ipc_perms->security;
48741da177e4SLinus Torvalds 
487550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
48761da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
48771da177e4SLinus Torvalds 
4878275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
48791da177e4SLinus Torvalds }
48801da177e4SLinus Torvalds 
48811da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
48821da177e4SLinus Torvalds {
48831da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
48841da177e4SLinus Torvalds }
48851da177e4SLinus Torvalds 
48861da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
48871da177e4SLinus Torvalds {
48881da177e4SLinus Torvalds 	msg_msg_free_security(msg);
48891da177e4SLinus Torvalds }
48901da177e4SLinus Torvalds 
48911da177e4SLinus Torvalds /* message queue security operations */
48921da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
48931da177e4SLinus Torvalds {
48941da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48952bf49690SThomas Liu 	struct common_audit_data ad;
4896275bb41eSDavid Howells 	u32 sid = current_sid();
48971da177e4SLinus Torvalds 	int rc;
48981da177e4SLinus Torvalds 
48991da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
49001da177e4SLinus Torvalds 	if (rc)
49011da177e4SLinus Torvalds 		return rc;
49021da177e4SLinus Torvalds 
49031da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49041da177e4SLinus Torvalds 
490550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
49061da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49071da177e4SLinus Torvalds 
4908275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49091da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
49101da177e4SLinus Torvalds 	if (rc) {
49111da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
49121da177e4SLinus Torvalds 		return rc;
49131da177e4SLinus Torvalds 	}
49141da177e4SLinus Torvalds 	return 0;
49151da177e4SLinus Torvalds }
49161da177e4SLinus Torvalds 
49171da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
49181da177e4SLinus Torvalds {
49191da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
49201da177e4SLinus Torvalds }
49211da177e4SLinus Torvalds 
49221da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
49231da177e4SLinus Torvalds {
49241da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49252bf49690SThomas Liu 	struct common_audit_data ad;
4926275bb41eSDavid Howells 	u32 sid = current_sid();
49271da177e4SLinus Torvalds 
49281da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49291da177e4SLinus Torvalds 
493050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
49311da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49321da177e4SLinus Torvalds 
4933275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49341da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
49351da177e4SLinus Torvalds }
49361da177e4SLinus Torvalds 
49371da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
49381da177e4SLinus Torvalds {
49391da177e4SLinus Torvalds 	int err;
49401da177e4SLinus Torvalds 	int perms;
49411da177e4SLinus Torvalds 
49421da177e4SLinus Torvalds 	switch (cmd) {
49431da177e4SLinus Torvalds 	case IPC_INFO:
49441da177e4SLinus Torvalds 	case MSG_INFO:
49451da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
49461da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
49471da177e4SLinus Torvalds 	case IPC_STAT:
49481da177e4SLinus Torvalds 	case MSG_STAT:
49491da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
49501da177e4SLinus Torvalds 		break;
49511da177e4SLinus Torvalds 	case IPC_SET:
49521da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
49531da177e4SLinus Torvalds 		break;
49541da177e4SLinus Torvalds 	case IPC_RMID:
49551da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
49561da177e4SLinus Torvalds 		break;
49571da177e4SLinus Torvalds 	default:
49581da177e4SLinus Torvalds 		return 0;
49591da177e4SLinus Torvalds 	}
49601da177e4SLinus Torvalds 
49616af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
49621da177e4SLinus Torvalds 	return err;
49631da177e4SLinus Torvalds }
49641da177e4SLinus Torvalds 
49651da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
49661da177e4SLinus Torvalds {
49671da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49681da177e4SLinus Torvalds 	struct msg_security_struct *msec;
49692bf49690SThomas Liu 	struct common_audit_data ad;
4970275bb41eSDavid Howells 	u32 sid = current_sid();
49711da177e4SLinus Torvalds 	int rc;
49721da177e4SLinus Torvalds 
49731da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49741da177e4SLinus Torvalds 	msec = msg->security;
49751da177e4SLinus Torvalds 
49761da177e4SLinus Torvalds 	/*
49771da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
49781da177e4SLinus Torvalds 	 */
49791da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
49801da177e4SLinus Torvalds 		/*
49811da177e4SLinus Torvalds 		 * Compute new sid based on current process and
49821da177e4SLinus Torvalds 		 * message queue this message will be stored in
49831da177e4SLinus Torvalds 		 */
4984275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
4985652bb9b0SEric Paris 					     NULL, &msec->sid);
49861da177e4SLinus Torvalds 		if (rc)
49871da177e4SLinus Torvalds 			return rc;
49881da177e4SLinus Torvalds 	}
49891da177e4SLinus Torvalds 
499050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
49911da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49921da177e4SLinus Torvalds 
49931da177e4SLinus Torvalds 	/* Can this process write to the queue? */
4994275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49951da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
49961da177e4SLinus Torvalds 	if (!rc)
49971da177e4SLinus Torvalds 		/* Can this process send the message */
4998275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
4999275bb41eSDavid Howells 				  MSG__SEND, &ad);
50001da177e4SLinus Torvalds 	if (!rc)
50011da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
5002275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5003275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
50041da177e4SLinus Torvalds 
50051da177e4SLinus Torvalds 	return rc;
50061da177e4SLinus Torvalds }
50071da177e4SLinus Torvalds 
50081da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
50091da177e4SLinus Torvalds 				    struct task_struct *target,
50101da177e4SLinus Torvalds 				    long type, int mode)
50111da177e4SLinus Torvalds {
50121da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50131da177e4SLinus Torvalds 	struct msg_security_struct *msec;
50142bf49690SThomas Liu 	struct common_audit_data ad;
5015275bb41eSDavid Howells 	u32 sid = task_sid(target);
50161da177e4SLinus Torvalds 	int rc;
50171da177e4SLinus Torvalds 
50181da177e4SLinus Torvalds 	isec = msq->q_perm.security;
50191da177e4SLinus Torvalds 	msec = msg->security;
50201da177e4SLinus Torvalds 
502150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50221da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
50231da177e4SLinus Torvalds 
5024275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
50251da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
50261da177e4SLinus Torvalds 	if (!rc)
5027275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
50281da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
50291da177e4SLinus Torvalds 	return rc;
50301da177e4SLinus Torvalds }
50311da177e4SLinus Torvalds 
50321da177e4SLinus Torvalds /* Shared Memory security operations */
50331da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
50341da177e4SLinus Torvalds {
50351da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50362bf49690SThomas Liu 	struct common_audit_data ad;
5037275bb41eSDavid Howells 	u32 sid = current_sid();
50381da177e4SLinus Torvalds 	int rc;
50391da177e4SLinus Torvalds 
50401da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
50411da177e4SLinus Torvalds 	if (rc)
50421da177e4SLinus Torvalds 		return rc;
50431da177e4SLinus Torvalds 
50441da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
50451da177e4SLinus Torvalds 
504650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50471da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
50481da177e4SLinus Torvalds 
5049275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
50501da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
50511da177e4SLinus Torvalds 	if (rc) {
50521da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
50531da177e4SLinus Torvalds 		return rc;
50541da177e4SLinus Torvalds 	}
50551da177e4SLinus Torvalds 	return 0;
50561da177e4SLinus Torvalds }
50571da177e4SLinus Torvalds 
50581da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
50591da177e4SLinus Torvalds {
50601da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
50611da177e4SLinus Torvalds }
50621da177e4SLinus Torvalds 
50631da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
50641da177e4SLinus Torvalds {
50651da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50662bf49690SThomas Liu 	struct common_audit_data ad;
5067275bb41eSDavid Howells 	u32 sid = current_sid();
50681da177e4SLinus Torvalds 
50691da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
50701da177e4SLinus Torvalds 
507150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
50721da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
50731da177e4SLinus Torvalds 
5074275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
50751da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
50761da177e4SLinus Torvalds }
50771da177e4SLinus Torvalds 
50781da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
50791da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
50801da177e4SLinus Torvalds {
50811da177e4SLinus Torvalds 	int perms;
50821da177e4SLinus Torvalds 	int err;
50831da177e4SLinus Torvalds 
50841da177e4SLinus Torvalds 	switch (cmd) {
50851da177e4SLinus Torvalds 	case IPC_INFO:
50861da177e4SLinus Torvalds 	case SHM_INFO:
50871da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
50881da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
50891da177e4SLinus Torvalds 	case IPC_STAT:
50901da177e4SLinus Torvalds 	case SHM_STAT:
50911da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
50921da177e4SLinus Torvalds 		break;
50931da177e4SLinus Torvalds 	case IPC_SET:
50941da177e4SLinus Torvalds 		perms = SHM__SETATTR;
50951da177e4SLinus Torvalds 		break;
50961da177e4SLinus Torvalds 	case SHM_LOCK:
50971da177e4SLinus Torvalds 	case SHM_UNLOCK:
50981da177e4SLinus Torvalds 		perms = SHM__LOCK;
50991da177e4SLinus Torvalds 		break;
51001da177e4SLinus Torvalds 	case IPC_RMID:
51011da177e4SLinus Torvalds 		perms = SHM__DESTROY;
51021da177e4SLinus Torvalds 		break;
51031da177e4SLinus Torvalds 	default:
51041da177e4SLinus Torvalds 		return 0;
51051da177e4SLinus Torvalds 	}
51061da177e4SLinus Torvalds 
51076af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
51081da177e4SLinus Torvalds 	return err;
51091da177e4SLinus Torvalds }
51101da177e4SLinus Torvalds 
51111da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
51121da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
51131da177e4SLinus Torvalds {
51141da177e4SLinus Torvalds 	u32 perms;
51151da177e4SLinus Torvalds 
51161da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
51171da177e4SLinus Torvalds 		perms = SHM__READ;
51181da177e4SLinus Torvalds 	else
51191da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
51201da177e4SLinus Torvalds 
51216af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
51221da177e4SLinus Torvalds }
51231da177e4SLinus Torvalds 
51241da177e4SLinus Torvalds /* Semaphore security operations */
51251da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
51261da177e4SLinus Torvalds {
51271da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51282bf49690SThomas Liu 	struct common_audit_data ad;
5129275bb41eSDavid Howells 	u32 sid = current_sid();
51301da177e4SLinus Torvalds 	int rc;
51311da177e4SLinus Torvalds 
51321da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
51331da177e4SLinus Torvalds 	if (rc)
51341da177e4SLinus Torvalds 		return rc;
51351da177e4SLinus Torvalds 
51361da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
51371da177e4SLinus Torvalds 
513850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51391da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
51401da177e4SLinus Torvalds 
5141275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
51421da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
51431da177e4SLinus Torvalds 	if (rc) {
51441da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
51451da177e4SLinus Torvalds 		return rc;
51461da177e4SLinus Torvalds 	}
51471da177e4SLinus Torvalds 	return 0;
51481da177e4SLinus Torvalds }
51491da177e4SLinus Torvalds 
51501da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
51511da177e4SLinus Torvalds {
51521da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
51531da177e4SLinus Torvalds }
51541da177e4SLinus Torvalds 
51551da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
51561da177e4SLinus Torvalds {
51571da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51582bf49690SThomas Liu 	struct common_audit_data ad;
5159275bb41eSDavid Howells 	u32 sid = current_sid();
51601da177e4SLinus Torvalds 
51611da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
51621da177e4SLinus Torvalds 
516350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51641da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
51651da177e4SLinus Torvalds 
5166275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
51671da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
51681da177e4SLinus Torvalds }
51691da177e4SLinus Torvalds 
51701da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
51711da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
51721da177e4SLinus Torvalds {
51731da177e4SLinus Torvalds 	int err;
51741da177e4SLinus Torvalds 	u32 perms;
51751da177e4SLinus Torvalds 
51761da177e4SLinus Torvalds 	switch (cmd) {
51771da177e4SLinus Torvalds 	case IPC_INFO:
51781da177e4SLinus Torvalds 	case SEM_INFO:
51791da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
51801da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
51811da177e4SLinus Torvalds 	case GETPID:
51821da177e4SLinus Torvalds 	case GETNCNT:
51831da177e4SLinus Torvalds 	case GETZCNT:
51841da177e4SLinus Torvalds 		perms = SEM__GETATTR;
51851da177e4SLinus Torvalds 		break;
51861da177e4SLinus Torvalds 	case GETVAL:
51871da177e4SLinus Torvalds 	case GETALL:
51881da177e4SLinus Torvalds 		perms = SEM__READ;
51891da177e4SLinus Torvalds 		break;
51901da177e4SLinus Torvalds 	case SETVAL:
51911da177e4SLinus Torvalds 	case SETALL:
51921da177e4SLinus Torvalds 		perms = SEM__WRITE;
51931da177e4SLinus Torvalds 		break;
51941da177e4SLinus Torvalds 	case IPC_RMID:
51951da177e4SLinus Torvalds 		perms = SEM__DESTROY;
51961da177e4SLinus Torvalds 		break;
51971da177e4SLinus Torvalds 	case IPC_SET:
51981da177e4SLinus Torvalds 		perms = SEM__SETATTR;
51991da177e4SLinus Torvalds 		break;
52001da177e4SLinus Torvalds 	case IPC_STAT:
52011da177e4SLinus Torvalds 	case SEM_STAT:
52021da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
52031da177e4SLinus Torvalds 		break;
52041da177e4SLinus Torvalds 	default:
52051da177e4SLinus Torvalds 		return 0;
52061da177e4SLinus Torvalds 	}
52071da177e4SLinus Torvalds 
52086af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
52091da177e4SLinus Torvalds 	return err;
52101da177e4SLinus Torvalds }
52111da177e4SLinus Torvalds 
52121da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
52131da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
52141da177e4SLinus Torvalds {
52151da177e4SLinus Torvalds 	u32 perms;
52161da177e4SLinus Torvalds 
52171da177e4SLinus Torvalds 	if (alter)
52181da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
52191da177e4SLinus Torvalds 	else
52201da177e4SLinus Torvalds 		perms = SEM__READ;
52211da177e4SLinus Torvalds 
52226af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
52231da177e4SLinus Torvalds }
52241da177e4SLinus Torvalds 
52251da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
52261da177e4SLinus Torvalds {
52271da177e4SLinus Torvalds 	u32 av = 0;
52281da177e4SLinus Torvalds 
52291da177e4SLinus Torvalds 	av = 0;
52301da177e4SLinus Torvalds 	if (flag & S_IRUGO)
52311da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
52321da177e4SLinus Torvalds 	if (flag & S_IWUGO)
52331da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
52341da177e4SLinus Torvalds 
52351da177e4SLinus Torvalds 	if (av == 0)
52361da177e4SLinus Torvalds 		return 0;
52371da177e4SLinus Torvalds 
52386af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
52391da177e4SLinus Torvalds }
52401da177e4SLinus Torvalds 
5241713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5242713a04aeSAhmed S. Darwish {
5243713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5244713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5245713a04aeSAhmed S. Darwish }
5246713a04aeSAhmed S. Darwish 
52471da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
52481da177e4SLinus Torvalds {
52491da177e4SLinus Torvalds 	if (inode)
52501da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
52511da177e4SLinus Torvalds }
52521da177e4SLinus Torvalds 
52531da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
525404ff9708SAl Viro 			       char *name, char **value)
52551da177e4SLinus Torvalds {
5256275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
52578c8570fbSDustin Kirkland 	u32 sid;
52581da177e4SLinus Torvalds 	int error;
525904ff9708SAl Viro 	unsigned len;
52601da177e4SLinus Torvalds 
52611da177e4SLinus Torvalds 	if (current != p) {
52623b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
52631da177e4SLinus Torvalds 		if (error)
52641da177e4SLinus Torvalds 			return error;
52651da177e4SLinus Torvalds 	}
52661da177e4SLinus Torvalds 
5267275bb41eSDavid Howells 	rcu_read_lock();
5268275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
52691da177e4SLinus Torvalds 
52701da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5271275bb41eSDavid Howells 		sid = __tsec->sid;
52721da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5273275bb41eSDavid Howells 		sid = __tsec->osid;
52741da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5275275bb41eSDavid Howells 		sid = __tsec->exec_sid;
52761da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5277275bb41eSDavid Howells 		sid = __tsec->create_sid;
52784eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5279275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
528042c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5281275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
52821da177e4SLinus Torvalds 	else
5283275bb41eSDavid Howells 		goto invalid;
5284275bb41eSDavid Howells 	rcu_read_unlock();
52851da177e4SLinus Torvalds 
52861da177e4SLinus Torvalds 	if (!sid)
52871da177e4SLinus Torvalds 		return 0;
52881da177e4SLinus Torvalds 
528904ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
529004ff9708SAl Viro 	if (error)
529104ff9708SAl Viro 		return error;
529204ff9708SAl Viro 	return len;
5293275bb41eSDavid Howells 
5294275bb41eSDavid Howells invalid:
5295275bb41eSDavid Howells 	rcu_read_unlock();
5296275bb41eSDavid Howells 	return -EINVAL;
52971da177e4SLinus Torvalds }
52981da177e4SLinus Torvalds 
52991da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
53001da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
53011da177e4SLinus Torvalds {
53021da177e4SLinus Torvalds 	struct task_security_struct *tsec;
53030356357cSRoland McGrath 	struct task_struct *tracer;
5304d84f4f99SDavid Howells 	struct cred *new;
5305d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
53061da177e4SLinus Torvalds 	int error;
53071da177e4SLinus Torvalds 	char *str = value;
53081da177e4SLinus Torvalds 
53091da177e4SLinus Torvalds 	if (current != p) {
53101da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
53111da177e4SLinus Torvalds 		   security attributes. */
53121da177e4SLinus Torvalds 		return -EACCES;
53131da177e4SLinus Torvalds 	}
53141da177e4SLinus Torvalds 
53151da177e4SLinus Torvalds 	/*
53161da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
53171da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
53181da177e4SLinus Torvalds 	 * above restriction is ever removed.
53191da177e4SLinus Torvalds 	 */
53201da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
53213b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
53221da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
53233b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
53244eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
53253b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
532642c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
53273b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
53281da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
53293b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
53301da177e4SLinus Torvalds 	else
53311da177e4SLinus Torvalds 		error = -EINVAL;
53321da177e4SLinus Torvalds 	if (error)
53331da177e4SLinus Torvalds 		return error;
53341da177e4SLinus Torvalds 
53351da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
53361da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
53371da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
53381da177e4SLinus Torvalds 			str[size-1] = 0;
53391da177e4SLinus Torvalds 			size--;
53401da177e4SLinus Torvalds 		}
53411da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
534212b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
5343d6ea83ecSEric Paris 			if (!capable(CAP_MAC_ADMIN)) {
5344d6ea83ecSEric Paris 				struct audit_buffer *ab;
5345d6ea83ecSEric Paris 				size_t audit_size;
5346d6ea83ecSEric Paris 
5347d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
5348d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
5349d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
5350d6ea83ecSEric Paris 					audit_size = size - 1;
5351d6ea83ecSEric Paris 				else
5352d6ea83ecSEric Paris 					audit_size = size;
5353d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5354d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
5355d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
5356d6ea83ecSEric Paris 				audit_log_end(ab);
5357d6ea83ecSEric Paris 
535812b29f34SStephen Smalley 				return error;
5359d6ea83ecSEric Paris 			}
536012b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
536112b29f34SStephen Smalley 							      &sid);
536212b29f34SStephen Smalley 		}
53631da177e4SLinus Torvalds 		if (error)
53641da177e4SLinus Torvalds 			return error;
53651da177e4SLinus Torvalds 	}
53661da177e4SLinus Torvalds 
5367d84f4f99SDavid Howells 	new = prepare_creds();
5368d84f4f99SDavid Howells 	if (!new)
5369d84f4f99SDavid Howells 		return -ENOMEM;
5370d84f4f99SDavid Howells 
53711da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
53721da177e4SLinus Torvalds 	   performed during the actual operation (execve,
53731da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5374d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
53751da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
53761da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5377d84f4f99SDavid Howells 	tsec = new->security;
5378d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
53791da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5380d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
53811da177e4SLinus Torvalds 		tsec->create_sid = sid;
5382d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
53834eb582cfSMichael LeMay 		error = may_create_key(sid, p);
53844eb582cfSMichael LeMay 		if (error)
5385d84f4f99SDavid Howells 			goto abort_change;
53864eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5387d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
538842c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5389d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5390d84f4f99SDavid Howells 		error = -EINVAL;
53911da177e4SLinus Torvalds 		if (sid == 0)
5392d84f4f99SDavid Howells 			goto abort_change;
5393d9250deaSKaiGai Kohei 
5394d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5395d84f4f99SDavid Howells 		error = -EPERM;
53965bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
5397d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5398d84f4f99SDavid Howells 			if (error)
5399d84f4f99SDavid Howells 				goto abort_change;
54001da177e4SLinus Torvalds 		}
54011da177e4SLinus Torvalds 
54021da177e4SLinus Torvalds 		/* Check permissions for the transition. */
54031da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
54041da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
54051da177e4SLinus Torvalds 		if (error)
5406d84f4f99SDavid Howells 			goto abort_change;
54071da177e4SLinus Torvalds 
54081da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
54091da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5410d84f4f99SDavid Howells 		ptsid = 0;
54111da177e4SLinus Torvalds 		task_lock(p);
541206d98473STejun Heo 		tracer = ptrace_parent(p);
5413d84f4f99SDavid Howells 		if (tracer)
5414d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
54151da177e4SLinus Torvalds 		task_unlock(p);
54161da177e4SLinus Torvalds 
5417d84f4f99SDavid Howells 		if (tracer) {
5418d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5419d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5420d84f4f99SDavid Howells 			if (error)
5421d84f4f99SDavid Howells 				goto abort_change;
5422d84f4f99SDavid Howells 		}
5423d84f4f99SDavid Howells 
5424d84f4f99SDavid Howells 		tsec->sid = sid;
5425d84f4f99SDavid Howells 	} else {
5426d84f4f99SDavid Howells 		error = -EINVAL;
5427d84f4f99SDavid Howells 		goto abort_change;
5428d84f4f99SDavid Howells 	}
5429d84f4f99SDavid Howells 
5430d84f4f99SDavid Howells 	commit_creds(new);
54311da177e4SLinus Torvalds 	return size;
5432d84f4f99SDavid Howells 
5433d84f4f99SDavid Howells abort_change:
5434d84f4f99SDavid Howells 	abort_creds(new);
5435d84f4f99SDavid Howells 	return error;
54361da177e4SLinus Torvalds }
54371da177e4SLinus Torvalds 
5438dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5439dc49c1f9SCatherine Zhang {
5440dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5441dc49c1f9SCatherine Zhang }
5442dc49c1f9SCatherine Zhang 
54437bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
544463cb3449SDavid Howells {
544563cb3449SDavid Howells 	return security_context_to_sid(secdata, seclen, secid);
544663cb3449SDavid Howells }
544763cb3449SDavid Howells 
5448dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5449dc49c1f9SCatherine Zhang {
5450dc49c1f9SCatherine Zhang 	kfree(secdata);
5451dc49c1f9SCatherine Zhang }
5452dc49c1f9SCatherine Zhang 
54531ee65e37SDavid P. Quigley /*
54541ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
54551ee65e37SDavid P. Quigley  */
54561ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
54571ee65e37SDavid P. Quigley {
54581ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
54591ee65e37SDavid P. Quigley }
54601ee65e37SDavid P. Quigley 
54611ee65e37SDavid P. Quigley /*
54621ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
54631ee65e37SDavid P. Quigley  */
54641ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
54651ee65e37SDavid P. Quigley {
54661ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
54671ee65e37SDavid P. Quigley }
54681ee65e37SDavid P. Quigley 
54691ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
54701ee65e37SDavid P. Quigley {
54711ee65e37SDavid P. Quigley 	int len = 0;
54721ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
54731ee65e37SDavid P. Quigley 						ctx, true);
54741ee65e37SDavid P. Quigley 	if (len < 0)
54751ee65e37SDavid P. Quigley 		return len;
54761ee65e37SDavid P. Quigley 	*ctxlen = len;
54771ee65e37SDavid P. Quigley 	return 0;
54781ee65e37SDavid P. Quigley }
5479d720024eSMichael LeMay #ifdef CONFIG_KEYS
5480d720024eSMichael LeMay 
5481d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
54827e047ef5SDavid Howells 			     unsigned long flags)
5483d720024eSMichael LeMay {
5484d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5485d720024eSMichael LeMay 	struct key_security_struct *ksec;
5486d720024eSMichael LeMay 
5487d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5488d720024eSMichael LeMay 	if (!ksec)
5489d720024eSMichael LeMay 		return -ENOMEM;
5490d720024eSMichael LeMay 
5491d84f4f99SDavid Howells 	tsec = cred->security;
5492d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5493d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
54944eb582cfSMichael LeMay 	else
5495d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5496d720024eSMichael LeMay 
5497275bb41eSDavid Howells 	k->security = ksec;
5498d720024eSMichael LeMay 	return 0;
5499d720024eSMichael LeMay }
5500d720024eSMichael LeMay 
5501d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5502d720024eSMichael LeMay {
5503d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5504d720024eSMichael LeMay 
5505d720024eSMichael LeMay 	k->security = NULL;
5506d720024eSMichael LeMay 	kfree(ksec);
5507d720024eSMichael LeMay }
5508d720024eSMichael LeMay 
5509d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5510d84f4f99SDavid Howells 				  const struct cred *cred,
5511d720024eSMichael LeMay 				  key_perm_t perm)
5512d720024eSMichael LeMay {
5513d720024eSMichael LeMay 	struct key *key;
5514d720024eSMichael LeMay 	struct key_security_struct *ksec;
5515275bb41eSDavid Howells 	u32 sid;
5516d720024eSMichael LeMay 
5517d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5518d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5519d720024eSMichael LeMay 	   appear to be created. */
5520d720024eSMichael LeMay 	if (perm == 0)
5521d720024eSMichael LeMay 		return 0;
5522d720024eSMichael LeMay 
5523d84f4f99SDavid Howells 	sid = cred_sid(cred);
5524275bb41eSDavid Howells 
5525275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5526275bb41eSDavid Howells 	ksec = key->security;
5527275bb41eSDavid Howells 
5528275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5529d720024eSMichael LeMay }
5530d720024eSMichael LeMay 
553170a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
553270a5bb72SDavid Howells {
553370a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
553470a5bb72SDavid Howells 	char *context = NULL;
553570a5bb72SDavid Howells 	unsigned len;
553670a5bb72SDavid Howells 	int rc;
553770a5bb72SDavid Howells 
553870a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
553970a5bb72SDavid Howells 	if (!rc)
554070a5bb72SDavid Howells 		rc = len;
554170a5bb72SDavid Howells 	*_buffer = context;
554270a5bb72SDavid Howells 	return rc;
554370a5bb72SDavid Howells }
554470a5bb72SDavid Howells 
5545d720024eSMichael LeMay #endif
5546d720024eSMichael LeMay 
55471da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5548076c54c5SAhmed S. Darwish 	.name =				"selinux",
5549076c54c5SAhmed S. Darwish 
55509e48858fSIngo Molnar 	.ptrace_access_check =		selinux_ptrace_access_check,
55515cd9c58fSDavid Howells 	.ptrace_traceme =		selinux_ptrace_traceme,
55521da177e4SLinus Torvalds 	.capget =			selinux_capget,
5553d84f4f99SDavid Howells 	.capset =			selinux_capset,
55541da177e4SLinus Torvalds 	.capable =			selinux_capable,
55551da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
55561da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
55571da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
55581da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
55591da177e4SLinus Torvalds 
55601da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
55611da177e4SLinus Torvalds 
5562a6f76f23SDavid Howells 	.bprm_set_creds =		selinux_bprm_set_creds,
5563a6f76f23SDavid Howells 	.bprm_committing_creds =	selinux_bprm_committing_creds,
5564a6f76f23SDavid Howells 	.bprm_committed_creds =		selinux_bprm_committed_creds,
55651da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
55661da177e4SLinus Torvalds 
55671da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
55681da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
55691da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
5570026eb167SEric Paris 	.sb_remount =			selinux_sb_remount,
55711da177e4SLinus Torvalds 	.sb_kern_mount =		selinux_sb_kern_mount,
55722069f457SEric Paris 	.sb_show_options =		selinux_sb_show_options,
55731da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
55741da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
55751da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5576c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5577c9180a57SEric Paris 	.sb_clone_mnt_opts =		selinux_sb_clone_mnt_opts,
5578e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5579e0007529SEric Paris 
55801da177e4SLinus Torvalds 
55811da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
55821da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
55835e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
55841da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
55851da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
55861da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
55871da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
55881da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
55891da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
55901da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
55911da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
55921da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
55931da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
55941da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
55951da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
55961da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
55971da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
55981da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
55991da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
56001da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
56011da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
56021da177e4SLinus Torvalds 	.inode_getsecurity =		selinux_inode_getsecurity,
56031da177e4SLinus Torvalds 	.inode_setsecurity =		selinux_inode_setsecurity,
56041da177e4SLinus Torvalds 	.inode_listsecurity =		selinux_inode_listsecurity,
5605713a04aeSAhmed S. Darwish 	.inode_getsecid =		selinux_inode_getsecid,
56061da177e4SLinus Torvalds 
56071da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
56081da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
56091da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
56101da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
5611e5467859SAl Viro 	.mmap_file =			selinux_mmap_file,
5612e5467859SAl Viro 	.mmap_addr =			selinux_mmap_addr,
56131da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
56141da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
56151da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
56161da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
56171da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
56181da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
56191da177e4SLinus Torvalds 
562083d49856SEric Paris 	.file_open =			selinux_file_open,
5621788e7dd4SYuichi Nakamura 
56221da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
5623ee18d64cSDavid Howells 	.cred_alloc_blank =		selinux_cred_alloc_blank,
5624f1752eecSDavid Howells 	.cred_free =			selinux_cred_free,
5625d84f4f99SDavid Howells 	.cred_prepare =			selinux_cred_prepare,
5626ee18d64cSDavid Howells 	.cred_transfer =		selinux_cred_transfer,
56273a3b7ce9SDavid Howells 	.kernel_act_as =		selinux_kernel_act_as,
56283a3b7ce9SDavid Howells 	.kernel_create_files_as =	selinux_kernel_create_files_as,
562925354c4fSEric Paris 	.kernel_module_request =	selinux_kernel_module_request,
56301da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
56311da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
56321da177e4SLinus Torvalds 	.task_getsid =			selinux_task_getsid,
5633f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
56341da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
563503e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5636a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
56371da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
56381da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
56391da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
564035601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
56411da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
56421da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
56431da177e4SLinus Torvalds 	.task_to_inode =		selinux_task_to_inode,
56441da177e4SLinus Torvalds 
56451da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5646713a04aeSAhmed S. Darwish 	.ipc_getsecid =			selinux_ipc_getsecid,
56471da177e4SLinus Torvalds 
56481da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
56491da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
56501da177e4SLinus Torvalds 
56511da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
56521da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
56531da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
56541da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
56551da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
56561da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
56571da177e4SLinus Torvalds 
56581da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
56591da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
56601da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
56611da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
56621da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
56631da177e4SLinus Torvalds 
56641da177e4SLinus Torvalds 	.sem_alloc_security =		selinux_sem_alloc_security,
56651da177e4SLinus Torvalds 	.sem_free_security =		selinux_sem_free_security,
56661da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
56671da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
56681da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
56691da177e4SLinus Torvalds 
56701da177e4SLinus Torvalds 	.d_instantiate =		selinux_d_instantiate,
56711da177e4SLinus Torvalds 
56721da177e4SLinus Torvalds 	.getprocattr =			selinux_getprocattr,
56731da177e4SLinus Torvalds 	.setprocattr =			selinux_setprocattr,
56741da177e4SLinus Torvalds 
5675dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
567663cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5677dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
56781ee65e37SDavid P. Quigley 	.inode_notifysecctx =		selinux_inode_notifysecctx,
56791ee65e37SDavid P. Quigley 	.inode_setsecctx =		selinux_inode_setsecctx,
56801ee65e37SDavid P. Quigley 	.inode_getsecctx =		selinux_inode_getsecctx,
5681dc49c1f9SCatherine Zhang 
56821da177e4SLinus Torvalds 	.unix_stream_connect =		selinux_socket_unix_stream_connect,
56831da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
56841da177e4SLinus Torvalds 
56851da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
56861da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
56871da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
56881da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
56891da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
56901da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
56911da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
56921da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
56931da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
56941da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
56951da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
56961da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
56971da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
56981da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
56992c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
57002c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
57011da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
57021da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
5703892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
5704beb8d13bSVenkat Yekkirala 	.sk_getsecid =			selinux_sk_getsecid,
57054237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
57064237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
57074237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
57086b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
57092606fd1fSEric Paris 	.secmark_relabel_packet =	selinux_secmark_relabel_packet,
57102606fd1fSEric Paris 	.secmark_refcount_inc =		selinux_secmark_refcount_inc,
57112606fd1fSEric Paris 	.secmark_refcount_dec =		selinux_secmark_refcount_dec,
57124237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
57135dbbaf2dSPaul Moore 	.tun_dev_alloc_security =	selinux_tun_dev_alloc_security,
57145dbbaf2dSPaul Moore 	.tun_dev_free_security =	selinux_tun_dev_free_security,
5715ed6d76e4SPaul Moore 	.tun_dev_create =		selinux_tun_dev_create,
57165dbbaf2dSPaul Moore 	.tun_dev_attach_queue =		selinux_tun_dev_attach_queue,
5717ed6d76e4SPaul Moore 	.tun_dev_attach =		selinux_tun_dev_attach,
57185dbbaf2dSPaul Moore 	.tun_dev_open =			selinux_tun_dev_open,
5719ca10b9e9SEric Dumazet 	.skb_owned_by =			selinux_skb_owned_by,
5720d28d1e08STrent Jaeger 
5721d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5722d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
5723d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
5724d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
5725c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
57262e5aa866SPaul Moore 	.xfrm_state_alloc =		selinux_xfrm_state_alloc,
57272e5aa866SPaul Moore 	.xfrm_state_alloc_acquire =	selinux_xfrm_state_alloc_acquire,
5728d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
5729c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
5730d28d1e08STrent Jaeger 	.xfrm_policy_lookup =		selinux_xfrm_policy_lookup,
5731e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
5732e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
57331da177e4SLinus Torvalds #endif
5734d720024eSMichael LeMay 
5735d720024eSMichael LeMay #ifdef CONFIG_KEYS
5736d720024eSMichael LeMay 	.key_alloc =			selinux_key_alloc,
5737d720024eSMichael LeMay 	.key_free =			selinux_key_free,
5738d720024eSMichael LeMay 	.key_permission =		selinux_key_permission,
573970a5bb72SDavid Howells 	.key_getsecurity =		selinux_key_getsecurity,
5740d720024eSMichael LeMay #endif
57419d57a7f9SAhmed S. Darwish 
57429d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
57439d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
57449d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
57459d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
57469d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
57479d57a7f9SAhmed S. Darwish #endif
57481da177e4SLinus Torvalds };
57491da177e4SLinus Torvalds 
57501da177e4SLinus Torvalds static __init int selinux_init(void)
57511da177e4SLinus Torvalds {
5752076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
5753076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
5754076c54c5SAhmed S. Darwish 		return 0;
5755076c54c5SAhmed S. Darwish 	}
5756076c54c5SAhmed S. Darwish 
57571da177e4SLinus Torvalds 	if (!selinux_enabled) {
57581da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
57591da177e4SLinus Torvalds 		return 0;
57601da177e4SLinus Torvalds 	}
57611da177e4SLinus Torvalds 
57621da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
57631da177e4SLinus Torvalds 
57641da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
5765d84f4f99SDavid Howells 	cred_init_security();
57661da177e4SLinus Torvalds 
5767fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5768fcaaade1SStephen Smalley 
57697cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
57707cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
577120c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
57721da177e4SLinus Torvalds 	avc_init();
57731da177e4SLinus Torvalds 
57741da177e4SLinus Torvalds 	if (register_security(&selinux_ops))
57751da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
57761da177e4SLinus Torvalds 
5777828dfe1dSEric Paris 	if (selinux_enforcing)
5778fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
5779828dfe1dSEric Paris 	else
5780fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
5781d720024eSMichael LeMay 
57821da177e4SLinus Torvalds 	return 0;
57831da177e4SLinus Torvalds }
57841da177e4SLinus Torvalds 
5785e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
5786e8c26255SAl Viro {
5787e8c26255SAl Viro 	superblock_doinit(sb, NULL);
5788e8c26255SAl Viro }
5789e8c26255SAl Viro 
57901da177e4SLinus Torvalds void selinux_complete_init(void)
57911da177e4SLinus Torvalds {
5792fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
57931da177e4SLinus Torvalds 
57941da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
5795fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
5796e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
57971da177e4SLinus Torvalds }
57981da177e4SLinus Torvalds 
57991da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
58001da177e4SLinus Torvalds    all processes and objects when they are created. */
58011da177e4SLinus Torvalds security_initcall(selinux_init);
58021da177e4SLinus Torvalds 
5803c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
58041da177e4SLinus Torvalds 
5805effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = {
5806effad8dfSPaul Moore 	{
5807effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
58081da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
58092597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
58106e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
58111da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
5812effad8dfSPaul Moore 	},
5813effad8dfSPaul Moore 	{
5814effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
5815effad8dfSPaul Moore 		.owner =	THIS_MODULE,
58162597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
5817effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5818effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5819948bf85cSPaul Moore 	},
5820948bf85cSPaul Moore 	{
5821948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
5822948bf85cSPaul Moore 		.owner =	THIS_MODULE,
58232597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
5824948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
5825948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5826effad8dfSPaul Moore 	}
58271da177e4SLinus Torvalds };
58281da177e4SLinus Torvalds 
58291da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
58301da177e4SLinus Torvalds 
5831effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = {
5832effad8dfSPaul Moore 	{
5833effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
58341da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
58352597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
58366e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
58371da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
5838effad8dfSPaul Moore 	},
5839effad8dfSPaul Moore 	{
5840effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
5841effad8dfSPaul Moore 		.owner =	THIS_MODULE,
58422597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
5843effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5844effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
5845effad8dfSPaul Moore 	}
58461da177e4SLinus Torvalds };
58471da177e4SLinus Torvalds 
58481da177e4SLinus Torvalds #endif	/* IPV6 */
58491da177e4SLinus Torvalds 
58501da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
58511da177e4SLinus Torvalds {
58521da177e4SLinus Torvalds 	int err = 0;
58531da177e4SLinus Torvalds 
58541da177e4SLinus Torvalds 	if (!selinux_enabled)
58551da177e4SLinus Torvalds 		goto out;
58561da177e4SLinus Torvalds 
5857fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
58581da177e4SLinus Torvalds 
58596c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
58601da177e4SLinus Torvalds 	if (err)
58616c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
58621da177e4SLinus Torvalds 
58631da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
58646c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
58651da177e4SLinus Torvalds 	if (err)
58666c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
58671da177e4SLinus Torvalds #endif	/* IPV6 */
5868d28d1e08STrent Jaeger 
58691da177e4SLinus Torvalds out:
58701da177e4SLinus Torvalds 	return err;
58711da177e4SLinus Torvalds }
58721da177e4SLinus Torvalds 
58731da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
58741da177e4SLinus Torvalds 
58751da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
58761da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
58771da177e4SLinus Torvalds {
5878fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
58791da177e4SLinus Torvalds 
58806c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
58811da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
58826c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
58831da177e4SLinus Torvalds #endif	/* IPV6 */
58841da177e4SLinus Torvalds }
58851da177e4SLinus Torvalds #endif
58861da177e4SLinus Torvalds 
5887c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
58881da177e4SLinus Torvalds 
58891da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
58901da177e4SLinus Torvalds #define selinux_nf_ip_exit()
58911da177e4SLinus Torvalds #endif
58921da177e4SLinus Torvalds 
5893c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
58941da177e4SLinus Torvalds 
58951da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
5896828dfe1dSEric Paris static int selinux_disabled;
5897828dfe1dSEric Paris 
58981da177e4SLinus Torvalds int selinux_disable(void)
58991da177e4SLinus Torvalds {
59001da177e4SLinus Torvalds 	if (ss_initialized) {
59011da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
59021da177e4SLinus Torvalds 		return -EINVAL;
59031da177e4SLinus Torvalds 	}
59041da177e4SLinus Torvalds 
59051da177e4SLinus Torvalds 	if (selinux_disabled) {
59061da177e4SLinus Torvalds 		/* Only do this once. */
59071da177e4SLinus Torvalds 		return -EINVAL;
59081da177e4SLinus Torvalds 	}
59091da177e4SLinus Torvalds 
59101da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
59111da177e4SLinus Torvalds 
59121da177e4SLinus Torvalds 	selinux_disabled = 1;
591330d55280SStephen Smalley 	selinux_enabled = 0;
59141da177e4SLinus Torvalds 
5915189b3b1cSwzt.wzt@gmail.com 	reset_security_ops();
59161da177e4SLinus Torvalds 
5917af8ff049SEric Paris 	/* Try to destroy the avc node cache */
5918af8ff049SEric Paris 	avc_disable();
5919af8ff049SEric Paris 
59201da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
59211da177e4SLinus Torvalds 	selinux_nf_ip_exit();
59221da177e4SLinus Torvalds 
59231da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
59241da177e4SLinus Torvalds 	exit_sel_fs();
59251da177e4SLinus Torvalds 
59261da177e4SLinus Torvalds 	return 0;
59271da177e4SLinus Torvalds }
59281da177e4SLinus Torvalds #endif
5929