xref: /openbmc/linux/security/selinux/hooks.c (revision 389fb800ac8be2832efedd19978a2b8ced37eb61)
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>
16effad8dfSPaul Moore  *  Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
17effad8dfSPaul Moore  *		Paul Moore <paul.moore@hp.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>
271da177e4SLinus Torvalds #include <linux/kernel.h>
280d094efeSRoland McGrath #include <linux/tracehook.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/sched.h>
311da177e4SLinus Torvalds #include <linux/security.h>
321da177e4SLinus Torvalds #include <linux/xattr.h>
331da177e4SLinus Torvalds #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/unistd.h>
351da177e4SLinus Torvalds #include <linux/mm.h>
361da177e4SLinus Torvalds #include <linux/mman.h>
371da177e4SLinus Torvalds #include <linux/slab.h>
381da177e4SLinus Torvalds #include <linux/pagemap.h>
391da177e4SLinus Torvalds #include <linux/swap.h>
401da177e4SLinus Torvalds #include <linux/spinlock.h>
411da177e4SLinus Torvalds #include <linux/syscalls.h>
421da177e4SLinus Torvalds #include <linux/file.h>
439f3acc31SAl Viro #include <linux/fdtable.h>
441da177e4SLinus Torvalds #include <linux/namei.h>
451da177e4SLinus Torvalds #include <linux/mount.h>
461da177e4SLinus Torvalds #include <linux/proc_fs.h>
471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
491da177e4SLinus Torvalds #include <linux/tty.h>
501da177e4SLinus Torvalds #include <net/icmp.h>
51227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
521da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
53220deb96SPaul Moore #include <net/net_namespace.h>
54d621d35eSPaul Moore #include <net/netlabel.h>
55f5269710SEric Paris #include <linux/uaccess.h>
561da177e4SLinus Torvalds #include <asm/ioctls.h>
57d621d35eSPaul Moore #include <asm/atomic.h>
581da177e4SLinus Torvalds #include <linux/bitops.h>
591da177e4SLinus Torvalds #include <linux/interrupt.h>
601da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
611da177e4SLinus Torvalds #include <linux/netlink.h>
621da177e4SLinus Torvalds #include <linux/tcp.h>
631da177e4SLinus Torvalds #include <linux/udp.h>
642ee92d46SJames Morris #include <linux/dccp.h>
651da177e4SLinus Torvalds #include <linux/quota.h>
661da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
671da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
681da177e4SLinus Torvalds #include <linux/parser.h>
691da177e4SLinus Torvalds #include <linux/nfs_mount.h>
701da177e4SLinus Torvalds #include <net/ipv6.h>
711da177e4SLinus Torvalds #include <linux/hugetlb.h>
721da177e4SLinus Torvalds #include <linux/personality.h>
731da177e4SLinus Torvalds #include <linux/sysctl.h>
741da177e4SLinus Torvalds #include <linux/audit.h>
756931dfc9SEric Paris #include <linux/string.h>
76877ce7c1SCatherine Zhang #include <linux/selinux.h>
7723970741SEric Paris #include <linux/mutex.h>
78f06febc9SFrank Mayhar #include <linux/posix-timers.h>
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #include "avc.h"
811da177e4SLinus Torvalds #include "objsec.h"
821da177e4SLinus Torvalds #include "netif.h"
83224dfbd8SPaul Moore #include "netnode.h"
843e112172SPaul Moore #include "netport.h"
85d28d1e08STrent Jaeger #include "xfrm.h"
86c60475bfSPaul Moore #include "netlabel.h"
879d57a7f9SAhmed S. Darwish #include "audit.h"
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux"
901da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
911da177e4SLinus Torvalds 
9211689d47SDavid P. Quigley #define NUM_SEL_MNT_OPTS 5
93c9180a57SEric Paris 
941da177e4SLinus Torvalds extern unsigned int policydb_loaded_version;
951da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
964e5ab4cbSJames Morris extern int selinux_compat_net;
9720510f2fSJames Morris extern struct security_operations *security_ops;
981da177e4SLinus Torvalds 
99d621d35eSPaul Moore /* SECMARK reference count */
100d621d35eSPaul Moore 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 
1301da177e4SLinus Torvalds 
1316f0f0fd4SJames Morris /*
1326f0f0fd4SJames Morris  * Minimal support for a secondary security module,
1336f0f0fd4SJames Morris  * just to allow the use of the capability module.
1346f0f0fd4SJames Morris  */
135828dfe1dSEric Paris static struct security_operations *secondary_ops;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized
1381da177e4SLinus Torvalds    before the policy was loaded. */
1391da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head);
1401da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock);
1411da177e4SLinus Torvalds 
142e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1437cae7e26SJames Morris 
144d621d35eSPaul Moore /**
145d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
146d621d35eSPaul Moore  *
147d621d35eSPaul Moore  * Description:
148d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
149d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
150d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
151d621d35eSPaul Moore  * enabled, false (0) if SECMARK is disabled.
152d621d35eSPaul Moore  *
153d621d35eSPaul Moore  */
154d621d35eSPaul Moore static int selinux_secmark_enabled(void)
155d621d35eSPaul Moore {
156d621d35eSPaul Moore 	return (atomic_read(&selinux_secmark_refcount) > 0);
157d621d35eSPaul Moore }
158d621d35eSPaul Moore 
159d84f4f99SDavid Howells /*
160d84f4f99SDavid Howells  * initialise the security for the init task
161d84f4f99SDavid Howells  */
162d84f4f99SDavid Howells static void cred_init_security(void)
1631da177e4SLinus Torvalds {
1643b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1651da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1661da177e4SLinus Torvalds 
16789d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1681da177e4SLinus Torvalds 	if (!tsec)
169d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1701da177e4SLinus Torvalds 
171d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
172f1752eecSDavid Howells 	cred->security = tsec;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175275bb41eSDavid Howells /*
17688e67f3bSDavid Howells  * get the security ID of a set of credentials
17788e67f3bSDavid Howells  */
17888e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
17988e67f3bSDavid Howells {
18088e67f3bSDavid Howells 	const struct task_security_struct *tsec;
18188e67f3bSDavid Howells 
18288e67f3bSDavid Howells 	tsec = cred->security;
18388e67f3bSDavid Howells 	return tsec->sid;
18488e67f3bSDavid Howells }
18588e67f3bSDavid Howells 
18688e67f3bSDavid Howells /*
1873b11a1deSDavid Howells  * get the objective security ID of a task
188275bb41eSDavid Howells  */
189275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
190275bb41eSDavid Howells {
191275bb41eSDavid Howells 	u32 sid;
192275bb41eSDavid Howells 
193275bb41eSDavid Howells 	rcu_read_lock();
19488e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
195275bb41eSDavid Howells 	rcu_read_unlock();
196275bb41eSDavid Howells 	return sid;
197275bb41eSDavid Howells }
198275bb41eSDavid Howells 
199275bb41eSDavid Howells /*
2003b11a1deSDavid Howells  * get the subjective security ID of the current task
201275bb41eSDavid Howells  */
202275bb41eSDavid Howells static inline u32 current_sid(void)
203275bb41eSDavid Howells {
204275bb41eSDavid Howells 	const struct task_security_struct *tsec = current_cred()->security;
205275bb41eSDavid Howells 
206275bb41eSDavid Howells 	return tsec->sid;
207275bb41eSDavid Howells }
208275bb41eSDavid Howells 
20988e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
21088e67f3bSDavid Howells 
2111da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct inode_security_struct *isec;
214275bb41eSDavid Howells 	u32 sid = current_sid();
2151da177e4SLinus Torvalds 
216a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2171da177e4SLinus Torvalds 	if (!isec)
2181da177e4SLinus Torvalds 		return -ENOMEM;
2191da177e4SLinus Torvalds 
22023970741SEric Paris 	mutex_init(&isec->lock);
2211da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2221da177e4SLinus Torvalds 	isec->inode = inode;
2231da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2241da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
225275bb41eSDavid Howells 	isec->task_sid = sid;
2261da177e4SLinus Torvalds 	inode->i_security = isec;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	return 0;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2341da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2371da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2381da177e4SLinus Torvalds 		list_del_init(&isec->list);
2391da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	inode->i_security = NULL;
2427cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct file_security_struct *fsec;
248275bb41eSDavid Howells 	u32 sid = current_sid();
2491da177e4SLinus Torvalds 
25026d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2511da177e4SLinus Torvalds 	if (!fsec)
2521da177e4SLinus Torvalds 		return -ENOMEM;
2531da177e4SLinus Torvalds 
254275bb41eSDavid Howells 	fsec->sid = sid;
255275bb41eSDavid Howells 	fsec->fown_sid = sid;
2561da177e4SLinus Torvalds 	file->f_security = fsec;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static void file_free_security(struct file *file)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2641da177e4SLinus Torvalds 	file->f_security = NULL;
2651da177e4SLinus Torvalds 	kfree(fsec);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2711da177e4SLinus Torvalds 
27289d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2731da177e4SLinus Torvalds 	if (!sbsec)
2741da177e4SLinus Torvalds 		return -ENOMEM;
2751da177e4SLinus Torvalds 
276bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2771da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->list);
2781da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2791da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2801da177e4SLinus Torvalds 	sbsec->sb = sb;
2811da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2821da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
283c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2841da177e4SLinus Torvalds 	sb->s_security = sbsec;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	return 0;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
2941da177e4SLinus Torvalds 	if (!list_empty(&sbsec->list))
2951da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
2961da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	sb->s_security = NULL;
2991da177e4SLinus Torvalds 	kfree(sbsec);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3027d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
3051da177e4SLinus Torvalds 
30689d155efSJames Morris 	ssec = kzalloc(sizeof(*ssec), priority);
3071da177e4SLinus Torvalds 	if (!ssec)
3081da177e4SLinus Torvalds 		return -ENOMEM;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	ssec->peer_sid = SECINITSID_UNLABELED;
311892c141eSVenkat Yekkirala 	ssec->sid = SECINITSID_UNLABELED;
3121da177e4SLinus Torvalds 	sk->sk_security = ssec;
3131da177e4SLinus Torvalds 
314*389fb800SPaul Moore 	selinux_netlbl_sk_security_reset(ssec);
31599f59ed0SPaul Moore 
3161da177e4SLinus Torvalds 	return 0;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static void sk_free_security(struct sock *sk)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct sk_security_struct *ssec = sk->sk_security;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	sk->sk_security = NULL;
3246c5b3fc0SPaul Moore 	selinux_netlbl_sk_security_free(ssec);
3251da177e4SLinus Torvalds 	kfree(ssec);
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /* The security server must be initialized before
3291da177e4SLinus Torvalds    any labeling or access decisions can be provided. */
3301da177e4SLinus Torvalds extern int ss_initialized;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds static char *labeling_behaviors[6] = {
3351da177e4SLinus Torvalds 	"uses xattr",
3361da177e4SLinus Torvalds 	"uses transition SIDs",
3371da177e4SLinus Torvalds 	"uses task SIDs",
3381da177e4SLinus Torvalds 	"uses genfs_contexts",
3391da177e4SLinus Torvalds 	"not configured for labeling",
3401da177e4SLinus Torvalds 	"uses mountpoint labeling",
3411da177e4SLinus Torvalds };
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds enum {
35131e87930SEric Paris 	Opt_error = -1,
3521da177e4SLinus Torvalds 	Opt_context = 1,
3531da177e4SLinus Torvalds 	Opt_fscontext = 2,
354c9180a57SEric Paris 	Opt_defcontext = 3,
355c9180a57SEric Paris 	Opt_rootcontext = 4,
35611689d47SDavid P. Quigley 	Opt_labelsupport = 5,
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
359a447c093SSteven Whitehouse static const match_table_t tokens = {
360832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
361832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
362832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
363832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
36411689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
36531e87930SEric Paris 	{Opt_error, NULL},
3661da177e4SLinus Torvalds };
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3691da177e4SLinus Torvalds 
370c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
371c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
372275bb41eSDavid Howells 			const struct cred *cred)
373c312feb2SEric Paris {
374275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
375c312feb2SEric Paris 	int rc;
376c312feb2SEric Paris 
377c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
378c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
379c312feb2SEric Paris 	if (rc)
380c312feb2SEric Paris 		return rc;
381c312feb2SEric Paris 
382c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
383c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
384c312feb2SEric Paris 	return rc;
385c312feb2SEric Paris }
386c312feb2SEric Paris 
3870808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3880808925eSEric Paris 			struct superblock_security_struct *sbsec,
389275bb41eSDavid Howells 			const struct cred *cred)
3900808925eSEric Paris {
391275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3920808925eSEric Paris 	int rc;
3930808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3940808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3950808925eSEric Paris 	if (rc)
3960808925eSEric Paris 		return rc;
3970808925eSEric Paris 
3980808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3990808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
4000808925eSEric Paris 	return rc;
4010808925eSEric Paris }
4020808925eSEric Paris 
403c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4061da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
407c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
4081da177e4SLinus Torvalds 	int rc = 0;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
4111da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
4121da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
4131da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
4141da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
4151da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
416c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
4171da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
4181da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
4191da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
4201da177e4SLinus Torvalds 			goto out;
4211da177e4SLinus Torvalds 		}
422c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
4231da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
4241da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
4251da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
4261da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
4271da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
4281da177e4SLinus Torvalds 			else
4291da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
4301da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
4311da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
4321da177e4SLinus Torvalds 			goto out;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
43611689d47SDavid P. Quigley 	sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
4371da177e4SLinus Torvalds 
438c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
439fadcdb45SEric Paris 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
4401da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
441c9180a57SEric Paris 	else
442fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
4431da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
4441da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
4451da177e4SLinus Torvalds 
44611689d47SDavid P. Quigley 	if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
44711689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
44811689d47SDavid P. Quigley 	    sbsec->behavior == SECURITY_FS_USE_NONE ||
44911689d47SDavid P. Quigley 	    sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
45011689d47SDavid P. Quigley 		sbsec->flags &= ~SE_SBLABELSUPP;
45111689d47SDavid P. Quigley 
4521da177e4SLinus Torvalds 	/* Initialize the root inode. */
453c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4561da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4571da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4581da177e4SLinus Torvalds 	   populates itself. */
4591da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4601da177e4SLinus Torvalds next_inode:
4611da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4621da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4631da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4641da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4651da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
4661da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4671da177e4SLinus Torvalds 		inode = igrab(inode);
4681da177e4SLinus Torvalds 		if (inode) {
4691da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4701da177e4SLinus Torvalds 				inode_doinit(inode);
4711da177e4SLinus Torvalds 			iput(inode);
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4741da177e4SLinus Torvalds 		list_del_init(&isec->list);
4751da177e4SLinus Torvalds 		goto next_inode;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4781da177e4SLinus Torvalds out:
479c9180a57SEric Paris 	return rc;
480c9180a57SEric Paris }
481c9180a57SEric Paris 
482c9180a57SEric Paris /*
483c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
484c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
485c9180a57SEric Paris  * mount options, or whatever.
486c9180a57SEric Paris  */
487c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
488e0007529SEric Paris 				struct security_mnt_opts *opts)
489c9180a57SEric Paris {
490c9180a57SEric Paris 	int rc = 0, i;
491c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
492c9180a57SEric Paris 	char *context = NULL;
493c9180a57SEric Paris 	u32 len;
494c9180a57SEric Paris 	char tmp;
495c9180a57SEric Paris 
496e0007529SEric Paris 	security_init_mnt_opts(opts);
497c9180a57SEric Paris 
4980d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
499c9180a57SEric Paris 		return -EINVAL;
500c9180a57SEric Paris 
501c9180a57SEric Paris 	if (!ss_initialized)
502c9180a57SEric Paris 		return -EINVAL;
503c9180a57SEric Paris 
5040d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
505c9180a57SEric Paris 	/* count the number of mount options for this sb */
506c9180a57SEric Paris 	for (i = 0; i < 8; i++) {
507c9180a57SEric Paris 		if (tmp & 0x01)
508e0007529SEric Paris 			opts->num_mnt_opts++;
509c9180a57SEric Paris 		tmp >>= 1;
510c9180a57SEric Paris 	}
51111689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
51211689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP)
51311689d47SDavid P. Quigley 		opts->num_mnt_opts++;
514c9180a57SEric Paris 
515e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
516e0007529SEric Paris 	if (!opts->mnt_opts) {
517c9180a57SEric Paris 		rc = -ENOMEM;
518c9180a57SEric Paris 		goto out_free;
519c9180a57SEric Paris 	}
520c9180a57SEric Paris 
521e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
522e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
523c9180a57SEric Paris 		rc = -ENOMEM;
524c9180a57SEric Paris 		goto out_free;
525c9180a57SEric Paris 	}
526c9180a57SEric Paris 
527c9180a57SEric Paris 	i = 0;
528c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
529c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
530c9180a57SEric Paris 		if (rc)
531c9180a57SEric Paris 			goto out_free;
532e0007529SEric Paris 		opts->mnt_opts[i] = context;
533e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
534c9180a57SEric Paris 	}
535c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
536c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
537c9180a57SEric Paris 		if (rc)
538c9180a57SEric Paris 			goto out_free;
539e0007529SEric Paris 		opts->mnt_opts[i] = context;
540e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
541c9180a57SEric Paris 	}
542c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
543c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
544c9180a57SEric Paris 		if (rc)
545c9180a57SEric Paris 			goto out_free;
546e0007529SEric Paris 		opts->mnt_opts[i] = context;
547e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
548c9180a57SEric Paris 	}
549c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
550c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
551c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
552c9180a57SEric Paris 
553c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
554c9180a57SEric Paris 		if (rc)
555c9180a57SEric Paris 			goto out_free;
556e0007529SEric Paris 		opts->mnt_opts[i] = context;
557e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
558c9180a57SEric Paris 	}
55911689d47SDavid P. Quigley 	if (sbsec->flags & SE_SBLABELSUPP) {
56011689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
56111689d47SDavid P. Quigley 		opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
56211689d47SDavid P. Quigley 	}
563c9180a57SEric Paris 
564e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
565c9180a57SEric Paris 
566c9180a57SEric Paris 	return 0;
567c9180a57SEric Paris 
568c9180a57SEric Paris out_free:
569e0007529SEric Paris 	security_free_mnt_opts(opts);
570c9180a57SEric Paris 	return rc;
571c9180a57SEric Paris }
572c9180a57SEric Paris 
573c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
574c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
575c9180a57SEric Paris {
5760d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5770d90a7ecSDavid P. Quigley 
578c9180a57SEric Paris 	/* check if the old mount command had the same options */
5790d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
580c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
581c9180a57SEric Paris 		    (old_sid != new_sid))
582c9180a57SEric Paris 			return 1;
583c9180a57SEric Paris 
584c9180a57SEric Paris 	/* check if we were passed the same options twice,
585c9180a57SEric Paris 	 * aka someone passed context=a,context=b
586c9180a57SEric Paris 	 */
5870d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5880d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
589c9180a57SEric Paris 			return 1;
590c9180a57SEric Paris 	return 0;
591c9180a57SEric Paris }
592e0007529SEric Paris 
593c9180a57SEric Paris /*
594c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
595c9180a57SEric Paris  * labeling information.
596c9180a57SEric Paris  */
597e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
598e0007529SEric Paris 				struct security_mnt_opts *opts)
599c9180a57SEric Paris {
600275bb41eSDavid Howells 	const struct cred *cred = current_cred();
601c9180a57SEric Paris 	int rc = 0, i;
602c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
603c9180a57SEric Paris 	const char *name = sb->s_type->name;
604089be43eSJames Morris 	struct inode *inode = sbsec->sb->s_root->d_inode;
605089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
606c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
607c9180a57SEric Paris 	u32 defcontext_sid = 0;
608e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
609e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
610e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
611c9180a57SEric Paris 
612c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
613c9180a57SEric Paris 
614c9180a57SEric Paris 	if (!ss_initialized) {
615c9180a57SEric Paris 		if (!num_opts) {
616c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
617c9180a57SEric Paris 			   after the initial policy is loaded and the security
618c9180a57SEric Paris 			   server is ready to handle calls. */
619c9180a57SEric Paris 			spin_lock(&sb_security_lock);
620c9180a57SEric Paris 			if (list_empty(&sbsec->list))
621c9180a57SEric Paris 				list_add(&sbsec->list, &superblock_security_head);
622c9180a57SEric Paris 			spin_unlock(&sb_security_lock);
623c9180a57SEric Paris 			goto out;
624c9180a57SEric Paris 		}
625c9180a57SEric Paris 		rc = -EINVAL;
626744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
627744ba35eSEric Paris 			"before the security server is initialized\n");
628c9180a57SEric Paris 		goto out;
629c9180a57SEric Paris 	}
630c9180a57SEric Paris 
631c9180a57SEric Paris 	/*
632e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
633e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
634e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
635e0007529SEric Paris 	 * we need to skip the double mount verification.
636e0007529SEric Paris 	 *
637e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
638e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
639e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
640e0007529SEric Paris 	 * will be used for both mounts)
641e0007529SEric Paris 	 */
6420d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
643e0007529SEric Paris 	    && (num_opts == 0))
644e0007529SEric Paris 		goto out;
645e0007529SEric Paris 
646e0007529SEric Paris 	/*
647c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
648c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
649c9180a57SEric Paris 	 * than once with different security options.
650c9180a57SEric Paris 	 */
651c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
652c9180a57SEric Paris 		u32 sid;
65311689d47SDavid P. Quigley 
65411689d47SDavid P. Quigley 		if (flags[i] == SE_SBLABELSUPP)
65511689d47SDavid P. Quigley 			continue;
656c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
657c9180a57SEric Paris 					     strlen(mount_options[i]), &sid);
658c9180a57SEric Paris 		if (rc) {
659c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
660c9180a57SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
661c9180a57SEric Paris 			       mount_options[i], sb->s_id, name, rc);
662c9180a57SEric Paris 			goto out;
663c9180a57SEric Paris 		}
664c9180a57SEric Paris 		switch (flags[i]) {
665c9180a57SEric Paris 		case FSCONTEXT_MNT:
666c9180a57SEric Paris 			fscontext_sid = sid;
667c9180a57SEric Paris 
668c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
669c9180a57SEric Paris 					fscontext_sid))
670c9180a57SEric Paris 				goto out_double_mount;
671c9180a57SEric Paris 
672c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
673c9180a57SEric Paris 			break;
674c9180a57SEric Paris 		case CONTEXT_MNT:
675c9180a57SEric Paris 			context_sid = sid;
676c9180a57SEric Paris 
677c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
678c9180a57SEric Paris 					context_sid))
679c9180a57SEric Paris 				goto out_double_mount;
680c9180a57SEric Paris 
681c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
682c9180a57SEric Paris 			break;
683c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
684c9180a57SEric Paris 			rootcontext_sid = sid;
685c9180a57SEric Paris 
686c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
687c9180a57SEric Paris 					rootcontext_sid))
688c9180a57SEric Paris 				goto out_double_mount;
689c9180a57SEric Paris 
690c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
691c9180a57SEric Paris 
692c9180a57SEric Paris 			break;
693c9180a57SEric Paris 		case DEFCONTEXT_MNT:
694c9180a57SEric Paris 			defcontext_sid = sid;
695c9180a57SEric Paris 
696c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
697c9180a57SEric Paris 					defcontext_sid))
698c9180a57SEric Paris 				goto out_double_mount;
699c9180a57SEric Paris 
700c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
701c9180a57SEric Paris 
702c9180a57SEric Paris 			break;
703c9180a57SEric Paris 		default:
704c9180a57SEric Paris 			rc = -EINVAL;
705c9180a57SEric Paris 			goto out;
706c9180a57SEric Paris 		}
707c9180a57SEric Paris 	}
708c9180a57SEric Paris 
7090d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
710c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
7110d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
712c9180a57SEric Paris 			goto out_double_mount;
713c9180a57SEric Paris 		rc = 0;
714c9180a57SEric Paris 		goto out;
715c9180a57SEric Paris 	}
716c9180a57SEric Paris 
717089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
7180d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
719c9180a57SEric Paris 
720c9180a57SEric Paris 	/* Determine the labeling behavior to use for this filesystem type. */
7210d90a7ecSDavid P. Quigley 	rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
722c9180a57SEric Paris 	if (rc) {
723c9180a57SEric Paris 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
724089be43eSJames Morris 		       __func__, sb->s_type->name, rc);
725c9180a57SEric Paris 		goto out;
726c9180a57SEric Paris 	}
727c9180a57SEric Paris 
728c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
729c9180a57SEric Paris 	if (fscontext_sid) {
730275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
731c9180a57SEric Paris 		if (rc)
732c9180a57SEric Paris 			goto out;
733c9180a57SEric Paris 
734c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
735c9180a57SEric Paris 	}
736c9180a57SEric Paris 
737c9180a57SEric Paris 	/*
738c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
739c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
740c9180a57SEric Paris 	 * the superblock context if not already set.
741c9180a57SEric Paris 	 */
742c9180a57SEric Paris 	if (context_sid) {
743c9180a57SEric Paris 		if (!fscontext_sid) {
744275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
745275bb41eSDavid Howells 							  cred);
746c9180a57SEric Paris 			if (rc)
747c9180a57SEric Paris 				goto out;
748c9180a57SEric Paris 			sbsec->sid = context_sid;
749c9180a57SEric Paris 		} else {
750275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
751275bb41eSDavid Howells 							     cred);
752c9180a57SEric Paris 			if (rc)
753c9180a57SEric Paris 				goto out;
754c9180a57SEric Paris 		}
755c9180a57SEric Paris 		if (!rootcontext_sid)
756c9180a57SEric Paris 			rootcontext_sid = context_sid;
757c9180a57SEric Paris 
758c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
759c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
760c9180a57SEric Paris 	}
761c9180a57SEric Paris 
762c9180a57SEric Paris 	if (rootcontext_sid) {
763275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
764275bb41eSDavid Howells 						     cred);
765c9180a57SEric Paris 		if (rc)
766c9180a57SEric Paris 			goto out;
767c9180a57SEric Paris 
768c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
769c9180a57SEric Paris 		root_isec->initialized = 1;
770c9180a57SEric Paris 	}
771c9180a57SEric Paris 
772c9180a57SEric Paris 	if (defcontext_sid) {
773c9180a57SEric Paris 		if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
774c9180a57SEric Paris 			rc = -EINVAL;
775c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
776c9180a57SEric Paris 			       "invalid for this filesystem type\n");
777c9180a57SEric Paris 			goto out;
778c9180a57SEric Paris 		}
779c9180a57SEric Paris 
780c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
781c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
782275bb41eSDavid Howells 							     sbsec, cred);
783c9180a57SEric Paris 			if (rc)
784c9180a57SEric Paris 				goto out;
785c9180a57SEric Paris 		}
786c9180a57SEric Paris 
787c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
788c9180a57SEric Paris 	}
789c9180a57SEric Paris 
790c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
791c9180a57SEric Paris out:
792bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
7931da177e4SLinus Torvalds 	return rc;
794c9180a57SEric Paris out_double_mount:
795c9180a57SEric Paris 	rc = -EINVAL;
796c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
797c9180a57SEric Paris 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
798c9180a57SEric Paris 	goto out;
799c9180a57SEric Paris }
800c9180a57SEric Paris 
801c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
802c9180a57SEric Paris 					struct super_block *newsb)
803c9180a57SEric Paris {
804c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
805c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
806c9180a57SEric Paris 
807c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
808c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
809c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
810c9180a57SEric Paris 
8110f5e6420SEric Paris 	/*
8120f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
8130f5e6420SEric Paris 	 * mount options.  thus we can safely put this sb on the list and deal
8140f5e6420SEric Paris 	 * with it later
8150f5e6420SEric Paris 	 */
8160f5e6420SEric Paris 	if (!ss_initialized) {
8170f5e6420SEric Paris 		spin_lock(&sb_security_lock);
8180f5e6420SEric Paris 		if (list_empty(&newsbsec->list))
8190f5e6420SEric Paris 			list_add(&newsbsec->list, &superblock_security_head);
8200f5e6420SEric Paris 		spin_unlock(&sb_security_lock);
8210f5e6420SEric Paris 		return;
8220f5e6420SEric Paris 	}
823c9180a57SEric Paris 
824c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8250d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
826c9180a57SEric Paris 
8275a552617SEric Paris 	/* if fs is reusing a sb, just let its options stand... */
8280d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
8295a552617SEric Paris 		return;
8305a552617SEric Paris 
831c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
832c9180a57SEric Paris 
833c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
834c9180a57SEric Paris 
835c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
836c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
837c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
838c9180a57SEric Paris 
839c9180a57SEric Paris 	if (set_context) {
840c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
841c9180a57SEric Paris 
842c9180a57SEric Paris 		if (!set_fscontext)
843c9180a57SEric Paris 			newsbsec->sid = sid;
844c9180a57SEric Paris 		if (!set_rootcontext) {
845c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
846c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
847c9180a57SEric Paris 			newisec->sid = sid;
848c9180a57SEric Paris 		}
849c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
850c9180a57SEric Paris 	}
851c9180a57SEric Paris 	if (set_rootcontext) {
852c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
853c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
854c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
855c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
856c9180a57SEric Paris 
857c9180a57SEric Paris 		newisec->sid = oldisec->sid;
858c9180a57SEric Paris 	}
859c9180a57SEric Paris 
860c9180a57SEric Paris 	sb_finish_set_opts(newsb);
861c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
862c9180a57SEric Paris }
863c9180a57SEric Paris 
8642e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
8652e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
866c9180a57SEric Paris {
867e0007529SEric Paris 	char *p;
868c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
869c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
870e0007529SEric Paris 	int rc, num_mnt_opts = 0;
871c9180a57SEric Paris 
872e0007529SEric Paris 	opts->num_mnt_opts = 0;
873c9180a57SEric Paris 
874c9180a57SEric Paris 	/* Standard string-based options. */
875c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
876c9180a57SEric Paris 		int token;
877c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
878c9180a57SEric Paris 
879c9180a57SEric Paris 		if (!*p)
880c9180a57SEric Paris 			continue;
881c9180a57SEric Paris 
882c9180a57SEric Paris 		token = match_token(p, tokens, args);
883c9180a57SEric Paris 
884c9180a57SEric Paris 		switch (token) {
885c9180a57SEric Paris 		case Opt_context:
886c9180a57SEric Paris 			if (context || defcontext) {
887c9180a57SEric Paris 				rc = -EINVAL;
888c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
889c9180a57SEric Paris 				goto out_err;
890c9180a57SEric Paris 			}
891c9180a57SEric Paris 			context = match_strdup(&args[0]);
892c9180a57SEric Paris 			if (!context) {
893c9180a57SEric Paris 				rc = -ENOMEM;
894c9180a57SEric Paris 				goto out_err;
895c9180a57SEric Paris 			}
896c9180a57SEric Paris 			break;
897c9180a57SEric Paris 
898c9180a57SEric Paris 		case Opt_fscontext:
899c9180a57SEric Paris 			if (fscontext) {
900c9180a57SEric Paris 				rc = -EINVAL;
901c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
902c9180a57SEric Paris 				goto out_err;
903c9180a57SEric Paris 			}
904c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
905c9180a57SEric Paris 			if (!fscontext) {
906c9180a57SEric Paris 				rc = -ENOMEM;
907c9180a57SEric Paris 				goto out_err;
908c9180a57SEric Paris 			}
909c9180a57SEric Paris 			break;
910c9180a57SEric Paris 
911c9180a57SEric Paris 		case Opt_rootcontext:
912c9180a57SEric Paris 			if (rootcontext) {
913c9180a57SEric Paris 				rc = -EINVAL;
914c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
915c9180a57SEric Paris 				goto out_err;
916c9180a57SEric Paris 			}
917c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
918c9180a57SEric Paris 			if (!rootcontext) {
919c9180a57SEric Paris 				rc = -ENOMEM;
920c9180a57SEric Paris 				goto out_err;
921c9180a57SEric Paris 			}
922c9180a57SEric Paris 			break;
923c9180a57SEric Paris 
924c9180a57SEric Paris 		case Opt_defcontext:
925c9180a57SEric Paris 			if (context || defcontext) {
926c9180a57SEric Paris 				rc = -EINVAL;
927c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
928c9180a57SEric Paris 				goto out_err;
929c9180a57SEric Paris 			}
930c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
931c9180a57SEric Paris 			if (!defcontext) {
932c9180a57SEric Paris 				rc = -ENOMEM;
933c9180a57SEric Paris 				goto out_err;
934c9180a57SEric Paris 			}
935c9180a57SEric Paris 			break;
93611689d47SDavid P. Quigley 		case Opt_labelsupport:
93711689d47SDavid P. Quigley 			break;
938c9180a57SEric Paris 		default:
939c9180a57SEric Paris 			rc = -EINVAL;
940c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
941c9180a57SEric Paris 			goto out_err;
942c9180a57SEric Paris 
943c9180a57SEric Paris 		}
944c9180a57SEric Paris 	}
945c9180a57SEric Paris 
946e0007529SEric Paris 	rc = -ENOMEM;
947e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
948e0007529SEric Paris 	if (!opts->mnt_opts)
949e0007529SEric Paris 		goto out_err;
950e0007529SEric Paris 
951e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
952e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
953e0007529SEric Paris 		kfree(opts->mnt_opts);
954e0007529SEric Paris 		goto out_err;
955c9180a57SEric Paris 	}
956c9180a57SEric Paris 
957e0007529SEric Paris 	if (fscontext) {
958e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
959e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
960e0007529SEric Paris 	}
961e0007529SEric Paris 	if (context) {
962e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
963e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
964e0007529SEric Paris 	}
965e0007529SEric Paris 	if (rootcontext) {
966e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
967e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
968e0007529SEric Paris 	}
969e0007529SEric Paris 	if (defcontext) {
970e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
971e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
972e0007529SEric Paris 	}
973e0007529SEric Paris 
974e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
975e0007529SEric Paris 	return 0;
976e0007529SEric Paris 
977c9180a57SEric Paris out_err:
978c9180a57SEric Paris 	kfree(context);
979c9180a57SEric Paris 	kfree(defcontext);
980c9180a57SEric Paris 	kfree(fscontext);
981c9180a57SEric Paris 	kfree(rootcontext);
982c9180a57SEric Paris 	return rc;
9831da177e4SLinus Torvalds }
984e0007529SEric Paris /*
985e0007529SEric Paris  * string mount options parsing and call set the sbsec
986e0007529SEric Paris  */
987e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
988e0007529SEric Paris {
989e0007529SEric Paris 	int rc = 0;
990e0007529SEric Paris 	char *options = data;
991e0007529SEric Paris 	struct security_mnt_opts opts;
992e0007529SEric Paris 
993e0007529SEric Paris 	security_init_mnt_opts(&opts);
994e0007529SEric Paris 
995e0007529SEric Paris 	if (!data)
996e0007529SEric Paris 		goto out;
997e0007529SEric Paris 
998e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
999e0007529SEric Paris 
1000e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1001e0007529SEric Paris 	if (rc)
1002e0007529SEric Paris 		goto out_err;
1003e0007529SEric Paris 
1004e0007529SEric Paris out:
1005e0007529SEric Paris 	rc = selinux_set_mnt_opts(sb, &opts);
1006e0007529SEric Paris 
1007e0007529SEric Paris out_err:
1008e0007529SEric Paris 	security_free_mnt_opts(&opts);
1009e0007529SEric Paris 	return rc;
1010e0007529SEric Paris }
10111da177e4SLinus Torvalds 
10123583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
10133583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
10142069f457SEric Paris {
10152069f457SEric Paris 	int i;
10162069f457SEric Paris 	char *prefix;
10172069f457SEric Paris 
10182069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
101911689d47SDavid P. Quigley 		char *has_comma;
102011689d47SDavid P. Quigley 
102111689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
102211689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
102311689d47SDavid P. Quigley 		else
102411689d47SDavid P. Quigley 			has_comma = NULL;
10252069f457SEric Paris 
10262069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
10272069f457SEric Paris 		case CONTEXT_MNT:
10282069f457SEric Paris 			prefix = CONTEXT_STR;
10292069f457SEric Paris 			break;
10302069f457SEric Paris 		case FSCONTEXT_MNT:
10312069f457SEric Paris 			prefix = FSCONTEXT_STR;
10322069f457SEric Paris 			break;
10332069f457SEric Paris 		case ROOTCONTEXT_MNT:
10342069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
10352069f457SEric Paris 			break;
10362069f457SEric Paris 		case DEFCONTEXT_MNT:
10372069f457SEric Paris 			prefix = DEFCONTEXT_STR;
10382069f457SEric Paris 			break;
103911689d47SDavid P. Quigley 		case SE_SBLABELSUPP:
104011689d47SDavid P. Quigley 			seq_putc(m, ',');
104111689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
104211689d47SDavid P. Quigley 			continue;
10432069f457SEric Paris 		default:
10442069f457SEric Paris 			BUG();
10452069f457SEric Paris 		};
10462069f457SEric Paris 		/* we need a comma before each option */
10472069f457SEric Paris 		seq_putc(m, ',');
10482069f457SEric Paris 		seq_puts(m, prefix);
10492069f457SEric Paris 		if (has_comma)
10502069f457SEric Paris 			seq_putc(m, '\"');
10512069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
10522069f457SEric Paris 		if (has_comma)
10532069f457SEric Paris 			seq_putc(m, '\"');
10542069f457SEric Paris 	}
10552069f457SEric Paris }
10562069f457SEric Paris 
10572069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
10582069f457SEric Paris {
10592069f457SEric Paris 	struct security_mnt_opts opts;
10602069f457SEric Paris 	int rc;
10612069f457SEric Paris 
10622069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1063383795c2SEric Paris 	if (rc) {
1064383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1065383795c2SEric Paris 		if (rc == -EINVAL)
1066383795c2SEric Paris 			rc = 0;
10672069f457SEric Paris 		return rc;
1068383795c2SEric Paris 	}
10692069f457SEric Paris 
10702069f457SEric Paris 	selinux_write_opts(m, &opts);
10712069f457SEric Paris 
10722069f457SEric Paris 	security_free_mnt_opts(&opts);
10732069f457SEric Paris 
10742069f457SEric Paris 	return rc;
10752069f457SEric Paris }
10762069f457SEric Paris 
10771da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
10801da177e4SLinus Torvalds 	case S_IFSOCK:
10811da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
10821da177e4SLinus Torvalds 	case S_IFLNK:
10831da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
10841da177e4SLinus Torvalds 	case S_IFREG:
10851da177e4SLinus Torvalds 		return SECCLASS_FILE;
10861da177e4SLinus Torvalds 	case S_IFBLK:
10871da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
10881da177e4SLinus Torvalds 	case S_IFDIR:
10891da177e4SLinus Torvalds 		return SECCLASS_DIR;
10901da177e4SLinus Torvalds 	case S_IFCHR:
10911da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
10921da177e4SLinus Torvalds 	case S_IFIFO:
10931da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	return SECCLASS_FILE;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
110013402580SJames Morris static inline int default_protocol_stream(int protocol)
110113402580SJames Morris {
110213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
110313402580SJames Morris }
110413402580SJames Morris 
110513402580SJames Morris static inline int default_protocol_dgram(int protocol)
110613402580SJames Morris {
110713402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
110813402580SJames Morris }
110913402580SJames Morris 
11101da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	switch (family) {
11131da177e4SLinus Torvalds 	case PF_UNIX:
11141da177e4SLinus Torvalds 		switch (type) {
11151da177e4SLinus Torvalds 		case SOCK_STREAM:
11161da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11171da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11181da177e4SLinus Torvalds 		case SOCK_DGRAM:
11191da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 		break;
11221da177e4SLinus Torvalds 	case PF_INET:
11231da177e4SLinus Torvalds 	case PF_INET6:
11241da177e4SLinus Torvalds 		switch (type) {
11251da177e4SLinus Torvalds 		case SOCK_STREAM:
112613402580SJames Morris 			if (default_protocol_stream(protocol))
11271da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
112813402580SJames Morris 			else
112913402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11301da177e4SLinus Torvalds 		case SOCK_DGRAM:
113113402580SJames Morris 			if (default_protocol_dgram(protocol))
11321da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
113313402580SJames Morris 			else
113413402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11352ee92d46SJames Morris 		case SOCK_DCCP:
11362ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
113713402580SJames Morris 		default:
11381da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		break;
11411da177e4SLinus Torvalds 	case PF_NETLINK:
11421da177e4SLinus Torvalds 		switch (protocol) {
11431da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11441da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11451da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
11461da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
1147216efaaaSJames Morris 		case NETLINK_INET_DIAG:
11481da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11491da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11501da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11511da177e4SLinus Torvalds 		case NETLINK_XFRM:
11521da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
11531da177e4SLinus Torvalds 		case NETLINK_SELINUX:
11541da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
11551da177e4SLinus Torvalds 		case NETLINK_AUDIT:
11561da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
11571da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
11581da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
11591da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
11601da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
11610c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
11620c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
11631da177e4SLinus Torvalds 		default:
11641da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
11651da177e4SLinus Torvalds 		}
11661da177e4SLinus Torvalds 	case PF_PACKET:
11671da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
11681da177e4SLinus Torvalds 	case PF_KEY:
11691da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
11703e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
11713e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11781da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
11791da177e4SLinus Torvalds 				u16 tclass,
11801da177e4SLinus Torvalds 				u32 *sid)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	int buflen, rc;
11831da177e4SLinus Torvalds 	char *buffer, *path, *end;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
11861da177e4SLinus Torvalds 	if (!buffer)
11871da177e4SLinus Torvalds 		return -ENOMEM;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	buflen = PAGE_SIZE;
11901da177e4SLinus Torvalds 	end = buffer+buflen;
11911da177e4SLinus Torvalds 	*--end = '\0';
11921da177e4SLinus Torvalds 	buflen--;
11931da177e4SLinus Torvalds 	path = end-1;
11941da177e4SLinus Torvalds 	*path = '/';
11951da177e4SLinus Torvalds 	while (de && de != de->parent) {
11961da177e4SLinus Torvalds 		buflen -= de->namelen + 1;
11971da177e4SLinus Torvalds 		if (buflen < 0)
11981da177e4SLinus Torvalds 			break;
11991da177e4SLinus Torvalds 		end -= de->namelen;
12001da177e4SLinus Torvalds 		memcpy(end, de->name, de->namelen);
12011da177e4SLinus Torvalds 		*--end = '/';
12021da177e4SLinus Torvalds 		path = end;
12031da177e4SLinus Torvalds 		de = de->parent;
12041da177e4SLinus Torvalds 	}
12051da177e4SLinus Torvalds 	rc = security_genfs_sid("proc", path, tclass, sid);
12061da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
12071da177e4SLinus Torvalds 	return rc;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds #else
12101da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
12111da177e4SLinus Torvalds 				u16 tclass,
12121da177e4SLinus Torvalds 				u32 *sid)
12131da177e4SLinus Torvalds {
12141da177e4SLinus Torvalds 	return -EINVAL;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds #endif
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
12191da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
12221da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12231da177e4SLinus Torvalds 	u32 sid;
12241da177e4SLinus Torvalds 	struct dentry *dentry;
12251da177e4SLinus Torvalds #define INITCONTEXTLEN 255
12261da177e4SLinus Torvalds 	char *context = NULL;
12271da177e4SLinus Torvalds 	unsigned len = 0;
12281da177e4SLinus Torvalds 	int rc = 0;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	if (isec->initialized)
12311da177e4SLinus Torvalds 		goto out;
12321da177e4SLinus Torvalds 
123323970741SEric Paris 	mutex_lock(&isec->lock);
12341da177e4SLinus Torvalds 	if (isec->initialized)
123523970741SEric Paris 		goto out_unlock;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
12380d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
12391da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
12401da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
12411da177e4SLinus Torvalds 		   server is ready to handle calls. */
12421da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
12431da177e4SLinus Torvalds 		if (list_empty(&isec->list))
12441da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
12451da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
124623970741SEric Paris 		goto out_unlock;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	switch (sbsec->behavior) {
12501da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
12511da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
12521da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
12531da177e4SLinus Torvalds 			break;
12541da177e4SLinus Torvalds 		}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
12571da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
12581da177e4SLinus Torvalds 		if (opt_dentry) {
12591da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
12601da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
12611da177e4SLinus Torvalds 		} else {
12621da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
12631da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
12641da177e4SLinus Torvalds 		}
12651da177e4SLinus Torvalds 		if (!dentry) {
1266df7f54c0SEric Paris 			/*
1267df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1268df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1269df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1270df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1271df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1272df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1273df7f54c0SEric Paris 			 * be used again by userspace.
1274df7f54c0SEric Paris 			 */
127523970741SEric Paris 			goto out_unlock;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
12794cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
12801da177e4SLinus Torvalds 		if (!context) {
12811da177e4SLinus Torvalds 			rc = -ENOMEM;
12821da177e4SLinus Torvalds 			dput(dentry);
128323970741SEric Paris 			goto out_unlock;
12841da177e4SLinus Torvalds 		}
12854cb912f1SEric Paris 		context[len] = '\0';
12861da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12871da177e4SLinus Torvalds 					   context, len);
12881da177e4SLinus Torvalds 		if (rc == -ERANGE) {
12891da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
12901da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
12911da177e4SLinus Torvalds 						   NULL, 0);
12921da177e4SLinus Torvalds 			if (rc < 0) {
12931da177e4SLinus Torvalds 				dput(dentry);
129423970741SEric Paris 				goto out_unlock;
12951da177e4SLinus Torvalds 			}
12961da177e4SLinus Torvalds 			kfree(context);
12971da177e4SLinus Torvalds 			len = rc;
12984cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
12991da177e4SLinus Torvalds 			if (!context) {
13001da177e4SLinus Torvalds 				rc = -ENOMEM;
13011da177e4SLinus Torvalds 				dput(dentry);
130223970741SEric Paris 				goto out_unlock;
13031da177e4SLinus Torvalds 			}
13044cb912f1SEric Paris 			context[len] = '\0';
13051da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
13061da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
13071da177e4SLinus Torvalds 						   context, len);
13081da177e4SLinus Torvalds 		}
13091da177e4SLinus Torvalds 		dput(dentry);
13101da177e4SLinus Torvalds 		if (rc < 0) {
13111da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1312744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1313dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
13141da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
13151da177e4SLinus Torvalds 				kfree(context);
131623970741SEric Paris 				goto out_unlock;
13171da177e4SLinus Torvalds 			}
13181da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
13191da177e4SLinus Torvalds 			sid = sbsec->def_sid;
13201da177e4SLinus Torvalds 			rc = 0;
13211da177e4SLinus Torvalds 		} else {
1322f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1323869ab514SStephen Smalley 							     sbsec->def_sid,
1324869ab514SStephen Smalley 							     GFP_NOFS);
13251da177e4SLinus Torvalds 			if (rc) {
13264ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
13274ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
13284ba0a8adSEric Paris 
13294ba0a8adSEric Paris 				if (rc == -EINVAL) {
13304ba0a8adSEric Paris 					if (printk_ratelimit())
13314ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
13324ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
13334ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
13344ba0a8adSEric Paris 				} else {
1335744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
13361da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
13374ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
13384ba0a8adSEric Paris 				}
13391da177e4SLinus Torvalds 				kfree(context);
13401da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
13411da177e4SLinus Torvalds 				rc = 0;
13421da177e4SLinus Torvalds 				break;
13431da177e4SLinus Torvalds 			}
13441da177e4SLinus Torvalds 		}
13451da177e4SLinus Torvalds 		kfree(context);
13461da177e4SLinus Torvalds 		isec->sid = sid;
13471da177e4SLinus Torvalds 		break;
13481da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
13491da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
13501da177e4SLinus Torvalds 		break;
13511da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
13521da177e4SLinus Torvalds 		/* Default to the fs SID. */
13531da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
13561da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
13571da177e4SLinus Torvalds 		rc = security_transition_sid(isec->task_sid,
13581da177e4SLinus Torvalds 					     sbsec->sid,
13591da177e4SLinus Torvalds 					     isec->sclass,
13601da177e4SLinus Torvalds 					     &sid);
13611da177e4SLinus Torvalds 		if (rc)
136223970741SEric Paris 			goto out_unlock;
13631da177e4SLinus Torvalds 		isec->sid = sid;
13641da177e4SLinus Torvalds 		break;
1365c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1366c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1367c312feb2SEric Paris 		break;
13681da177e4SLinus Torvalds 	default:
1369c312feb2SEric Paris 		/* Default to the fs superblock SID. */
13701da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
13711da177e4SLinus Torvalds 
13720d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
13731da177e4SLinus Torvalds 			struct proc_inode *proci = PROC_I(inode);
13741da177e4SLinus Torvalds 			if (proci->pde) {
13751da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
13761da177e4SLinus Torvalds 				rc = selinux_proc_get_sid(proci->pde,
13771da177e4SLinus Torvalds 							  isec->sclass,
13781da177e4SLinus Torvalds 							  &sid);
13791da177e4SLinus Torvalds 				if (rc)
138023970741SEric Paris 					goto out_unlock;
13811da177e4SLinus Torvalds 				isec->sid = sid;
13821da177e4SLinus Torvalds 			}
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 		break;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	isec->initialized = 1;
13881da177e4SLinus Torvalds 
138923970741SEric Paris out_unlock:
139023970741SEric Paris 	mutex_unlock(&isec->lock);
13911da177e4SLinus Torvalds out:
13921da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
13931da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
13941da177e4SLinus Torvalds 	return rc;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
13981da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	u32 perm = 0;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	switch (sig) {
14031da177e4SLinus Torvalds 	case SIGCHLD:
14041da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
14051da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
14061da177e4SLinus Torvalds 		break;
14071da177e4SLinus Torvalds 	case SIGKILL:
14081da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14091da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
14101da177e4SLinus Torvalds 		break;
14111da177e4SLinus Torvalds 	case SIGSTOP:
14121da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14131da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
14141da177e4SLinus Torvalds 		break;
14151da177e4SLinus Torvalds 	default:
14161da177e4SLinus Torvalds 		/* All other signals. */
14171da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
14181da177e4SLinus Torvalds 		break;
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	return perm;
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
1424275bb41eSDavid Howells /*
1425d84f4f99SDavid Howells  * Check permission between a pair of credentials
1426d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1427d84f4f99SDavid Howells  */
1428d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1429d84f4f99SDavid Howells 			 const struct cred *target,
1430d84f4f99SDavid Howells 			 u32 perms)
1431d84f4f99SDavid Howells {
1432d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1433d84f4f99SDavid Howells 
1434d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1435d84f4f99SDavid Howells }
1436d84f4f99SDavid Howells 
1437d84f4f99SDavid Howells /*
143888e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1439275bb41eSDavid Howells  * fork check, ptrace check, etc.
1440275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
14413b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1442275bb41eSDavid Howells  */
1443275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1444275bb41eSDavid Howells 			 const struct task_struct *tsk2,
14451da177e4SLinus Torvalds 			 u32 perms)
14461da177e4SLinus Torvalds {
1447275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1448275bb41eSDavid Howells 	u32 sid1, sid2;
14491da177e4SLinus Torvalds 
1450275bb41eSDavid Howells 	rcu_read_lock();
1451275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1452275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1453275bb41eSDavid Howells 	rcu_read_unlock();
1454275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14573b11a1deSDavid Howells /*
14583b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
14593b11a1deSDavid Howells  * fork check, ptrace check, etc.
14603b11a1deSDavid Howells  * current is the actor and tsk2 is the target
14613b11a1deSDavid Howells  * - this uses current's subjective creds
14623b11a1deSDavid Howells  */
14633b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
14643b11a1deSDavid Howells 			    u32 perms)
14653b11a1deSDavid Howells {
14663b11a1deSDavid Howells 	u32 sid, tsid;
14673b11a1deSDavid Howells 
14683b11a1deSDavid Howells 	sid = current_sid();
14693b11a1deSDavid Howells 	tsid = task_sid(tsk);
14703b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
14713b11a1deSDavid Howells }
14723b11a1deSDavid Howells 
1473b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1474b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1475b68e418cSStephen Smalley #endif
1476b68e418cSStephen Smalley 
14771da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
14781da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk,
14793699c53cSDavid Howells 			       const struct cred *cred,
148006112163SEric Paris 			       int cap, int audit)
14811da177e4SLinus Torvalds {
14821da177e4SLinus Torvalds 	struct avc_audit_data ad;
148306112163SEric Paris 	struct av_decision avd;
1484b68e418cSStephen Smalley 	u16 sclass;
14853699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1486b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
148706112163SEric Paris 	int rc;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, CAP);
14901da177e4SLinus Torvalds 	ad.tsk = tsk;
14911da177e4SLinus Torvalds 	ad.u.cap = cap;
14921da177e4SLinus Torvalds 
1493b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1494b68e418cSStephen Smalley 	case 0:
1495b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1496b68e418cSStephen Smalley 		break;
1497b68e418cSStephen Smalley 	case 1:
1498b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1499b68e418cSStephen Smalley 		break;
1500b68e418cSStephen Smalley 	default:
1501b68e418cSStephen Smalley 		printk(KERN_ERR
1502b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1503b68e418cSStephen Smalley 		BUG();
1504b68e418cSStephen Smalley 	}
150506112163SEric Paris 
1506275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
150706112163SEric Paris 	if (audit == SECURITY_CAP_AUDIT)
1508275bb41eSDavid Howells 		avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
150906112163SEric Paris 	return rc;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
15131da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
15141da177e4SLinus Torvalds 			   u32 perms)
15151da177e4SLinus Torvalds {
1516275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
15171da177e4SLinus Torvalds 
1518275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
15191da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
15231da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
15241da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
152588e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
15261da177e4SLinus Torvalds 			  struct inode *inode,
15271da177e4SLinus Torvalds 			  u32 perms,
15281da177e4SLinus Torvalds 			  struct avc_audit_data *adp)
15291da177e4SLinus Torvalds {
15301da177e4SLinus Torvalds 	struct inode_security_struct *isec;
15311da177e4SLinus Torvalds 	struct avc_audit_data ad;
1532275bb41eSDavid Howells 	u32 sid;
15331da177e4SLinus Torvalds 
1534bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1535bbaca6c2SStephen Smalley 		return 0;
1536bbaca6c2SStephen Smalley 
153788e67f3bSDavid Howells 	sid = cred_sid(cred);
15381da177e4SLinus Torvalds 	isec = inode->i_security;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	if (!adp) {
15411da177e4SLinus Torvalds 		adp = &ad;
15421da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, FS);
15431da177e4SLinus Torvalds 		ad.u.fs.inode = inode;
15441da177e4SLinus Torvalds 	}
15451da177e4SLinus Torvalds 
1546275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
15501da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
15511da177e4SLinus Torvalds    pathname if needed. */
155288e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
15531da177e4SLinus Torvalds 				  struct vfsmount *mnt,
15541da177e4SLinus Torvalds 				  struct dentry *dentry,
15551da177e4SLinus Torvalds 				  u32 av)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15581da177e4SLinus Torvalds 	struct avc_audit_data ad;
155988e67f3bSDavid Howells 
15601da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
156144707fdfSJan Blunck 	ad.u.fs.path.mnt = mnt;
156244707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
156388e67f3bSDavid Howells 	return inode_has_perm(cred, inode, av, &ad);
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
15671da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
15681da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
15691da177e4SLinus Torvalds    check a particular permission to the file.
15701da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
15711da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
15721da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
15731da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
157488e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
15751da177e4SLinus Torvalds 			 struct file *file,
15761da177e4SLinus Torvalds 			 u32 av)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
157944707fdfSJan Blunck 	struct inode *inode = file->f_path.dentry->d_inode;
15801da177e4SLinus Torvalds 	struct avc_audit_data ad;
158188e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
15821da177e4SLinus Torvalds 	int rc;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
158544707fdfSJan Blunck 	ad.u.fs.path = file->f_path;
15861da177e4SLinus Torvalds 
1587275bb41eSDavid Howells 	if (sid != fsec->sid) {
1588275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
15891da177e4SLinus Torvalds 				  SECCLASS_FD,
15901da177e4SLinus Torvalds 				  FD__USE,
15911da177e4SLinus Torvalds 				  &ad);
15921da177e4SLinus Torvalds 		if (rc)
159388e67f3bSDavid Howells 			goto out;
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
159788e67f3bSDavid Howells 	rc = 0;
15981da177e4SLinus Torvalds 	if (av)
159988e67f3bSDavid Howells 		rc = inode_has_perm(cred, inode, av, &ad);
16001da177e4SLinus Torvalds 
160188e67f3bSDavid Howells out:
160288e67f3bSDavid Howells 	return rc;
16031da177e4SLinus Torvalds }
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds /* Check whether a task can create a file. */
16061da177e4SLinus Torvalds static int may_create(struct inode *dir,
16071da177e4SLinus Torvalds 		      struct dentry *dentry,
16081da177e4SLinus Torvalds 		      u16 tclass)
16091da177e4SLinus Torvalds {
1610275bb41eSDavid Howells 	const struct cred *cred = current_cred();
1611275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
16121da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
16131da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1614275bb41eSDavid Howells 	u32 sid, newsid;
16151da177e4SLinus Torvalds 	struct avc_audit_data ad;
16161da177e4SLinus Torvalds 	int rc;
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	dsec = dir->i_security;
16191da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
16201da177e4SLinus Torvalds 
1621275bb41eSDavid Howells 	sid = tsec->sid;
1622275bb41eSDavid Howells 	newsid = tsec->create_sid;
1623275bb41eSDavid Howells 
16241da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
162544707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
16261da177e4SLinus Torvalds 
1627275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
16281da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
16291da177e4SLinus Torvalds 			  &ad);
16301da177e4SLinus Torvalds 	if (rc)
16311da177e4SLinus Torvalds 		return rc;
16321da177e4SLinus Torvalds 
1633cd89596fSDavid P. Quigley 	if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
1634275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid, tclass, &newsid);
16351da177e4SLinus Torvalds 		if (rc)
16361da177e4SLinus Torvalds 			return rc;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
1639275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
16401da177e4SLinus Torvalds 	if (rc)
16411da177e4SLinus Torvalds 		return rc;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
16441da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
16451da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
16484eb582cfSMichael LeMay /* Check whether a task can create a key. */
16494eb582cfSMichael LeMay static int may_create_key(u32 ksid,
16504eb582cfSMichael LeMay 			  struct task_struct *ctx)
16514eb582cfSMichael LeMay {
1652275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
16534eb582cfSMichael LeMay 
1654275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
16554eb582cfSMichael LeMay }
16564eb582cfSMichael LeMay 
16571da177e4SLinus Torvalds #define MAY_LINK	0
16581da177e4SLinus Torvalds #define MAY_UNLINK	1
16591da177e4SLinus Torvalds #define MAY_RMDIR	2
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
16621da177e4SLinus Torvalds static int may_link(struct inode *dir,
16631da177e4SLinus Torvalds 		    struct dentry *dentry,
16641da177e4SLinus Torvalds 		    int kind)
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
16681da177e4SLinus Torvalds 	struct avc_audit_data ad;
1669275bb41eSDavid Howells 	u32 sid = current_sid();
16701da177e4SLinus Torvalds 	u32 av;
16711da177e4SLinus Torvalds 	int rc;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	dsec = dir->i_security;
16741da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
167744707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	av = DIR__SEARCH;
16801da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1681275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
16821da177e4SLinus Torvalds 	if (rc)
16831da177e4SLinus Torvalds 		return rc;
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	switch (kind) {
16861da177e4SLinus Torvalds 	case MAY_LINK:
16871da177e4SLinus Torvalds 		av = FILE__LINK;
16881da177e4SLinus Torvalds 		break;
16891da177e4SLinus Torvalds 	case MAY_UNLINK:
16901da177e4SLinus Torvalds 		av = FILE__UNLINK;
16911da177e4SLinus Torvalds 		break;
16921da177e4SLinus Torvalds 	case MAY_RMDIR:
16931da177e4SLinus Torvalds 		av = DIR__RMDIR;
16941da177e4SLinus Torvalds 		break;
16951da177e4SLinus Torvalds 	default:
1696744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1697744ba35eSEric Paris 			__func__, kind);
16981da177e4SLinus Torvalds 		return 0;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
1701275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
17021da177e4SLinus Torvalds 	return rc;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
17061da177e4SLinus Torvalds 			     struct dentry *old_dentry,
17071da177e4SLinus Torvalds 			     struct inode *new_dir,
17081da177e4SLinus Torvalds 			     struct dentry *new_dentry)
17091da177e4SLinus Torvalds {
17101da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
17111da177e4SLinus Torvalds 	struct avc_audit_data ad;
1712275bb41eSDavid Howells 	u32 sid = current_sid();
17131da177e4SLinus Torvalds 	u32 av;
17141da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
17151da177e4SLinus Torvalds 	int rc;
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
17181da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
17191da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
17201da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
17231da177e4SLinus Torvalds 
172444707fdfSJan Blunck 	ad.u.fs.path.dentry = old_dentry;
1725275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
17261da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
17271da177e4SLinus Torvalds 	if (rc)
17281da177e4SLinus Torvalds 		return rc;
1729275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
17301da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
17311da177e4SLinus Torvalds 	if (rc)
17321da177e4SLinus Torvalds 		return rc;
17331da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1734275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
17351da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
17361da177e4SLinus Torvalds 		if (rc)
17371da177e4SLinus Torvalds 			return rc;
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds 
174044707fdfSJan Blunck 	ad.u.fs.path.dentry = new_dentry;
17411da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
17421da177e4SLinus Torvalds 	if (new_dentry->d_inode)
17431da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1744275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
17451da177e4SLinus Torvalds 	if (rc)
17461da177e4SLinus Torvalds 		return rc;
17471da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
17481da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
17491da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1750275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
17511da177e4SLinus Torvalds 				  new_isec->sclass,
17521da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
17531da177e4SLinus Torvalds 		if (rc)
17541da177e4SLinus Torvalds 			return rc;
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	return 0;
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
176188e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
17621da177e4SLinus Torvalds 			       struct super_block *sb,
17631da177e4SLinus Torvalds 			       u32 perms,
17641da177e4SLinus Torvalds 			       struct avc_audit_data *ad)
17651da177e4SLinus Torvalds {
17661da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
176788e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	sbsec = sb->s_security;
1770275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
17741da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
17751da177e4SLinus Torvalds {
17761da177e4SLinus Torvalds 	u32 av = 0;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	if ((mode & S_IFMT) != S_IFDIR) {
17791da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17801da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
17811da177e4SLinus Torvalds 		if (mask & MAY_READ)
17821da177e4SLinus Torvalds 			av |= FILE__READ;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
17851da177e4SLinus Torvalds 			av |= FILE__APPEND;
17861da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
17871da177e4SLinus Torvalds 			av |= FILE__WRITE;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	} else {
17901da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
17911da177e4SLinus Torvalds 			av |= DIR__SEARCH;
17921da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
17931da177e4SLinus Torvalds 			av |= DIR__WRITE;
17941da177e4SLinus Torvalds 		if (mask & MAY_READ)
17951da177e4SLinus Torvalds 			av |= DIR__READ;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	return av;
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
18021da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
18031da177e4SLinus Torvalds {
18041da177e4SLinus Torvalds 	u32 av = 0;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
18071da177e4SLinus Torvalds 		av |= FILE__READ;
18081da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
18091da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
18101da177e4SLinus Torvalds 			av |= FILE__APPEND;
18111da177e4SLinus Torvalds 		else
18121da177e4SLinus Torvalds 			av |= FILE__WRITE;
18131da177e4SLinus Torvalds 	}
18140794c66dSStephen Smalley 	if (!av) {
18150794c66dSStephen Smalley 		/*
18160794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
18170794c66dSStephen Smalley 		 */
18180794c66dSStephen Smalley 		av = FILE__IOCTL;
18190794c66dSStephen Smalley 	}
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	return av;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18248b6a5a37SEric Paris /*
18258b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
18268b6a5a37SEric Paris  * open permission.
18278b6a5a37SEric Paris  */
18288b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
18298b6a5a37SEric Paris {
18308b6a5a37SEric Paris 	u32 av = file_to_av(file);
18318b6a5a37SEric Paris 
18328b6a5a37SEric Paris 	if (selinux_policycap_openperm) {
18338b6a5a37SEric Paris 		mode_t mode = file->f_path.dentry->d_inode->i_mode;
18348b6a5a37SEric Paris 		/*
18358b6a5a37SEric Paris 		 * lnk files and socks do not really have an 'open'
18368b6a5a37SEric Paris 		 */
18378b6a5a37SEric Paris 		if (S_ISREG(mode))
18388b6a5a37SEric Paris 			av |= FILE__OPEN;
18398b6a5a37SEric Paris 		else if (S_ISCHR(mode))
18408b6a5a37SEric Paris 			av |= CHR_FILE__OPEN;
18418b6a5a37SEric Paris 		else if (S_ISBLK(mode))
18428b6a5a37SEric Paris 			av |= BLK_FILE__OPEN;
18438b6a5a37SEric Paris 		else if (S_ISFIFO(mode))
18448b6a5a37SEric Paris 			av |= FIFO_FILE__OPEN;
18458b6a5a37SEric Paris 		else if (S_ISDIR(mode))
18468b6a5a37SEric Paris 			av |= DIR__OPEN;
18476a25b27dSEric Paris 		else if (S_ISSOCK(mode))
18486a25b27dSEric Paris 			av |= SOCK_FILE__OPEN;
18498b6a5a37SEric Paris 		else
18508b6a5a37SEric Paris 			printk(KERN_ERR "SELinux: WARNING: inside %s with "
18518b6a5a37SEric Paris 				"unknown mode:%o\n", __func__, mode);
18528b6a5a37SEric Paris 	}
18538b6a5a37SEric Paris 	return av;
18548b6a5a37SEric Paris }
18558b6a5a37SEric Paris 
18561da177e4SLinus Torvalds /* Hook functions begin here. */
18571da177e4SLinus Torvalds 
18585cd9c58fSDavid Howells static int selinux_ptrace_may_access(struct task_struct *child,
1859006ebb40SStephen Smalley 				     unsigned int mode)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	int rc;
18621da177e4SLinus Torvalds 
1863200ac532SEric Paris 	rc = cap_ptrace_may_access(child, mode);
18641da177e4SLinus Torvalds 	if (rc)
18651da177e4SLinus Torvalds 		return rc;
18661da177e4SLinus Torvalds 
1867006ebb40SStephen Smalley 	if (mode == PTRACE_MODE_READ) {
1868275bb41eSDavid Howells 		u32 sid = current_sid();
1869275bb41eSDavid Howells 		u32 csid = task_sid(child);
1870275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1871006ebb40SStephen Smalley 	}
1872006ebb40SStephen Smalley 
18733b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
18745cd9c58fSDavid Howells }
18755cd9c58fSDavid Howells 
18765cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
18775cd9c58fSDavid Howells {
18785cd9c58fSDavid Howells 	int rc;
18795cd9c58fSDavid Howells 
1880200ac532SEric Paris 	rc = cap_ptrace_traceme(parent);
18815cd9c58fSDavid Howells 	if (rc)
18825cd9c58fSDavid Howells 		return rc;
18835cd9c58fSDavid Howells 
18845cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
18881da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int error;
18911da177e4SLinus Torvalds 
18923b11a1deSDavid Howells 	error = current_has_perm(target, PROCESS__GETCAP);
18931da177e4SLinus Torvalds 	if (error)
18941da177e4SLinus Torvalds 		return error;
18951da177e4SLinus Torvalds 
1896200ac532SEric Paris 	return cap_capget(target, effective, inheritable, permitted);
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
1899d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
1900d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
190115a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
190215a2460eSDavid Howells 			  const kernel_cap_t *permitted)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	int error;
19051da177e4SLinus Torvalds 
1906200ac532SEric Paris 	error = cap_capset(new, old,
1907d84f4f99SDavid Howells 				      effective, inheritable, permitted);
19081da177e4SLinus Torvalds 	if (error)
19091da177e4SLinus Torvalds 		return error;
19101da177e4SLinus Torvalds 
1911d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19145626d3e8SJames Morris /*
19155626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
19165626d3e8SJames Morris  * which was removed).
19175626d3e8SJames Morris  *
19185626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
19195626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
19205626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
19215626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
19225626d3e8SJames Morris  */
19235626d3e8SJames Morris 
19243699c53cSDavid Howells static int selinux_capable(struct task_struct *tsk, const struct cred *cred,
19253699c53cSDavid Howells 			   int cap, int audit)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	int rc;
19281da177e4SLinus Torvalds 
1929200ac532SEric Paris 	rc = cap_capable(tsk, cred, cap, audit);
19301da177e4SLinus Torvalds 	if (rc)
19311da177e4SLinus Torvalds 		return rc;
19321da177e4SLinus Torvalds 
19333699c53cSDavid Howells 	return task_has_capability(tsk, cred, cap, audit);
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
19363fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid)
19373fbfa981SEric W. Biederman {
19383fbfa981SEric W. Biederman 	int buflen, rc;
19393fbfa981SEric W. Biederman 	char *buffer, *path, *end;
19403fbfa981SEric W. Biederman 
19413fbfa981SEric W. Biederman 	rc = -ENOMEM;
19423fbfa981SEric W. Biederman 	buffer = (char *)__get_free_page(GFP_KERNEL);
19433fbfa981SEric W. Biederman 	if (!buffer)
19443fbfa981SEric W. Biederman 		goto out;
19453fbfa981SEric W. Biederman 
19463fbfa981SEric W. Biederman 	buflen = PAGE_SIZE;
19473fbfa981SEric W. Biederman 	end = buffer+buflen;
19483fbfa981SEric W. Biederman 	*--end = '\0';
19493fbfa981SEric W. Biederman 	buflen--;
19503fbfa981SEric W. Biederman 	path = end-1;
19513fbfa981SEric W. Biederman 	*path = '/';
19523fbfa981SEric W. Biederman 	while (table) {
19533fbfa981SEric W. Biederman 		const char *name = table->procname;
19543fbfa981SEric W. Biederman 		size_t namelen = strlen(name);
19553fbfa981SEric W. Biederman 		buflen -= namelen + 1;
19563fbfa981SEric W. Biederman 		if (buflen < 0)
19573fbfa981SEric W. Biederman 			goto out_free;
19583fbfa981SEric W. Biederman 		end -= namelen;
19593fbfa981SEric W. Biederman 		memcpy(end, name, namelen);
19603fbfa981SEric W. Biederman 		*--end = '/';
19613fbfa981SEric W. Biederman 		path = end;
19623fbfa981SEric W. Biederman 		table = table->parent;
19633fbfa981SEric W. Biederman 	}
1964b599fdfdSEric W. Biederman 	buflen -= 4;
1965b599fdfdSEric W. Biederman 	if (buflen < 0)
1966b599fdfdSEric W. Biederman 		goto out_free;
1967b599fdfdSEric W. Biederman 	end -= 4;
1968b599fdfdSEric W. Biederman 	memcpy(end, "/sys", 4);
1969b599fdfdSEric W. Biederman 	path = end;
19703fbfa981SEric W. Biederman 	rc = security_genfs_sid("proc", path, tclass, sid);
19713fbfa981SEric W. Biederman out_free:
19723fbfa981SEric W. Biederman 	free_page((unsigned long)buffer);
19733fbfa981SEric W. Biederman out:
19743fbfa981SEric W. Biederman 	return rc;
19753fbfa981SEric W. Biederman }
19763fbfa981SEric W. Biederman 
19771da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	int error = 0;
19801da177e4SLinus Torvalds 	u32 av;
1981275bb41eSDavid Howells 	u32 tsid, sid;
19821da177e4SLinus Torvalds 	int rc;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	rc = secondary_ops->sysctl(table, op);
19851da177e4SLinus Torvalds 	if (rc)
19861da177e4SLinus Torvalds 		return rc;
19871da177e4SLinus Torvalds 
1988275bb41eSDavid Howells 	sid = current_sid();
19891da177e4SLinus Torvalds 
19903fbfa981SEric W. Biederman 	rc = selinux_sysctl_get_sid(table, (op == 0001) ?
19911da177e4SLinus Torvalds 				    SECCLASS_DIR : SECCLASS_FILE, &tsid);
19921da177e4SLinus Torvalds 	if (rc) {
19931da177e4SLinus Torvalds 		/* Default to the well-defined sysctl SID. */
19941da177e4SLinus Torvalds 		tsid = SECINITSID_SYSCTL;
19951da177e4SLinus Torvalds 	}
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	/* The op values are "defined" in sysctl.c, thereby creating
19981da177e4SLinus Torvalds 	 * a bad coupling between this module and sysctl.c */
19991da177e4SLinus Torvalds 	if (op == 001) {
2000275bb41eSDavid Howells 		error = avc_has_perm(sid, tsid,
20011da177e4SLinus Torvalds 				     SECCLASS_DIR, DIR__SEARCH, NULL);
20021da177e4SLinus Torvalds 	} else {
20031da177e4SLinus Torvalds 		av = 0;
20041da177e4SLinus Torvalds 		if (op & 004)
20051da177e4SLinus Torvalds 			av |= FILE__READ;
20061da177e4SLinus Torvalds 		if (op & 002)
20071da177e4SLinus Torvalds 			av |= FILE__WRITE;
20081da177e4SLinus Torvalds 		if (av)
2009275bb41eSDavid Howells 			error = avc_has_perm(sid, tsid,
20101da177e4SLinus Torvalds 					     SECCLASS_FILE, av, NULL);
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	return error;
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
20171da177e4SLinus Torvalds {
201888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
20191da177e4SLinus Torvalds 	int rc = 0;
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	if (!sb)
20221da177e4SLinus Torvalds 		return 0;
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	switch (cmds) {
20251da177e4SLinus Torvalds 	case Q_SYNC:
20261da177e4SLinus Torvalds 	case Q_QUOTAON:
20271da177e4SLinus Torvalds 	case Q_QUOTAOFF:
20281da177e4SLinus Torvalds 	case Q_SETINFO:
20291da177e4SLinus Torvalds 	case Q_SETQUOTA:
203088e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
20311da177e4SLinus Torvalds 		break;
20321da177e4SLinus Torvalds 	case Q_GETFMT:
20331da177e4SLinus Torvalds 	case Q_GETINFO:
20341da177e4SLinus Torvalds 	case Q_GETQUOTA:
203588e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
20361da177e4SLinus Torvalds 		break;
20371da177e4SLinus Torvalds 	default:
20381da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
20391da177e4SLinus Torvalds 		break;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 	return rc;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
20451da177e4SLinus Torvalds {
204688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
204788e67f3bSDavid Howells 
204888e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON);
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds static int selinux_syslog(int type)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	int rc;
20541da177e4SLinus Torvalds 
2055200ac532SEric Paris 	rc = cap_syslog(type);
20561da177e4SLinus Torvalds 	if (rc)
20571da177e4SLinus Torvalds 		return rc;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	switch (type) {
20601da177e4SLinus Torvalds 	case 3:		/* Read last kernel messages */
20611da177e4SLinus Torvalds 	case 10:	/* Return size of the log buffer */
20621da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
20631da177e4SLinus Torvalds 		break;
20641da177e4SLinus Torvalds 	case 6:		/* Disable logging to console */
20651da177e4SLinus Torvalds 	case 7:		/* Enable logging to console */
20661da177e4SLinus Torvalds 	case 8:		/* Set level of messages printed to console */
20671da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
20681da177e4SLinus Torvalds 		break;
20691da177e4SLinus Torvalds 	case 0:		/* Close log */
20701da177e4SLinus Torvalds 	case 1:		/* Open log */
20711da177e4SLinus Torvalds 	case 2:		/* Read from log */
20721da177e4SLinus Torvalds 	case 4:		/* Read/clear last kernel messages */
20731da177e4SLinus Torvalds 	case 5:		/* Clear ring buffer */
20741da177e4SLinus Torvalds 	default:
20751da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
20761da177e4SLinus Torvalds 		break;
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds 	return rc;
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds /*
20821da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
20831da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
20841da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
20851da177e4SLinus Torvalds  *
20861da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
20871da177e4SLinus Torvalds  * processes that allocate mappings.
20881da177e4SLinus Torvalds  */
208934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
20901da177e4SLinus Torvalds {
20911da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
20921da177e4SLinus Torvalds 
20933699c53cSDavid Howells 	rc = selinux_capable(current, current_cred(), CAP_SYS_ADMIN,
20943699c53cSDavid Howells 			     SECURITY_CAP_NOAUDIT);
20951da177e4SLinus Torvalds 	if (rc == 0)
20961da177e4SLinus Torvalds 		cap_sys_admin = 1;
20971da177e4SLinus Torvalds 
209834b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds /* binprm security operations */
21021da177e4SLinus Torvalds 
2103a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
21041da177e4SLinus Torvalds {
2105a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2106a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
21071da177e4SLinus Torvalds 	struct inode_security_struct *isec;
21081da177e4SLinus Torvalds 	struct avc_audit_data ad;
2109a6f76f23SDavid Howells 	struct inode *inode = bprm->file->f_path.dentry->d_inode;
21101da177e4SLinus Torvalds 	int rc;
21111da177e4SLinus Torvalds 
2112200ac532SEric Paris 	rc = cap_bprm_set_creds(bprm);
21131da177e4SLinus Torvalds 	if (rc)
21141da177e4SLinus Torvalds 		return rc;
21151da177e4SLinus Torvalds 
2116a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2117a6f76f23SDavid Howells 	 * the script interpreter */
2118a6f76f23SDavid Howells 	if (bprm->cred_prepared)
21191da177e4SLinus Torvalds 		return 0;
21201da177e4SLinus Torvalds 
2121a6f76f23SDavid Howells 	old_tsec = current_security();
2122a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
21231da177e4SLinus Torvalds 	isec = inode->i_security;
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	/* Default to the current task SID. */
2126a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2127a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
21281da177e4SLinus Torvalds 
212928eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2130a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2131a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2132a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
21331da177e4SLinus Torvalds 
2134a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2135a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
21361da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2137a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
21381da177e4SLinus Torvalds 	} else {
21391da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2140a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2141a6f76f23SDavid Howells 					     SECCLASS_PROCESS, &new_tsec->sid);
21421da177e4SLinus Torvalds 		if (rc)
21431da177e4SLinus Torvalds 			return rc;
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
214744707fdfSJan Blunck 	ad.u.fs.path = bprm->file->f_path;
21481da177e4SLinus Torvalds 
21493d5ff529SJosef Sipek 	if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
2150a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->sid;
21511da177e4SLinus Torvalds 
2152a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2153a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
21541da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
21551da177e4SLinus Torvalds 		if (rc)
21561da177e4SLinus Torvalds 			return rc;
21571da177e4SLinus Torvalds 	} else {
21581da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2159a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
21601da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
21611da177e4SLinus Torvalds 		if (rc)
21621da177e4SLinus Torvalds 			return rc;
21631da177e4SLinus Torvalds 
2164a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
21651da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
21661da177e4SLinus Torvalds 		if (rc)
21671da177e4SLinus Torvalds 			return rc;
21681da177e4SLinus Torvalds 
2169a6f76f23SDavid Howells 		/* Check for shared state */
2170a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2171a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2172a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2173a6f76f23SDavid Howells 					  NULL);
2174a6f76f23SDavid Howells 			if (rc)
2175a6f76f23SDavid Howells 				return -EPERM;
21761da177e4SLinus Torvalds 		}
21771da177e4SLinus Torvalds 
2178a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2179a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2180a6f76f23SDavid Howells 		if (bprm->unsafe &
2181a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2182a6f76f23SDavid Howells 			struct task_struct *tracer;
2183a6f76f23SDavid Howells 			struct task_security_struct *sec;
2184a6f76f23SDavid Howells 			u32 ptsid = 0;
2185a6f76f23SDavid Howells 
2186a6f76f23SDavid Howells 			rcu_read_lock();
2187a6f76f23SDavid Howells 			tracer = tracehook_tracer_task(current);
2188a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2189a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2190a6f76f23SDavid Howells 				ptsid = sec->sid;
2191a6f76f23SDavid Howells 			}
2192a6f76f23SDavid Howells 			rcu_read_unlock();
2193a6f76f23SDavid Howells 
2194a6f76f23SDavid Howells 			if (ptsid != 0) {
2195a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2196a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2197a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2198a6f76f23SDavid Howells 				if (rc)
2199a6f76f23SDavid Howells 					return -EPERM;
2200a6f76f23SDavid Howells 			}
2201a6f76f23SDavid Howells 		}
2202a6f76f23SDavid Howells 
2203a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2204a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2205a6f76f23SDavid Howells 	}
2206a6f76f23SDavid Howells 
22071da177e4SLinus Torvalds 	return 0;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
22111da177e4SLinus Torvalds {
2212275bb41eSDavid Howells 	const struct cred *cred = current_cred();
2213275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
2214275bb41eSDavid Howells 	u32 sid, osid;
22151da177e4SLinus Torvalds 	int atsecure = 0;
22161da177e4SLinus Torvalds 
2217275bb41eSDavid Howells 	sid = tsec->sid;
2218275bb41eSDavid Howells 	osid = tsec->osid;
2219275bb41eSDavid Howells 
2220275bb41eSDavid Howells 	if (osid != sid) {
22211da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
22221da177e4SLinus Torvalds 		   the noatsecure permission is granted between
22231da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2224275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
22251da177e4SLinus Torvalds 					SECCLASS_PROCESS,
22261da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
22271da177e4SLinus Torvalds 	}
22281da177e4SLinus Torvalds 
2229200ac532SEric Paris 	return (atsecure || cap_bprm_secureexec(bprm));
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount;
22331da177e4SLinus Torvalds extern struct dentry *selinux_null;
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2236745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2237745ca247SDavid Howells 					    struct files_struct *files)
22381da177e4SLinus Torvalds {
22391da177e4SLinus Torvalds 	struct avc_audit_data ad;
22401da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2241b20c8122SStephen Smalley 	struct tty_struct *tty;
2242badf1662SDipankar Sarma 	struct fdtable *fdt;
22431da177e4SLinus Torvalds 	long j = -1;
224424ec839cSPeter Zijlstra 	int drop_tty = 0;
22451da177e4SLinus Torvalds 
224624ec839cSPeter Zijlstra 	tty = get_current_tty();
22471da177e4SLinus Torvalds 	if (tty) {
22481da177e4SLinus Torvalds 		file_list_lock();
224937dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
225037dd0bd0SEric Paris 			struct inode *inode;
225137dd0bd0SEric Paris 
22521da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
22531da177e4SLinus Torvalds 			   Use inode_has_perm on the tty inode directly rather
22541da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
22551da177e4SLinus Torvalds 			   file may belong to another process and we are only
22561da177e4SLinus Torvalds 			   interested in the inode-based check here. */
225737dd0bd0SEric Paris 			file = list_first_entry(&tty->tty_files, struct file, f_u.fu_list);
225837dd0bd0SEric Paris 			inode = file->f_path.dentry->d_inode;
225988e67f3bSDavid Howells 			if (inode_has_perm(cred, inode,
22601da177e4SLinus Torvalds 					   FILE__READ | FILE__WRITE, NULL)) {
226124ec839cSPeter Zijlstra 				drop_tty = 1;
22621da177e4SLinus Torvalds 			}
22631da177e4SLinus Torvalds 		}
22641da177e4SLinus Torvalds 		file_list_unlock();
2265452a00d2SAlan Cox 		tty_kref_put(tty);
22661da177e4SLinus Torvalds 	}
226798a27ba4SEric W. Biederman 	/* Reset controlling tty. */
226898a27ba4SEric W. Biederman 	if (drop_tty)
226998a27ba4SEric W. Biederman 		no_tty();
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	spin_lock(&files->file_lock);
22761da177e4SLinus Torvalds 	for (;;) {
22771da177e4SLinus Torvalds 		unsigned long set, i;
22781da177e4SLinus Torvalds 		int fd;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 		j++;
22811da177e4SLinus Torvalds 		i = j * __NFDBITS;
2282badf1662SDipankar Sarma 		fdt = files_fdtable(files);
2283bbea9f69SVadim Lobanov 		if (i >= fdt->max_fds)
22841da177e4SLinus Torvalds 			break;
2285badf1662SDipankar Sarma 		set = fdt->open_fds->fds_bits[j];
22861da177e4SLinus Torvalds 		if (!set)
22871da177e4SLinus Torvalds 			continue;
22881da177e4SLinus Torvalds 		spin_unlock(&files->file_lock);
22891da177e4SLinus Torvalds 		for ( ; set ; i++, set >>= 1) {
22901da177e4SLinus Torvalds 			if (set & 1) {
22911da177e4SLinus Torvalds 				file = fget(i);
22921da177e4SLinus Torvalds 				if (!file)
22931da177e4SLinus Torvalds 					continue;
229488e67f3bSDavid Howells 				if (file_has_perm(cred,
22951da177e4SLinus Torvalds 						  file,
22961da177e4SLinus Torvalds 						  file_to_av(file))) {
22971da177e4SLinus Torvalds 					sys_close(i);
22981da177e4SLinus Torvalds 					fd = get_unused_fd();
22991da177e4SLinus Torvalds 					if (fd != i) {
23001da177e4SLinus Torvalds 						if (fd >= 0)
23011da177e4SLinus Torvalds 							put_unused_fd(fd);
23021da177e4SLinus Torvalds 						fput(file);
23031da177e4SLinus Torvalds 						continue;
23041da177e4SLinus Torvalds 					}
23051da177e4SLinus Torvalds 					if (devnull) {
2306095975daSNick Piggin 						get_file(devnull);
23071da177e4SLinus Torvalds 					} else {
2308745ca247SDavid Howells 						devnull = dentry_open(
2309745ca247SDavid Howells 							dget(selinux_null),
2310745ca247SDavid Howells 							mntget(selinuxfs_mount),
2311745ca247SDavid Howells 							O_RDWR, cred);
2312fc5d81e6SAkinobu Mita 						if (IS_ERR(devnull)) {
2313fc5d81e6SAkinobu Mita 							devnull = NULL;
23141da177e4SLinus Torvalds 							put_unused_fd(fd);
23151da177e4SLinus Torvalds 							fput(file);
23161da177e4SLinus Torvalds 							continue;
23171da177e4SLinus Torvalds 						}
23181da177e4SLinus Torvalds 					}
23191da177e4SLinus Torvalds 					fd_install(fd, devnull);
23201da177e4SLinus Torvalds 				}
23211da177e4SLinus Torvalds 				fput(file);
23221da177e4SLinus Torvalds 			}
23231da177e4SLinus Torvalds 		}
23241da177e4SLinus Torvalds 		spin_lock(&files->file_lock);
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	}
23271da177e4SLinus Torvalds 	spin_unlock(&files->file_lock);
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds /*
2331a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
23321da177e4SLinus Torvalds  */
2333a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
23341da177e4SLinus Torvalds {
2335a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23361da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
23371da177e4SLinus Torvalds 	int rc, i;
23381da177e4SLinus Torvalds 
2339a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2340a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
23411da177e4SLinus Torvalds 		return;
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2344a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
23451da177e4SLinus Torvalds 
2346a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2347a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2348a6f76f23SDavid Howells 
2349a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2350a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2351a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2352a6f76f23SDavid Howells 	 *
2353a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2354a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2355a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2356a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2357a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2358a6f76f23SDavid Howells 	 */
2359a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2360a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2361a6f76f23SDavid Howells 	if (rc) {
2362a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2363a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2364a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2365a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2366a6f76f23SDavid Howells 		}
2367a6f76f23SDavid Howells 		update_rlimit_cpu(rlim->rlim_cur);
2368a6f76f23SDavid Howells 	}
2369a6f76f23SDavid Howells }
2370a6f76f23SDavid Howells 
2371a6f76f23SDavid Howells /*
2372a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2373a6f76f23SDavid Howells  * due to exec
2374a6f76f23SDavid Howells  */
2375a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2376a6f76f23SDavid Howells {
2377a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2378a6f76f23SDavid Howells 	struct itimerval itimer;
2379a6f76f23SDavid Howells 	struct sighand_struct *psig;
2380a6f76f23SDavid Howells 	u32 osid, sid;
2381a6f76f23SDavid Howells 	int rc, i;
2382a6f76f23SDavid Howells 	unsigned long flags;
2383a6f76f23SDavid Howells 
2384a6f76f23SDavid Howells 	osid = tsec->osid;
2385a6f76f23SDavid Howells 	sid = tsec->sid;
2386a6f76f23SDavid Howells 
2387a6f76f23SDavid Howells 	if (sid == osid)
2388a6f76f23SDavid Howells 		return;
2389a6f76f23SDavid Howells 
2390a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2391a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2392a6f76f23SDavid Howells 	 * flush and unblock signals.
2393a6f76f23SDavid Howells 	 *
2394a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2395a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2396a6f76f23SDavid Howells 	 */
2397a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
23981da177e4SLinus Torvalds 	if (rc) {
23991da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
24001da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
24011da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
24021da177e4SLinus Torvalds 		flush_signals(current);
24031da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
24041da177e4SLinus Torvalds 		flush_signal_handlers(current, 1);
24051da177e4SLinus Torvalds 		sigemptyset(&current->blocked);
24061da177e4SLinus Torvalds 		recalc_sigpending();
24071da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
24081da177e4SLinus Torvalds 	}
24091da177e4SLinus Torvalds 
2410a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2411a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
241241d9f9c5SEric Paris 	read_lock_irq(&tasklist_lock);
241341d9f9c5SEric Paris 	psig = current->parent->sighand;
241441d9f9c5SEric Paris 	spin_lock_irqsave(&psig->siglock, flags);
24151da177e4SLinus Torvalds 	wake_up_interruptible(&current->parent->signal->wait_chldexit);
241641d9f9c5SEric Paris 	spin_unlock_irqrestore(&psig->siglock, flags);
241741d9f9c5SEric Paris 	read_unlock_irq(&tasklist_lock);
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds /* superblock security operations */
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
24281da177e4SLinus Torvalds {
24291da177e4SLinus Torvalds 	superblock_free_security(sb);
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	if (plen > olen)
24351da177e4SLinus Torvalds 		return 0;
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
24381da177e4SLinus Torvalds }
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
24411da177e4SLinus Torvalds {
2442832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2443832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2444832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
244511689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
244611689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
24501da177e4SLinus Torvalds {
24511da177e4SLinus Torvalds 	if (!*first) {
24521da177e4SLinus Torvalds 		**to = ',';
24531da177e4SLinus Torvalds 		*to += 1;
24543528a953SCory Olmo 	} else
24551da177e4SLinus Torvalds 		*first = 0;
24561da177e4SLinus Torvalds 	memcpy(*to, from, len);
24571da177e4SLinus Torvalds 	*to += len;
24581da177e4SLinus Torvalds }
24591da177e4SLinus Torvalds 
24603528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
24613528a953SCory Olmo 				       int len)
24623528a953SCory Olmo {
24633528a953SCory Olmo 	int current_size = 0;
24643528a953SCory Olmo 
24653528a953SCory Olmo 	if (!*first) {
24663528a953SCory Olmo 		**to = '|';
24673528a953SCory Olmo 		*to += 1;
2468828dfe1dSEric Paris 	} else
24693528a953SCory Olmo 		*first = 0;
24703528a953SCory Olmo 
24713528a953SCory Olmo 	while (current_size < len) {
24723528a953SCory Olmo 		if (*from != '"') {
24733528a953SCory Olmo 			**to = *from;
24743528a953SCory Olmo 			*to += 1;
24753528a953SCory Olmo 		}
24763528a953SCory Olmo 		from += 1;
24773528a953SCory Olmo 		current_size += 1;
24783528a953SCory Olmo 	}
24793528a953SCory Olmo }
24803528a953SCory Olmo 
2481e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
24841da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
24851da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
24863528a953SCory Olmo 	int open_quote = 0;
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	in_curr = orig;
24891da177e4SLinus Torvalds 	sec_curr = copy;
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
24921da177e4SLinus Torvalds 	if (!nosec) {
24931da177e4SLinus Torvalds 		rc = -ENOMEM;
24941da177e4SLinus Torvalds 		goto out;
24951da177e4SLinus Torvalds 	}
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	nosec_save = nosec;
24981da177e4SLinus Torvalds 	fnosec = fsec = 1;
24991da177e4SLinus Torvalds 	in_save = in_end = orig;
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	do {
25023528a953SCory Olmo 		if (*in_end == '"')
25033528a953SCory Olmo 			open_quote = !open_quote;
25043528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
25053528a953SCory Olmo 				*in_end == '\0') {
25061da177e4SLinus Torvalds 			int len = in_end - in_curr;
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
25093528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
25101da177e4SLinus Torvalds 			else
25111da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 			in_curr = in_end + 1;
25141da177e4SLinus Torvalds 		}
25151da177e4SLinus Torvalds 	} while (*in_end++);
25161da177e4SLinus Torvalds 
25176931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2518da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
25191da177e4SLinus Torvalds out:
25201da177e4SLinus Torvalds 	return rc;
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds 
252312204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
25241da177e4SLinus Torvalds {
252588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25261da177e4SLinus Torvalds 	struct avc_audit_data ad;
25271da177e4SLinus Torvalds 	int rc;
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
25301da177e4SLinus Torvalds 	if (rc)
25311da177e4SLinus Torvalds 		return rc;
25321da177e4SLinus Torvalds 
253374192246SJames Morris 	/* Allow all mounts performed by the kernel */
253474192246SJames Morris 	if (flags & MS_KERNMOUNT)
253574192246SJames Morris 		return 0;
253674192246SJames Morris 
25371da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
253844707fdfSJan Blunck 	ad.u.fs.path.dentry = sb->s_root;
253988e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
2542726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
25431da177e4SLinus Torvalds {
254488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25451da177e4SLinus Torvalds 	struct avc_audit_data ad;
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
254844707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry->d_sb->s_root;
254988e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds static int selinux_mount(char *dev_name,
2553b5266eb4SAl Viro 			 struct path *path,
25541da177e4SLinus Torvalds 			 char *type,
25551da177e4SLinus Torvalds 			 unsigned long flags,
25561da177e4SLinus Torvalds 			 void *data)
25571da177e4SLinus Torvalds {
255888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
256188e67f3bSDavid Howells 		return superblock_has_perm(cred, path->mnt->mnt_sb,
25621da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
25631da177e4SLinus Torvalds 	else
256488e67f3bSDavid Howells 		return dentry_has_perm(cred, path->mnt, path->dentry,
25651da177e4SLinus Torvalds 				       FILE__MOUNTON);
25661da177e4SLinus Torvalds }
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
25691da177e4SLinus Torvalds {
257088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
25711da177e4SLinus Torvalds 
257288e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
25731da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
25741da177e4SLinus Torvalds }
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds /* inode security operations */
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
25791da177e4SLinus Torvalds {
25801da177e4SLinus Torvalds 	return inode_alloc_security(inode);
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
25841da177e4SLinus Torvalds {
25851da177e4SLinus Torvalds 	inode_free_security(inode);
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25885e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
25895e41ff9eSStephen Smalley 				       char **name, void **value,
25905e41ff9eSStephen Smalley 				       size_t *len)
25915e41ff9eSStephen Smalley {
2592275bb41eSDavid Howells 	const struct cred *cred = current_cred();
2593275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
25945e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
25955e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2596275bb41eSDavid Howells 	u32 sid, newsid, clen;
25975e41ff9eSStephen Smalley 	int rc;
2598570bc1c2SStephen Smalley 	char *namep = NULL, *context;
25995e41ff9eSStephen Smalley 
26005e41ff9eSStephen Smalley 	dsec = dir->i_security;
26015e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
26025e41ff9eSStephen Smalley 
2603275bb41eSDavid Howells 	sid = tsec->sid;
26045e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2605275bb41eSDavid Howells 
2606cd89596fSDavid P. Quigley 	if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
2607275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
26085e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
26095e41ff9eSStephen Smalley 					     &newsid);
26105e41ff9eSStephen Smalley 		if (rc) {
26115e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
26125e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
26135e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2614dd6f953aSHarvey Harrison 			       __func__,
26155e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
26165e41ff9eSStephen Smalley 			return rc;
26175e41ff9eSStephen Smalley 		}
26185e41ff9eSStephen Smalley 	}
26195e41ff9eSStephen Smalley 
2620296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
26210d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2622296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2623296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2624296fddf7SEric Paris 		isec->sid = newsid;
2625296fddf7SEric Paris 		isec->initialized = 1;
2626296fddf7SEric Paris 	}
26275e41ff9eSStephen Smalley 
2628cd89596fSDavid P. Quigley 	if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
262925a74f3bSStephen Smalley 		return -EOPNOTSUPP;
263025a74f3bSStephen Smalley 
2631570bc1c2SStephen Smalley 	if (name) {
2632a02fe132SJosef Bacik 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
26335e41ff9eSStephen Smalley 		if (!namep)
26345e41ff9eSStephen Smalley 			return -ENOMEM;
26355e41ff9eSStephen Smalley 		*name = namep;
2636570bc1c2SStephen Smalley 	}
26375e41ff9eSStephen Smalley 
2638570bc1c2SStephen Smalley 	if (value && len) {
263912b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
26405e41ff9eSStephen Smalley 		if (rc) {
26415e41ff9eSStephen Smalley 			kfree(namep);
26425e41ff9eSStephen Smalley 			return rc;
26435e41ff9eSStephen Smalley 		}
26445e41ff9eSStephen Smalley 		*value = context;
2645570bc1c2SStephen Smalley 		*len = clen;
2646570bc1c2SStephen Smalley 	}
26475e41ff9eSStephen Smalley 
26485e41ff9eSStephen Smalley 	return 0;
26495e41ff9eSStephen Smalley }
26505e41ff9eSStephen Smalley 
26511da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
26521da177e4SLinus Torvalds {
26531da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
26621da177e4SLinus Torvalds {
26631da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
26721da177e4SLinus Torvalds {
26731da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
26791da177e4SLinus Torvalds }
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
26821da177e4SLinus Torvalds {
26831da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
26871da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
26881da177e4SLinus Torvalds {
26891da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
26931da177e4SLinus Torvalds {
269488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
269588e67f3bSDavid Howells 
269688e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__READ);
26971da177e4SLinus Torvalds }
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
27001da177e4SLinus Torvalds {
270188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27021da177e4SLinus Torvalds 
270388e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__READ);
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
2706b77b0646SAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
27071da177e4SLinus Torvalds {
270888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	if (!mask) {
27111da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
27121da177e4SLinus Torvalds 		return 0;
27131da177e4SLinus Torvalds 	}
27141da177e4SLinus Torvalds 
271588e67f3bSDavid Howells 	return inode_has_perm(cred, inode,
27168b6a5a37SEric Paris 			      file_mask_to_av(inode->i_mode, mask), NULL);
27171da177e4SLinus Torvalds }
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
27201da177e4SLinus Torvalds {
272188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	if (iattr->ia_valid & ATTR_FORCE)
27241da177e4SLinus Torvalds 		return 0;
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 	if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
27271da177e4SLinus Torvalds 			       ATTR_ATIME_SET | ATTR_MTIME_SET))
272888e67f3bSDavid Howells 		return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR);
27291da177e4SLinus Torvalds 
273088e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__WRITE);
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();
273688e67f3bSDavid Howells 
273788e67f3bSDavid Howells 	return dentry_has_perm(cred, mnt, dentry, FILE__GETATTR);
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
27408f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2741b5376771SSerge E. Hallyn {
274288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
274388e67f3bSDavid Howells 
2744b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2745b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2746b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2747b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2748b5376771SSerge E. Hallyn 				return -EPERM;
2749b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2750b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2751b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2752b5376771SSerge E. Hallyn 			return -EPERM;
2753b5376771SSerge E. Hallyn 		}
2754b5376771SSerge E. Hallyn 	}
2755b5376771SSerge E. Hallyn 
2756b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2757b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
275888e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR);
2759b5376771SSerge E. Hallyn }
2760b5376771SSerge E. Hallyn 
27618f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
27628f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
27631da177e4SLinus Torvalds {
27641da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
27651da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27661da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
27671da177e4SLinus Torvalds 	struct avc_audit_data ad;
2768275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
27691da177e4SLinus Torvalds 	int rc = 0;
27701da177e4SLinus Torvalds 
2771b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2772b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
2775cd89596fSDavid P. Quigley 	if (!(sbsec->flags & SE_SBLABELSUPP))
27761da177e4SLinus Torvalds 		return -EOPNOTSUPP;
27771da177e4SLinus Torvalds 
27783bd858abSSatyam Sharma 	if (!is_owner_or_cap(inode))
27791da177e4SLinus Torvalds 		return -EPERM;
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
278244707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
27831da177e4SLinus Torvalds 
2784275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
27851da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
27861da177e4SLinus Torvalds 	if (rc)
27871da177e4SLinus Torvalds 		return rc;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
279012b29f34SStephen Smalley 	if (rc == -EINVAL) {
279112b29f34SStephen Smalley 		if (!capable(CAP_MAC_ADMIN))
279212b29f34SStephen Smalley 			return rc;
279312b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
279412b29f34SStephen Smalley 	}
27951da177e4SLinus Torvalds 	if (rc)
27961da177e4SLinus Torvalds 		return rc;
27971da177e4SLinus Torvalds 
2798275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
27991da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
28001da177e4SLinus Torvalds 	if (rc)
28011da177e4SLinus Torvalds 		return rc;
28021da177e4SLinus Torvalds 
2803275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
28041da177e4SLinus Torvalds 					  isec->sclass);
28051da177e4SLinus Torvalds 	if (rc)
28061da177e4SLinus Torvalds 		return rc;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	return avc_has_perm(newsid,
28091da177e4SLinus Torvalds 			    sbsec->sid,
28101da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
28111da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
28121da177e4SLinus Torvalds 			    &ad);
28131da177e4SLinus Torvalds }
28141da177e4SLinus Torvalds 
28158f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
28168f0cfa52SDavid Howells 					const void *value, size_t size,
28178f0cfa52SDavid Howells 					int flags)
28181da177e4SLinus Torvalds {
28191da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28201da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28211da177e4SLinus Torvalds 	u32 newsid;
28221da177e4SLinus Torvalds 	int rc;
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
28251da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
28261da177e4SLinus Torvalds 		return;
28271da177e4SLinus Torvalds 	}
28281da177e4SLinus Torvalds 
282912b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
28301da177e4SLinus Torvalds 	if (rc) {
283112b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
283212b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
283312b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
28341da177e4SLinus Torvalds 		return;
28351da177e4SLinus Torvalds 	}
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 	isec->sid = newsid;
28381da177e4SLinus Torvalds 	return;
28391da177e4SLinus Torvalds }
28401da177e4SLinus Torvalds 
28418f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
28421da177e4SLinus Torvalds {
284388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
284488e67f3bSDavid Howells 
284588e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR);
28461da177e4SLinus Torvalds }
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
28491da177e4SLinus Torvalds {
285088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
285188e67f3bSDavid Howells 
285288e67f3bSDavid Howells 	return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR);
28531da177e4SLinus Torvalds }
28541da177e4SLinus Torvalds 
28558f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
28561da177e4SLinus Torvalds {
2857b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2858b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
28611da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
28621da177e4SLinus Torvalds 	return -EACCES;
28631da177e4SLinus Torvalds }
28641da177e4SLinus Torvalds 
2865d381d8a9SJames Morris /*
2866abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
2867d381d8a9SJames Morris  *
2868d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2869d381d8a9SJames Morris  */
287042492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
28711da177e4SLinus Torvalds {
287242492594SDavid P. Quigley 	u32 size;
287342492594SDavid P. Quigley 	int error;
287442492594SDavid P. Quigley 	char *context = NULL;
28751da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28761da177e4SLinus Torvalds 
28778c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
28788c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
28791da177e4SLinus Torvalds 
2880abc69bb6SStephen Smalley 	/*
2881abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
2882abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
2883abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
2884abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
2885abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
2886abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
2887abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
2888abc69bb6SStephen Smalley 	 */
28893699c53cSDavid Howells 	error = selinux_capable(current, current_cred(), CAP_MAC_ADMIN,
28903699c53cSDavid Howells 				SECURITY_CAP_NOAUDIT);
2891abc69bb6SStephen Smalley 	if (!error)
2892abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
2893abc69bb6SStephen Smalley 						      &size);
2894abc69bb6SStephen Smalley 	else
289542492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
289642492594SDavid P. Quigley 	if (error)
289742492594SDavid P. Quigley 		return error;
289842492594SDavid P. Quigley 	error = size;
289942492594SDavid P. Quigley 	if (alloc) {
290042492594SDavid P. Quigley 		*buffer = context;
290142492594SDavid P. Quigley 		goto out_nofree;
290242492594SDavid P. Quigley 	}
290342492594SDavid P. Quigley 	kfree(context);
290442492594SDavid P. Quigley out_nofree:
290542492594SDavid P. Quigley 	return error;
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
29091da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
29101da177e4SLinus Torvalds {
29111da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29121da177e4SLinus Torvalds 	u32 newsid;
29131da177e4SLinus Torvalds 	int rc;
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
29161da177e4SLinus Torvalds 		return -EOPNOTSUPP;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	if (!value || !size)
29191da177e4SLinus Torvalds 		return -EACCES;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	rc = security_context_to_sid((void *)value, size, &newsid);
29221da177e4SLinus Torvalds 	if (rc)
29231da177e4SLinus Torvalds 		return rc;
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 	isec->sid = newsid;
29261da177e4SLinus Torvalds 	return 0;
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
29301da177e4SLinus Torvalds {
29311da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
29321da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
29331da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
29341da177e4SLinus Torvalds 	return len;
29351da177e4SLinus Torvalds }
29361da177e4SLinus Torvalds 
2937713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2938713a04aeSAhmed S. Darwish {
2939713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
2940713a04aeSAhmed S. Darwish 	*secid = isec->sid;
2941713a04aeSAhmed S. Darwish }
2942713a04aeSAhmed S. Darwish 
29431da177e4SLinus Torvalds /* file security operations */
29441da177e4SLinus Torvalds 
2945788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
29461da177e4SLinus Torvalds {
294788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
29483d5ff529SJosef Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
29491da177e4SLinus Torvalds 
29501da177e4SLinus Torvalds 	if (!mask) {
29511da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
29521da177e4SLinus Torvalds 		return 0;
29531da177e4SLinus Torvalds 	}
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
29561da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
29571da177e4SLinus Torvalds 		mask |= MAY_APPEND;
29581da177e4SLinus Torvalds 
2959*389fb800SPaul Moore 	return file_has_perm(cred, file,
29601da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
2963788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
2964788e7dd4SYuichi Nakamura {
2965*389fb800SPaul Moore 	if (!mask)
2966788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
2967788e7dd4SYuichi Nakamura 		return 0;
2968788e7dd4SYuichi Nakamura 
2969788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
2970788e7dd4SYuichi Nakamura }
2971788e7dd4SYuichi Nakamura 
29721da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
29731da177e4SLinus Torvalds {
29741da177e4SLinus Torvalds 	return file_alloc_security(file);
29751da177e4SLinus Torvalds }
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
29781da177e4SLinus Torvalds {
29791da177e4SLinus Torvalds 	file_free_security(file);
29801da177e4SLinus Torvalds }
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
29831da177e4SLinus Torvalds 			      unsigned long arg)
29841da177e4SLinus Torvalds {
298588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2986242631c4SStephen Smalley 	u32 av = 0;
29871da177e4SLinus Torvalds 
2988242631c4SStephen Smalley 	if (_IOC_DIR(cmd) & _IOC_WRITE)
2989242631c4SStephen Smalley 		av |= FILE__WRITE;
2990242631c4SStephen Smalley 	if (_IOC_DIR(cmd) & _IOC_READ)
2991242631c4SStephen Smalley 		av |= FILE__READ;
2992242631c4SStephen Smalley 	if (!av)
2993242631c4SStephen Smalley 		av = FILE__IOCTL;
29941da177e4SLinus Torvalds 
299588e67f3bSDavid Howells 	return file_has_perm(cred, file, av);
29961da177e4SLinus Torvalds }
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
29991da177e4SLinus Torvalds {
300088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3001d84f4f99SDavid Howells 	int rc = 0;
300288e67f3bSDavid Howells 
30031da177e4SLinus Torvalds #ifndef CONFIG_PPC32
30041da177e4SLinus Torvalds 	if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
30051da177e4SLinus Torvalds 		/*
30061da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
30071da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
30081da177e4SLinus Torvalds 		 * This has an additional check.
30091da177e4SLinus Torvalds 		 */
3010d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
30111da177e4SLinus Torvalds 		if (rc)
3012d84f4f99SDavid Howells 			goto error;
30131da177e4SLinus Torvalds 	}
30141da177e4SLinus Torvalds #endif
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	if (file) {
30171da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
30181da177e4SLinus Torvalds 		u32 av = FILE__READ;
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
30211da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
30221da177e4SLinus Torvalds 			av |= FILE__WRITE;
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
30251da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
30261da177e4SLinus Torvalds 
302788e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
30281da177e4SLinus Torvalds 	}
3029d84f4f99SDavid Howells 
3030d84f4f99SDavid Howells error:
3031d84f4f99SDavid Howells 	return rc;
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot,
3035ed032189SEric Paris 			     unsigned long prot, unsigned long flags,
3036ed032189SEric Paris 			     unsigned long addr, unsigned long addr_only)
30371da177e4SLinus Torvalds {
3038ed032189SEric Paris 	int rc = 0;
3039275bb41eSDavid Howells 	u32 sid = current_sid();
30401da177e4SLinus Torvalds 
3041ed032189SEric Paris 	if (addr < mmap_min_addr)
3042ed032189SEric Paris 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3043ed032189SEric Paris 				  MEMPROTECT__MMAP_ZERO, NULL);
3044ed032189SEric Paris 	if (rc || addr_only)
30451da177e4SLinus Torvalds 		return rc;
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 	if (selinux_checkreqprot)
30481da177e4SLinus Torvalds 		prot = reqprot;
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
30511da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
30551da177e4SLinus Torvalds 				 unsigned long reqprot,
30561da177e4SLinus Torvalds 				 unsigned long prot)
30571da177e4SLinus Torvalds {
305888e67f3bSDavid Howells 	const struct cred *cred = current_cred();
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	if (selinux_checkreqprot)
30611da177e4SLinus Torvalds 		prot = reqprot;
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds #ifndef CONFIG_PPC32
3064db4c9641SStephen Smalley 	if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3065d541bbeeSJames Morris 		int rc = 0;
3066db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3067db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3068d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3069db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
30706b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
30716b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
30723b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3073db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3074db4c9641SStephen Smalley 			/*
3075db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3076db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3077db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3078db4c9641SStephen Smalley 			 * modified content.  This typically should only
3079db4c9641SStephen Smalley 			 * occur for text relocations.
3080db4c9641SStephen Smalley 			 */
3081d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3082db4c9641SStephen Smalley 		}
30836b992197SLorenzo Hernandez García-Hierro 		if (rc)
30846b992197SLorenzo Hernandez García-Hierro 			return rc;
30856b992197SLorenzo Hernandez García-Hierro 	}
30861da177e4SLinus Torvalds #endif
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
30921da177e4SLinus Torvalds {
309388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
309488e67f3bSDavid Howells 
309588e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
30981da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
30991da177e4SLinus Torvalds 			      unsigned long arg)
31001da177e4SLinus Torvalds {
310188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
31021da177e4SLinus Torvalds 	int err = 0;
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds 	switch (cmd) {
31051da177e4SLinus Torvalds 	case F_SETFL:
31063d5ff529SJosef Sipek 		if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
31071da177e4SLinus Torvalds 			err = -EINVAL;
31081da177e4SLinus Torvalds 			break;
31091da177e4SLinus Torvalds 		}
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
311288e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
31131da177e4SLinus Torvalds 			break;
31141da177e4SLinus Torvalds 		}
31151da177e4SLinus Torvalds 		/* fall through */
31161da177e4SLinus Torvalds 	case F_SETOWN:
31171da177e4SLinus Torvalds 	case F_SETSIG:
31181da177e4SLinus Torvalds 	case F_GETFL:
31191da177e4SLinus Torvalds 	case F_GETOWN:
31201da177e4SLinus Torvalds 	case F_GETSIG:
31211da177e4SLinus Torvalds 		/* Just check FD__USE permission */
312288e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
31231da177e4SLinus Torvalds 		break;
31241da177e4SLinus Torvalds 	case F_GETLK:
31251da177e4SLinus Torvalds 	case F_SETLK:
31261da177e4SLinus Torvalds 	case F_SETLKW:
31271da177e4SLinus Torvalds #if BITS_PER_LONG == 32
31281da177e4SLinus Torvalds 	case F_GETLK64:
31291da177e4SLinus Torvalds 	case F_SETLK64:
31301da177e4SLinus Torvalds 	case F_SETLKW64:
31311da177e4SLinus Torvalds #endif
31323d5ff529SJosef Sipek 		if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
31331da177e4SLinus Torvalds 			err = -EINVAL;
31341da177e4SLinus Torvalds 			break;
31351da177e4SLinus Torvalds 		}
313688e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
31371da177e4SLinus Torvalds 		break;
31381da177e4SLinus Torvalds 	}
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	return err;
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds 	struct file_security_struct *fsec;
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	fsec = file->f_security;
3148275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 	return 0;
31511da177e4SLinus Torvalds }
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
31541da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
31551da177e4SLinus Torvalds {
31561da177e4SLinus Torvalds 	struct file *file;
3157275bb41eSDavid Howells 	u32 sid = current_sid();
31581da177e4SLinus Torvalds 	u32 perm;
31591da177e4SLinus Torvalds 	struct file_security_struct *fsec;
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3162b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds 	fsec = file->f_security;
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds 	if (!signum)
31671da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
31681da177e4SLinus Torvalds 	else
31691da177e4SLinus Torvalds 		perm = signal_to_av(signum);
31701da177e4SLinus Torvalds 
3171275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
31721da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
31761da177e4SLinus Torvalds {
317788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
317888e67f3bSDavid Howells 
317988e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
31801da177e4SLinus Torvalds }
31811da177e4SLinus Torvalds 
3182745ca247SDavid Howells static int selinux_dentry_open(struct file *file, const struct cred *cred)
3183788e7dd4SYuichi Nakamura {
3184788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3185788e7dd4SYuichi Nakamura 	struct inode *inode;
3186788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3187d84f4f99SDavid Howells 
3188788e7dd4SYuichi Nakamura 	inode = file->f_path.dentry->d_inode;
3189788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3190788e7dd4SYuichi Nakamura 	isec = inode->i_security;
3191788e7dd4SYuichi Nakamura 	/*
3192788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3193788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3194788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3195788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3196788e7dd4SYuichi Nakamura 	 * struct as its SID.
3197788e7dd4SYuichi Nakamura 	 */
3198788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3199788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3200788e7dd4SYuichi Nakamura 	/*
3201788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3202788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3203788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3204788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3205788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3206788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3207788e7dd4SYuichi Nakamura 	 */
320888e67f3bSDavid Howells 	return inode_has_perm(cred, inode, open_file_to_av(file), NULL);
3209788e7dd4SYuichi Nakamura }
3210788e7dd4SYuichi Nakamura 
32111da177e4SLinus Torvalds /* task security operations */
32121da177e4SLinus Torvalds 
32131da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
32141da177e4SLinus Torvalds {
32153b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
32161da177e4SLinus Torvalds }
32171da177e4SLinus Torvalds 
3218f1752eecSDavid Howells /*
3219f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3220f1752eecSDavid Howells  */
3221f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
32221da177e4SLinus Torvalds {
3223f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3224f1752eecSDavid Howells 	cred->security = NULL;
3225f1752eecSDavid Howells 	kfree(tsec);
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
3228d84f4f99SDavid Howells /*
3229d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3230d84f4f99SDavid Howells  */
3231d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3232d84f4f99SDavid Howells 				gfp_t gfp)
3233d84f4f99SDavid Howells {
3234d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3235d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3236d84f4f99SDavid Howells 
3237d84f4f99SDavid Howells 	old_tsec = old->security;
3238d84f4f99SDavid Howells 
3239d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3240d84f4f99SDavid Howells 	if (!tsec)
3241d84f4f99SDavid Howells 		return -ENOMEM;
3242d84f4f99SDavid Howells 
3243d84f4f99SDavid Howells 	new->security = tsec;
3244d84f4f99SDavid Howells 	return 0;
3245d84f4f99SDavid Howells }
3246d84f4f99SDavid Howells 
3247d84f4f99SDavid Howells /*
32483a3b7ce9SDavid Howells  * set the security data for a kernel service
32493a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
32503a3b7ce9SDavid Howells  */
32513a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
32523a3b7ce9SDavid Howells {
32533a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
32543a3b7ce9SDavid Howells 	u32 sid = current_sid();
32553a3b7ce9SDavid Howells 	int ret;
32563a3b7ce9SDavid Howells 
32573a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
32583a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
32593a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
32603a3b7ce9SDavid Howells 			   NULL);
32613a3b7ce9SDavid Howells 	if (ret == 0) {
32623a3b7ce9SDavid Howells 		tsec->sid = secid;
32633a3b7ce9SDavid Howells 		tsec->create_sid = 0;
32643a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
32653a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
32663a3b7ce9SDavid Howells 	}
32673a3b7ce9SDavid Howells 	return ret;
32683a3b7ce9SDavid Howells }
32693a3b7ce9SDavid Howells 
32703a3b7ce9SDavid Howells /*
32713a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
32723a3b7ce9SDavid Howells  * objective context of the specified inode
32733a3b7ce9SDavid Howells  */
32743a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
32753a3b7ce9SDavid Howells {
32763a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
32773a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
32783a3b7ce9SDavid Howells 	u32 sid = current_sid();
32793a3b7ce9SDavid Howells 	int ret;
32803a3b7ce9SDavid Howells 
32813a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
32823a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
32833a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
32843a3b7ce9SDavid Howells 			   NULL);
32853a3b7ce9SDavid Howells 
32863a3b7ce9SDavid Howells 	if (ret == 0)
32873a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
32883a3b7ce9SDavid Howells 	return 0;
32893a3b7ce9SDavid Howells }
32903a3b7ce9SDavid Howells 
32911da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
32921da177e4SLinus Torvalds {
32933b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
32941da177e4SLinus Torvalds }
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
32971da177e4SLinus Torvalds {
32983b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
32991da177e4SLinus Torvalds }
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
33021da177e4SLinus Torvalds {
33033b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
33041da177e4SLinus Torvalds }
33051da177e4SLinus Torvalds 
3306f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3307f9008e4cSDavid Quigley {
3308275bb41eSDavid Howells 	*secid = task_sid(p);
3309f9008e4cSDavid Quigley }
3310f9008e4cSDavid Quigley 
33111da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
33121da177e4SLinus Torvalds {
33131da177e4SLinus Torvalds 	int rc;
33141da177e4SLinus Torvalds 
3315200ac532SEric Paris 	rc = cap_task_setnice(p, nice);
33161da177e4SLinus Torvalds 	if (rc)
33171da177e4SLinus Torvalds 		return rc;
33181da177e4SLinus Torvalds 
33193b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
33201da177e4SLinus Torvalds }
33211da177e4SLinus Torvalds 
332203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
332303e68060SJames Morris {
3324b5376771SSerge E. Hallyn 	int rc;
3325b5376771SSerge E. Hallyn 
3326200ac532SEric Paris 	rc = cap_task_setioprio(p, ioprio);
3327b5376771SSerge E. Hallyn 	if (rc)
3328b5376771SSerge E. Hallyn 		return rc;
3329b5376771SSerge E. Hallyn 
33303b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
333103e68060SJames Morris }
333203e68060SJames Morris 
3333a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3334a1836a42SDavid Quigley {
33353b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3336a1836a42SDavid Quigley }
3337a1836a42SDavid Quigley 
33381da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
33391da177e4SLinus Torvalds {
33401da177e4SLinus Torvalds 	struct rlimit *old_rlim = current->signal->rlim + resource;
33411da177e4SLinus Torvalds 
33421da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
33431da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
33441da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3345d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
33461da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
33473b11a1deSDavid Howells 		return current_has_perm(current, PROCESS__SETRLIMIT);
33481da177e4SLinus Torvalds 
33491da177e4SLinus Torvalds 	return 0;
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
33531da177e4SLinus Torvalds {
3354b5376771SSerge E. Hallyn 	int rc;
3355b5376771SSerge E. Hallyn 
3356200ac532SEric Paris 	rc = cap_task_setscheduler(p, policy, lp);
3357b5376771SSerge E. Hallyn 	if (rc)
3358b5376771SSerge E. Hallyn 		return rc;
3359b5376771SSerge E. Hallyn 
33603b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
33611da177e4SLinus Torvalds }
33621da177e4SLinus Torvalds 
33631da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
33641da177e4SLinus Torvalds {
33653b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
33661da177e4SLinus Torvalds }
33671da177e4SLinus Torvalds 
336835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
336935601547SDavid Quigley {
33703b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
337135601547SDavid Quigley }
337235601547SDavid Quigley 
3373f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3374f9008e4cSDavid Quigley 				int sig, u32 secid)
33751da177e4SLinus Torvalds {
33761da177e4SLinus Torvalds 	u32 perm;
33771da177e4SLinus Torvalds 	int rc;
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 	if (!sig)
33801da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
33811da177e4SLinus Torvalds 	else
33821da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3383f9008e4cSDavid Quigley 	if (secid)
3384275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3385275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3386f9008e4cSDavid Quigley 	else
33873b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3388f9008e4cSDavid Quigley 	return rc;
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
33921da177e4SLinus Torvalds {
33938a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
33941da177e4SLinus Torvalds }
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
33971da177e4SLinus Torvalds 				  struct inode *inode)
33981da177e4SLinus Torvalds {
33991da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3400275bb41eSDavid Howells 	u32 sid = task_sid(p);
34011da177e4SLinus Torvalds 
3402275bb41eSDavid Howells 	isec->sid = sid;
34031da177e4SLinus Torvalds 	isec->initialized = 1;
34041da177e4SLinus Torvalds }
34051da177e4SLinus Torvalds 
34061da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
340767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
340867f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
34091da177e4SLinus Torvalds {
34101da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
34111da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
34121da177e4SLinus Torvalds 
3413bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
34141da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
34151da177e4SLinus Torvalds 	if (ih == NULL)
34161da177e4SLinus Torvalds 		goto out;
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
34191da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
34201da177e4SLinus Torvalds 		goto out;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 	ad->u.net.v4info.saddr = ih->saddr;
34231da177e4SLinus Torvalds 	ad->u.net.v4info.daddr = ih->daddr;
34241da177e4SLinus Torvalds 	ret = 0;
34251da177e4SLinus Torvalds 
342667f83cbfSVenkat Yekkirala 	if (proto)
342767f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
342867f83cbfSVenkat Yekkirala 
34291da177e4SLinus Torvalds 	switch (ih->protocol) {
34301da177e4SLinus Torvalds 	case IPPROTO_TCP: {
34311da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
34321da177e4SLinus Torvalds 
34331da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
34341da177e4SLinus Torvalds 			break;
34351da177e4SLinus Torvalds 
34361da177e4SLinus Torvalds 		offset += ihlen;
34371da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
34381da177e4SLinus Torvalds 		if (th == NULL)
34391da177e4SLinus Torvalds 			break;
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
34421da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
34431da177e4SLinus Torvalds 		break;
34441da177e4SLinus Torvalds 	}
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds 	case IPPROTO_UDP: {
34471da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
34481da177e4SLinus Torvalds 
34491da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
34501da177e4SLinus Torvalds 			break;
34511da177e4SLinus Torvalds 
34521da177e4SLinus Torvalds 		offset += ihlen;
34531da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
34541da177e4SLinus Torvalds 		if (uh == NULL)
34551da177e4SLinus Torvalds 			break;
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds 		ad->u.net.sport = uh->source;
34581da177e4SLinus Torvalds 		ad->u.net.dport = uh->dest;
34591da177e4SLinus Torvalds 		break;
34601da177e4SLinus Torvalds 	}
34611da177e4SLinus Torvalds 
34622ee92d46SJames Morris 	case IPPROTO_DCCP: {
34632ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
34642ee92d46SJames Morris 
34652ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
34662ee92d46SJames Morris 			break;
34672ee92d46SJames Morris 
34682ee92d46SJames Morris 		offset += ihlen;
34692ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
34702ee92d46SJames Morris 		if (dh == NULL)
34712ee92d46SJames Morris 			break;
34722ee92d46SJames Morris 
34732ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
34742ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
34752ee92d46SJames Morris 		break;
34762ee92d46SJames Morris 	}
34772ee92d46SJames Morris 
34781da177e4SLinus Torvalds 	default:
34791da177e4SLinus Torvalds 		break;
34801da177e4SLinus Torvalds 	}
34811da177e4SLinus Torvalds out:
34821da177e4SLinus Torvalds 	return ret;
34831da177e4SLinus Torvalds }
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
348867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
348967f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
34901da177e4SLinus Torvalds {
34911da177e4SLinus Torvalds 	u8 nexthdr;
34921da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
34931da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
34941da177e4SLinus Torvalds 
3495bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
34961da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
34971da177e4SLinus Torvalds 	if (ip6 == NULL)
34981da177e4SLinus Torvalds 		goto out;
34991da177e4SLinus Torvalds 
35001da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
35011da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
35021da177e4SLinus Torvalds 	ret = 0;
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
35051da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
35060d3d077cSHerbert Xu 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
35071da177e4SLinus Torvalds 	if (offset < 0)
35081da177e4SLinus Torvalds 		goto out;
35091da177e4SLinus Torvalds 
351067f83cbfSVenkat Yekkirala 	if (proto)
351167f83cbfSVenkat Yekkirala 		*proto = nexthdr;
351267f83cbfSVenkat Yekkirala 
35131da177e4SLinus Torvalds 	switch (nexthdr) {
35141da177e4SLinus Torvalds 	case IPPROTO_TCP: {
35151da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
35161da177e4SLinus Torvalds 
35171da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
35181da177e4SLinus Torvalds 		if (th == NULL)
35191da177e4SLinus Torvalds 			break;
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
35221da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
35231da177e4SLinus Torvalds 		break;
35241da177e4SLinus Torvalds 	}
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 	case IPPROTO_UDP: {
35271da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
35281da177e4SLinus Torvalds 
35291da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
35301da177e4SLinus Torvalds 		if (uh == NULL)
35311da177e4SLinus Torvalds 			break;
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds 		ad->u.net.sport = uh->source;
35341da177e4SLinus Torvalds 		ad->u.net.dport = uh->dest;
35351da177e4SLinus Torvalds 		break;
35361da177e4SLinus Torvalds 	}
35371da177e4SLinus Torvalds 
35382ee92d46SJames Morris 	case IPPROTO_DCCP: {
35392ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
35402ee92d46SJames Morris 
35412ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
35422ee92d46SJames Morris 		if (dh == NULL)
35432ee92d46SJames Morris 			break;
35442ee92d46SJames Morris 
35452ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
35462ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
35472ee92d46SJames Morris 		break;
35482ee92d46SJames Morris 	}
35492ee92d46SJames Morris 
35501da177e4SLinus Torvalds 	/* includes fragments */
35511da177e4SLinus Torvalds 	default:
35521da177e4SLinus Torvalds 		break;
35531da177e4SLinus Torvalds 	}
35541da177e4SLinus Torvalds out:
35551da177e4SLinus Torvalds 	return ret;
35561da177e4SLinus Torvalds }
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds #endif /* IPV6 */
35591da177e4SLinus Torvalds 
35601da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
3561cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
35621da177e4SLinus Torvalds {
3563cf9481e2SDavid Howells 	char *addrp;
3564cf9481e2SDavid Howells 	int ret;
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds 	switch (ad->u.net.family) {
35671da177e4SLinus Torvalds 	case PF_INET:
356867f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3569cf9481e2SDavid Howells 		if (ret)
3570cf9481e2SDavid Howells 			goto parse_error;
3571cf9481e2SDavid Howells 		addrp = (char *)(src ? &ad->u.net.v4info.saddr :
35721da177e4SLinus Torvalds 				       &ad->u.net.v4info.daddr);
3573cf9481e2SDavid Howells 		goto okay;
35741da177e4SLinus Torvalds 
35751da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
35761da177e4SLinus Torvalds 	case PF_INET6:
357767f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3578cf9481e2SDavid Howells 		if (ret)
3579cf9481e2SDavid Howells 			goto parse_error;
3580cf9481e2SDavid Howells 		addrp = (char *)(src ? &ad->u.net.v6info.saddr :
35811da177e4SLinus Torvalds 				       &ad->u.net.v6info.daddr);
3582cf9481e2SDavid Howells 		goto okay;
35831da177e4SLinus Torvalds #endif	/* IPV6 */
35841da177e4SLinus Torvalds 	default:
3585cf9481e2SDavid Howells 		addrp = NULL;
3586cf9481e2SDavid Howells 		goto okay;
35871da177e4SLinus Torvalds 	}
35881da177e4SLinus Torvalds 
3589cf9481e2SDavid Howells parse_error:
359071f1cb05SPaul Moore 	printk(KERN_WARNING
359171f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
359271f1cb05SPaul Moore 	       " unable to parse packet\n");
35931da177e4SLinus Torvalds 	return ret;
3594cf9481e2SDavid Howells 
3595cf9481e2SDavid Howells okay:
3596cf9481e2SDavid Howells 	if (_addrp)
3597cf9481e2SDavid Howells 		*_addrp = addrp;
3598cf9481e2SDavid Howells 	return 0;
35991da177e4SLinus Torvalds }
36001da177e4SLinus Torvalds 
36014f6a993fSPaul Moore /**
3602220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
36034f6a993fSPaul Moore  * @skb: the packet
360475e22910SPaul Moore  * @family: protocol family
3605220deb96SPaul Moore  * @sid: the packet's peer label SID
36064f6a993fSPaul Moore  *
36074f6a993fSPaul Moore  * Description:
3608220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3609220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3610220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3611220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3612220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3613220deb96SPaul Moore  * peer labels.
36144f6a993fSPaul Moore  *
36154f6a993fSPaul Moore  */
3616220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
36174f6a993fSPaul Moore {
361871f1cb05SPaul Moore 	int err;
36194f6a993fSPaul Moore 	u32 xfrm_sid;
36204f6a993fSPaul Moore 	u32 nlbl_sid;
3621220deb96SPaul Moore 	u32 nlbl_type;
36224f6a993fSPaul Moore 
36234f6a993fSPaul Moore 	selinux_skb_xfrm_sid(skb, &xfrm_sid);
36245dbe1eb0SPaul Moore 	selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3625220deb96SPaul Moore 
362671f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
362771f1cb05SPaul Moore 	if (unlikely(err)) {
362871f1cb05SPaul Moore 		printk(KERN_WARNING
362971f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
363071f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3631220deb96SPaul Moore 		return -EACCES;
363271f1cb05SPaul Moore 	}
3633220deb96SPaul Moore 
3634220deb96SPaul Moore 	return 0;
36354f6a993fSPaul Moore }
36364f6a993fSPaul Moore 
36371da177e4SLinus Torvalds /* socket security operations */
36381da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock,
36391da177e4SLinus Torvalds 			   u32 perms)
36401da177e4SLinus Torvalds {
36411da177e4SLinus Torvalds 	struct inode_security_struct *isec;
36421da177e4SLinus Torvalds 	struct avc_audit_data ad;
3643275bb41eSDavid Howells 	u32 sid;
36441da177e4SLinus Torvalds 	int err = 0;
36451da177e4SLinus Torvalds 
36461da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	if (isec->sid == SECINITSID_KERNEL)
36491da177e4SLinus Torvalds 		goto out;
3650275bb41eSDavid Howells 	sid = task_sid(task);
36511da177e4SLinus Torvalds 
36521da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, NET);
36531da177e4SLinus Torvalds 	ad.u.net.sk = sock->sk;
3654275bb41eSDavid Howells 	err = avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
36551da177e4SLinus Torvalds 
36561da177e4SLinus Torvalds out:
36571da177e4SLinus Torvalds 	return err;
36581da177e4SLinus Torvalds }
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
36611da177e4SLinus Torvalds 				 int protocol, int kern)
36621da177e4SLinus Torvalds {
3663275bb41eSDavid Howells 	const struct cred *cred = current_cred();
3664275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3665275bb41eSDavid Howells 	u32 sid, newsid;
3666275bb41eSDavid Howells 	u16 secclass;
36671da177e4SLinus Torvalds 	int err = 0;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	if (kern)
36701da177e4SLinus Torvalds 		goto out;
36711da177e4SLinus Torvalds 
3672275bb41eSDavid Howells 	sid = tsec->sid;
3673275bb41eSDavid Howells 	newsid = tsec->sockcreate_sid ?: sid;
3674275bb41eSDavid Howells 
3675275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
3676275bb41eSDavid Howells 	err = avc_has_perm(sid, newsid, secclass, SOCKET__CREATE, NULL);
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds out:
36791da177e4SLinus Torvalds 	return err;
36801da177e4SLinus Torvalds }
36811da177e4SLinus Torvalds 
36827420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
36831da177e4SLinus Torvalds 				      int type, int protocol, int kern)
36841da177e4SLinus Torvalds {
3685275bb41eSDavid Howells 	const struct cred *cred = current_cred();
3686275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
36871da177e4SLinus Torvalds 	struct inode_security_struct *isec;
3688892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
3689275bb41eSDavid Howells 	u32 sid, newsid;
3690275bb41eSDavid Howells 	int err = 0;
3691275bb41eSDavid Howells 
3692275bb41eSDavid Howells 	sid = tsec->sid;
3693275bb41eSDavid Howells 	newsid = tsec->sockcreate_sid;
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
36961da177e4SLinus Torvalds 
3697275bb41eSDavid Howells 	if (kern)
3698275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
3699275bb41eSDavid Howells 	else if (newsid)
3700275bb41eSDavid Howells 		isec->sid = newsid;
3701275bb41eSDavid Howells 	else
3702275bb41eSDavid Howells 		isec->sid = sid;
3703275bb41eSDavid Howells 
37041da177e4SLinus Torvalds 	isec->sclass = socket_type_to_security_class(family, type, protocol);
37051da177e4SLinus Torvalds 	isec->initialized = 1;
37061da177e4SLinus Torvalds 
3707892c141eSVenkat Yekkirala 	if (sock->sk) {
3708892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3709892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
3710220deb96SPaul Moore 		sksec->sclass = isec->sclass;
3711*389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
3712892c141eSVenkat Yekkirala 	}
3713892c141eSVenkat Yekkirala 
37147420ed23SVenkat Yekkirala 	return err;
37151da177e4SLinus Torvalds }
37161da177e4SLinus Torvalds 
37171da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
37181da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
37191da177e4SLinus Torvalds    permission check between the socket and the port number. */
37201da177e4SLinus Torvalds 
37211da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
37221da177e4SLinus Torvalds {
37231da177e4SLinus Torvalds 	u16 family;
37241da177e4SLinus Torvalds 	int err;
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__BIND);
37271da177e4SLinus Torvalds 	if (err)
37281da177e4SLinus Torvalds 		goto out;
37291da177e4SLinus Torvalds 
37301da177e4SLinus Torvalds 	/*
37311da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
373213402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
373313402580SJames Morris 	 * check the first address now.
37341da177e4SLinus Torvalds 	 */
37351da177e4SLinus Torvalds 	family = sock->sk->sk_family;
37361da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
37371da177e4SLinus Torvalds 		char *addrp;
37381da177e4SLinus Torvalds 		struct inode_security_struct *isec;
37391da177e4SLinus Torvalds 		struct avc_audit_data ad;
37401da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
37411da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
37421da177e4SLinus Torvalds 		unsigned short snum;
37431da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
3744e399f982SJames Morris 		u32 sid, node_perm;
37451da177e4SLinus Torvalds 
37461da177e4SLinus Torvalds 		isec = SOCK_INODE(sock)->i_security;
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds 		if (family == PF_INET) {
37491da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
37501da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
37511da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
37521da177e4SLinus Torvalds 		} else {
37531da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
37541da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
37551da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
37561da177e4SLinus Torvalds 		}
37571da177e4SLinus Torvalds 
3758227b60f5SStephen Hemminger 		if (snum) {
3759227b60f5SStephen Hemminger 			int low, high;
3760227b60f5SStephen Hemminger 
3761227b60f5SStephen Hemminger 			inet_get_local_port_range(&low, &high);
3762227b60f5SStephen Hemminger 
3763227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
37643e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
37653e112172SPaul Moore 						      snum, &sid);
37661da177e4SLinus Torvalds 				if (err)
37671da177e4SLinus Torvalds 					goto out;
37681da177e4SLinus Torvalds 				AVC_AUDIT_DATA_INIT(&ad, NET);
37691da177e4SLinus Torvalds 				ad.u.net.sport = htons(snum);
37701da177e4SLinus Torvalds 				ad.u.net.family = family;
37711da177e4SLinus Torvalds 				err = avc_has_perm(isec->sid, sid,
37721da177e4SLinus Torvalds 						   isec->sclass,
37731da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
37741da177e4SLinus Torvalds 				if (err)
37751da177e4SLinus Torvalds 					goto out;
37761da177e4SLinus Torvalds 			}
3777227b60f5SStephen Hemminger 		}
37781da177e4SLinus Torvalds 
377913402580SJames Morris 		switch (isec->sclass) {
378013402580SJames Morris 		case SECCLASS_TCP_SOCKET:
37811da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
37821da177e4SLinus Torvalds 			break;
37831da177e4SLinus Torvalds 
378413402580SJames Morris 		case SECCLASS_UDP_SOCKET:
37851da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
37861da177e4SLinus Torvalds 			break;
37871da177e4SLinus Torvalds 
37882ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
37892ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
37902ee92d46SJames Morris 			break;
37912ee92d46SJames Morris 
37921da177e4SLinus Torvalds 		default:
37931da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
37941da177e4SLinus Torvalds 			break;
37951da177e4SLinus Torvalds 		}
37961da177e4SLinus Torvalds 
3797224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
37981da177e4SLinus Torvalds 		if (err)
37991da177e4SLinus Torvalds 			goto out;
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, NET);
38021da177e4SLinus Torvalds 		ad.u.net.sport = htons(snum);
38031da177e4SLinus Torvalds 		ad.u.net.family = family;
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds 		if (family == PF_INET)
38061da177e4SLinus Torvalds 			ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
38071da177e4SLinus Torvalds 		else
38081da177e4SLinus Torvalds 			ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, sid,
38111da177e4SLinus Torvalds 				   isec->sclass, node_perm, &ad);
38121da177e4SLinus Torvalds 		if (err)
38131da177e4SLinus Torvalds 			goto out;
38141da177e4SLinus Torvalds 	}
38151da177e4SLinus Torvalds out:
38161da177e4SLinus Torvalds 	return err;
38171da177e4SLinus Torvalds }
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
38201da177e4SLinus Torvalds {
3821014ab19aSPaul Moore 	struct sock *sk = sock->sk;
38221da177e4SLinus Torvalds 	struct inode_security_struct *isec;
38231da177e4SLinus Torvalds 	int err;
38241da177e4SLinus Torvalds 
38251da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__CONNECT);
38261da177e4SLinus Torvalds 	if (err)
38271da177e4SLinus Torvalds 		return err;
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 	/*
38302ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
38311da177e4SLinus Torvalds 	 */
38321da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
38332ee92d46SJames Morris 	if (isec->sclass == SECCLASS_TCP_SOCKET ||
38342ee92d46SJames Morris 	    isec->sclass == SECCLASS_DCCP_SOCKET) {
38351da177e4SLinus Torvalds 		struct avc_audit_data ad;
38361da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
38371da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
38381da177e4SLinus Torvalds 		unsigned short snum;
38392ee92d46SJames Morris 		u32 sid, perm;
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
38421da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3843911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
38441da177e4SLinus Torvalds 				return -EINVAL;
38451da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
38461da177e4SLinus Torvalds 		} else {
38471da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3848911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
38491da177e4SLinus Torvalds 				return -EINVAL;
38501da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
38511da177e4SLinus Torvalds 		}
38521da177e4SLinus Torvalds 
38533e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
38541da177e4SLinus Torvalds 		if (err)
38551da177e4SLinus Torvalds 			goto out;
38561da177e4SLinus Torvalds 
38572ee92d46SJames Morris 		perm = (isec->sclass == SECCLASS_TCP_SOCKET) ?
38582ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
38592ee92d46SJames Morris 
38601da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, NET);
38611da177e4SLinus Torvalds 		ad.u.net.dport = htons(snum);
38621da177e4SLinus Torvalds 		ad.u.net.family = sk->sk_family;
38632ee92d46SJames Morris 		err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad);
38641da177e4SLinus Torvalds 		if (err)
38651da177e4SLinus Torvalds 			goto out;
38661da177e4SLinus Torvalds 	}
38671da177e4SLinus Torvalds 
3868014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
3869014ab19aSPaul Moore 
38701da177e4SLinus Torvalds out:
38711da177e4SLinus Torvalds 	return err;
38721da177e4SLinus Torvalds }
38731da177e4SLinus Torvalds 
38741da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
38751da177e4SLinus Torvalds {
38761da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__LISTEN);
38771da177e4SLinus Torvalds }
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
38801da177e4SLinus Torvalds {
38811da177e4SLinus Torvalds 	int err;
38821da177e4SLinus Torvalds 	struct inode_security_struct *isec;
38831da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__ACCEPT);
38861da177e4SLinus Torvalds 	if (err)
38871da177e4SLinus Torvalds 		return err;
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
38901da177e4SLinus Torvalds 
38911da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
38921da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
38931da177e4SLinus Torvalds 	newisec->sid = isec->sid;
38941da177e4SLinus Torvalds 	newisec->initialized = 1;
38951da177e4SLinus Torvalds 
38961da177e4SLinus Torvalds 	return 0;
38971da177e4SLinus Torvalds }
38981da177e4SLinus Torvalds 
38991da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
39001da177e4SLinus Torvalds 				  int size)
39011da177e4SLinus Torvalds {
3902*389fb800SPaul Moore 	return socket_has_perm(current, sock, SOCKET__WRITE);
39031da177e4SLinus Torvalds }
39041da177e4SLinus Torvalds 
39051da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
39061da177e4SLinus Torvalds 				  int size, int flags)
39071da177e4SLinus Torvalds {
39081da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__READ);
39091da177e4SLinus Torvalds }
39101da177e4SLinus Torvalds 
39111da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
39121da177e4SLinus Torvalds {
39131da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
39141da177e4SLinus Torvalds }
39151da177e4SLinus Torvalds 
39161da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
39171da177e4SLinus Torvalds {
39181da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
39191da177e4SLinus Torvalds }
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
39221da177e4SLinus Torvalds {
3923f8687afeSPaul Moore 	int err;
3924f8687afeSPaul Moore 
3925f8687afeSPaul Moore 	err = socket_has_perm(current, sock, SOCKET__SETOPT);
3926f8687afeSPaul Moore 	if (err)
3927f8687afeSPaul Moore 		return err;
3928f8687afeSPaul Moore 
3929f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
39301da177e4SLinus Torvalds }
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
39331da177e4SLinus Torvalds 				     int optname)
39341da177e4SLinus Torvalds {
39351da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETOPT);
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
39391da177e4SLinus Torvalds {
39401da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
39411da177e4SLinus Torvalds }
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock,
39441da177e4SLinus Torvalds 					      struct socket *other,
39451da177e4SLinus Torvalds 					      struct sock *newsk)
39461da177e4SLinus Torvalds {
39471da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
39481da177e4SLinus Torvalds 	struct inode_security_struct *isec;
39491da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
39501da177e4SLinus Torvalds 	struct avc_audit_data ad;
39511da177e4SLinus Torvalds 	int err;
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
39541da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
39551da177e4SLinus Torvalds 
39561da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, NET);
39571da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
39581da177e4SLinus Torvalds 
39591da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
39601da177e4SLinus Torvalds 			   isec->sclass,
39611da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
39621da177e4SLinus Torvalds 	if (err)
39631da177e4SLinus Torvalds 		return err;
39641da177e4SLinus Torvalds 
39651da177e4SLinus Torvalds 	/* connecting socket */
39661da177e4SLinus Torvalds 	ssec = sock->sk->sk_security;
39671da177e4SLinus Torvalds 	ssec->peer_sid = other_isec->sid;
39681da177e4SLinus Torvalds 
39691da177e4SLinus Torvalds 	/* server child socket */
39701da177e4SLinus Torvalds 	ssec = newsk->sk_security;
39711da177e4SLinus Torvalds 	ssec->peer_sid = isec->sid;
39724237c75cSVenkat Yekkirala 	err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid);
39731da177e4SLinus Torvalds 
39744237c75cSVenkat Yekkirala 	return err;
39751da177e4SLinus Torvalds }
39761da177e4SLinus Torvalds 
39771da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
39781da177e4SLinus Torvalds 					struct socket *other)
39791da177e4SLinus Torvalds {
39801da177e4SLinus Torvalds 	struct inode_security_struct *isec;
39811da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
39821da177e4SLinus Torvalds 	struct avc_audit_data ad;
39831da177e4SLinus Torvalds 	int err;
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
39861da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, NET);
39891da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
39901da177e4SLinus Torvalds 
39911da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
39921da177e4SLinus Torvalds 			   isec->sclass, SOCKET__SENDTO, &ad);
39931da177e4SLinus Torvalds 	if (err)
39941da177e4SLinus Torvalds 		return err;
39951da177e4SLinus Torvalds 
39961da177e4SLinus Torvalds 	return 0;
39971da177e4SLinus Torvalds }
39981da177e4SLinus Torvalds 
3999effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4000effad8dfSPaul Moore 				    u32 peer_sid,
4001effad8dfSPaul Moore 				    struct avc_audit_data *ad)
4002effad8dfSPaul Moore {
4003effad8dfSPaul Moore 	int err;
4004effad8dfSPaul Moore 	u32 if_sid;
4005effad8dfSPaul Moore 	u32 node_sid;
4006effad8dfSPaul Moore 
4007effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
4008effad8dfSPaul Moore 	if (err)
4009effad8dfSPaul Moore 		return err;
4010effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4011effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4012effad8dfSPaul Moore 	if (err)
4013effad8dfSPaul Moore 		return err;
4014effad8dfSPaul Moore 
4015effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4016effad8dfSPaul Moore 	if (err)
4017effad8dfSPaul Moore 		return err;
4018effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4019effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4020effad8dfSPaul Moore }
4021effad8dfSPaul Moore 
4022220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
4023220deb96SPaul Moore 						struct sk_buff *skb,
4024224dfbd8SPaul Moore 						struct avc_audit_data *ad,
4025220deb96SPaul Moore 						u16 family,
4026220deb96SPaul Moore 						char *addrp)
40271da177e4SLinus Torvalds {
4028220deb96SPaul Moore 	int err;
4029220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4030220deb96SPaul Moore 	u16 sk_class;
4031220deb96SPaul Moore 	u32 netif_perm, node_perm, recv_perm;
4032220deb96SPaul Moore 	u32 port_sid, node_sid, if_sid, sk_sid;
40334237c75cSVenkat Yekkirala 
4034220deb96SPaul Moore 	sk_sid = sksec->sid;
4035220deb96SPaul Moore 	sk_class = sksec->sclass;
40361da177e4SLinus Torvalds 
4037220deb96SPaul Moore 	switch (sk_class) {
40381da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
40391da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_RECV;
40401da177e4SLinus Torvalds 		node_perm = NODE__UDP_RECV;
40411da177e4SLinus Torvalds 		recv_perm = UDP_SOCKET__RECV_MSG;
40421da177e4SLinus Torvalds 		break;
40431da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
40441da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_RECV;
40451da177e4SLinus Torvalds 		node_perm = NODE__TCP_RECV;
40461da177e4SLinus Torvalds 		recv_perm = TCP_SOCKET__RECV_MSG;
40471da177e4SLinus Torvalds 		break;
40482ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
40492ee92d46SJames Morris 		netif_perm = NETIF__DCCP_RECV;
40502ee92d46SJames Morris 		node_perm = NODE__DCCP_RECV;
40512ee92d46SJames Morris 		recv_perm = DCCP_SOCKET__RECV_MSG;
40522ee92d46SJames Morris 		break;
40531da177e4SLinus Torvalds 	default:
40541da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_RECV;
40551da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_RECV;
4056220deb96SPaul Moore 		recv_perm = 0;
40571da177e4SLinus Torvalds 		break;
40581da177e4SLinus Torvalds 	}
40591da177e4SLinus Torvalds 
4060220deb96SPaul Moore 	err = sel_netif_sid(skb->iif, &if_sid);
40611da177e4SLinus Torvalds 	if (err)
4062220deb96SPaul Moore 		return err;
4063220deb96SPaul Moore 	err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
4064220deb96SPaul Moore 	if (err)
4065220deb96SPaul Moore 		return err;
40661da177e4SLinus Torvalds 
4067224dfbd8SPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
40681da177e4SLinus Torvalds 	if (err)
4069220deb96SPaul Moore 		return err;
4070220deb96SPaul Moore 	err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
40711da177e4SLinus Torvalds 	if (err)
4072220deb96SPaul Moore 		return err;
40731da177e4SLinus Torvalds 
4074220deb96SPaul Moore 	if (!recv_perm)
4075220deb96SPaul Moore 		return 0;
40763e112172SPaul Moore 	err = sel_netport_sid(sk->sk_protocol,
40773e112172SPaul Moore 			      ntohs(ad->u.net.sport), &port_sid);
407871f1cb05SPaul Moore 	if (unlikely(err)) {
407971f1cb05SPaul Moore 		printk(KERN_WARNING
408071f1cb05SPaul Moore 		       "SELinux: failure in"
408171f1cb05SPaul Moore 		       " selinux_sock_rcv_skb_iptables_compat(),"
408271f1cb05SPaul Moore 		       " network port label not found\n");
4083220deb96SPaul Moore 		return err;
408471f1cb05SPaul Moore 	}
4085220deb96SPaul Moore 	return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad);
40861da177e4SLinus Torvalds }
4087d28d1e08STrent Jaeger 
4088220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4089d8395c87SPaul Moore 				       u16 family)
4090220deb96SPaul Moore {
4091277d342fSPaul Moore 	int err = 0;
4092220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4093220deb96SPaul Moore 	u32 peer_sid;
4094220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
4095d8395c87SPaul Moore 	struct avc_audit_data ad;
4096d8395c87SPaul Moore 	char *addrp;
4097d8395c87SPaul Moore 
4098d8395c87SPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4099d8395c87SPaul Moore 	ad.u.net.netif = skb->iif;
4100d8395c87SPaul Moore 	ad.u.net.family = family;
4101d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4102d8395c87SPaul Moore 	if (err)
4103d8395c87SPaul Moore 		return err;
4104220deb96SPaul Moore 
4105220deb96SPaul Moore 	if (selinux_compat_net)
4106d8395c87SPaul Moore 		err = selinux_sock_rcv_skb_iptables_compat(sk, skb, &ad,
4107220deb96SPaul Moore 							   family, addrp);
4108277d342fSPaul Moore 	else if (selinux_secmark_enabled())
4109220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4110d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4111220deb96SPaul Moore 	if (err)
4112220deb96SPaul Moore 		return err;
4113220deb96SPaul Moore 
4114220deb96SPaul Moore 	if (selinux_policycap_netpeer) {
4115220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4116220deb96SPaul Moore 		if (err)
4117220deb96SPaul Moore 			return err;
4118220deb96SPaul Moore 		err = avc_has_perm(sk_sid, peer_sid,
4119d8395c87SPaul Moore 				   SECCLASS_PEER, PEER__RECV, &ad);
4120dfaebe98SPaul Moore 		if (err)
4121dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4122220deb96SPaul Moore 	} else {
4123d8395c87SPaul Moore 		err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4124220deb96SPaul Moore 		if (err)
4125220deb96SPaul Moore 			return err;
4126d8395c87SPaul Moore 		err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4127220deb96SPaul Moore 	}
4128220deb96SPaul Moore 
41294e5ab4cbSJames Morris 	return err;
41304e5ab4cbSJames Morris }
4131d28d1e08STrent Jaeger 
41324e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
41334e5ab4cbSJames Morris {
4134220deb96SPaul Moore 	int err;
41354237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4136220deb96SPaul Moore 	u16 family = sk->sk_family;
4137220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
4138220deb96SPaul Moore 	struct avc_audit_data ad;
4139220deb96SPaul Moore 	char *addrp;
4140d8395c87SPaul Moore 	u8 secmark_active;
4141d8395c87SPaul Moore 	u8 peerlbl_active;
41424e5ab4cbSJames Morris 
41434e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4144220deb96SPaul Moore 		return 0;
41454e5ab4cbSJames Morris 
41464e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
414787fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
41484e5ab4cbSJames Morris 		family = PF_INET;
41494e5ab4cbSJames Morris 
4150d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4151d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4152d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4153d8395c87SPaul Moore 	 * as fast and as clean as possible. */
4154d8395c87SPaul Moore 	if (selinux_compat_net || !selinux_policycap_netpeer)
4155d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4156d8395c87SPaul Moore 
4157d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
4158d8395c87SPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4159d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4160d8395c87SPaul Moore 		return 0;
4161d8395c87SPaul Moore 
41624e5ab4cbSJames Morris 	AVC_AUDIT_DATA_INIT(&ad, NET);
4163da5645a2SPaul Moore 	ad.u.net.netif = skb->iif;
41644e5ab4cbSJames Morris 	ad.u.net.family = family;
4165224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
41664e5ab4cbSJames Morris 	if (err)
4167220deb96SPaul Moore 		return err;
41684e5ab4cbSJames Morris 
4169d8395c87SPaul Moore 	if (peerlbl_active) {
4170d621d35eSPaul Moore 		u32 peer_sid;
4171220deb96SPaul Moore 
4172220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4173220deb96SPaul Moore 		if (err)
4174220deb96SPaul Moore 			return err;
4175effad8dfSPaul Moore 		err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family,
4176effad8dfSPaul Moore 					       peer_sid, &ad);
4177dfaebe98SPaul Moore 		if (err) {
4178dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4179effad8dfSPaul Moore 			return err;
4180dfaebe98SPaul Moore 		}
4181d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4182d621d35eSPaul Moore 				   PEER__RECV, &ad);
4183dfaebe98SPaul Moore 		if (err)
4184dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4185d621d35eSPaul Moore 	}
4186d621d35eSPaul Moore 
4187d8395c87SPaul Moore 	if (secmark_active) {
4188effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4189effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4190effad8dfSPaul Moore 		if (err)
4191effad8dfSPaul Moore 			return err;
4192effad8dfSPaul Moore 	}
4193effad8dfSPaul Moore 
4194d621d35eSPaul Moore 	return err;
41951da177e4SLinus Torvalds }
41961da177e4SLinus Torvalds 
41972c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
41981da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
41991da177e4SLinus Torvalds {
42001da177e4SLinus Torvalds 	int err = 0;
42011da177e4SLinus Torvalds 	char *scontext;
42021da177e4SLinus Torvalds 	u32 scontext_len;
42031da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
42041da177e4SLinus Torvalds 	struct inode_security_struct *isec;
42053de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
42061da177e4SLinus Torvalds 
42071da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
42082c7946a7SCatherine Zhang 
42093de4bab5SPaul Moore 	if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
42103de4bab5SPaul Moore 	    isec->sclass == SECCLASS_TCP_SOCKET) {
42116b877699SVenkat Yekkirala 		ssec = sock->sk->sk_security;
42126b877699SVenkat Yekkirala 		peer_sid = ssec->peer_sid;
42136b877699SVenkat Yekkirala 	}
42142c7946a7SCatherine Zhang 	if (peer_sid == SECSID_NULL) {
42152c7946a7SCatherine Zhang 		err = -ENOPROTOOPT;
42162c7946a7SCatherine Zhang 		goto out;
42172c7946a7SCatherine Zhang 	}
42181da177e4SLinus Torvalds 
42192c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
42201da177e4SLinus Torvalds 
42211da177e4SLinus Torvalds 	if (err)
42221da177e4SLinus Torvalds 		goto out;
42231da177e4SLinus Torvalds 
42241da177e4SLinus Torvalds 	if (scontext_len > len) {
42251da177e4SLinus Torvalds 		err = -ERANGE;
42261da177e4SLinus Torvalds 		goto out_len;
42271da177e4SLinus Torvalds 	}
42281da177e4SLinus Torvalds 
42291da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
42301da177e4SLinus Torvalds 		err = -EFAULT;
42311da177e4SLinus Torvalds 
42321da177e4SLinus Torvalds out_len:
42331da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
42341da177e4SLinus Torvalds 		err = -EFAULT;
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds 	kfree(scontext);
42371da177e4SLinus Torvalds out:
42381da177e4SLinus Torvalds 	return err;
42391da177e4SLinus Torvalds }
42401da177e4SLinus Torvalds 
4241dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
42422c7946a7SCatherine Zhang {
4243dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
424475e22910SPaul Moore 	u16 family;
4245877ce7c1SCatherine Zhang 
4246aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4247aa862900SPaul Moore 		family = PF_INET;
4248aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4249aa862900SPaul Moore 		family = PF_INET6;
4250aa862900SPaul Moore 	else if (sock)
425175e22910SPaul Moore 		family = sock->sk->sk_family;
425275e22910SPaul Moore 	else
425375e22910SPaul Moore 		goto out;
425475e22910SPaul Moore 
425575e22910SPaul Moore 	if (sock && family == PF_UNIX)
4256713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
42573de4bab5SPaul Moore 	else if (skb)
4258220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
42592c7946a7SCatherine Zhang 
426075e22910SPaul Moore out:
4261dc49c1f9SCatherine Zhang 	*secid = peer_secid;
426275e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
426375e22910SPaul Moore 		return -EINVAL;
426475e22910SPaul Moore 	return 0;
42652c7946a7SCatherine Zhang }
42662c7946a7SCatherine Zhang 
42677d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
42681da177e4SLinus Torvalds {
42691da177e4SLinus Torvalds 	return sk_alloc_security(sk, family, priority);
42701da177e4SLinus Torvalds }
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
42731da177e4SLinus Torvalds {
42741da177e4SLinus Torvalds 	sk_free_security(sk);
42751da177e4SLinus Torvalds }
42761da177e4SLinus Torvalds 
4277892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4278892c141eSVenkat Yekkirala {
4279892c141eSVenkat Yekkirala 	struct sk_security_struct *ssec = sk->sk_security;
4280892c141eSVenkat Yekkirala 	struct sk_security_struct *newssec = newsk->sk_security;
4281892c141eSVenkat Yekkirala 
4282892c141eSVenkat Yekkirala 	newssec->sid = ssec->sid;
4283892c141eSVenkat Yekkirala 	newssec->peer_sid = ssec->peer_sid;
4284220deb96SPaul Moore 	newssec->sclass = ssec->sclass;
428599f59ed0SPaul Moore 
4286*389fb800SPaul Moore 	selinux_netlbl_sk_security_reset(newssec);
4287892c141eSVenkat Yekkirala }
4288892c141eSVenkat Yekkirala 
4289beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4290d28d1e08STrent Jaeger {
4291d28d1e08STrent Jaeger 	if (!sk)
4292beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4293892c141eSVenkat Yekkirala 	else {
4294892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4295d28d1e08STrent Jaeger 
4296beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4297892c141eSVenkat Yekkirala 	}
4298d28d1e08STrent Jaeger }
4299d28d1e08STrent Jaeger 
43009a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
43014237c75cSVenkat Yekkirala {
43024237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
43034237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43044237c75cSVenkat Yekkirala 
43052148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
43062148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
43074237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4308220deb96SPaul Moore 	sksec->sclass = isec->sclass;
43094237c75cSVenkat Yekkirala }
43104237c75cSVenkat Yekkirala 
43119a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
43124237c75cSVenkat Yekkirala 				     struct request_sock *req)
43134237c75cSVenkat Yekkirala {
43144237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43154237c75cSVenkat Yekkirala 	int err;
4316aa862900SPaul Moore 	u16 family = sk->sk_family;
43177420ed23SVenkat Yekkirala 	u32 newsid;
43184237c75cSVenkat Yekkirala 	u32 peersid;
43194237c75cSVenkat Yekkirala 
4320aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4321aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4322aa862900SPaul Moore 		family = PF_INET;
4323aa862900SPaul Moore 
4324aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4325220deb96SPaul Moore 	if (err)
4326220deb96SPaul Moore 		return err;
4327a51c64f1SVenkat Yekkirala 	if (peersid == SECSID_NULL) {
4328a51c64f1SVenkat Yekkirala 		req->secid = sksec->sid;
43293de4bab5SPaul Moore 		req->peer_secid = SECSID_NULL;
4330*389fb800SPaul Moore 	} else {
43314237c75cSVenkat Yekkirala 		err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
43324237c75cSVenkat Yekkirala 		if (err)
43334237c75cSVenkat Yekkirala 			return err;
43344237c75cSVenkat Yekkirala 		req->secid = newsid;
43356b877699SVenkat Yekkirala 		req->peer_secid = peersid;
4336*389fb800SPaul Moore 	}
4337*389fb800SPaul Moore 
4338*389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
43394237c75cSVenkat Yekkirala }
43404237c75cSVenkat Yekkirala 
43419a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
43429a673e56SAdrian Bunk 				   const struct request_sock *req)
43434237c75cSVenkat Yekkirala {
43444237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
43454237c75cSVenkat Yekkirala 
43464237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
43476b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
43484237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
43494237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
43504237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
43514237c75cSVenkat Yekkirala 	   time it will have been created and available. */
435299f59ed0SPaul Moore 
43539f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
43549f2ad665SPaul Moore 	 * thread with access to newsksec */
4355*389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
43564237c75cSVenkat Yekkirala }
43574237c75cSVenkat Yekkirala 
4358014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
43596b877699SVenkat Yekkirala {
4360aa862900SPaul Moore 	u16 family = sk->sk_family;
43616b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
43626b877699SVenkat 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 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
43686b877699SVenkat Yekkirala }
43696b877699SVenkat Yekkirala 
43709a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
43719a673e56SAdrian Bunk 				      struct flowi *fl)
43724237c75cSVenkat Yekkirala {
43734237c75cSVenkat Yekkirala 	fl->secid = req->secid;
43744237c75cSVenkat Yekkirala }
43754237c75cSVenkat Yekkirala 
43761da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
43771da177e4SLinus Torvalds {
43781da177e4SLinus Torvalds 	int err = 0;
43791da177e4SLinus Torvalds 	u32 perm;
43801da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
43811da177e4SLinus Torvalds 	struct socket *sock = sk->sk_socket;
43821da177e4SLinus Torvalds 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
43831da177e4SLinus Torvalds 
43841da177e4SLinus Torvalds 	if (skb->len < NLMSG_SPACE(0)) {
43851da177e4SLinus Torvalds 		err = -EINVAL;
43861da177e4SLinus Torvalds 		goto out;
43871da177e4SLinus Torvalds 	}
4388b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
43911da177e4SLinus Torvalds 	if (err) {
43921da177e4SLinus Torvalds 		if (err == -EINVAL) {
43939ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
43941da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
43951da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
43961da177e4SLinus Torvalds 				  nlh->nlmsg_type, isec->sclass);
439739c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
43981da177e4SLinus Torvalds 				err = 0;
43991da177e4SLinus Torvalds 		}
44001da177e4SLinus Torvalds 
44011da177e4SLinus Torvalds 		/* Ignore */
44021da177e4SLinus Torvalds 		if (err == -ENOENT)
44031da177e4SLinus Torvalds 			err = 0;
44041da177e4SLinus Torvalds 		goto out;
44051da177e4SLinus Torvalds 	}
44061da177e4SLinus Torvalds 
44071da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, perm);
44081da177e4SLinus Torvalds out:
44091da177e4SLinus Torvalds 	return err;
44101da177e4SLinus Torvalds }
44111da177e4SLinus Torvalds 
44121da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
44131da177e4SLinus Torvalds 
4414effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4415effad8dfSPaul Moore 				       u16 family)
44161da177e4SLinus Torvalds {
4417dfaebe98SPaul Moore 	int err;
4418effad8dfSPaul Moore 	char *addrp;
4419effad8dfSPaul Moore 	u32 peer_sid;
4420effad8dfSPaul Moore 	struct avc_audit_data ad;
4421effad8dfSPaul Moore 	u8 secmark_active;
4422948bf85cSPaul Moore 	u8 netlbl_active;
4423effad8dfSPaul Moore 	u8 peerlbl_active;
44244237c75cSVenkat Yekkirala 
4425effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4426effad8dfSPaul Moore 		return NF_ACCEPT;
44274237c75cSVenkat Yekkirala 
4428effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4429948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
4430948bf85cSPaul Moore 	peerlbl_active = netlbl_active || selinux_xfrm_enabled();
4431effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4432effad8dfSPaul Moore 		return NF_ACCEPT;
44334237c75cSVenkat Yekkirala 
4434d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4435d8395c87SPaul Moore 		return NF_DROP;
4436d8395c87SPaul Moore 
4437effad8dfSPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4438effad8dfSPaul Moore 	ad.u.net.netif = ifindex;
4439effad8dfSPaul Moore 	ad.u.net.family = family;
4440effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4441effad8dfSPaul Moore 		return NF_DROP;
44421da177e4SLinus Torvalds 
4443dfaebe98SPaul Moore 	if (peerlbl_active) {
4444dfaebe98SPaul Moore 		err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4445dfaebe98SPaul Moore 					       peer_sid, &ad);
4446dfaebe98SPaul Moore 		if (err) {
4447dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4448effad8dfSPaul Moore 			return NF_DROP;
4449dfaebe98SPaul Moore 		}
4450dfaebe98SPaul Moore 	}
4451effad8dfSPaul Moore 
4452effad8dfSPaul Moore 	if (secmark_active)
4453effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4454effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4455effad8dfSPaul Moore 			return NF_DROP;
4456effad8dfSPaul Moore 
4457948bf85cSPaul Moore 	if (netlbl_active)
4458948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4459948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4460948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4461948bf85cSPaul Moore 		 * protection */
4462948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4463948bf85cSPaul Moore 			return NF_DROP;
4464948bf85cSPaul Moore 
4465effad8dfSPaul Moore 	return NF_ACCEPT;
4466effad8dfSPaul Moore }
4467effad8dfSPaul Moore 
4468effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4469effad8dfSPaul Moore 					 struct sk_buff *skb,
4470effad8dfSPaul Moore 					 const struct net_device *in,
4471effad8dfSPaul Moore 					 const struct net_device *out,
4472effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4473effad8dfSPaul Moore {
4474effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET);
4475effad8dfSPaul Moore }
4476effad8dfSPaul Moore 
4477effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4478effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4479effad8dfSPaul Moore 					 struct sk_buff *skb,
4480effad8dfSPaul Moore 					 const struct net_device *in,
4481effad8dfSPaul Moore 					 const struct net_device *out,
4482effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4483effad8dfSPaul Moore {
4484effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4485effad8dfSPaul Moore }
4486effad8dfSPaul Moore #endif	/* IPV6 */
4487effad8dfSPaul Moore 
4488948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4489948bf85cSPaul Moore 				      u16 family)
4490948bf85cSPaul Moore {
4491948bf85cSPaul Moore 	u32 sid;
4492948bf85cSPaul Moore 
4493948bf85cSPaul Moore 	if (!netlbl_enabled())
4494948bf85cSPaul Moore 		return NF_ACCEPT;
4495948bf85cSPaul Moore 
4496948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4497948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4498948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
4499948bf85cSPaul Moore 	if (skb->sk) {
4500948bf85cSPaul Moore 		struct sk_security_struct *sksec = skb->sk->sk_security;
4501948bf85cSPaul Moore 		sid = sksec->sid;
4502948bf85cSPaul Moore 	} else
4503948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4504948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4505948bf85cSPaul Moore 		return NF_DROP;
4506948bf85cSPaul Moore 
4507948bf85cSPaul Moore 	return NF_ACCEPT;
4508948bf85cSPaul Moore }
4509948bf85cSPaul Moore 
4510948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum,
4511948bf85cSPaul Moore 					struct sk_buff *skb,
4512948bf85cSPaul Moore 					const struct net_device *in,
4513948bf85cSPaul Moore 					const struct net_device *out,
4514948bf85cSPaul Moore 					int (*okfn)(struct sk_buff *))
4515948bf85cSPaul Moore {
4516948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4517948bf85cSPaul Moore }
4518948bf85cSPaul Moore 
4519effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk,
4520effad8dfSPaul Moore 						int ifindex,
4521effad8dfSPaul Moore 						struct avc_audit_data *ad,
4522effad8dfSPaul Moore 						u16 family, char *addrp)
4523effad8dfSPaul Moore {
4524effad8dfSPaul Moore 	int err;
4525effad8dfSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4526effad8dfSPaul Moore 	u16 sk_class;
4527effad8dfSPaul Moore 	u32 netif_perm, node_perm, send_perm;
4528effad8dfSPaul Moore 	u32 port_sid, node_sid, if_sid, sk_sid;
4529effad8dfSPaul Moore 
4530effad8dfSPaul Moore 	sk_sid = sksec->sid;
4531effad8dfSPaul Moore 	sk_class = sksec->sclass;
4532effad8dfSPaul Moore 
4533effad8dfSPaul Moore 	switch (sk_class) {
45341da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
45351da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_SEND;
45361da177e4SLinus Torvalds 		node_perm = NODE__UDP_SEND;
45371da177e4SLinus Torvalds 		send_perm = UDP_SOCKET__SEND_MSG;
45381da177e4SLinus Torvalds 		break;
45391da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
45401da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_SEND;
45411da177e4SLinus Torvalds 		node_perm = NODE__TCP_SEND;
45421da177e4SLinus Torvalds 		send_perm = TCP_SOCKET__SEND_MSG;
45431da177e4SLinus Torvalds 		break;
45442ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
45452ee92d46SJames Morris 		netif_perm = NETIF__DCCP_SEND;
45462ee92d46SJames Morris 		node_perm = NODE__DCCP_SEND;
45472ee92d46SJames Morris 		send_perm = DCCP_SOCKET__SEND_MSG;
45482ee92d46SJames Morris 		break;
45491da177e4SLinus Torvalds 	default:
45501da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_SEND;
45511da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_SEND;
4552effad8dfSPaul Moore 		send_perm = 0;
45531da177e4SLinus Torvalds 		break;
45541da177e4SLinus Torvalds 	}
45551da177e4SLinus Torvalds 
4556effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
45574e5ab4cbSJames Morris 	if (err)
4558effad8dfSPaul Moore 		return err;
4559effad8dfSPaul Moore 	err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
4560effad8dfSPaul Moore 		return err;
45611da177e4SLinus Torvalds 
4562224dfbd8SPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
45634e5ab4cbSJames Morris 	if (err)
4564effad8dfSPaul Moore 		return err;
4565effad8dfSPaul Moore 	err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
45664e5ab4cbSJames Morris 	if (err)
4567effad8dfSPaul Moore 		return err;
45681da177e4SLinus Torvalds 
4569effad8dfSPaul Moore 	if (send_perm != 0)
4570effad8dfSPaul Moore 		return 0;
45711da177e4SLinus Torvalds 
45723e112172SPaul Moore 	err = sel_netport_sid(sk->sk_protocol,
45733e112172SPaul Moore 			      ntohs(ad->u.net.dport), &port_sid);
457471f1cb05SPaul Moore 	if (unlikely(err)) {
457571f1cb05SPaul Moore 		printk(KERN_WARNING
457671f1cb05SPaul Moore 		       "SELinux: failure in"
457771f1cb05SPaul Moore 		       " selinux_ip_postroute_iptables_compat(),"
457871f1cb05SPaul Moore 		       " network port label not found\n");
45794e5ab4cbSJames Morris 		return err;
458071f1cb05SPaul Moore 	}
4581effad8dfSPaul Moore 	return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad);
45821da177e4SLinus Torvalds }
45831da177e4SLinus Torvalds 
4584effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4585effad8dfSPaul Moore 						int ifindex,
4586d8395c87SPaul Moore 						u16 family)
45874e5ab4cbSJames Morris {
4588effad8dfSPaul Moore 	struct sock *sk = skb->sk;
45894237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
4590d8395c87SPaul Moore 	struct avc_audit_data ad;
4591d8395c87SPaul Moore 	char *addrp;
4592d8395c87SPaul Moore 	u8 proto;
45934e5ab4cbSJames Morris 
4594effad8dfSPaul Moore 	if (sk == NULL)
4595effad8dfSPaul Moore 		return NF_ACCEPT;
45964237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
45974e5ab4cbSJames Morris 
4598d8395c87SPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4599d8395c87SPaul Moore 	ad.u.net.netif = ifindex;
4600d8395c87SPaul Moore 	ad.u.net.family = family;
4601d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4602d8395c87SPaul Moore 		return NF_DROP;
4603d8395c87SPaul Moore 
4604effad8dfSPaul Moore 	if (selinux_compat_net) {
4605effad8dfSPaul Moore 		if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex,
4606d8395c87SPaul Moore 							 &ad, family, addrp))
4607effad8dfSPaul Moore 			return NF_DROP;
4608277d342fSPaul Moore 	} else if (selinux_secmark_enabled()) {
4609effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4610d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
4611effad8dfSPaul Moore 			return NF_DROP;
46121da177e4SLinus Torvalds 	}
46131da177e4SLinus Torvalds 
4614effad8dfSPaul Moore 	if (selinux_policycap_netpeer)
4615d8395c87SPaul Moore 		if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4616effad8dfSPaul Moore 			return NF_DROP;
4617effad8dfSPaul Moore 
4618effad8dfSPaul Moore 	return NF_ACCEPT;
4619effad8dfSPaul Moore }
4620effad8dfSPaul Moore 
4621effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4622effad8dfSPaul Moore 					 u16 family)
4623effad8dfSPaul Moore {
4624effad8dfSPaul Moore 	u32 secmark_perm;
4625effad8dfSPaul Moore 	u32 peer_sid;
4626effad8dfSPaul Moore 	struct sock *sk;
4627effad8dfSPaul Moore 	struct avc_audit_data ad;
4628effad8dfSPaul Moore 	char *addrp;
4629effad8dfSPaul Moore 	u8 secmark_active;
4630effad8dfSPaul Moore 	u8 peerlbl_active;
4631effad8dfSPaul Moore 
4632effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4633effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4634effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4635effad8dfSPaul Moore 	 * as fast and as clean as possible. */
4636effad8dfSPaul Moore 	if (selinux_compat_net || !selinux_policycap_netpeer)
4637d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4638def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
4639effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4640effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4641effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4642effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4643effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4644effad8dfSPaul Moore 	 *       is NULL, in this case go ahead and apply access control. */
4645effad8dfSPaul Moore 	if (skb->dst != NULL && skb->dst->xfrm != NULL)
4646effad8dfSPaul Moore 		return NF_ACCEPT;
4647def8b4faSAlexey Dobriyan #endif
4648effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4649effad8dfSPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4650effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4651effad8dfSPaul Moore 		return NF_ACCEPT;
4652effad8dfSPaul Moore 
4653d8395c87SPaul Moore 	/* if the packet is being forwarded then get the peer label from the
4654d8395c87SPaul Moore 	 * packet itself; otherwise check to see if it is from a local
4655d8395c87SPaul Moore 	 * application or the kernel, if from an application get the peer label
4656d8395c87SPaul Moore 	 * from the sending socket, otherwise use the kernel's sid */
4657effad8dfSPaul Moore 	sk = skb->sk;
4658d8395c87SPaul Moore 	if (sk == NULL) {
4659d8395c87SPaul Moore 		switch (family) {
4660d8395c87SPaul Moore 		case PF_INET:
4661d8395c87SPaul Moore 			if (IPCB(skb)->flags & IPSKB_FORWARDED)
4662d8395c87SPaul Moore 				secmark_perm = PACKET__FORWARD_OUT;
4663d8395c87SPaul Moore 			else
4664d8395c87SPaul Moore 				secmark_perm = PACKET__SEND;
4665d8395c87SPaul Moore 			break;
4666d8395c87SPaul Moore 		case PF_INET6:
4667d8395c87SPaul Moore 			if (IP6CB(skb)->flags & IP6SKB_FORWARDED)
4668d8395c87SPaul Moore 				secmark_perm = PACKET__FORWARD_OUT;
4669d8395c87SPaul Moore 			else
4670d8395c87SPaul Moore 				secmark_perm = PACKET__SEND;
4671d8395c87SPaul Moore 			break;
4672d8395c87SPaul Moore 		default:
4673d8395c87SPaul Moore 			return NF_DROP;
4674d8395c87SPaul Moore 		}
4675d8395c87SPaul Moore 		if (secmark_perm == PACKET__FORWARD_OUT) {
4676d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4677d8395c87SPaul Moore 				return NF_DROP;
4678d8395c87SPaul Moore 		} else
4679d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
4680d8395c87SPaul Moore 	} else {
4681effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4682effad8dfSPaul Moore 		peer_sid = sksec->sid;
4683effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4684effad8dfSPaul Moore 	}
4685effad8dfSPaul Moore 
4686d8395c87SPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4687d8395c87SPaul Moore 	ad.u.net.netif = ifindex;
4688d8395c87SPaul Moore 	ad.u.net.family = family;
4689d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
4690d8395c87SPaul Moore 		return NF_DROP;
4691d8395c87SPaul Moore 
4692effad8dfSPaul Moore 	if (secmark_active)
4693effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4694effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
4695effad8dfSPaul Moore 			return NF_DROP;
4696effad8dfSPaul Moore 
4697effad8dfSPaul Moore 	if (peerlbl_active) {
4698effad8dfSPaul Moore 		u32 if_sid;
4699effad8dfSPaul Moore 		u32 node_sid;
4700effad8dfSPaul Moore 
4701effad8dfSPaul Moore 		if (sel_netif_sid(ifindex, &if_sid))
4702effad8dfSPaul Moore 			return NF_DROP;
4703effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
4704effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
4705effad8dfSPaul Moore 			return NF_DROP;
4706effad8dfSPaul Moore 
4707effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
4708effad8dfSPaul Moore 			return NF_DROP;
4709effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
4710effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
4711effad8dfSPaul Moore 			return NF_DROP;
4712effad8dfSPaul Moore 	}
4713effad8dfSPaul Moore 
4714effad8dfSPaul Moore 	return NF_ACCEPT;
4715effad8dfSPaul Moore }
4716effad8dfSPaul Moore 
4717effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4718a224be76SDavid S. Miller 					   struct sk_buff *skb,
47191da177e4SLinus Torvalds 					   const struct net_device *in,
47201da177e4SLinus Torvalds 					   const struct net_device *out,
47211da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
47221da177e4SLinus Torvalds {
4723effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET);
47241da177e4SLinus Torvalds }
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4727effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4728a224be76SDavid S. Miller 					   struct sk_buff *skb,
47291da177e4SLinus Torvalds 					   const struct net_device *in,
47301da177e4SLinus Torvalds 					   const struct net_device *out,
47311da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
47321da177e4SLinus Torvalds {
4733effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
47341da177e4SLinus Torvalds }
47351da177e4SLinus Torvalds #endif	/* IPV6 */
47361da177e4SLinus Torvalds 
47371da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
47401da177e4SLinus Torvalds {
47411da177e4SLinus Torvalds 	int err;
47421da177e4SLinus Torvalds 
4743200ac532SEric Paris 	err = cap_netlink_send(sk, skb);
47441da177e4SLinus Torvalds 	if (err)
47451da177e4SLinus Torvalds 		return err;
47461da177e4SLinus Torvalds 
47471da177e4SLinus Torvalds 	if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
47481da177e4SLinus Torvalds 		err = selinux_nlmsg_perm(sk, skb);
47491da177e4SLinus Torvalds 
47501da177e4SLinus Torvalds 	return err;
47511da177e4SLinus Torvalds }
47521da177e4SLinus Torvalds 
4753c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability)
47541da177e4SLinus Torvalds {
4755c7bdb545SDarrel Goeddel 	int err;
4756c7bdb545SDarrel Goeddel 	struct avc_audit_data ad;
4757c7bdb545SDarrel Goeddel 
4758200ac532SEric Paris 	err = cap_netlink_recv(skb, capability);
4759c7bdb545SDarrel Goeddel 	if (err)
4760c7bdb545SDarrel Goeddel 		return err;
4761c7bdb545SDarrel Goeddel 
4762c7bdb545SDarrel Goeddel 	AVC_AUDIT_DATA_INIT(&ad, CAP);
4763c7bdb545SDarrel Goeddel 	ad.u.cap = capability;
4764c7bdb545SDarrel Goeddel 
4765c7bdb545SDarrel Goeddel 	return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid,
4766c7bdb545SDarrel Goeddel 			    SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad);
47671da177e4SLinus Torvalds }
47681da177e4SLinus Torvalds 
47691da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
47701da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
47711da177e4SLinus Torvalds 			      u16 sclass)
47721da177e4SLinus Torvalds {
47731da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
4774275bb41eSDavid Howells 	u32 sid;
47751da177e4SLinus Torvalds 
477689d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
47771da177e4SLinus Torvalds 	if (!isec)
47781da177e4SLinus Torvalds 		return -ENOMEM;
47791da177e4SLinus Torvalds 
4780275bb41eSDavid Howells 	sid = task_sid(task);
47811da177e4SLinus Torvalds 	isec->sclass = sclass;
4782275bb41eSDavid Howells 	isec->sid = sid;
47831da177e4SLinus Torvalds 	perm->security = isec;
47841da177e4SLinus Torvalds 
47851da177e4SLinus Torvalds 	return 0;
47861da177e4SLinus Torvalds }
47871da177e4SLinus Torvalds 
47881da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
47891da177e4SLinus Torvalds {
47901da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
47911da177e4SLinus Torvalds 	perm->security = NULL;
47921da177e4SLinus Torvalds 	kfree(isec);
47931da177e4SLinus Torvalds }
47941da177e4SLinus Torvalds 
47951da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
47961da177e4SLinus Torvalds {
47971da177e4SLinus Torvalds 	struct msg_security_struct *msec;
47981da177e4SLinus Torvalds 
479989d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
48001da177e4SLinus Torvalds 	if (!msec)
48011da177e4SLinus Torvalds 		return -ENOMEM;
48021da177e4SLinus Torvalds 
48031da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
48041da177e4SLinus Torvalds 	msg->security = msec;
48051da177e4SLinus Torvalds 
48061da177e4SLinus Torvalds 	return 0;
48071da177e4SLinus Torvalds }
48081da177e4SLinus Torvalds 
48091da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
48101da177e4SLinus Torvalds {
48111da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
48121da177e4SLinus Torvalds 
48131da177e4SLinus Torvalds 	msg->security = NULL;
48141da177e4SLinus Torvalds 	kfree(msec);
48151da177e4SLinus Torvalds }
48161da177e4SLinus Torvalds 
48171da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
48186af963f1SStephen Smalley 			u32 perms)
48191da177e4SLinus Torvalds {
48201da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48211da177e4SLinus Torvalds 	struct avc_audit_data ad;
4822275bb41eSDavid Howells 	u32 sid = current_sid();
48231da177e4SLinus Torvalds 
48241da177e4SLinus Torvalds 	isec = ipc_perms->security;
48251da177e4SLinus Torvalds 
48261da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48271da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
48281da177e4SLinus Torvalds 
4829275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
48301da177e4SLinus Torvalds }
48311da177e4SLinus Torvalds 
48321da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
48331da177e4SLinus Torvalds {
48341da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
48351da177e4SLinus Torvalds }
48361da177e4SLinus Torvalds 
48371da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
48381da177e4SLinus Torvalds {
48391da177e4SLinus Torvalds 	msg_msg_free_security(msg);
48401da177e4SLinus Torvalds }
48411da177e4SLinus Torvalds 
48421da177e4SLinus Torvalds /* message queue security operations */
48431da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
48441da177e4SLinus Torvalds {
48451da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48461da177e4SLinus Torvalds 	struct avc_audit_data ad;
4847275bb41eSDavid Howells 	u32 sid = current_sid();
48481da177e4SLinus Torvalds 	int rc;
48491da177e4SLinus Torvalds 
48501da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
48511da177e4SLinus Torvalds 	if (rc)
48521da177e4SLinus Torvalds 		return rc;
48531da177e4SLinus Torvalds 
48541da177e4SLinus Torvalds 	isec = msq->q_perm.security;
48551da177e4SLinus Torvalds 
48561da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48571da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
48581da177e4SLinus Torvalds 
4859275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
48601da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
48611da177e4SLinus Torvalds 	if (rc) {
48621da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
48631da177e4SLinus Torvalds 		return rc;
48641da177e4SLinus Torvalds 	}
48651da177e4SLinus Torvalds 	return 0;
48661da177e4SLinus Torvalds }
48671da177e4SLinus Torvalds 
48681da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
48691da177e4SLinus Torvalds {
48701da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
48711da177e4SLinus Torvalds }
48721da177e4SLinus Torvalds 
48731da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
48741da177e4SLinus Torvalds {
48751da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48761da177e4SLinus Torvalds 	struct avc_audit_data ad;
4877275bb41eSDavid Howells 	u32 sid = current_sid();
48781da177e4SLinus Torvalds 
48791da177e4SLinus Torvalds 	isec = msq->q_perm.security;
48801da177e4SLinus Torvalds 
48811da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48821da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
48831da177e4SLinus Torvalds 
4884275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
48851da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
48861da177e4SLinus Torvalds }
48871da177e4SLinus Torvalds 
48881da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
48891da177e4SLinus Torvalds {
48901da177e4SLinus Torvalds 	int err;
48911da177e4SLinus Torvalds 	int perms;
48921da177e4SLinus Torvalds 
48931da177e4SLinus Torvalds 	switch (cmd) {
48941da177e4SLinus Torvalds 	case IPC_INFO:
48951da177e4SLinus Torvalds 	case MSG_INFO:
48961da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
48971da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
48981da177e4SLinus Torvalds 	case IPC_STAT:
48991da177e4SLinus Torvalds 	case MSG_STAT:
49001da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
49011da177e4SLinus Torvalds 		break;
49021da177e4SLinus Torvalds 	case IPC_SET:
49031da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
49041da177e4SLinus Torvalds 		break;
49051da177e4SLinus Torvalds 	case IPC_RMID:
49061da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
49071da177e4SLinus Torvalds 		break;
49081da177e4SLinus Torvalds 	default:
49091da177e4SLinus Torvalds 		return 0;
49101da177e4SLinus Torvalds 	}
49111da177e4SLinus Torvalds 
49126af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
49131da177e4SLinus Torvalds 	return err;
49141da177e4SLinus Torvalds }
49151da177e4SLinus Torvalds 
49161da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
49171da177e4SLinus Torvalds {
49181da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49191da177e4SLinus Torvalds 	struct msg_security_struct *msec;
49201da177e4SLinus Torvalds 	struct avc_audit_data ad;
4921275bb41eSDavid Howells 	u32 sid = current_sid();
49221da177e4SLinus Torvalds 	int rc;
49231da177e4SLinus Torvalds 
49241da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49251da177e4SLinus Torvalds 	msec = msg->security;
49261da177e4SLinus Torvalds 
49271da177e4SLinus Torvalds 	/*
49281da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
49291da177e4SLinus Torvalds 	 */
49301da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
49311da177e4SLinus Torvalds 		/*
49321da177e4SLinus Torvalds 		 * Compute new sid based on current process and
49331da177e4SLinus Torvalds 		 * message queue this message will be stored in
49341da177e4SLinus Torvalds 		 */
4935275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
49361da177e4SLinus Torvalds 					     &msec->sid);
49371da177e4SLinus Torvalds 		if (rc)
49381da177e4SLinus Torvalds 			return rc;
49391da177e4SLinus Torvalds 	}
49401da177e4SLinus Torvalds 
49411da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
49421da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds 	/* Can this process write to the queue? */
4945275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
49461da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
49471da177e4SLinus Torvalds 	if (!rc)
49481da177e4SLinus Torvalds 		/* Can this process send the message */
4949275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
4950275bb41eSDavid Howells 				  MSG__SEND, &ad);
49511da177e4SLinus Torvalds 	if (!rc)
49521da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
4953275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
4954275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
49551da177e4SLinus Torvalds 
49561da177e4SLinus Torvalds 	return rc;
49571da177e4SLinus Torvalds }
49581da177e4SLinus Torvalds 
49591da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
49601da177e4SLinus Torvalds 				    struct task_struct *target,
49611da177e4SLinus Torvalds 				    long type, int mode)
49621da177e4SLinus Torvalds {
49631da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49641da177e4SLinus Torvalds 	struct msg_security_struct *msec;
49651da177e4SLinus Torvalds 	struct avc_audit_data ad;
4966275bb41eSDavid Howells 	u32 sid = task_sid(target);
49671da177e4SLinus Torvalds 	int rc;
49681da177e4SLinus Torvalds 
49691da177e4SLinus Torvalds 	isec = msq->q_perm.security;
49701da177e4SLinus Torvalds 	msec = msg->security;
49711da177e4SLinus Torvalds 
49721da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
49731da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
49741da177e4SLinus Torvalds 
4975275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
49761da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
49771da177e4SLinus Torvalds 	if (!rc)
4978275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
49791da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
49801da177e4SLinus Torvalds 	return rc;
49811da177e4SLinus Torvalds }
49821da177e4SLinus Torvalds 
49831da177e4SLinus Torvalds /* Shared Memory security operations */
49841da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
49851da177e4SLinus Torvalds {
49861da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49871da177e4SLinus Torvalds 	struct avc_audit_data ad;
4988275bb41eSDavid Howells 	u32 sid = current_sid();
49891da177e4SLinus Torvalds 	int rc;
49901da177e4SLinus Torvalds 
49911da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
49921da177e4SLinus Torvalds 	if (rc)
49931da177e4SLinus Torvalds 		return rc;
49941da177e4SLinus Torvalds 
49951da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
49961da177e4SLinus Torvalds 
49971da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
49981da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
49991da177e4SLinus Torvalds 
5000275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
50011da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
50021da177e4SLinus Torvalds 	if (rc) {
50031da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
50041da177e4SLinus Torvalds 		return rc;
50051da177e4SLinus Torvalds 	}
50061da177e4SLinus Torvalds 	return 0;
50071da177e4SLinus Torvalds }
50081da177e4SLinus Torvalds 
50091da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
50101da177e4SLinus Torvalds {
50111da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
50121da177e4SLinus Torvalds }
50131da177e4SLinus Torvalds 
50141da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
50151da177e4SLinus Torvalds {
50161da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50171da177e4SLinus Torvalds 	struct avc_audit_data ad;
5018275bb41eSDavid Howells 	u32 sid = current_sid();
50191da177e4SLinus Torvalds 
50201da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
50211da177e4SLinus Torvalds 
50221da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
50231da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
50241da177e4SLinus Torvalds 
5025275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
50261da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
50271da177e4SLinus Torvalds }
50281da177e4SLinus Torvalds 
50291da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
50301da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
50311da177e4SLinus Torvalds {
50321da177e4SLinus Torvalds 	int perms;
50331da177e4SLinus Torvalds 	int err;
50341da177e4SLinus Torvalds 
50351da177e4SLinus Torvalds 	switch (cmd) {
50361da177e4SLinus Torvalds 	case IPC_INFO:
50371da177e4SLinus Torvalds 	case SHM_INFO:
50381da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
50391da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
50401da177e4SLinus Torvalds 	case IPC_STAT:
50411da177e4SLinus Torvalds 	case SHM_STAT:
50421da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
50431da177e4SLinus Torvalds 		break;
50441da177e4SLinus Torvalds 	case IPC_SET:
50451da177e4SLinus Torvalds 		perms = SHM__SETATTR;
50461da177e4SLinus Torvalds 		break;
50471da177e4SLinus Torvalds 	case SHM_LOCK:
50481da177e4SLinus Torvalds 	case SHM_UNLOCK:
50491da177e4SLinus Torvalds 		perms = SHM__LOCK;
50501da177e4SLinus Torvalds 		break;
50511da177e4SLinus Torvalds 	case IPC_RMID:
50521da177e4SLinus Torvalds 		perms = SHM__DESTROY;
50531da177e4SLinus Torvalds 		break;
50541da177e4SLinus Torvalds 	default:
50551da177e4SLinus Torvalds 		return 0;
50561da177e4SLinus Torvalds 	}
50571da177e4SLinus Torvalds 
50586af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
50591da177e4SLinus Torvalds 	return err;
50601da177e4SLinus Torvalds }
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
50631da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
50641da177e4SLinus Torvalds {
50651da177e4SLinus Torvalds 	u32 perms;
50661da177e4SLinus Torvalds 
50671da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
50681da177e4SLinus Torvalds 		perms = SHM__READ;
50691da177e4SLinus Torvalds 	else
50701da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
50711da177e4SLinus Torvalds 
50726af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
50731da177e4SLinus Torvalds }
50741da177e4SLinus Torvalds 
50751da177e4SLinus Torvalds /* Semaphore security operations */
50761da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
50771da177e4SLinus Torvalds {
50781da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
50791da177e4SLinus Torvalds 	struct avc_audit_data ad;
5080275bb41eSDavid Howells 	u32 sid = current_sid();
50811da177e4SLinus Torvalds 	int rc;
50821da177e4SLinus Torvalds 
50831da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
50841da177e4SLinus Torvalds 	if (rc)
50851da177e4SLinus Torvalds 		return rc;
50861da177e4SLinus Torvalds 
50871da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
50881da177e4SLinus Torvalds 
50891da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
50901da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
50911da177e4SLinus Torvalds 
5092275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
50931da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
50941da177e4SLinus Torvalds 	if (rc) {
50951da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
50961da177e4SLinus Torvalds 		return rc;
50971da177e4SLinus Torvalds 	}
50981da177e4SLinus Torvalds 	return 0;
50991da177e4SLinus Torvalds }
51001da177e4SLinus Torvalds 
51011da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
51021da177e4SLinus Torvalds {
51031da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
51041da177e4SLinus Torvalds }
51051da177e4SLinus Torvalds 
51061da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
51071da177e4SLinus Torvalds {
51081da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51091da177e4SLinus Torvalds 	struct avc_audit_data ad;
5110275bb41eSDavid Howells 	u32 sid = current_sid();
51111da177e4SLinus Torvalds 
51121da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
51131da177e4SLinus Torvalds 
51141da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
51151da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
51161da177e4SLinus Torvalds 
5117275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
51181da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
51191da177e4SLinus Torvalds }
51201da177e4SLinus Torvalds 
51211da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
51221da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
51231da177e4SLinus Torvalds {
51241da177e4SLinus Torvalds 	int err;
51251da177e4SLinus Torvalds 	u32 perms;
51261da177e4SLinus Torvalds 
51271da177e4SLinus Torvalds 	switch (cmd) {
51281da177e4SLinus Torvalds 	case IPC_INFO:
51291da177e4SLinus Torvalds 	case SEM_INFO:
51301da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
51311da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
51321da177e4SLinus Torvalds 	case GETPID:
51331da177e4SLinus Torvalds 	case GETNCNT:
51341da177e4SLinus Torvalds 	case GETZCNT:
51351da177e4SLinus Torvalds 		perms = SEM__GETATTR;
51361da177e4SLinus Torvalds 		break;
51371da177e4SLinus Torvalds 	case GETVAL:
51381da177e4SLinus Torvalds 	case GETALL:
51391da177e4SLinus Torvalds 		perms = SEM__READ;
51401da177e4SLinus Torvalds 		break;
51411da177e4SLinus Torvalds 	case SETVAL:
51421da177e4SLinus Torvalds 	case SETALL:
51431da177e4SLinus Torvalds 		perms = SEM__WRITE;
51441da177e4SLinus Torvalds 		break;
51451da177e4SLinus Torvalds 	case IPC_RMID:
51461da177e4SLinus Torvalds 		perms = SEM__DESTROY;
51471da177e4SLinus Torvalds 		break;
51481da177e4SLinus Torvalds 	case IPC_SET:
51491da177e4SLinus Torvalds 		perms = SEM__SETATTR;
51501da177e4SLinus Torvalds 		break;
51511da177e4SLinus Torvalds 	case IPC_STAT:
51521da177e4SLinus Torvalds 	case SEM_STAT:
51531da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
51541da177e4SLinus Torvalds 		break;
51551da177e4SLinus Torvalds 	default:
51561da177e4SLinus Torvalds 		return 0;
51571da177e4SLinus Torvalds 	}
51581da177e4SLinus Torvalds 
51596af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
51601da177e4SLinus Torvalds 	return err;
51611da177e4SLinus Torvalds }
51621da177e4SLinus Torvalds 
51631da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
51641da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
51651da177e4SLinus Torvalds {
51661da177e4SLinus Torvalds 	u32 perms;
51671da177e4SLinus Torvalds 
51681da177e4SLinus Torvalds 	if (alter)
51691da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
51701da177e4SLinus Torvalds 	else
51711da177e4SLinus Torvalds 		perms = SEM__READ;
51721da177e4SLinus Torvalds 
51736af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
51741da177e4SLinus Torvalds }
51751da177e4SLinus Torvalds 
51761da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
51771da177e4SLinus Torvalds {
51781da177e4SLinus Torvalds 	u32 av = 0;
51791da177e4SLinus Torvalds 
51801da177e4SLinus Torvalds 	av = 0;
51811da177e4SLinus Torvalds 	if (flag & S_IRUGO)
51821da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
51831da177e4SLinus Torvalds 	if (flag & S_IWUGO)
51841da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
51851da177e4SLinus Torvalds 
51861da177e4SLinus Torvalds 	if (av == 0)
51871da177e4SLinus Torvalds 		return 0;
51881da177e4SLinus Torvalds 
51896af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
51901da177e4SLinus Torvalds }
51911da177e4SLinus Torvalds 
5192713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5193713a04aeSAhmed S. Darwish {
5194713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5195713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5196713a04aeSAhmed S. Darwish }
5197713a04aeSAhmed S. Darwish 
51981da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
51991da177e4SLinus Torvalds {
52001da177e4SLinus Torvalds 	if (inode)
52011da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
52021da177e4SLinus Torvalds }
52031da177e4SLinus Torvalds 
52041da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
520504ff9708SAl Viro 			       char *name, char **value)
52061da177e4SLinus Torvalds {
5207275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
52088c8570fbSDustin Kirkland 	u32 sid;
52091da177e4SLinus Torvalds 	int error;
521004ff9708SAl Viro 	unsigned len;
52111da177e4SLinus Torvalds 
52121da177e4SLinus Torvalds 	if (current != p) {
52133b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
52141da177e4SLinus Torvalds 		if (error)
52151da177e4SLinus Torvalds 			return error;
52161da177e4SLinus Torvalds 	}
52171da177e4SLinus Torvalds 
5218275bb41eSDavid Howells 	rcu_read_lock();
5219275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
52201da177e4SLinus Torvalds 
52211da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5222275bb41eSDavid Howells 		sid = __tsec->sid;
52231da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5224275bb41eSDavid Howells 		sid = __tsec->osid;
52251da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5226275bb41eSDavid Howells 		sid = __tsec->exec_sid;
52271da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5228275bb41eSDavid Howells 		sid = __tsec->create_sid;
52294eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5230275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
523142c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5232275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
52331da177e4SLinus Torvalds 	else
5234275bb41eSDavid Howells 		goto invalid;
5235275bb41eSDavid Howells 	rcu_read_unlock();
52361da177e4SLinus Torvalds 
52371da177e4SLinus Torvalds 	if (!sid)
52381da177e4SLinus Torvalds 		return 0;
52391da177e4SLinus Torvalds 
524004ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
524104ff9708SAl Viro 	if (error)
524204ff9708SAl Viro 		return error;
524304ff9708SAl Viro 	return len;
5244275bb41eSDavid Howells 
5245275bb41eSDavid Howells invalid:
5246275bb41eSDavid Howells 	rcu_read_unlock();
5247275bb41eSDavid Howells 	return -EINVAL;
52481da177e4SLinus Torvalds }
52491da177e4SLinus Torvalds 
52501da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
52511da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
52521da177e4SLinus Torvalds {
52531da177e4SLinus Torvalds 	struct task_security_struct *tsec;
52540356357cSRoland McGrath 	struct task_struct *tracer;
5255d84f4f99SDavid Howells 	struct cred *new;
5256d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
52571da177e4SLinus Torvalds 	int error;
52581da177e4SLinus Torvalds 	char *str = value;
52591da177e4SLinus Torvalds 
52601da177e4SLinus Torvalds 	if (current != p) {
52611da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
52621da177e4SLinus Torvalds 		   security attributes. */
52631da177e4SLinus Torvalds 		return -EACCES;
52641da177e4SLinus Torvalds 	}
52651da177e4SLinus Torvalds 
52661da177e4SLinus Torvalds 	/*
52671da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
52681da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
52691da177e4SLinus Torvalds 	 * above restriction is ever removed.
52701da177e4SLinus Torvalds 	 */
52711da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
52723b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
52731da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
52743b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
52754eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
52763b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
527742c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
52783b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
52791da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
52803b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
52811da177e4SLinus Torvalds 	else
52821da177e4SLinus Torvalds 		error = -EINVAL;
52831da177e4SLinus Torvalds 	if (error)
52841da177e4SLinus Torvalds 		return error;
52851da177e4SLinus Torvalds 
52861da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
52871da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
52881da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
52891da177e4SLinus Torvalds 			str[size-1] = 0;
52901da177e4SLinus Torvalds 			size--;
52911da177e4SLinus Torvalds 		}
52921da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
529312b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
529412b29f34SStephen Smalley 			if (!capable(CAP_MAC_ADMIN))
529512b29f34SStephen Smalley 				return error;
529612b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
529712b29f34SStephen Smalley 							      &sid);
529812b29f34SStephen Smalley 		}
52991da177e4SLinus Torvalds 		if (error)
53001da177e4SLinus Torvalds 			return error;
53011da177e4SLinus Torvalds 	}
53021da177e4SLinus Torvalds 
5303d84f4f99SDavid Howells 	new = prepare_creds();
5304d84f4f99SDavid Howells 	if (!new)
5305d84f4f99SDavid Howells 		return -ENOMEM;
5306d84f4f99SDavid Howells 
53071da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
53081da177e4SLinus Torvalds 	   performed during the actual operation (execve,
53091da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5310d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
53111da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
53121da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5313d84f4f99SDavid Howells 	tsec = new->security;
5314d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
53151da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5316d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
53171da177e4SLinus Torvalds 		tsec->create_sid = sid;
5318d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
53194eb582cfSMichael LeMay 		error = may_create_key(sid, p);
53204eb582cfSMichael LeMay 		if (error)
5321d84f4f99SDavid Howells 			goto abort_change;
53224eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5323d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
532442c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5325d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5326d84f4f99SDavid Howells 		error = -EINVAL;
53271da177e4SLinus Torvalds 		if (sid == 0)
5328d84f4f99SDavid Howells 			goto abort_change;
5329d9250deaSKaiGai Kohei 
5330d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5331d84f4f99SDavid Howells 		error = -EPERM;
5332d84f4f99SDavid Howells 		if (!is_single_threaded(p)) {
5333d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5334d84f4f99SDavid Howells 			if (error)
5335d84f4f99SDavid Howells 				goto abort_change;
53361da177e4SLinus Torvalds 		}
53371da177e4SLinus Torvalds 
53381da177e4SLinus Torvalds 		/* Check permissions for the transition. */
53391da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
53401da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
53411da177e4SLinus Torvalds 		if (error)
5342d84f4f99SDavid Howells 			goto abort_change;
53431da177e4SLinus Torvalds 
53441da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
53451da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5346d84f4f99SDavid Howells 		ptsid = 0;
53471da177e4SLinus Torvalds 		task_lock(p);
53480d094efeSRoland McGrath 		tracer = tracehook_tracer_task(p);
5349d84f4f99SDavid Howells 		if (tracer)
5350d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
53511da177e4SLinus Torvalds 		task_unlock(p);
53521da177e4SLinus Torvalds 
5353d84f4f99SDavid Howells 		if (tracer) {
5354d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5355d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5356d84f4f99SDavid Howells 			if (error)
5357d84f4f99SDavid Howells 				goto abort_change;
5358d84f4f99SDavid Howells 		}
5359d84f4f99SDavid Howells 
5360d84f4f99SDavid Howells 		tsec->sid = sid;
5361d84f4f99SDavid Howells 	} else {
5362d84f4f99SDavid Howells 		error = -EINVAL;
5363d84f4f99SDavid Howells 		goto abort_change;
5364d84f4f99SDavid Howells 	}
5365d84f4f99SDavid Howells 
5366d84f4f99SDavid Howells 	commit_creds(new);
53671da177e4SLinus Torvalds 	return size;
5368d84f4f99SDavid Howells 
5369d84f4f99SDavid Howells abort_change:
5370d84f4f99SDavid Howells 	abort_creds(new);
5371d84f4f99SDavid Howells 	return error;
53721da177e4SLinus Torvalds }
53731da177e4SLinus Torvalds 
5374dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5375dc49c1f9SCatherine Zhang {
5376dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5377dc49c1f9SCatherine Zhang }
5378dc49c1f9SCatherine Zhang 
53797bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
538063cb3449SDavid Howells {
538163cb3449SDavid Howells 	return security_context_to_sid(secdata, seclen, secid);
538263cb3449SDavid Howells }
538363cb3449SDavid Howells 
5384dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5385dc49c1f9SCatherine Zhang {
5386dc49c1f9SCatherine Zhang 	kfree(secdata);
5387dc49c1f9SCatherine Zhang }
5388dc49c1f9SCatherine Zhang 
5389d720024eSMichael LeMay #ifdef CONFIG_KEYS
5390d720024eSMichael LeMay 
5391d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
53927e047ef5SDavid Howells 			     unsigned long flags)
5393d720024eSMichael LeMay {
5394d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5395d720024eSMichael LeMay 	struct key_security_struct *ksec;
5396d720024eSMichael LeMay 
5397d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5398d720024eSMichael LeMay 	if (!ksec)
5399d720024eSMichael LeMay 		return -ENOMEM;
5400d720024eSMichael LeMay 
5401d84f4f99SDavid Howells 	tsec = cred->security;
5402d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5403d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
54044eb582cfSMichael LeMay 	else
5405d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5406d720024eSMichael LeMay 
5407275bb41eSDavid Howells 	k->security = ksec;
5408d720024eSMichael LeMay 	return 0;
5409d720024eSMichael LeMay }
5410d720024eSMichael LeMay 
5411d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5412d720024eSMichael LeMay {
5413d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5414d720024eSMichael LeMay 
5415d720024eSMichael LeMay 	k->security = NULL;
5416d720024eSMichael LeMay 	kfree(ksec);
5417d720024eSMichael LeMay }
5418d720024eSMichael LeMay 
5419d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5420d84f4f99SDavid Howells 				  const struct cred *cred,
5421d720024eSMichael LeMay 				  key_perm_t perm)
5422d720024eSMichael LeMay {
5423d720024eSMichael LeMay 	struct key *key;
5424d720024eSMichael LeMay 	struct key_security_struct *ksec;
5425275bb41eSDavid Howells 	u32 sid;
5426d720024eSMichael LeMay 
5427d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5428d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5429d720024eSMichael LeMay 	   appear to be created. */
5430d720024eSMichael LeMay 	if (perm == 0)
5431d720024eSMichael LeMay 		return 0;
5432d720024eSMichael LeMay 
5433d84f4f99SDavid Howells 	sid = cred_sid(cred);
5434275bb41eSDavid Howells 
5435275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5436275bb41eSDavid Howells 	ksec = key->security;
5437275bb41eSDavid Howells 
5438275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5439d720024eSMichael LeMay }
5440d720024eSMichael LeMay 
544170a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
544270a5bb72SDavid Howells {
544370a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
544470a5bb72SDavid Howells 	char *context = NULL;
544570a5bb72SDavid Howells 	unsigned len;
544670a5bb72SDavid Howells 	int rc;
544770a5bb72SDavid Howells 
544870a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
544970a5bb72SDavid Howells 	if (!rc)
545070a5bb72SDavid Howells 		rc = len;
545170a5bb72SDavid Howells 	*_buffer = context;
545270a5bb72SDavid Howells 	return rc;
545370a5bb72SDavid Howells }
545470a5bb72SDavid Howells 
5455d720024eSMichael LeMay #endif
5456d720024eSMichael LeMay 
54571da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5458076c54c5SAhmed S. Darwish 	.name =				"selinux",
5459076c54c5SAhmed S. Darwish 
54605cd9c58fSDavid Howells 	.ptrace_may_access =		selinux_ptrace_may_access,
54615cd9c58fSDavid Howells 	.ptrace_traceme =		selinux_ptrace_traceme,
54621da177e4SLinus Torvalds 	.capget =			selinux_capget,
5463d84f4f99SDavid Howells 	.capset =			selinux_capset,
54641da177e4SLinus Torvalds 	.sysctl =			selinux_sysctl,
54651da177e4SLinus Torvalds 	.capable =			selinux_capable,
54661da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
54671da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
54681da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
54691da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
54701da177e4SLinus Torvalds 
54711da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
54721da177e4SLinus Torvalds 	.netlink_recv =			selinux_netlink_recv,
54731da177e4SLinus Torvalds 
5474a6f76f23SDavid Howells 	.bprm_set_creds =		selinux_bprm_set_creds,
5475a6f76f23SDavid Howells 	.bprm_committing_creds =	selinux_bprm_committing_creds,
5476a6f76f23SDavid Howells 	.bprm_committed_creds =		selinux_bprm_committed_creds,
54771da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
54781da177e4SLinus Torvalds 
54791da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
54801da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
54811da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
54821da177e4SLinus Torvalds 	.sb_kern_mount =		selinux_sb_kern_mount,
54832069f457SEric Paris 	.sb_show_options =		selinux_sb_show_options,
54841da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
54851da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
54861da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5487c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5488c9180a57SEric Paris 	.sb_clone_mnt_opts =		selinux_sb_clone_mnt_opts,
5489e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5490e0007529SEric Paris 
54911da177e4SLinus Torvalds 
54921da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
54931da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
54945e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
54951da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
54961da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
54971da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
54981da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
54991da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
55001da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
55011da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
55021da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
55031da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
55041da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
55051da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
55061da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
55071da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
55081da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
55091da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
55101da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
55111da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
55121da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
55131da177e4SLinus Torvalds 	.inode_getsecurity =		selinux_inode_getsecurity,
55141da177e4SLinus Torvalds 	.inode_setsecurity =		selinux_inode_setsecurity,
55151da177e4SLinus Torvalds 	.inode_listsecurity =		selinux_inode_listsecurity,
5516713a04aeSAhmed S. Darwish 	.inode_getsecid =		selinux_inode_getsecid,
55171da177e4SLinus Torvalds 
55181da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
55191da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
55201da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
55211da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
55221da177e4SLinus Torvalds 	.file_mmap =			selinux_file_mmap,
55231da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
55241da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
55251da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
55261da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
55271da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
55281da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
55291da177e4SLinus Torvalds 
5530788e7dd4SYuichi Nakamura 	.dentry_open =			selinux_dentry_open,
5531788e7dd4SYuichi Nakamura 
55321da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
5533f1752eecSDavid Howells 	.cred_free =			selinux_cred_free,
5534d84f4f99SDavid Howells 	.cred_prepare =			selinux_cred_prepare,
55353a3b7ce9SDavid Howells 	.kernel_act_as =		selinux_kernel_act_as,
55363a3b7ce9SDavid Howells 	.kernel_create_files_as =	selinux_kernel_create_files_as,
55371da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
55381da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
55391da177e4SLinus Torvalds 	.task_getsid =			selinux_task_getsid,
5540f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
55411da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
554203e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5543a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
55441da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
55451da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
55461da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
554735601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
55481da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
55491da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
55501da177e4SLinus Torvalds 	.task_to_inode =		selinux_task_to_inode,
55511da177e4SLinus Torvalds 
55521da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5553713a04aeSAhmed S. Darwish 	.ipc_getsecid =			selinux_ipc_getsecid,
55541da177e4SLinus Torvalds 
55551da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
55561da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
55571da177e4SLinus Torvalds 
55581da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
55591da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
55601da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
55611da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
55621da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
55631da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
55641da177e4SLinus Torvalds 
55651da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
55661da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
55671da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
55681da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
55691da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
55701da177e4SLinus Torvalds 
55711da177e4SLinus Torvalds 	.sem_alloc_security =		selinux_sem_alloc_security,
55721da177e4SLinus Torvalds 	.sem_free_security =		selinux_sem_free_security,
55731da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
55741da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
55751da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
55761da177e4SLinus Torvalds 
55771da177e4SLinus Torvalds 	.d_instantiate =		selinux_d_instantiate,
55781da177e4SLinus Torvalds 
55791da177e4SLinus Torvalds 	.getprocattr =			selinux_getprocattr,
55801da177e4SLinus Torvalds 	.setprocattr =			selinux_setprocattr,
55811da177e4SLinus Torvalds 
5582dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
558363cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5584dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
5585dc49c1f9SCatherine Zhang 
55861da177e4SLinus Torvalds 	.unix_stream_connect =		selinux_socket_unix_stream_connect,
55871da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
55881da177e4SLinus Torvalds 
55891da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
55901da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
55911da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
55921da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
55931da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
55941da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
55951da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
55961da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
55971da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
55981da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
55991da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
56001da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
56011da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
56021da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
56032c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
56042c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
56051da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
56061da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
5607892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
5608beb8d13bSVenkat Yekkirala 	.sk_getsecid =			selinux_sk_getsecid,
56094237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
56104237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
56114237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
56126b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
56134237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
5614d28d1e08STrent Jaeger 
5615d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5616d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
5617d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
5618d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
5619c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
5620d28d1e08STrent Jaeger 	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
5621d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
5622c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
5623d28d1e08STrent Jaeger 	.xfrm_policy_lookup =		selinux_xfrm_policy_lookup,
5624e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
5625e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
56261da177e4SLinus Torvalds #endif
5627d720024eSMichael LeMay 
5628d720024eSMichael LeMay #ifdef CONFIG_KEYS
5629d720024eSMichael LeMay 	.key_alloc =			selinux_key_alloc,
5630d720024eSMichael LeMay 	.key_free =			selinux_key_free,
5631d720024eSMichael LeMay 	.key_permission =		selinux_key_permission,
563270a5bb72SDavid Howells 	.key_getsecurity =		selinux_key_getsecurity,
5633d720024eSMichael LeMay #endif
56349d57a7f9SAhmed S. Darwish 
56359d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
56369d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
56379d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
56389d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
56399d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
56409d57a7f9SAhmed S. Darwish #endif
56411da177e4SLinus Torvalds };
56421da177e4SLinus Torvalds 
56431da177e4SLinus Torvalds static __init int selinux_init(void)
56441da177e4SLinus Torvalds {
5645076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
5646076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
5647076c54c5SAhmed S. Darwish 		return 0;
5648076c54c5SAhmed S. Darwish 	}
5649076c54c5SAhmed S. Darwish 
56501da177e4SLinus Torvalds 	if (!selinux_enabled) {
56511da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
56521da177e4SLinus Torvalds 		return 0;
56531da177e4SLinus Torvalds 	}
56541da177e4SLinus Torvalds 
56551da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
56561da177e4SLinus Torvalds 
56571da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
5658d84f4f99SDavid Howells 	cred_init_security();
56591da177e4SLinus Torvalds 
56607cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
56617cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
566220c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
56631da177e4SLinus Torvalds 	avc_init();
56641da177e4SLinus Torvalds 
56656f0f0fd4SJames Morris 	secondary_ops = security_ops;
56661da177e4SLinus Torvalds 	if (!secondary_ops)
56671da177e4SLinus Torvalds 		panic("SELinux: No initial security operations\n");
56681da177e4SLinus Torvalds 	if (register_security(&selinux_ops))
56691da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
56701da177e4SLinus Torvalds 
5671828dfe1dSEric Paris 	if (selinux_enforcing)
5672fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
5673828dfe1dSEric Paris 	else
5674fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
5675d720024eSMichael LeMay 
56761da177e4SLinus Torvalds 	return 0;
56771da177e4SLinus Torvalds }
56781da177e4SLinus Torvalds 
56791da177e4SLinus Torvalds void selinux_complete_init(void)
56801da177e4SLinus Torvalds {
5681fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
56821da177e4SLinus Torvalds 
56831da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
5684fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
5685ba0c19edSStephen Smalley 	spin_lock(&sb_lock);
56861da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
56871da177e4SLinus Torvalds next_sb:
56881da177e4SLinus Torvalds 	if (!list_empty(&superblock_security_head)) {
56891da177e4SLinus Torvalds 		struct superblock_security_struct *sbsec =
56901da177e4SLinus Torvalds 				list_entry(superblock_security_head.next,
56911da177e4SLinus Torvalds 					   struct superblock_security_struct,
56921da177e4SLinus Torvalds 					   list);
56931da177e4SLinus Torvalds 		struct super_block *sb = sbsec->sb;
56941da177e4SLinus Torvalds 		sb->s_count++;
56951da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
5696ba0c19edSStephen Smalley 		spin_unlock(&sb_lock);
56971da177e4SLinus Torvalds 		down_read(&sb->s_umount);
56981da177e4SLinus Torvalds 		if (sb->s_root)
56991da177e4SLinus Torvalds 			superblock_doinit(sb, NULL);
57001da177e4SLinus Torvalds 		drop_super(sb);
5701ba0c19edSStephen Smalley 		spin_lock(&sb_lock);
57021da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
57031da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
57041da177e4SLinus Torvalds 		goto next_sb;
57051da177e4SLinus Torvalds 	}
57061da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
5707ba0c19edSStephen Smalley 	spin_unlock(&sb_lock);
57081da177e4SLinus Torvalds }
57091da177e4SLinus Torvalds 
57101da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
57111da177e4SLinus Torvalds    all processes and objects when they are created. */
57121da177e4SLinus Torvalds security_initcall(selinux_init);
57131da177e4SLinus Torvalds 
5714c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
57151da177e4SLinus Torvalds 
5716effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = {
5717effad8dfSPaul Moore 	{
5718effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
57191da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
57201da177e4SLinus Torvalds 		.pf =		PF_INET,
57216e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
57221da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
5723effad8dfSPaul Moore 	},
5724effad8dfSPaul Moore 	{
5725effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
5726effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5727effad8dfSPaul Moore 		.pf =		PF_INET,
5728effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5729effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5730948bf85cSPaul Moore 	},
5731948bf85cSPaul Moore 	{
5732948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
5733948bf85cSPaul Moore 		.owner =	THIS_MODULE,
5734948bf85cSPaul Moore 		.pf =		PF_INET,
5735948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
5736948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5737effad8dfSPaul Moore 	}
57381da177e4SLinus Torvalds };
57391da177e4SLinus Torvalds 
57401da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
57411da177e4SLinus Torvalds 
5742effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = {
5743effad8dfSPaul Moore 	{
5744effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
57451da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
57461da177e4SLinus Torvalds 		.pf =		PF_INET6,
57476e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
57481da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
5749effad8dfSPaul Moore 	},
5750effad8dfSPaul Moore 	{
5751effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
5752effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5753effad8dfSPaul Moore 		.pf =		PF_INET6,
5754effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5755effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
5756effad8dfSPaul Moore 	}
57571da177e4SLinus Torvalds };
57581da177e4SLinus Torvalds 
57591da177e4SLinus Torvalds #endif	/* IPV6 */
57601da177e4SLinus Torvalds 
57611da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
57621da177e4SLinus Torvalds {
57631da177e4SLinus Torvalds 	int err = 0;
57641da177e4SLinus Torvalds 
57651da177e4SLinus Torvalds 	if (!selinux_enabled)
57661da177e4SLinus Torvalds 		goto out;
57671da177e4SLinus Torvalds 
5768fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
57691da177e4SLinus Torvalds 
57706c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
57711da177e4SLinus Torvalds 	if (err)
57726c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
57731da177e4SLinus Torvalds 
57741da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
57756c5a9d2eSAlexey Dobriyan 	err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
57761da177e4SLinus Torvalds 	if (err)
57776c5a9d2eSAlexey Dobriyan 		panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
57781da177e4SLinus Torvalds #endif	/* IPV6 */
5779d28d1e08STrent Jaeger 
57801da177e4SLinus Torvalds out:
57811da177e4SLinus Torvalds 	return err;
57821da177e4SLinus Torvalds }
57831da177e4SLinus Torvalds 
57841da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
57851da177e4SLinus Torvalds 
57861da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
57871da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
57881da177e4SLinus Torvalds {
5789fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
57901da177e4SLinus Torvalds 
57916c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
57921da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
57936c5a9d2eSAlexey Dobriyan 	nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
57941da177e4SLinus Torvalds #endif	/* IPV6 */
57951da177e4SLinus Torvalds }
57961da177e4SLinus Torvalds #endif
57971da177e4SLinus Torvalds 
5798c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
57991da177e4SLinus Torvalds 
58001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
58011da177e4SLinus Torvalds #define selinux_nf_ip_exit()
58021da177e4SLinus Torvalds #endif
58031da177e4SLinus Torvalds 
5804c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
58051da177e4SLinus Torvalds 
58061da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
5807828dfe1dSEric Paris static int selinux_disabled;
5808828dfe1dSEric Paris 
58091da177e4SLinus Torvalds int selinux_disable(void)
58101da177e4SLinus Torvalds {
58111da177e4SLinus Torvalds 	extern void exit_sel_fs(void);
58121da177e4SLinus Torvalds 
58131da177e4SLinus Torvalds 	if (ss_initialized) {
58141da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
58151da177e4SLinus Torvalds 		return -EINVAL;
58161da177e4SLinus Torvalds 	}
58171da177e4SLinus Torvalds 
58181da177e4SLinus Torvalds 	if (selinux_disabled) {
58191da177e4SLinus Torvalds 		/* Only do this once. */
58201da177e4SLinus Torvalds 		return -EINVAL;
58211da177e4SLinus Torvalds 	}
58221da177e4SLinus Torvalds 
58231da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
58241da177e4SLinus Torvalds 
58251da177e4SLinus Torvalds 	selinux_disabled = 1;
582630d55280SStephen Smalley 	selinux_enabled = 0;
58271da177e4SLinus Torvalds 
58281da177e4SLinus Torvalds 	/* Reset security_ops to the secondary module, dummy or capability. */
58291da177e4SLinus Torvalds 	security_ops = secondary_ops;
58301da177e4SLinus Torvalds 
58311da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
58321da177e4SLinus Torvalds 	selinux_nf_ip_exit();
58331da177e4SLinus Torvalds 
58341da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
58351da177e4SLinus Torvalds 	exit_sel_fs();
58361da177e4SLinus Torvalds 
58371da177e4SLinus Torvalds 	return 0;
58381da177e4SLinus Torvalds }
58391da177e4SLinus Torvalds #endif
5840