xref: /openbmc/linux/security/selinux/hooks.c (revision 3f7036a071b879da017eddaedb10fba173fdf1ff)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  NSA Security-Enhanced Linux (SELinux) security module
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This file contains the SELinux hook function implementations.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
71da177e4SLinus Torvalds  *	      Chris Vance, <cvance@nai.com>
81da177e4SLinus Torvalds  *	      Wayne Salamon, <wsalamon@nai.com>
91da177e4SLinus Torvalds  *	      James Morris <jmorris@redhat.com>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
122069f457SEric Paris  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
132069f457SEric Paris  *					   Eric Paris <eparis@redhat.com>
141da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
151da177e4SLinus Torvalds  *			    <dgoeddel@trustedcs.com>
16ed6d76e4SPaul Moore  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
1782c21bfaSPaul Moore  *	Paul Moore <paul@paul-moore.com>
18788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19788e7dd4SYuichi Nakamura  *		       Yuichi Nakamura <ynakam@hitachisoft.jp>
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
221da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
231da177e4SLinus Torvalds  *	as published by the Free Software Foundation.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/init.h>
270b24dcb7SEric Paris #include <linux/kd.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
290d094efeSRoland McGrath #include <linux/tracehook.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
311da177e4SLinus Torvalds #include <linux/sched.h>
321da177e4SLinus Torvalds #include <linux/security.h>
331da177e4SLinus Torvalds #include <linux/xattr.h>
341da177e4SLinus Torvalds #include <linux/capability.h>
351da177e4SLinus Torvalds #include <linux/unistd.h>
361da177e4SLinus Torvalds #include <linux/mm.h>
371da177e4SLinus Torvalds #include <linux/mman.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds #include <linux/pagemap.h>
400b24dcb7SEric Paris #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/swap.h>
421da177e4SLinus Torvalds #include <linux/spinlock.h>
431da177e4SLinus Torvalds #include <linux/syscalls.h>
442a7dba39SEric Paris #include <linux/dcache.h>
451da177e4SLinus Torvalds #include <linux/file.h>
469f3acc31SAl Viro #include <linux/fdtable.h>
471da177e4SLinus Torvalds #include <linux/namei.h>
481da177e4SLinus Torvalds #include <linux/mount.h>
491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
501da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
511da177e4SLinus Torvalds #include <linux/tty.h>
521da177e4SLinus Torvalds #include <net/icmp.h>
53227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
54ca10b9e9SEric Dumazet #include <net/sock.h>
551da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
5647180068SPaul Moore #include <net/inet_connection_sock.h>
57220deb96SPaul Moore #include <net/net_namespace.h>
58d621d35eSPaul Moore #include <net/netlabel.h>
59f5269710SEric Paris #include <linux/uaccess.h>
601da177e4SLinus Torvalds #include <asm/ioctls.h>
6160063497SArun Sharma #include <linux/atomic.h>
621da177e4SLinus Torvalds #include <linux/bitops.h>
631da177e4SLinus Torvalds #include <linux/interrupt.h>
641da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
6577954983SHong zhi guo #include <net/netlink.h>
661da177e4SLinus Torvalds #include <linux/tcp.h>
671da177e4SLinus Torvalds #include <linux/udp.h>
682ee92d46SJames Morris #include <linux/dccp.h>
691da177e4SLinus Torvalds #include <linux/quota.h>
701da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
711da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
721da177e4SLinus Torvalds #include <linux/parser.h>
731da177e4SLinus Torvalds #include <linux/nfs_mount.h>
741da177e4SLinus Torvalds #include <net/ipv6.h>
751da177e4SLinus Torvalds #include <linux/hugetlb.h>
761da177e4SLinus Torvalds #include <linux/personality.h>
771da177e4SLinus Torvalds #include <linux/audit.h>
786931dfc9SEric Paris #include <linux/string.h>
79877ce7c1SCatherine Zhang #include <linux/selinux.h>
8023970741SEric Paris #include <linux/mutex.h>
81f06febc9SFrank Mayhar #include <linux/posix-timers.h>
8200234592SKees Cook #include <linux/syslog.h>
833486740aSSerge E. Hallyn #include <linux/user_namespace.h>
8444fc7ea0SPaul Gortmaker #include <linux/export.h>
8540401530SAl Viro #include <linux/msg.h>
8640401530SAl Viro #include <linux/shm.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #include "avc.h"
891da177e4SLinus Torvalds #include "objsec.h"
901da177e4SLinus Torvalds #include "netif.h"
91224dfbd8SPaul Moore #include "netnode.h"
923e112172SPaul Moore #include "netport.h"
93d28d1e08STrent Jaeger #include "xfrm.h"
94c60475bfSPaul Moore #include "netlabel.h"
959d57a7f9SAhmed S. Darwish #include "audit.h"
967b98a585SJames Morris #include "avc_ss.h"
971da177e4SLinus Torvalds 
98d621d35eSPaul Moore /* SECMARK reference count */
9956a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
100d621d35eSPaul Moore 
1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
102828dfe1dSEric Paris int selinux_enforcing;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1051da177e4SLinus Torvalds {
106f5269710SEric Paris 	unsigned long enforcing;
10729707b20SJingoo Han 	if (!kstrtoul(str, 0, &enforcing))
108f5269710SEric Paris 		selinux_enforcing = enforcing ? 1 : 0;
1091da177e4SLinus Torvalds 	return 1;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1121da177e4SLinus Torvalds #endif
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1151da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1181da177e4SLinus Torvalds {
119f5269710SEric Paris 	unsigned long enabled;
12029707b20SJingoo Han 	if (!kstrtoul(str, 0, &enabled))
121f5269710SEric Paris 		selinux_enabled = enabled ? 1 : 0;
1221da177e4SLinus Torvalds 	return 1;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
12530d55280SStephen Smalley #else
12630d55280SStephen Smalley int selinux_enabled = 1;
1271da177e4SLinus Torvalds #endif
1281da177e4SLinus Torvalds 
129e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1307cae7e26SJames Morris 
131d621d35eSPaul Moore /**
132d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
133d621d35eSPaul Moore  *
134d621d35eSPaul Moore  * Description:
135d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
136d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
137d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
1382be4d74fSChris PeBenito  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
1392be4d74fSChris PeBenito  * policy capability is enabled, SECMARK is always considered enabled.
140d621d35eSPaul Moore  *
141d621d35eSPaul Moore  */
142d621d35eSPaul Moore static int selinux_secmark_enabled(void)
143d621d35eSPaul Moore {
1442be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
1452be4d74fSChris PeBenito }
1462be4d74fSChris PeBenito 
1472be4d74fSChris PeBenito /**
1482be4d74fSChris PeBenito  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
1492be4d74fSChris PeBenito  *
1502be4d74fSChris PeBenito  * Description:
1512be4d74fSChris PeBenito  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
1522be4d74fSChris PeBenito  * (1) if any are enabled or false (0) if neither are enabled.  If the
1532be4d74fSChris PeBenito  * always_check_network policy capability is enabled, peer labeling
1542be4d74fSChris PeBenito  * is always considered enabled.
1552be4d74fSChris PeBenito  *
1562be4d74fSChris PeBenito  */
1572be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void)
1582be4d74fSChris PeBenito {
1592be4d74fSChris PeBenito 	return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
160d621d35eSPaul Moore }
161d621d35eSPaul Moore 
162615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event)
163615e51fdSPaul Moore {
164615e51fdSPaul Moore 	if (event == AVC_CALLBACK_RESET) {
165615e51fdSPaul Moore 		sel_netif_flush();
166615e51fdSPaul Moore 		sel_netnode_flush();
167615e51fdSPaul Moore 		sel_netport_flush();
168615e51fdSPaul Moore 		synchronize_net();
169615e51fdSPaul Moore 	}
170615e51fdSPaul Moore 	return 0;
171615e51fdSPaul Moore }
172615e51fdSPaul Moore 
173d84f4f99SDavid Howells /*
174d84f4f99SDavid Howells  * initialise the security for the init task
175d84f4f99SDavid Howells  */
176d84f4f99SDavid Howells static void cred_init_security(void)
1771da177e4SLinus Torvalds {
1783b11a1deSDavid Howells 	struct cred *cred = (struct cred *) current->real_cred;
1791da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1801da177e4SLinus Torvalds 
18189d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1821da177e4SLinus Torvalds 	if (!tsec)
183d84f4f99SDavid Howells 		panic("SELinux:  Failed to initialize initial task.\n");
1841da177e4SLinus Torvalds 
185d84f4f99SDavid Howells 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
186f1752eecSDavid Howells 	cred->security = tsec;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
189275bb41eSDavid Howells /*
19088e67f3bSDavid Howells  * get the security ID of a set of credentials
19188e67f3bSDavid Howells  */
19288e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred)
19388e67f3bSDavid Howells {
19488e67f3bSDavid Howells 	const struct task_security_struct *tsec;
19588e67f3bSDavid Howells 
19688e67f3bSDavid Howells 	tsec = cred->security;
19788e67f3bSDavid Howells 	return tsec->sid;
19888e67f3bSDavid Howells }
19988e67f3bSDavid Howells 
20088e67f3bSDavid Howells /*
2013b11a1deSDavid Howells  * get the objective security ID of a task
202275bb41eSDavid Howells  */
203275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task)
204275bb41eSDavid Howells {
205275bb41eSDavid Howells 	u32 sid;
206275bb41eSDavid Howells 
207275bb41eSDavid Howells 	rcu_read_lock();
20888e67f3bSDavid Howells 	sid = cred_sid(__task_cred(task));
209275bb41eSDavid Howells 	rcu_read_unlock();
210275bb41eSDavid Howells 	return sid;
211275bb41eSDavid Howells }
212275bb41eSDavid Howells 
213275bb41eSDavid Howells /*
2143b11a1deSDavid Howells  * get the subjective security ID of the current task
215275bb41eSDavid Howells  */
216275bb41eSDavid Howells static inline u32 current_sid(void)
217275bb41eSDavid Howells {
2185fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
219275bb41eSDavid Howells 
220275bb41eSDavid Howells 	return tsec->sid;
221275bb41eSDavid Howells }
222275bb41eSDavid Howells 
22388e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */
22488e67f3bSDavid Howells 
2251da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	struct inode_security_struct *isec;
228275bb41eSDavid Howells 	u32 sid = current_sid();
2291da177e4SLinus Torvalds 
230a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
2311da177e4SLinus Torvalds 	if (!isec)
2321da177e4SLinus Torvalds 		return -ENOMEM;
2331da177e4SLinus Torvalds 
23423970741SEric Paris 	mutex_init(&isec->lock);
2351da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
2361da177e4SLinus Torvalds 	isec->inode = inode;
2371da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
2381da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
239275bb41eSDavid Howells 	isec->task_sid = sid;
2401da177e4SLinus Torvalds 	inode->i_security = isec;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	return 0;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2453dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head)
2463dc91d43SSteven Rostedt {
2473dc91d43SSteven Rostedt 	struct inode_security_struct *isec;
2483dc91d43SSteven Rostedt 
2493dc91d43SSteven Rostedt 	isec = container_of(head, struct inode_security_struct, rcu);
2503dc91d43SSteven Rostedt 	kmem_cache_free(sel_inode_cache, isec);
2513dc91d43SSteven Rostedt }
2523dc91d43SSteven Rostedt 
2531da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2561da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2591da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2601da177e4SLinus Torvalds 		list_del_init(&isec->list);
2611da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2621da177e4SLinus Torvalds 
2633dc91d43SSteven Rostedt 	/*
2643dc91d43SSteven Rostedt 	 * The inode may still be referenced in a path walk and
2653dc91d43SSteven Rostedt 	 * a call to selinux_inode_permission() can be made
2663dc91d43SSteven Rostedt 	 * after inode_free_security() is called. Ideally, the VFS
2673dc91d43SSteven Rostedt 	 * wouldn't do this, but fixing that is a much harder
2683dc91d43SSteven Rostedt 	 * job. For now, simply free the i_security via RCU, and
2693dc91d43SSteven Rostedt 	 * leave the current inode->i_security pointer intact.
2703dc91d43SSteven Rostedt 	 * The inode will be freed after the RCU grace period too.
2713dc91d43SSteven Rostedt 	 */
2723dc91d43SSteven Rostedt 	call_rcu(&isec->rcu, inode_free_rcu);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	struct file_security_struct *fsec;
278275bb41eSDavid Howells 	u32 sid = current_sid();
2791da177e4SLinus Torvalds 
28026d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2811da177e4SLinus Torvalds 	if (!fsec)
2821da177e4SLinus Torvalds 		return -ENOMEM;
2831da177e4SLinus Torvalds 
284275bb41eSDavid Howells 	fsec->sid = sid;
285275bb41eSDavid Howells 	fsec->fown_sid = sid;
2861da177e4SLinus Torvalds 	file->f_security = fsec;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	return 0;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void file_free_security(struct file *file)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2941da177e4SLinus Torvalds 	file->f_security = NULL;
2951da177e4SLinus Torvalds 	kfree(fsec);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
3011da177e4SLinus Torvalds 
30289d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
3031da177e4SLinus Torvalds 	if (!sbsec)
3041da177e4SLinus Torvalds 		return -ENOMEM;
3051da177e4SLinus Torvalds 
306bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
3071da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
3081da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
3091da177e4SLinus Torvalds 	sbsec->sb = sb;
3101da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
3111da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
312c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
3131da177e4SLinus Torvalds 	sb->s_security = sbsec;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	return 0;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3211da177e4SLinus Torvalds 	sb->s_security = NULL;
3221da177e4SLinus Torvalds 	kfree(sbsec);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3261da177e4SLinus Torvalds 
327eb9ae686SDavid Quigley static const char *labeling_behaviors[7] = {
3281da177e4SLinus Torvalds 	"uses xattr",
3291da177e4SLinus Torvalds 	"uses transition SIDs",
3301da177e4SLinus Torvalds 	"uses task SIDs",
3311da177e4SLinus Torvalds 	"uses genfs_contexts",
3321da177e4SLinus Torvalds 	"not configured for labeling",
3331da177e4SLinus Torvalds 	"uses mountpoint labeling",
334eb9ae686SDavid Quigley 	"uses native labeling",
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds enum {
34531e87930SEric Paris 	Opt_error = -1,
3461da177e4SLinus Torvalds 	Opt_context = 1,
3471da177e4SLinus Torvalds 	Opt_fscontext = 2,
348c9180a57SEric Paris 	Opt_defcontext = 3,
349c9180a57SEric Paris 	Opt_rootcontext = 4,
35011689d47SDavid P. Quigley 	Opt_labelsupport = 5,
351d355987fSEric Paris 	Opt_nextmntopt = 6,
3521da177e4SLinus Torvalds };
3531da177e4SLinus Torvalds 
354d355987fSEric Paris #define NUM_SEL_MNT_OPTS	(Opt_nextmntopt - 1)
355d355987fSEric Paris 
356a447c093SSteven Whitehouse static const match_table_t tokens = {
357832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
358832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
359832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
360832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
36111689d47SDavid P. Quigley 	{Opt_labelsupport, LABELSUPP_STR},
36231e87930SEric Paris 	{Opt_error, NULL},
3631da177e4SLinus Torvalds };
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3661da177e4SLinus Torvalds 
367c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
368c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
369275bb41eSDavid Howells 			const struct cred *cred)
370c312feb2SEric Paris {
371275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
372c312feb2SEric Paris 	int rc;
373c312feb2SEric Paris 
374c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
375c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
376c312feb2SEric Paris 	if (rc)
377c312feb2SEric Paris 		return rc;
378c312feb2SEric Paris 
379c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
380c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
381c312feb2SEric Paris 	return rc;
382c312feb2SEric Paris }
383c312feb2SEric Paris 
3840808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3850808925eSEric Paris 			struct superblock_security_struct *sbsec,
386275bb41eSDavid Howells 			const struct cred *cred)
3870808925eSEric Paris {
388275bb41eSDavid Howells 	const struct task_security_struct *tsec = cred->security;
3890808925eSEric Paris 	int rc;
3900808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3910808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3920808925eSEric Paris 	if (rc)
3930808925eSEric Paris 		return rc;
3940808925eSEric Paris 
3950808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3960808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3970808925eSEric Paris 	return rc;
3980808925eSEric Paris }
3990808925eSEric Paris 
400b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb)
401b43e725dSEric Paris {
402b43e725dSEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
403b43e725dSEric Paris 
404d5f3a5f6SMark Salyzyn 	return sbsec->behavior == SECURITY_FS_USE_XATTR ||
405b43e725dSEric Paris 		sbsec->behavior == SECURITY_FS_USE_TRANS ||
406d5f3a5f6SMark Salyzyn 		sbsec->behavior == SECURITY_FS_USE_TASK ||
407d5f3a5f6SMark Salyzyn 		/* Special handling. Genfs but also in-core setxattr handler */
408d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "sysfs") ||
409d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "pstore") ||
410d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "debugfs") ||
411d5f3a5f6SMark Salyzyn 		!strcmp(sb->s_type->name, "rootfs");
412b43e725dSEric Paris }
413b43e725dSEric Paris 
414c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
4171da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
418c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
4191da177e4SLinus Torvalds 	int rc = 0;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
4221da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
4231da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
4241da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
4251da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
4261da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
427c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
42829b1deb2SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
42929b1deb2SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
4301da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
4311da177e4SLinus Torvalds 			goto out;
4321da177e4SLinus Torvalds 		}
433c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
4341da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
4351da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
4361da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
43729b1deb2SLinus Torvalds 				       "%s) has no security xattr handler\n",
43829b1deb2SLinus Torvalds 				       sb->s_id, sb->s_type->name);
4391da177e4SLinus Torvalds 			else
4401da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
44129b1deb2SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
44229b1deb2SLinus Torvalds 				       sb->s_type->name, -rc);
4431da177e4SLinus Torvalds 			goto out;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
447c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
44829b1deb2SLinus Torvalds 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
44929b1deb2SLinus Torvalds 		       sb->s_id, sb->s_type->name);
4501da177e4SLinus Torvalds 
451eadcabc6SEric Paris 	sbsec->flags |= SE_SBINITIALIZED;
452b43e725dSEric Paris 	if (selinux_is_sblabel_mnt(sb))
45312f348b9SEric Paris 		sbsec->flags |= SBLABEL_MNT;
454ddd29ec6SDavid P. Quigley 
4551da177e4SLinus Torvalds 	/* Initialize the root inode. */
456c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4591da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4601da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4611da177e4SLinus Torvalds 	   populates itself. */
4621da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4631da177e4SLinus Torvalds next_inode:
4641da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4651da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4661da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4671da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4681da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
469923190d3SStephen Smalley 		list_del_init(&isec->list);
4701da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4711da177e4SLinus Torvalds 		inode = igrab(inode);
4721da177e4SLinus Torvalds 		if (inode) {
4731da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4741da177e4SLinus Torvalds 				inode_doinit(inode);
4751da177e4SLinus Torvalds 			iput(inode);
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4781da177e4SLinus Torvalds 		goto next_inode;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4811da177e4SLinus Torvalds out:
482c9180a57SEric Paris 	return rc;
483c9180a57SEric Paris }
484c9180a57SEric Paris 
485c9180a57SEric Paris /*
486c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
487c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
488c9180a57SEric Paris  * mount options, or whatever.
489c9180a57SEric Paris  */
490c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
491e0007529SEric Paris 				struct security_mnt_opts *opts)
492c9180a57SEric Paris {
493c9180a57SEric Paris 	int rc = 0, i;
494c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
495c9180a57SEric Paris 	char *context = NULL;
496c9180a57SEric Paris 	u32 len;
497c9180a57SEric Paris 	char tmp;
498c9180a57SEric Paris 
499e0007529SEric Paris 	security_init_mnt_opts(opts);
500c9180a57SEric Paris 
5010d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
502c9180a57SEric Paris 		return -EINVAL;
503c9180a57SEric Paris 
504c9180a57SEric Paris 	if (!ss_initialized)
505c9180a57SEric Paris 		return -EINVAL;
506c9180a57SEric Paris 
507af8e50ccSEric Paris 	/* make sure we always check enough bits to cover the mask */
508af8e50ccSEric Paris 	BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
509af8e50ccSEric Paris 
5100d90a7ecSDavid P. Quigley 	tmp = sbsec->flags & SE_MNTMASK;
511c9180a57SEric Paris 	/* count the number of mount options for this sb */
512af8e50ccSEric Paris 	for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
513c9180a57SEric Paris 		if (tmp & 0x01)
514e0007529SEric Paris 			opts->num_mnt_opts++;
515c9180a57SEric Paris 		tmp >>= 1;
516c9180a57SEric Paris 	}
51711689d47SDavid P. Quigley 	/* Check if the Label support flag is set */
5180b4bdb35SEric Paris 	if (sbsec->flags & SBLABEL_MNT)
51911689d47SDavid P. Quigley 		opts->num_mnt_opts++;
520c9180a57SEric Paris 
521e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
522e0007529SEric Paris 	if (!opts->mnt_opts) {
523c9180a57SEric Paris 		rc = -ENOMEM;
524c9180a57SEric Paris 		goto out_free;
525c9180a57SEric Paris 	}
526c9180a57SEric Paris 
527e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
528e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
529c9180a57SEric Paris 		rc = -ENOMEM;
530c9180a57SEric Paris 		goto out_free;
531c9180a57SEric Paris 	}
532c9180a57SEric Paris 
533c9180a57SEric Paris 	i = 0;
534c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
535c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
536c9180a57SEric Paris 		if (rc)
537c9180a57SEric Paris 			goto out_free;
538e0007529SEric Paris 		opts->mnt_opts[i] = context;
539e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
540c9180a57SEric Paris 	}
541c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
542c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
543c9180a57SEric Paris 		if (rc)
544c9180a57SEric Paris 			goto out_free;
545e0007529SEric Paris 		opts->mnt_opts[i] = context;
546e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
547c9180a57SEric Paris 	}
548c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
549c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
550c9180a57SEric Paris 		if (rc)
551c9180a57SEric Paris 			goto out_free;
552e0007529SEric Paris 		opts->mnt_opts[i] = context;
553e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
554c9180a57SEric Paris 	}
555c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
556c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
557c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
558c9180a57SEric Paris 
559c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
560c9180a57SEric Paris 		if (rc)
561c9180a57SEric Paris 			goto out_free;
562e0007529SEric Paris 		opts->mnt_opts[i] = context;
563e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
564c9180a57SEric Paris 	}
56512f348b9SEric Paris 	if (sbsec->flags & SBLABEL_MNT) {
56611689d47SDavid P. Quigley 		opts->mnt_opts[i] = NULL;
56712f348b9SEric Paris 		opts->mnt_opts_flags[i++] = SBLABEL_MNT;
56811689d47SDavid P. Quigley 	}
569c9180a57SEric Paris 
570e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
571c9180a57SEric Paris 
572c9180a57SEric Paris 	return 0;
573c9180a57SEric Paris 
574c9180a57SEric Paris out_free:
575e0007529SEric Paris 	security_free_mnt_opts(opts);
576c9180a57SEric Paris 	return rc;
577c9180a57SEric Paris }
578c9180a57SEric Paris 
579c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
580c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
581c9180a57SEric Paris {
5820d90a7ecSDavid P. Quigley 	char mnt_flags = sbsec->flags & SE_MNTMASK;
5830d90a7ecSDavid P. Quigley 
584c9180a57SEric Paris 	/* check if the old mount command had the same options */
5850d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED)
586c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
587c9180a57SEric Paris 		    (old_sid != new_sid))
588c9180a57SEric Paris 			return 1;
589c9180a57SEric Paris 
590c9180a57SEric Paris 	/* check if we were passed the same options twice,
591c9180a57SEric Paris 	 * aka someone passed context=a,context=b
592c9180a57SEric Paris 	 */
5930d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED))
5940d90a7ecSDavid P. Quigley 		if (mnt_flags & flag)
595c9180a57SEric Paris 			return 1;
596c9180a57SEric Paris 	return 0;
597c9180a57SEric Paris }
598e0007529SEric Paris 
599c9180a57SEric Paris /*
600c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
601c9180a57SEric Paris  * labeling information.
602c9180a57SEric Paris  */
603e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
604649f6e77SDavid Quigley 				struct security_mnt_opts *opts,
605649f6e77SDavid Quigley 				unsigned long kern_flags,
606649f6e77SDavid Quigley 				unsigned long *set_kern_flags)
607c9180a57SEric Paris {
608275bb41eSDavid Howells 	const struct cred *cred = current_cred();
609c9180a57SEric Paris 	int rc = 0, i;
610c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
61129b1deb2SLinus Torvalds 	const char *name = sb->s_type->name;
612089be43eSJames Morris 	struct inode *inode = sbsec->sb->s_root->d_inode;
613089be43eSJames Morris 	struct inode_security_struct *root_isec = inode->i_security;
614c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
615c9180a57SEric Paris 	u32 defcontext_sid = 0;
616e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
617e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
618e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
619c9180a57SEric Paris 
620c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
621c9180a57SEric Paris 
622c9180a57SEric Paris 	if (!ss_initialized) {
623c9180a57SEric Paris 		if (!num_opts) {
624c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
625c9180a57SEric Paris 			   after the initial policy is loaded and the security
626c9180a57SEric Paris 			   server is ready to handle calls. */
627c9180a57SEric Paris 			goto out;
628c9180a57SEric Paris 		}
629c9180a57SEric Paris 		rc = -EINVAL;
630744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
631744ba35eSEric Paris 			"before the security server is initialized\n");
632c9180a57SEric Paris 		goto out;
633c9180a57SEric Paris 	}
634649f6e77SDavid Quigley 	if (kern_flags && !set_kern_flags) {
635649f6e77SDavid Quigley 		/* Specifying internal flags without providing a place to
636649f6e77SDavid Quigley 		 * place the results is not allowed */
637649f6e77SDavid Quigley 		rc = -EINVAL;
638649f6e77SDavid Quigley 		goto out;
639649f6e77SDavid Quigley 	}
640c9180a57SEric Paris 
641c9180a57SEric Paris 	/*
642e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
643e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
644e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
645e0007529SEric Paris 	 * we need to skip the double mount verification.
646e0007529SEric Paris 	 *
647e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
648e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
649e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
650e0007529SEric Paris 	 * will be used for both mounts)
651e0007529SEric Paris 	 */
6520d90a7ecSDavid P. Quigley 	if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
653e0007529SEric Paris 	    && (num_opts == 0))
654e0007529SEric Paris 		goto out;
655e0007529SEric Paris 
656e0007529SEric Paris 	/*
657c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
658c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
659c9180a57SEric Paris 	 * than once with different security options.
660c9180a57SEric Paris 	 */
661c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
662c9180a57SEric Paris 		u32 sid;
66311689d47SDavid P. Quigley 
66412f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
66511689d47SDavid P. Quigley 			continue;
666c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
66752a4c640SNikolay Aleksandrov 					     strlen(mount_options[i]), &sid, GFP_KERNEL);
668c9180a57SEric Paris 		if (rc) {
669c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
67029b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
67129b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, name, rc);
672c9180a57SEric Paris 			goto out;
673c9180a57SEric Paris 		}
674c9180a57SEric Paris 		switch (flags[i]) {
675c9180a57SEric Paris 		case FSCONTEXT_MNT:
676c9180a57SEric Paris 			fscontext_sid = sid;
677c9180a57SEric Paris 
678c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
679c9180a57SEric Paris 					fscontext_sid))
680c9180a57SEric Paris 				goto out_double_mount;
681c9180a57SEric Paris 
682c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
683c9180a57SEric Paris 			break;
684c9180a57SEric Paris 		case CONTEXT_MNT:
685c9180a57SEric Paris 			context_sid = sid;
686c9180a57SEric Paris 
687c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
688c9180a57SEric Paris 					context_sid))
689c9180a57SEric Paris 				goto out_double_mount;
690c9180a57SEric Paris 
691c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
692c9180a57SEric Paris 			break;
693c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
694c9180a57SEric Paris 			rootcontext_sid = sid;
695c9180a57SEric Paris 
696c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
697c9180a57SEric Paris 					rootcontext_sid))
698c9180a57SEric Paris 				goto out_double_mount;
699c9180a57SEric Paris 
700c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
701c9180a57SEric Paris 
702c9180a57SEric Paris 			break;
703c9180a57SEric Paris 		case DEFCONTEXT_MNT:
704c9180a57SEric Paris 			defcontext_sid = sid;
705c9180a57SEric Paris 
706c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
707c9180a57SEric Paris 					defcontext_sid))
708c9180a57SEric Paris 				goto out_double_mount;
709c9180a57SEric Paris 
710c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
711c9180a57SEric Paris 
712c9180a57SEric Paris 			break;
713c9180a57SEric Paris 		default:
714c9180a57SEric Paris 			rc = -EINVAL;
715c9180a57SEric Paris 			goto out;
716c9180a57SEric Paris 		}
717c9180a57SEric Paris 	}
718c9180a57SEric Paris 
7190d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
720c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
7210d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_MNTMASK) && !num_opts)
722c9180a57SEric Paris 			goto out_double_mount;
723c9180a57SEric Paris 		rc = 0;
724c9180a57SEric Paris 		goto out;
725c9180a57SEric Paris 	}
726c9180a57SEric Paris 
727089be43eSJames Morris 	if (strcmp(sb->s_type->name, "proc") == 0)
7280d90a7ecSDavid P. Quigley 		sbsec->flags |= SE_SBPROC;
729c9180a57SEric Paris 
730eb9ae686SDavid Quigley 	if (!sbsec->behavior) {
731eb9ae686SDavid Quigley 		/*
732eb9ae686SDavid Quigley 		 * Determine the labeling behavior to use for this
733eb9ae686SDavid Quigley 		 * filesystem type.
734eb9ae686SDavid Quigley 		 */
735a64c54cfSEric Paris 		rc = security_fs_use(sb);
736c9180a57SEric Paris 		if (rc) {
737eb9ae686SDavid Quigley 			printk(KERN_WARNING
738eb9ae686SDavid Quigley 				"%s: security_fs_use(%s) returned %d\n",
739089be43eSJames Morris 					__func__, sb->s_type->name, rc);
740c9180a57SEric Paris 			goto out;
741c9180a57SEric Paris 		}
742eb9ae686SDavid Quigley 	}
743c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
744c9180a57SEric Paris 	if (fscontext_sid) {
745275bb41eSDavid Howells 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
746c9180a57SEric Paris 		if (rc)
747c9180a57SEric Paris 			goto out;
748c9180a57SEric Paris 
749c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
750c9180a57SEric Paris 	}
751c9180a57SEric Paris 
752c9180a57SEric Paris 	/*
753c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
754c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
755c9180a57SEric Paris 	 * the superblock context if not already set.
756c9180a57SEric Paris 	 */
757eb9ae686SDavid Quigley 	if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
758eb9ae686SDavid Quigley 		sbsec->behavior = SECURITY_FS_USE_NATIVE;
759eb9ae686SDavid Quigley 		*set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
760eb9ae686SDavid Quigley 	}
761eb9ae686SDavid Quigley 
762c9180a57SEric Paris 	if (context_sid) {
763c9180a57SEric Paris 		if (!fscontext_sid) {
764275bb41eSDavid Howells 			rc = may_context_mount_sb_relabel(context_sid, sbsec,
765275bb41eSDavid Howells 							  cred);
766c9180a57SEric Paris 			if (rc)
767c9180a57SEric Paris 				goto out;
768c9180a57SEric Paris 			sbsec->sid = context_sid;
769c9180a57SEric Paris 		} else {
770275bb41eSDavid Howells 			rc = may_context_mount_inode_relabel(context_sid, sbsec,
771275bb41eSDavid Howells 							     cred);
772c9180a57SEric Paris 			if (rc)
773c9180a57SEric Paris 				goto out;
774c9180a57SEric Paris 		}
775c9180a57SEric Paris 		if (!rootcontext_sid)
776c9180a57SEric Paris 			rootcontext_sid = context_sid;
777c9180a57SEric Paris 
778c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
779c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
780c9180a57SEric Paris 	}
781c9180a57SEric Paris 
782c9180a57SEric Paris 	if (rootcontext_sid) {
783275bb41eSDavid Howells 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
784275bb41eSDavid Howells 						     cred);
785c9180a57SEric Paris 		if (rc)
786c9180a57SEric Paris 			goto out;
787c9180a57SEric Paris 
788c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
789c9180a57SEric Paris 		root_isec->initialized = 1;
790c9180a57SEric Paris 	}
791c9180a57SEric Paris 
792c9180a57SEric Paris 	if (defcontext_sid) {
793eb9ae686SDavid Quigley 		if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
794eb9ae686SDavid Quigley 			sbsec->behavior != SECURITY_FS_USE_NATIVE) {
795c9180a57SEric Paris 			rc = -EINVAL;
796c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
797c9180a57SEric Paris 			       "invalid for this filesystem type\n");
798c9180a57SEric Paris 			goto out;
799c9180a57SEric Paris 		}
800c9180a57SEric Paris 
801c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
802c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
803275bb41eSDavid Howells 							     sbsec, cred);
804c9180a57SEric Paris 			if (rc)
805c9180a57SEric Paris 				goto out;
806c9180a57SEric Paris 		}
807c9180a57SEric Paris 
808c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
809c9180a57SEric Paris 	}
810c9180a57SEric Paris 
811c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
812c9180a57SEric Paris out:
813bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
8141da177e4SLinus Torvalds 	return rc;
815c9180a57SEric Paris out_double_mount:
816c9180a57SEric Paris 	rc = -EINVAL;
817c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
81829b1deb2SLinus Torvalds 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
819c9180a57SEric Paris 	goto out;
820c9180a57SEric Paris }
821c9180a57SEric Paris 
822094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb,
823094f7b69SJeff Layton 				    const struct super_block *newsb)
824094f7b69SJeff Layton {
825094f7b69SJeff Layton 	struct superblock_security_struct *old = oldsb->s_security;
826094f7b69SJeff Layton 	struct superblock_security_struct *new = newsb->s_security;
827094f7b69SJeff Layton 	char oldflags = old->flags & SE_MNTMASK;
828094f7b69SJeff Layton 	char newflags = new->flags & SE_MNTMASK;
829094f7b69SJeff Layton 
830094f7b69SJeff Layton 	if (oldflags != newflags)
831094f7b69SJeff Layton 		goto mismatch;
832094f7b69SJeff Layton 	if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
833094f7b69SJeff Layton 		goto mismatch;
834094f7b69SJeff Layton 	if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
835094f7b69SJeff Layton 		goto mismatch;
836094f7b69SJeff Layton 	if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
837094f7b69SJeff Layton 		goto mismatch;
838094f7b69SJeff Layton 	if (oldflags & ROOTCONTEXT_MNT) {
839094f7b69SJeff Layton 		struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
840094f7b69SJeff Layton 		struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
841094f7b69SJeff Layton 		if (oldroot->sid != newroot->sid)
842094f7b69SJeff Layton 			goto mismatch;
843094f7b69SJeff Layton 	}
844094f7b69SJeff Layton 	return 0;
845094f7b69SJeff Layton mismatch:
846094f7b69SJeff Layton 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
847094f7b69SJeff Layton 			    "different security settings for (dev %s, "
848094f7b69SJeff Layton 			    "type %s)\n", newsb->s_id, newsb->s_type->name);
849094f7b69SJeff Layton 	return -EBUSY;
850094f7b69SJeff Layton }
851094f7b69SJeff Layton 
852094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
853c9180a57SEric Paris 					struct super_block *newsb)
854c9180a57SEric Paris {
855c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
856c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
857c9180a57SEric Paris 
858c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
859c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
860c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
861c9180a57SEric Paris 
8620f5e6420SEric Paris 	/*
8630f5e6420SEric Paris 	 * if the parent was able to be mounted it clearly had no special lsm
864e8c26255SAl Viro 	 * mount options.  thus we can safely deal with this superblock later
8650f5e6420SEric Paris 	 */
866e8c26255SAl Viro 	if (!ss_initialized)
867094f7b69SJeff Layton 		return 0;
868c9180a57SEric Paris 
869c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
8700d90a7ecSDavid P. Quigley 	BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
871c9180a57SEric Paris 
872094f7b69SJeff Layton 	/* if fs is reusing a sb, make sure that the contexts match */
8730d90a7ecSDavid P. Quigley 	if (newsbsec->flags & SE_SBINITIALIZED)
874094f7b69SJeff Layton 		return selinux_cmp_sb_context(oldsb, newsb);
8755a552617SEric Paris 
876c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
877c9180a57SEric Paris 
878c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
879c9180a57SEric Paris 
880c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
881c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
882c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
883c9180a57SEric Paris 
884c9180a57SEric Paris 	if (set_context) {
885c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
886c9180a57SEric Paris 
887c9180a57SEric Paris 		if (!set_fscontext)
888c9180a57SEric Paris 			newsbsec->sid = sid;
889c9180a57SEric Paris 		if (!set_rootcontext) {
890c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
891c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
892c9180a57SEric Paris 			newisec->sid = sid;
893c9180a57SEric Paris 		}
894c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
895c9180a57SEric Paris 	}
896c9180a57SEric Paris 	if (set_rootcontext) {
897c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
898c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
899c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
900c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
901c9180a57SEric Paris 
902c9180a57SEric Paris 		newisec->sid = oldisec->sid;
903c9180a57SEric Paris 	}
904c9180a57SEric Paris 
905c9180a57SEric Paris 	sb_finish_set_opts(newsb);
906c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
907094f7b69SJeff Layton 	return 0;
908c9180a57SEric Paris }
909c9180a57SEric Paris 
9102e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
9112e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
912c9180a57SEric Paris {
913e0007529SEric Paris 	char *p;
914c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
915c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
916e0007529SEric Paris 	int rc, num_mnt_opts = 0;
917c9180a57SEric Paris 
918e0007529SEric Paris 	opts->num_mnt_opts = 0;
919c9180a57SEric Paris 
920c9180a57SEric Paris 	/* Standard string-based options. */
921c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
922c9180a57SEric Paris 		int token;
923c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
924c9180a57SEric Paris 
925c9180a57SEric Paris 		if (!*p)
926c9180a57SEric Paris 			continue;
927c9180a57SEric Paris 
928c9180a57SEric Paris 		token = match_token(p, tokens, args);
929c9180a57SEric Paris 
930c9180a57SEric Paris 		switch (token) {
931c9180a57SEric Paris 		case Opt_context:
932c9180a57SEric Paris 			if (context || defcontext) {
933c9180a57SEric Paris 				rc = -EINVAL;
934c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
935c9180a57SEric Paris 				goto out_err;
936c9180a57SEric Paris 			}
937c9180a57SEric Paris 			context = match_strdup(&args[0]);
938c9180a57SEric Paris 			if (!context) {
939c9180a57SEric Paris 				rc = -ENOMEM;
940c9180a57SEric Paris 				goto out_err;
941c9180a57SEric Paris 			}
942c9180a57SEric Paris 			break;
943c9180a57SEric Paris 
944c9180a57SEric Paris 		case Opt_fscontext:
945c9180a57SEric Paris 			if (fscontext) {
946c9180a57SEric Paris 				rc = -EINVAL;
947c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
948c9180a57SEric Paris 				goto out_err;
949c9180a57SEric Paris 			}
950c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
951c9180a57SEric Paris 			if (!fscontext) {
952c9180a57SEric Paris 				rc = -ENOMEM;
953c9180a57SEric Paris 				goto out_err;
954c9180a57SEric Paris 			}
955c9180a57SEric Paris 			break;
956c9180a57SEric Paris 
957c9180a57SEric Paris 		case Opt_rootcontext:
958c9180a57SEric Paris 			if (rootcontext) {
959c9180a57SEric Paris 				rc = -EINVAL;
960c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
961c9180a57SEric Paris 				goto out_err;
962c9180a57SEric Paris 			}
963c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
964c9180a57SEric Paris 			if (!rootcontext) {
965c9180a57SEric Paris 				rc = -ENOMEM;
966c9180a57SEric Paris 				goto out_err;
967c9180a57SEric Paris 			}
968c9180a57SEric Paris 			break;
969c9180a57SEric Paris 
970c9180a57SEric Paris 		case Opt_defcontext:
971c9180a57SEric Paris 			if (context || defcontext) {
972c9180a57SEric Paris 				rc = -EINVAL;
973c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
974c9180a57SEric Paris 				goto out_err;
975c9180a57SEric Paris 			}
976c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
977c9180a57SEric Paris 			if (!defcontext) {
978c9180a57SEric Paris 				rc = -ENOMEM;
979c9180a57SEric Paris 				goto out_err;
980c9180a57SEric Paris 			}
981c9180a57SEric Paris 			break;
98211689d47SDavid P. Quigley 		case Opt_labelsupport:
98311689d47SDavid P. Quigley 			break;
984c9180a57SEric Paris 		default:
985c9180a57SEric Paris 			rc = -EINVAL;
986c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
987c9180a57SEric Paris 			goto out_err;
988c9180a57SEric Paris 
989c9180a57SEric Paris 		}
990c9180a57SEric Paris 	}
991c9180a57SEric Paris 
992e0007529SEric Paris 	rc = -ENOMEM;
993e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
994e0007529SEric Paris 	if (!opts->mnt_opts)
995e0007529SEric Paris 		goto out_err;
996e0007529SEric Paris 
997e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
998e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
999e0007529SEric Paris 		kfree(opts->mnt_opts);
1000e0007529SEric Paris 		goto out_err;
1001c9180a57SEric Paris 	}
1002c9180a57SEric Paris 
1003e0007529SEric Paris 	if (fscontext) {
1004e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
1005e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1006e0007529SEric Paris 	}
1007e0007529SEric Paris 	if (context) {
1008e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
1009e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1010e0007529SEric Paris 	}
1011e0007529SEric Paris 	if (rootcontext) {
1012e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
1013e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1014e0007529SEric Paris 	}
1015e0007529SEric Paris 	if (defcontext) {
1016e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
1017e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1018e0007529SEric Paris 	}
1019e0007529SEric Paris 
1020e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
1021e0007529SEric Paris 	return 0;
1022e0007529SEric Paris 
1023c9180a57SEric Paris out_err:
1024c9180a57SEric Paris 	kfree(context);
1025c9180a57SEric Paris 	kfree(defcontext);
1026c9180a57SEric Paris 	kfree(fscontext);
1027c9180a57SEric Paris 	kfree(rootcontext);
1028c9180a57SEric Paris 	return rc;
10291da177e4SLinus Torvalds }
1030e0007529SEric Paris /*
1031e0007529SEric Paris  * string mount options parsing and call set the sbsec
1032e0007529SEric Paris  */
1033e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
1034e0007529SEric Paris {
1035e0007529SEric Paris 	int rc = 0;
1036e0007529SEric Paris 	char *options = data;
1037e0007529SEric Paris 	struct security_mnt_opts opts;
1038e0007529SEric Paris 
1039e0007529SEric Paris 	security_init_mnt_opts(&opts);
1040e0007529SEric Paris 
1041e0007529SEric Paris 	if (!data)
1042e0007529SEric Paris 		goto out;
1043e0007529SEric Paris 
1044e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1045e0007529SEric Paris 
1046e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
1047e0007529SEric Paris 	if (rc)
1048e0007529SEric Paris 		goto out_err;
1049e0007529SEric Paris 
1050e0007529SEric Paris out:
1051649f6e77SDavid Quigley 	rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1052e0007529SEric Paris 
1053e0007529SEric Paris out_err:
1054e0007529SEric Paris 	security_free_mnt_opts(&opts);
1055e0007529SEric Paris 	return rc;
1056e0007529SEric Paris }
10571da177e4SLinus Torvalds 
10583583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m,
10593583a711SAdrian Bunk 			       struct security_mnt_opts *opts)
10602069f457SEric Paris {
10612069f457SEric Paris 	int i;
10622069f457SEric Paris 	char *prefix;
10632069f457SEric Paris 
10642069f457SEric Paris 	for (i = 0; i < opts->num_mnt_opts; i++) {
106511689d47SDavid P. Quigley 		char *has_comma;
106611689d47SDavid P. Quigley 
106711689d47SDavid P. Quigley 		if (opts->mnt_opts[i])
106811689d47SDavid P. Quigley 			has_comma = strchr(opts->mnt_opts[i], ',');
106911689d47SDavid P. Quigley 		else
107011689d47SDavid P. Quigley 			has_comma = NULL;
10712069f457SEric Paris 
10722069f457SEric Paris 		switch (opts->mnt_opts_flags[i]) {
10732069f457SEric Paris 		case CONTEXT_MNT:
10742069f457SEric Paris 			prefix = CONTEXT_STR;
10752069f457SEric Paris 			break;
10762069f457SEric Paris 		case FSCONTEXT_MNT:
10772069f457SEric Paris 			prefix = FSCONTEXT_STR;
10782069f457SEric Paris 			break;
10792069f457SEric Paris 		case ROOTCONTEXT_MNT:
10802069f457SEric Paris 			prefix = ROOTCONTEXT_STR;
10812069f457SEric Paris 			break;
10822069f457SEric Paris 		case DEFCONTEXT_MNT:
10832069f457SEric Paris 			prefix = DEFCONTEXT_STR;
10842069f457SEric Paris 			break;
108512f348b9SEric Paris 		case SBLABEL_MNT:
108611689d47SDavid P. Quigley 			seq_putc(m, ',');
108711689d47SDavid P. Quigley 			seq_puts(m, LABELSUPP_STR);
108811689d47SDavid P. Quigley 			continue;
10892069f457SEric Paris 		default:
10902069f457SEric Paris 			BUG();
1091a35c6c83SEric Paris 			return;
10922069f457SEric Paris 		};
10932069f457SEric Paris 		/* we need a comma before each option */
10942069f457SEric Paris 		seq_putc(m, ',');
10952069f457SEric Paris 		seq_puts(m, prefix);
10962069f457SEric Paris 		if (has_comma)
10972069f457SEric Paris 			seq_putc(m, '\"');
10982069f457SEric Paris 		seq_puts(m, opts->mnt_opts[i]);
10992069f457SEric Paris 		if (has_comma)
11002069f457SEric Paris 			seq_putc(m, '\"');
11012069f457SEric Paris 	}
11022069f457SEric Paris }
11032069f457SEric Paris 
11042069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
11052069f457SEric Paris {
11062069f457SEric Paris 	struct security_mnt_opts opts;
11072069f457SEric Paris 	int rc;
11082069f457SEric Paris 
11092069f457SEric Paris 	rc = selinux_get_mnt_opts(sb, &opts);
1110383795c2SEric Paris 	if (rc) {
1111383795c2SEric Paris 		/* before policy load we may get EINVAL, don't show anything */
1112383795c2SEric Paris 		if (rc == -EINVAL)
1113383795c2SEric Paris 			rc = 0;
11142069f457SEric Paris 		return rc;
1115383795c2SEric Paris 	}
11162069f457SEric Paris 
11172069f457SEric Paris 	selinux_write_opts(m, &opts);
11182069f457SEric Paris 
11192069f457SEric Paris 	security_free_mnt_opts(&opts);
11202069f457SEric Paris 
11212069f457SEric Paris 	return rc;
11222069f457SEric Paris }
11232069f457SEric Paris 
11241da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
11251da177e4SLinus Torvalds {
11261da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
11271da177e4SLinus Torvalds 	case S_IFSOCK:
11281da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
11291da177e4SLinus Torvalds 	case S_IFLNK:
11301da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
11311da177e4SLinus Torvalds 	case S_IFREG:
11321da177e4SLinus Torvalds 		return SECCLASS_FILE;
11331da177e4SLinus Torvalds 	case S_IFBLK:
11341da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
11351da177e4SLinus Torvalds 	case S_IFDIR:
11361da177e4SLinus Torvalds 		return SECCLASS_DIR;
11371da177e4SLinus Torvalds 	case S_IFCHR:
11381da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
11391da177e4SLinus Torvalds 	case S_IFIFO:
11401da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	return SECCLASS_FILE;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
114713402580SJames Morris static inline int default_protocol_stream(int protocol)
114813402580SJames Morris {
114913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
115013402580SJames Morris }
115113402580SJames Morris 
115213402580SJames Morris static inline int default_protocol_dgram(int protocol)
115313402580SJames Morris {
115413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
115513402580SJames Morris }
115613402580SJames Morris 
11571da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	switch (family) {
11601da177e4SLinus Torvalds 	case PF_UNIX:
11611da177e4SLinus Torvalds 		switch (type) {
11621da177e4SLinus Torvalds 		case SOCK_STREAM:
11631da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
11641da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
11651da177e4SLinus Torvalds 		case SOCK_DGRAM:
11661da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
11671da177e4SLinus Torvalds 		}
11681da177e4SLinus Torvalds 		break;
11691da177e4SLinus Torvalds 	case PF_INET:
11701da177e4SLinus Torvalds 	case PF_INET6:
11711da177e4SLinus Torvalds 		switch (type) {
11721da177e4SLinus Torvalds 		case SOCK_STREAM:
117313402580SJames Morris 			if (default_protocol_stream(protocol))
11741da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
117513402580SJames Morris 			else
117613402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11771da177e4SLinus Torvalds 		case SOCK_DGRAM:
117813402580SJames Morris 			if (default_protocol_dgram(protocol))
11791da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
118013402580SJames Morris 			else
118113402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
11822ee92d46SJames Morris 		case SOCK_DCCP:
11832ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
118413402580SJames Morris 		default:
11851da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
11861da177e4SLinus Torvalds 		}
11871da177e4SLinus Torvalds 		break;
11881da177e4SLinus Torvalds 	case PF_NETLINK:
11891da177e4SLinus Torvalds 		switch (protocol) {
11901da177e4SLinus Torvalds 		case NETLINK_ROUTE:
11911da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
11921da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
11931da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
11947f1fb60cSPavel Emelyanov 		case NETLINK_SOCK_DIAG:
11951da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
11961da177e4SLinus Torvalds 		case NETLINK_NFLOG:
11971da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
11981da177e4SLinus Torvalds 		case NETLINK_XFRM:
11991da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
12001da177e4SLinus Torvalds 		case NETLINK_SELINUX:
12011da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
12021da177e4SLinus Torvalds 		case NETLINK_AUDIT:
12031da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
12041da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
12051da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
12061da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
12071da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
12080c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
12090c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
12101da177e4SLinus Torvalds 		default:
12111da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
12121da177e4SLinus Torvalds 		}
12131da177e4SLinus Torvalds 	case PF_PACKET:
12141da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
12151da177e4SLinus Torvalds 	case PF_KEY:
12161da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
12173e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
12183e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12258e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12261da177e4SLinus Torvalds 				u16 tclass,
12271da177e4SLinus Torvalds 				u32 *sid)
12281da177e4SLinus Torvalds {
12298e6c9693SLucian Adrian Grijincu 	int rc;
12308e6c9693SLucian Adrian Grijincu 	char *buffer, *path;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	buffer = (char *)__get_free_page(GFP_KERNEL);
12331da177e4SLinus Torvalds 	if (!buffer)
12341da177e4SLinus Torvalds 		return -ENOMEM;
12351da177e4SLinus Torvalds 
12368e6c9693SLucian Adrian Grijincu 	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
12378e6c9693SLucian Adrian Grijincu 	if (IS_ERR(path))
12388e6c9693SLucian Adrian Grijincu 		rc = PTR_ERR(path);
12398e6c9693SLucian Adrian Grijincu 	else {
12408e6c9693SLucian Adrian Grijincu 		/* each process gets a /proc/PID/ entry. Strip off the
12418e6c9693SLucian Adrian Grijincu 		 * PID part to get a valid selinux labeling.
12428e6c9693SLucian Adrian Grijincu 		 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
12438e6c9693SLucian Adrian Grijincu 		while (path[1] >= '0' && path[1] <= '9') {
12448e6c9693SLucian Adrian Grijincu 			path[1] = '/';
12458e6c9693SLucian Adrian Grijincu 			path++;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 		rc = security_genfs_sid("proc", path, tclass, sid);
12488e6c9693SLucian Adrian Grijincu 	}
12491da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
12501da177e4SLinus Torvalds 	return rc;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds #else
12538e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry,
12541da177e4SLinus Torvalds 				u16 tclass,
12551da177e4SLinus Torvalds 				u32 *sid)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	return -EINVAL;
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds #endif
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
12621da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
12651da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12661da177e4SLinus Torvalds 	u32 sid;
12671da177e4SLinus Torvalds 	struct dentry *dentry;
12681da177e4SLinus Torvalds #define INITCONTEXTLEN 255
12691da177e4SLinus Torvalds 	char *context = NULL;
12701da177e4SLinus Torvalds 	unsigned len = 0;
12711da177e4SLinus Torvalds 	int rc = 0;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	if (isec->initialized)
12741da177e4SLinus Torvalds 		goto out;
12751da177e4SLinus Torvalds 
127623970741SEric Paris 	mutex_lock(&isec->lock);
12771da177e4SLinus Torvalds 	if (isec->initialized)
127823970741SEric Paris 		goto out_unlock;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
12810d90a7ecSDavid P. Quigley 	if (!(sbsec->flags & SE_SBINITIALIZED)) {
12821da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
12831da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
12841da177e4SLinus Torvalds 		   server is ready to handle calls. */
12851da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
12861da177e4SLinus Torvalds 		if (list_empty(&isec->list))
12871da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
12881da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
128923970741SEric Paris 		goto out_unlock;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	switch (sbsec->behavior) {
1293eb9ae686SDavid Quigley 	case SECURITY_FS_USE_NATIVE:
1294eb9ae686SDavid Quigley 		break;
12951da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
12961da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
12971da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
12981da177e4SLinus Torvalds 			break;
12991da177e4SLinus Torvalds 		}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
13021da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
13031da177e4SLinus Torvalds 		if (opt_dentry) {
13041da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
13051da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
13061da177e4SLinus Torvalds 		} else {
13071da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
13081da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 		if (!dentry) {
1311df7f54c0SEric Paris 			/*
1312df7f54c0SEric Paris 			 * this is can be hit on boot when a file is accessed
1313df7f54c0SEric Paris 			 * before the policy is loaded.  When we load policy we
1314df7f54c0SEric Paris 			 * may find inodes that have no dentry on the
1315df7f54c0SEric Paris 			 * sbsec->isec_head list.  No reason to complain as these
1316df7f54c0SEric Paris 			 * will get fixed up the next time we go through
1317df7f54c0SEric Paris 			 * inode_doinit with a dentry, before these inodes could
1318df7f54c0SEric Paris 			 * be used again by userspace.
1319df7f54c0SEric Paris 			 */
132023970741SEric Paris 			goto out_unlock;
13211da177e4SLinus Torvalds 		}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
13244cb912f1SEric Paris 		context = kmalloc(len+1, GFP_NOFS);
13251da177e4SLinus Torvalds 		if (!context) {
13261da177e4SLinus Torvalds 			rc = -ENOMEM;
13271da177e4SLinus Torvalds 			dput(dentry);
132823970741SEric Paris 			goto out_unlock;
13291da177e4SLinus Torvalds 		}
13304cb912f1SEric Paris 		context[len] = '\0';
13311da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13321da177e4SLinus Torvalds 					   context, len);
13331da177e4SLinus Torvalds 		if (rc == -ERANGE) {
1334314dabb8SJames Morris 			kfree(context);
1335314dabb8SJames Morris 
13361da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
13371da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
13381da177e4SLinus Torvalds 						   NULL, 0);
13391da177e4SLinus Torvalds 			if (rc < 0) {
13401da177e4SLinus Torvalds 				dput(dentry);
134123970741SEric Paris 				goto out_unlock;
13421da177e4SLinus Torvalds 			}
13431da177e4SLinus Torvalds 			len = rc;
13444cb912f1SEric Paris 			context = kmalloc(len+1, GFP_NOFS);
13451da177e4SLinus Torvalds 			if (!context) {
13461da177e4SLinus Torvalds 				rc = -ENOMEM;
13471da177e4SLinus Torvalds 				dput(dentry);
134823970741SEric Paris 				goto out_unlock;
13491da177e4SLinus Torvalds 			}
13504cb912f1SEric Paris 			context[len] = '\0';
13511da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
13521da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
13531da177e4SLinus Torvalds 						   context, len);
13541da177e4SLinus Torvalds 		}
13551da177e4SLinus Torvalds 		dput(dentry);
13561da177e4SLinus Torvalds 		if (rc < 0) {
13571da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1358744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1359dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
13601da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
13611da177e4SLinus Torvalds 				kfree(context);
136223970741SEric Paris 				goto out_unlock;
13631da177e4SLinus Torvalds 			}
13641da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
13651da177e4SLinus Torvalds 			sid = sbsec->def_sid;
13661da177e4SLinus Torvalds 			rc = 0;
13671da177e4SLinus Torvalds 		} else {
1368f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1369869ab514SStephen Smalley 							     sbsec->def_sid,
1370869ab514SStephen Smalley 							     GFP_NOFS);
13711da177e4SLinus Torvalds 			if (rc) {
13724ba0a8adSEric Paris 				char *dev = inode->i_sb->s_id;
13734ba0a8adSEric Paris 				unsigned long ino = inode->i_ino;
13744ba0a8adSEric Paris 
13754ba0a8adSEric Paris 				if (rc == -EINVAL) {
13764ba0a8adSEric Paris 					if (printk_ratelimit())
13774ba0a8adSEric Paris 						printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
13784ba0a8adSEric Paris 							"context=%s.  This indicates you may need to relabel the inode or the "
13794ba0a8adSEric Paris 							"filesystem in question.\n", ino, dev, context);
13804ba0a8adSEric Paris 				} else {
1381744ba35eSEric Paris 					printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
13821da177e4SLinus Torvalds 					       "returned %d for dev=%s ino=%ld\n",
13834ba0a8adSEric Paris 					       __func__, context, -rc, dev, ino);
13844ba0a8adSEric Paris 				}
13851da177e4SLinus Torvalds 				kfree(context);
13861da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
13871da177e4SLinus Torvalds 				rc = 0;
13881da177e4SLinus Torvalds 				break;
13891da177e4SLinus Torvalds 			}
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 		kfree(context);
13921da177e4SLinus Torvalds 		isec->sid = sid;
13931da177e4SLinus Torvalds 		break;
13941da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
13951da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
13961da177e4SLinus Torvalds 		break;
13971da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
13981da177e4SLinus Torvalds 		/* Default to the fs SID. */
13991da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
14021da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
1403652bb9b0SEric Paris 		rc = security_transition_sid(isec->task_sid, sbsec->sid,
1404652bb9b0SEric Paris 					     isec->sclass, NULL, &sid);
14051da177e4SLinus Torvalds 		if (rc)
140623970741SEric Paris 			goto out_unlock;
14071da177e4SLinus Torvalds 		isec->sid = sid;
14081da177e4SLinus Torvalds 		break;
1409c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1410c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1411c312feb2SEric Paris 		break;
14121da177e4SLinus Torvalds 	default:
1413c312feb2SEric Paris 		/* Default to the fs superblock SID. */
14141da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
14151da177e4SLinus Torvalds 
14160d90a7ecSDavid P. Quigley 		if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
1417f64410ecSPaul Moore 			/* We must have a dentry to determine the label on
1418f64410ecSPaul Moore 			 * procfs inodes */
1419f64410ecSPaul Moore 			if (opt_dentry)
1420f64410ecSPaul Moore 				/* Called from d_instantiate or
1421f64410ecSPaul Moore 				 * d_splice_alias. */
1422f64410ecSPaul Moore 				dentry = dget(opt_dentry);
1423f64410ecSPaul Moore 			else
1424f64410ecSPaul Moore 				/* Called from selinux_complete_init, try to
1425f64410ecSPaul Moore 				 * find a dentry. */
1426f64410ecSPaul Moore 				dentry = d_find_alias(inode);
1427f64410ecSPaul Moore 			/*
1428f64410ecSPaul Moore 			 * This can be hit on boot when a file is accessed
1429f64410ecSPaul Moore 			 * before the policy is loaded.  When we load policy we
1430f64410ecSPaul Moore 			 * may find inodes that have no dentry on the
1431f64410ecSPaul Moore 			 * sbsec->isec_head list.  No reason to complain as
1432f64410ecSPaul Moore 			 * these will get fixed up the next time we go through
1433f64410ecSPaul Moore 			 * inode_doinit() with a dentry, before these inodes
1434f64410ecSPaul Moore 			 * could be used again by userspace.
1435f64410ecSPaul Moore 			 */
1436f64410ecSPaul Moore 			if (!dentry)
1437f64410ecSPaul Moore 				goto out_unlock;
14381da177e4SLinus Torvalds 			isec->sclass = inode_mode_to_security_class(inode->i_mode);
1439f64410ecSPaul Moore 			rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
1440f64410ecSPaul Moore 			dput(dentry);
14411da177e4SLinus Torvalds 			if (rc)
144223970741SEric Paris 				goto out_unlock;
14431da177e4SLinus Torvalds 			isec->sid = sid;
14441da177e4SLinus Torvalds 		}
14451da177e4SLinus Torvalds 		break;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	isec->initialized = 1;
14491da177e4SLinus Torvalds 
145023970741SEric Paris out_unlock:
145123970741SEric Paris 	mutex_unlock(&isec->lock);
14521da177e4SLinus Torvalds out:
14531da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
14541da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
14551da177e4SLinus Torvalds 	return rc;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
14591da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds 	u32 perm = 0;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	switch (sig) {
14641da177e4SLinus Torvalds 	case SIGCHLD:
14651da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
14661da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
14671da177e4SLinus Torvalds 		break;
14681da177e4SLinus Torvalds 	case SIGKILL:
14691da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14701da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
14711da177e4SLinus Torvalds 		break;
14721da177e4SLinus Torvalds 	case SIGSTOP:
14731da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
14741da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
14751da177e4SLinus Torvalds 		break;
14761da177e4SLinus Torvalds 	default:
14771da177e4SLinus Torvalds 		/* All other signals. */
14781da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
14791da177e4SLinus Torvalds 		break;
14801da177e4SLinus Torvalds 	}
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	return perm;
14831da177e4SLinus Torvalds }
14841da177e4SLinus Torvalds 
1485275bb41eSDavid Howells /*
1486d84f4f99SDavid Howells  * Check permission between a pair of credentials
1487d84f4f99SDavid Howells  * fork check, ptrace check, etc.
1488d84f4f99SDavid Howells  */
1489d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor,
1490d84f4f99SDavid Howells 			 const struct cred *target,
1491d84f4f99SDavid Howells 			 u32 perms)
1492d84f4f99SDavid Howells {
1493d84f4f99SDavid Howells 	u32 asid = cred_sid(actor), tsid = cred_sid(target);
1494d84f4f99SDavid Howells 
1495d84f4f99SDavid Howells 	return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1496d84f4f99SDavid Howells }
1497d84f4f99SDavid Howells 
1498d84f4f99SDavid Howells /*
149988e67f3bSDavid Howells  * Check permission between a pair of tasks, e.g. signal checks,
1500275bb41eSDavid Howells  * fork check, ptrace check, etc.
1501275bb41eSDavid Howells  * tsk1 is the actor and tsk2 is the target
15023b11a1deSDavid Howells  * - this uses the default subjective creds of tsk1
1503275bb41eSDavid Howells  */
1504275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1,
1505275bb41eSDavid Howells 			 const struct task_struct *tsk2,
15061da177e4SLinus Torvalds 			 u32 perms)
15071da177e4SLinus Torvalds {
1508275bb41eSDavid Howells 	const struct task_security_struct *__tsec1, *__tsec2;
1509275bb41eSDavid Howells 	u32 sid1, sid2;
15101da177e4SLinus Torvalds 
1511275bb41eSDavid Howells 	rcu_read_lock();
1512275bb41eSDavid Howells 	__tsec1 = __task_cred(tsk1)->security;	sid1 = __tsec1->sid;
1513275bb41eSDavid Howells 	__tsec2 = __task_cred(tsk2)->security;	sid2 = __tsec2->sid;
1514275bb41eSDavid Howells 	rcu_read_unlock();
1515275bb41eSDavid Howells 	return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
15183b11a1deSDavid Howells /*
15193b11a1deSDavid Howells  * Check permission between current and another task, e.g. signal checks,
15203b11a1deSDavid Howells  * fork check, ptrace check, etc.
15213b11a1deSDavid Howells  * current is the actor and tsk2 is the target
15223b11a1deSDavid Howells  * - this uses current's subjective creds
15233b11a1deSDavid Howells  */
15243b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk,
15253b11a1deSDavid Howells 			    u32 perms)
15263b11a1deSDavid Howells {
15273b11a1deSDavid Howells 	u32 sid, tsid;
15283b11a1deSDavid Howells 
15293b11a1deSDavid Howells 	sid = current_sid();
15303b11a1deSDavid Howells 	tsid = task_sid(tsk);
15313b11a1deSDavid Howells 	return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
15323b11a1deSDavid Howells }
15333b11a1deSDavid Howells 
1534b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1535b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1536b68e418cSStephen Smalley #endif
1537b68e418cSStephen Smalley 
15381da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
15396a9de491SEric Paris static int cred_has_capability(const struct cred *cred,
154006112163SEric Paris 			       int cap, int audit)
15411da177e4SLinus Torvalds {
15422bf49690SThomas Liu 	struct common_audit_data ad;
154306112163SEric Paris 	struct av_decision avd;
1544b68e418cSStephen Smalley 	u16 sclass;
15453699c53cSDavid Howells 	u32 sid = cred_sid(cred);
1546b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
154706112163SEric Paris 	int rc;
15481da177e4SLinus Torvalds 
154950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_CAP;
15501da177e4SLinus Torvalds 	ad.u.cap = cap;
15511da177e4SLinus Torvalds 
1552b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1553b68e418cSStephen Smalley 	case 0:
1554b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1555b68e418cSStephen Smalley 		break;
1556b68e418cSStephen Smalley 	case 1:
1557b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1558b68e418cSStephen Smalley 		break;
1559b68e418cSStephen Smalley 	default:
1560b68e418cSStephen Smalley 		printk(KERN_ERR
1561b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1562b68e418cSStephen Smalley 		BUG();
1563a35c6c83SEric Paris 		return -EINVAL;
1564b68e418cSStephen Smalley 	}
156506112163SEric Paris 
1566275bb41eSDavid Howells 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
15679ade0cf4SEric Paris 	if (audit == SECURITY_CAP_AUDIT) {
1568ab354062SLinus Torvalds 		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
15699ade0cf4SEric Paris 		if (rc2)
15709ade0cf4SEric Paris 			return rc2;
15719ade0cf4SEric Paris 	}
157206112163SEric Paris 	return rc;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
15761da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
15771da177e4SLinus Torvalds 			   u32 perms)
15781da177e4SLinus Torvalds {
1579275bb41eSDavid Howells 	u32 sid = task_sid(tsk);
15801da177e4SLinus Torvalds 
1581275bb41eSDavid Howells 	return avc_has_perm(sid, SECINITSID_KERNEL,
15821da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
15861da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
15871da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
158888e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred,
15891da177e4SLinus Torvalds 			  struct inode *inode,
15901da177e4SLinus Torvalds 			  u32 perms,
159119e49834SLinus Torvalds 			  struct common_audit_data *adp)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1594275bb41eSDavid Howells 	u32 sid;
15951da177e4SLinus Torvalds 
1596e0e81739SDavid Howells 	validate_creds(cred);
1597e0e81739SDavid Howells 
1598bbaca6c2SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
1599bbaca6c2SStephen Smalley 		return 0;
1600bbaca6c2SStephen Smalley 
160188e67f3bSDavid Howells 	sid = cred_sid(cred);
16021da177e4SLinus Torvalds 	isec = inode->i_security;
16031da177e4SLinus Torvalds 
160419e49834SLinus Torvalds 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
16081da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
16091da177e4SLinus Torvalds    pathname if needed. */
161088e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred,
16111da177e4SLinus Torvalds 				  struct dentry *dentry,
16121da177e4SLinus Torvalds 				  u32 av)
16131da177e4SLinus Torvalds {
16141da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16152bf49690SThomas Liu 	struct common_audit_data ad;
161688e67f3bSDavid Howells 
161750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
16182875fa00SEric Paris 	ad.u.dentry = dentry;
161919e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16202875fa00SEric Paris }
16212875fa00SEric Paris 
16222875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing
16232875fa00SEric Paris    the path to help the auditing code to more easily generate the
16242875fa00SEric Paris    pathname if needed. */
16252875fa00SEric Paris static inline int path_has_perm(const struct cred *cred,
1626*3f7036a0SAl Viro 				const struct path *path,
16272875fa00SEric Paris 				u32 av)
16282875fa00SEric Paris {
16292875fa00SEric Paris 	struct inode *inode = path->dentry->d_inode;
16302875fa00SEric Paris 	struct common_audit_data ad;
16312875fa00SEric Paris 
163250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
16332875fa00SEric Paris 	ad.u.path = *path;
163419e49834SLinus Torvalds 	return inode_has_perm(cred, inode, av, &ad);
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
163713f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */
163813f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred,
163913f8e981SDavid Howells 				     struct file *file,
164013f8e981SDavid Howells 				     u32 av)
164113f8e981SDavid Howells {
164213f8e981SDavid Howells 	struct common_audit_data ad;
164313f8e981SDavid Howells 
164413f8e981SDavid Howells 	ad.type = LSM_AUDIT_DATA_PATH;
164513f8e981SDavid Howells 	ad.u.path = file->f_path;
164619e49834SLinus Torvalds 	return inode_has_perm(cred, file_inode(file), av, &ad);
164713f8e981SDavid Howells }
164813f8e981SDavid Howells 
16491da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
16501da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
16511da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
16521da177e4SLinus Torvalds    check a particular permission to the file.
16531da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
16541da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
16551da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
16561da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
165788e67f3bSDavid Howells static int file_has_perm(const struct cred *cred,
16581da177e4SLinus Torvalds 			 struct file *file,
16591da177e4SLinus Torvalds 			 u32 av)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
1662496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16632bf49690SThomas Liu 	struct common_audit_data ad;
166488e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
16651da177e4SLinus Torvalds 	int rc;
16661da177e4SLinus Torvalds 
166750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
1668f48b7399SEric Paris 	ad.u.path = file->f_path;
16691da177e4SLinus Torvalds 
1670275bb41eSDavid Howells 	if (sid != fsec->sid) {
1671275bb41eSDavid Howells 		rc = avc_has_perm(sid, fsec->sid,
16721da177e4SLinus Torvalds 				  SECCLASS_FD,
16731da177e4SLinus Torvalds 				  FD__USE,
16741da177e4SLinus Torvalds 				  &ad);
16751da177e4SLinus Torvalds 		if (rc)
167688e67f3bSDavid Howells 			goto out;
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
168088e67f3bSDavid Howells 	rc = 0;
16811da177e4SLinus Torvalds 	if (av)
168219e49834SLinus Torvalds 		rc = inode_has_perm(cred, inode, av, &ad);
16831da177e4SLinus Torvalds 
168488e67f3bSDavid Howells out:
168588e67f3bSDavid Howells 	return rc;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds /* Check whether a task can create a file. */
16891da177e4SLinus Torvalds static int may_create(struct inode *dir,
16901da177e4SLinus Torvalds 		      struct dentry *dentry,
16911da177e4SLinus Torvalds 		      u16 tclass)
16921da177e4SLinus Torvalds {
16935fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
16941da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
16951da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
1696275bb41eSDavid Howells 	u32 sid, newsid;
16972bf49690SThomas Liu 	struct common_audit_data ad;
16981da177e4SLinus Torvalds 	int rc;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	dsec = dir->i_security;
17011da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
17021da177e4SLinus Torvalds 
1703275bb41eSDavid Howells 	sid = tsec->sid;
1704275bb41eSDavid Howells 	newsid = tsec->create_sid;
1705275bb41eSDavid Howells 
170650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1707a269434dSEric Paris 	ad.u.dentry = dentry;
17081da177e4SLinus Torvalds 
1709275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
17101da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
17111da177e4SLinus Torvalds 			  &ad);
17121da177e4SLinus Torvalds 	if (rc)
17131da177e4SLinus Torvalds 		return rc;
17141da177e4SLinus Torvalds 
171512f348b9SEric Paris 	if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
1716cb1e922fSEric Paris 		rc = security_transition_sid(sid, dsec->sid, tclass,
1717cb1e922fSEric Paris 					     &dentry->d_name, &newsid);
17181da177e4SLinus Torvalds 		if (rc)
17191da177e4SLinus Torvalds 			return rc;
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 
1722275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
17231da177e4SLinus Torvalds 	if (rc)
17241da177e4SLinus Torvalds 		return rc;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
17271da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
17281da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
17314eb582cfSMichael LeMay /* Check whether a task can create a key. */
17324eb582cfSMichael LeMay static int may_create_key(u32 ksid,
17334eb582cfSMichael LeMay 			  struct task_struct *ctx)
17344eb582cfSMichael LeMay {
1735275bb41eSDavid Howells 	u32 sid = task_sid(ctx);
17364eb582cfSMichael LeMay 
1737275bb41eSDavid Howells 	return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
17384eb582cfSMichael LeMay }
17394eb582cfSMichael LeMay 
17401da177e4SLinus Torvalds #define MAY_LINK	0
17411da177e4SLinus Torvalds #define MAY_UNLINK	1
17421da177e4SLinus Torvalds #define MAY_RMDIR	2
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
17451da177e4SLinus Torvalds static int may_link(struct inode *dir,
17461da177e4SLinus Torvalds 		    struct dentry *dentry,
17471da177e4SLinus Torvalds 		    int kind)
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds {
17501da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
17512bf49690SThomas Liu 	struct common_audit_data ad;
1752275bb41eSDavid Howells 	u32 sid = current_sid();
17531da177e4SLinus Torvalds 	u32 av;
17541da177e4SLinus Torvalds 	int rc;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	dsec = dir->i_security;
17571da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
17581da177e4SLinus Torvalds 
175950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
1760a269434dSEric Paris 	ad.u.dentry = dentry;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	av = DIR__SEARCH;
17631da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1764275bb41eSDavid Howells 	rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
17651da177e4SLinus Torvalds 	if (rc)
17661da177e4SLinus Torvalds 		return rc;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	switch (kind) {
17691da177e4SLinus Torvalds 	case MAY_LINK:
17701da177e4SLinus Torvalds 		av = FILE__LINK;
17711da177e4SLinus Torvalds 		break;
17721da177e4SLinus Torvalds 	case MAY_UNLINK:
17731da177e4SLinus Torvalds 		av = FILE__UNLINK;
17741da177e4SLinus Torvalds 		break;
17751da177e4SLinus Torvalds 	case MAY_RMDIR:
17761da177e4SLinus Torvalds 		av = DIR__RMDIR;
17771da177e4SLinus Torvalds 		break;
17781da177e4SLinus Torvalds 	default:
1779744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1780744ba35eSEric Paris 			__func__, kind);
17811da177e4SLinus Torvalds 		return 0;
17821da177e4SLinus Torvalds 	}
17831da177e4SLinus Torvalds 
1784275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
17851da177e4SLinus Torvalds 	return rc;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
17891da177e4SLinus Torvalds 			     struct dentry *old_dentry,
17901da177e4SLinus Torvalds 			     struct inode *new_dir,
17911da177e4SLinus Torvalds 			     struct dentry *new_dentry)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
17942bf49690SThomas Liu 	struct common_audit_data ad;
1795275bb41eSDavid Howells 	u32 sid = current_sid();
17961da177e4SLinus Torvalds 	u32 av;
17971da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
17981da177e4SLinus Torvalds 	int rc;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
18011da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
1802e36cb0b8SDavid Howells 	old_is_dir = d_is_dir(old_dentry);
18031da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
18041da177e4SLinus Torvalds 
180550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
18061da177e4SLinus Torvalds 
1807a269434dSEric Paris 	ad.u.dentry = old_dentry;
1808275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
18091da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
18101da177e4SLinus Torvalds 	if (rc)
18111da177e4SLinus Torvalds 		return rc;
1812275bb41eSDavid Howells 	rc = avc_has_perm(sid, old_isec->sid,
18131da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
18141da177e4SLinus Torvalds 	if (rc)
18151da177e4SLinus Torvalds 		return rc;
18161da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
1817275bb41eSDavid Howells 		rc = avc_has_perm(sid, old_isec->sid,
18181da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
18191da177e4SLinus Torvalds 		if (rc)
18201da177e4SLinus Torvalds 			return rc;
18211da177e4SLinus Torvalds 	}
18221da177e4SLinus Torvalds 
1823a269434dSEric Paris 	ad.u.dentry = new_dentry;
18241da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
18252c616d4dSDavid Howells 	if (d_is_positive(new_dentry))
18261da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
1827275bb41eSDavid Howells 	rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
18281da177e4SLinus Torvalds 	if (rc)
18291da177e4SLinus Torvalds 		return rc;
18302c616d4dSDavid Howells 	if (d_is_positive(new_dentry)) {
18311da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
1832e36cb0b8SDavid Howells 		new_is_dir = d_is_dir(new_dentry);
1833275bb41eSDavid Howells 		rc = avc_has_perm(sid, new_isec->sid,
18341da177e4SLinus Torvalds 				  new_isec->sclass,
18351da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
18361da177e4SLinus Torvalds 		if (rc)
18371da177e4SLinus Torvalds 			return rc;
18381da177e4SLinus Torvalds 	}
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	return 0;
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
184488e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred,
18451da177e4SLinus Torvalds 			       struct super_block *sb,
18461da177e4SLinus Torvalds 			       u32 perms,
18472bf49690SThomas Liu 			       struct common_audit_data *ad)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
185088e67f3bSDavid Howells 	u32 sid = cred_sid(cred);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	sbsec = sb->s_security;
1853275bb41eSDavid Howells 	return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
18571da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	u32 av = 0;
18601da177e4SLinus Torvalds 
1861dba19c60SAl Viro 	if (!S_ISDIR(mode)) {
18621da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18631da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
18641da177e4SLinus Torvalds 		if (mask & MAY_READ)
18651da177e4SLinus Torvalds 			av |= FILE__READ;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
18681da177e4SLinus Torvalds 			av |= FILE__APPEND;
18691da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
18701da177e4SLinus Torvalds 			av |= FILE__WRITE;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	} else {
18731da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
18741da177e4SLinus Torvalds 			av |= DIR__SEARCH;
18751da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
18761da177e4SLinus Torvalds 			av |= DIR__WRITE;
18771da177e4SLinus Torvalds 		if (mask & MAY_READ)
18781da177e4SLinus Torvalds 			av |= DIR__READ;
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	return av;
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
18851da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
18861da177e4SLinus Torvalds {
18871da177e4SLinus Torvalds 	u32 av = 0;
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
18901da177e4SLinus Torvalds 		av |= FILE__READ;
18911da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
18921da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
18931da177e4SLinus Torvalds 			av |= FILE__APPEND;
18941da177e4SLinus Torvalds 		else
18951da177e4SLinus Torvalds 			av |= FILE__WRITE;
18961da177e4SLinus Torvalds 	}
18970794c66dSStephen Smalley 	if (!av) {
18980794c66dSStephen Smalley 		/*
18990794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
19000794c66dSStephen Smalley 		 */
19010794c66dSStephen Smalley 		av = FILE__IOCTL;
19020794c66dSStephen Smalley 	}
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	return av;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19078b6a5a37SEric Paris /*
19088b6a5a37SEric Paris  * Convert a file to an access vector and include the correct open
19098b6a5a37SEric Paris  * open permission.
19108b6a5a37SEric Paris  */
19118b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file)
19128b6a5a37SEric Paris {
19138b6a5a37SEric Paris 	u32 av = file_to_av(file);
19148b6a5a37SEric Paris 
191549b7b8deSEric Paris 	if (selinux_policycap_openperm)
19168b6a5a37SEric Paris 		av |= FILE__OPEN;
191749b7b8deSEric Paris 
19188b6a5a37SEric Paris 	return av;
19198b6a5a37SEric Paris }
19208b6a5a37SEric Paris 
19211da177e4SLinus Torvalds /* Hook functions begin here. */
19221da177e4SLinus Torvalds 
192379af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr)
192479af7307SStephen Smalley {
192579af7307SStephen Smalley 	u32 mysid = current_sid();
192679af7307SStephen Smalley 	u32 mgrsid = task_sid(mgr);
192779af7307SStephen Smalley 
192879af7307SStephen Smalley 	return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER,
192979af7307SStephen Smalley 			    BINDER__SET_CONTEXT_MGR, NULL);
193079af7307SStephen Smalley }
193179af7307SStephen Smalley 
193279af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from,
193379af7307SStephen Smalley 				      struct task_struct *to)
193479af7307SStephen Smalley {
193579af7307SStephen Smalley 	u32 mysid = current_sid();
193679af7307SStephen Smalley 	u32 fromsid = task_sid(from);
193779af7307SStephen Smalley 	u32 tosid = task_sid(to);
193879af7307SStephen Smalley 	int rc;
193979af7307SStephen Smalley 
194079af7307SStephen Smalley 	if (mysid != fromsid) {
194179af7307SStephen Smalley 		rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
194279af7307SStephen Smalley 				  BINDER__IMPERSONATE, NULL);
194379af7307SStephen Smalley 		if (rc)
194479af7307SStephen Smalley 			return rc;
194579af7307SStephen Smalley 	}
194679af7307SStephen Smalley 
194779af7307SStephen Smalley 	return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
194879af7307SStephen Smalley 			    NULL);
194979af7307SStephen Smalley }
195079af7307SStephen Smalley 
195179af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from,
195279af7307SStephen Smalley 					  struct task_struct *to)
195379af7307SStephen Smalley {
195479af7307SStephen Smalley 	u32 fromsid = task_sid(from);
195579af7307SStephen Smalley 	u32 tosid = task_sid(to);
195679af7307SStephen Smalley 
195779af7307SStephen Smalley 	return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
195879af7307SStephen Smalley 			    NULL);
195979af7307SStephen Smalley }
196079af7307SStephen Smalley 
196179af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from,
196279af7307SStephen Smalley 					struct task_struct *to,
196379af7307SStephen Smalley 					struct file *file)
196479af7307SStephen Smalley {
196579af7307SStephen Smalley 	u32 sid = task_sid(to);
196679af7307SStephen Smalley 	struct file_security_struct *fsec = file->f_security;
196779af7307SStephen Smalley 	struct inode *inode = file->f_path.dentry->d_inode;
196879af7307SStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
196979af7307SStephen Smalley 	struct common_audit_data ad;
197079af7307SStephen Smalley 	int rc;
197179af7307SStephen Smalley 
197279af7307SStephen Smalley 	ad.type = LSM_AUDIT_DATA_PATH;
197379af7307SStephen Smalley 	ad.u.path = file->f_path;
197479af7307SStephen Smalley 
197579af7307SStephen Smalley 	if (sid != fsec->sid) {
197679af7307SStephen Smalley 		rc = avc_has_perm(sid, fsec->sid,
197779af7307SStephen Smalley 				  SECCLASS_FD,
197879af7307SStephen Smalley 				  FD__USE,
197979af7307SStephen Smalley 				  &ad);
198079af7307SStephen Smalley 		if (rc)
198179af7307SStephen Smalley 			return rc;
198279af7307SStephen Smalley 	}
198379af7307SStephen Smalley 
198479af7307SStephen Smalley 	if (unlikely(IS_PRIVATE(inode)))
198579af7307SStephen Smalley 		return 0;
198679af7307SStephen Smalley 
198779af7307SStephen Smalley 	return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
198879af7307SStephen Smalley 			    &ad);
198979af7307SStephen Smalley }
199079af7307SStephen Smalley 
19919e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child,
1992006ebb40SStephen Smalley 				     unsigned int mode)
19931da177e4SLinus Torvalds {
19941da177e4SLinus Torvalds 	int rc;
19951da177e4SLinus Torvalds 
19969e48858fSIngo Molnar 	rc = cap_ptrace_access_check(child, mode);
19971da177e4SLinus Torvalds 	if (rc)
19981da177e4SLinus Torvalds 		return rc;
19991da177e4SLinus Torvalds 
200069f594a3SEric Paris 	if (mode & PTRACE_MODE_READ) {
2001275bb41eSDavid Howells 		u32 sid = current_sid();
2002275bb41eSDavid Howells 		u32 csid = task_sid(child);
2003275bb41eSDavid Howells 		return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2004006ebb40SStephen Smalley 	}
2005006ebb40SStephen Smalley 
20063b11a1deSDavid Howells 	return current_has_perm(child, PROCESS__PTRACE);
20075cd9c58fSDavid Howells }
20085cd9c58fSDavid Howells 
20095cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent)
20105cd9c58fSDavid Howells {
20115cd9c58fSDavid Howells 	int rc;
20125cd9c58fSDavid Howells 
2013200ac532SEric Paris 	rc = cap_ptrace_traceme(parent);
20145cd9c58fSDavid Howells 	if (rc)
20155cd9c58fSDavid Howells 		return rc;
20165cd9c58fSDavid Howells 
20175cd9c58fSDavid Howells 	return task_has_perm(parent, current, PROCESS__PTRACE);
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
20211da177e4SLinus Torvalds 			  kernel_cap_t *inheritable, kernel_cap_t *permitted)
20221da177e4SLinus Torvalds {
20231da177e4SLinus Torvalds 	int error;
20241da177e4SLinus Torvalds 
20253b11a1deSDavid Howells 	error = current_has_perm(target, PROCESS__GETCAP);
20261da177e4SLinus Torvalds 	if (error)
20271da177e4SLinus Torvalds 		return error;
20281da177e4SLinus Torvalds 
2029200ac532SEric Paris 	return cap_capget(target, effective, inheritable, permitted);
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
2032d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old,
2033d84f4f99SDavid Howells 			  const kernel_cap_t *effective,
203415a2460eSDavid Howells 			  const kernel_cap_t *inheritable,
203515a2460eSDavid Howells 			  const kernel_cap_t *permitted)
20361da177e4SLinus Torvalds {
20371da177e4SLinus Torvalds 	int error;
20381da177e4SLinus Torvalds 
2039200ac532SEric Paris 	error = cap_capset(new, old,
2040d84f4f99SDavid Howells 				      effective, inheritable, permitted);
20411da177e4SLinus Torvalds 	if (error)
20421da177e4SLinus Torvalds 		return error;
20431da177e4SLinus Torvalds 
2044d84f4f99SDavid Howells 	return cred_has_perm(old, new, PROCESS__SETCAP);
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
20475626d3e8SJames Morris /*
20485626d3e8SJames Morris  * (This comment used to live with the selinux_task_setuid hook,
20495626d3e8SJames Morris  * which was removed).
20505626d3e8SJames Morris  *
20515626d3e8SJames Morris  * Since setuid only affects the current process, and since the SELinux
20525626d3e8SJames Morris  * controls are not based on the Linux identity attributes, SELinux does not
20535626d3e8SJames Morris  * need to control this operation.  However, SELinux does control the use of
20545626d3e8SJames Morris  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
20555626d3e8SJames Morris  */
20565626d3e8SJames Morris 
20576a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
20586a9de491SEric Paris 			   int cap, int audit)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	int rc;
20611da177e4SLinus Torvalds 
20626a9de491SEric Paris 	rc = cap_capable(cred, ns, cap, audit);
20631da177e4SLinus Torvalds 	if (rc)
20641da177e4SLinus Torvalds 		return rc;
20651da177e4SLinus Torvalds 
20666a9de491SEric Paris 	return cred_has_capability(cred, cap, audit);
20671da177e4SLinus Torvalds }
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
20701da177e4SLinus Torvalds {
207188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
20721da177e4SLinus Torvalds 	int rc = 0;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	if (!sb)
20751da177e4SLinus Torvalds 		return 0;
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	switch (cmds) {
20781da177e4SLinus Torvalds 	case Q_SYNC:
20791da177e4SLinus Torvalds 	case Q_QUOTAON:
20801da177e4SLinus Torvalds 	case Q_QUOTAOFF:
20811da177e4SLinus Torvalds 	case Q_SETINFO:
20821da177e4SLinus Torvalds 	case Q_SETQUOTA:
208388e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
20841da177e4SLinus Torvalds 		break;
20851da177e4SLinus Torvalds 	case Q_GETFMT:
20861da177e4SLinus Torvalds 	case Q_GETINFO:
20871da177e4SLinus Torvalds 	case Q_GETQUOTA:
208888e67f3bSDavid Howells 		rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
20891da177e4SLinus Torvalds 		break;
20901da177e4SLinus Torvalds 	default:
20911da177e4SLinus Torvalds 		rc = 0;  /* let the kernel handle invalid cmds */
20921da177e4SLinus Torvalds 		break;
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 	return rc;
20951da177e4SLinus Torvalds }
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
20981da177e4SLinus Torvalds {
209988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
210088e67f3bSDavid Howells 
21012875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__QUOTAON);
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
210412b3052cSEric Paris static int selinux_syslog(int type)
21051da177e4SLinus Torvalds {
21061da177e4SLinus Torvalds 	int rc;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	switch (type) {
2109d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_ALL:	/* Read last kernel messages */
2110d78ca3cdSKees Cook 	case SYSLOG_ACTION_SIZE_BUFFER:	/* Return size of the log buffer */
21111da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_READ);
21121da177e4SLinus Torvalds 		break;
2113d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_OFF:	/* Disable logging to console */
2114d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_ON:	/* Enable logging to console */
2115d78ca3cdSKees Cook 	/* Set level of messages printed to console */
2116d78ca3cdSKees Cook 	case SYSLOG_ACTION_CONSOLE_LEVEL:
21171da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
21181da177e4SLinus Torvalds 		break;
2119d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLOSE:	/* Close log */
2120d78ca3cdSKees Cook 	case SYSLOG_ACTION_OPEN:	/* Open log */
2121d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ:	/* Read from log */
2122d78ca3cdSKees Cook 	case SYSLOG_ACTION_READ_CLEAR:	/* Read/clear last kernel messages */
2123d78ca3cdSKees Cook 	case SYSLOG_ACTION_CLEAR:	/* Clear ring buffer */
21241da177e4SLinus Torvalds 	default:
21251da177e4SLinus Torvalds 		rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
21261da177e4SLinus Torvalds 		break;
21271da177e4SLinus Torvalds 	}
21281da177e4SLinus Torvalds 	return rc;
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds /*
21321da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
21331da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
21341da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
21351da177e4SLinus Torvalds  *
21361da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
21371da177e4SLinus Torvalds  * processes that allocate mappings.
21381da177e4SLinus Torvalds  */
213934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
21401da177e4SLinus Torvalds {
21411da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
21421da177e4SLinus Torvalds 
21436a9de491SEric Paris 	rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
21443699c53cSDavid Howells 			     SECURITY_CAP_NOAUDIT);
21451da177e4SLinus Torvalds 	if (rc == 0)
21461da177e4SLinus Torvalds 		cap_sys_admin = 1;
21471da177e4SLinus Torvalds 
214834b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds /* binprm security operations */
21521da177e4SLinus Torvalds 
21537b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm,
21547b0d0b40SStephen Smalley 			    const struct task_security_struct *old_tsec,
21557b0d0b40SStephen Smalley 			    const struct task_security_struct *new_tsec)
21567b0d0b40SStephen Smalley {
21577b0d0b40SStephen Smalley 	int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
21587b0d0b40SStephen Smalley 	int nosuid = (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID);
21597b0d0b40SStephen Smalley 	int rc;
21607b0d0b40SStephen Smalley 
21617b0d0b40SStephen Smalley 	if (!nnp && !nosuid)
21627b0d0b40SStephen Smalley 		return 0; /* neither NNP nor nosuid */
21637b0d0b40SStephen Smalley 
21647b0d0b40SStephen Smalley 	if (new_tsec->sid == old_tsec->sid)
21657b0d0b40SStephen Smalley 		return 0; /* No change in credentials */
21667b0d0b40SStephen Smalley 
21677b0d0b40SStephen Smalley 	/*
21687b0d0b40SStephen Smalley 	 * The only transitions we permit under NNP or nosuid
21697b0d0b40SStephen Smalley 	 * are transitions to bounded SIDs, i.e. SIDs that are
21707b0d0b40SStephen Smalley 	 * guaranteed to only be allowed a subset of the permissions
21717b0d0b40SStephen Smalley 	 * of the current SID.
21727b0d0b40SStephen Smalley 	 */
21737b0d0b40SStephen Smalley 	rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
21747b0d0b40SStephen Smalley 	if (rc) {
21757b0d0b40SStephen Smalley 		/*
21767b0d0b40SStephen Smalley 		 * On failure, preserve the errno values for NNP vs nosuid.
21777b0d0b40SStephen Smalley 		 * NNP:  Operation not permitted for caller.
21787b0d0b40SStephen Smalley 		 * nosuid:  Permission denied to file.
21797b0d0b40SStephen Smalley 		 */
21807b0d0b40SStephen Smalley 		if (nnp)
21817b0d0b40SStephen Smalley 			return -EPERM;
21827b0d0b40SStephen Smalley 		else
21837b0d0b40SStephen Smalley 			return -EACCES;
21847b0d0b40SStephen Smalley 	}
21857b0d0b40SStephen Smalley 	return 0;
21867b0d0b40SStephen Smalley }
21877b0d0b40SStephen Smalley 
2188a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm)
21891da177e4SLinus Torvalds {
2190a6f76f23SDavid Howells 	const struct task_security_struct *old_tsec;
2191a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
21921da177e4SLinus Torvalds 	struct inode_security_struct *isec;
21932bf49690SThomas Liu 	struct common_audit_data ad;
2194496ad9aaSAl Viro 	struct inode *inode = file_inode(bprm->file);
21951da177e4SLinus Torvalds 	int rc;
21961da177e4SLinus Torvalds 
2197200ac532SEric Paris 	rc = cap_bprm_set_creds(bprm);
21981da177e4SLinus Torvalds 	if (rc)
21991da177e4SLinus Torvalds 		return rc;
22001da177e4SLinus Torvalds 
2201a6f76f23SDavid Howells 	/* SELinux context only depends on initial program or script and not
2202a6f76f23SDavid Howells 	 * the script interpreter */
2203a6f76f23SDavid Howells 	if (bprm->cred_prepared)
22041da177e4SLinus Torvalds 		return 0;
22051da177e4SLinus Torvalds 
2206a6f76f23SDavid Howells 	old_tsec = current_security();
2207a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
22081da177e4SLinus Torvalds 	isec = inode->i_security;
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	/* Default to the current task SID. */
2211a6f76f23SDavid Howells 	new_tsec->sid = old_tsec->sid;
2212a6f76f23SDavid Howells 	new_tsec->osid = old_tsec->sid;
22131da177e4SLinus Torvalds 
221428eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
2215a6f76f23SDavid Howells 	new_tsec->create_sid = 0;
2216a6f76f23SDavid Howells 	new_tsec->keycreate_sid = 0;
2217a6f76f23SDavid Howells 	new_tsec->sockcreate_sid = 0;
22181da177e4SLinus Torvalds 
2219a6f76f23SDavid Howells 	if (old_tsec->exec_sid) {
2220a6f76f23SDavid Howells 		new_tsec->sid = old_tsec->exec_sid;
22211da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
2222a6f76f23SDavid Howells 		new_tsec->exec_sid = 0;
2223259e5e6cSAndy Lutomirski 
22247b0d0b40SStephen Smalley 		/* Fail on NNP or nosuid if not an allowed transition. */
22257b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
22267b0d0b40SStephen Smalley 		if (rc)
22277b0d0b40SStephen Smalley 			return rc;
22281da177e4SLinus Torvalds 	} else {
22291da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
2230a6f76f23SDavid Howells 		rc = security_transition_sid(old_tsec->sid, isec->sid,
2231652bb9b0SEric Paris 					     SECCLASS_PROCESS, NULL,
2232652bb9b0SEric Paris 					     &new_tsec->sid);
22331da177e4SLinus Torvalds 		if (rc)
22341da177e4SLinus Torvalds 			return rc;
22357b0d0b40SStephen Smalley 
22367b0d0b40SStephen Smalley 		/*
22377b0d0b40SStephen Smalley 		 * Fallback to old SID on NNP or nosuid if not an allowed
22387b0d0b40SStephen Smalley 		 * transition.
22397b0d0b40SStephen Smalley 		 */
22407b0d0b40SStephen Smalley 		rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
22417b0d0b40SStephen Smalley 		if (rc)
22427b0d0b40SStephen Smalley 			new_tsec->sid = old_tsec->sid;
22431da177e4SLinus Torvalds 	}
22441da177e4SLinus Torvalds 
224550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_PATH;
2246f48b7399SEric Paris 	ad.u.path = bprm->file->f_path;
22471da177e4SLinus Torvalds 
2248a6f76f23SDavid Howells 	if (new_tsec->sid == old_tsec->sid) {
2249a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, isec->sid,
22501da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
22511da177e4SLinus Torvalds 		if (rc)
22521da177e4SLinus Torvalds 			return rc;
22531da177e4SLinus Torvalds 	} else {
22541da177e4SLinus Torvalds 		/* Check permissions for the transition. */
2255a6f76f23SDavid Howells 		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
22561da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
22571da177e4SLinus Torvalds 		if (rc)
22581da177e4SLinus Torvalds 			return rc;
22591da177e4SLinus Torvalds 
2260a6f76f23SDavid Howells 		rc = avc_has_perm(new_tsec->sid, isec->sid,
22611da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
22621da177e4SLinus Torvalds 		if (rc)
22631da177e4SLinus Torvalds 			return rc;
22641da177e4SLinus Torvalds 
2265a6f76f23SDavid Howells 		/* Check for shared state */
2266a6f76f23SDavid Howells 		if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2267a6f76f23SDavid Howells 			rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2268a6f76f23SDavid Howells 					  SECCLASS_PROCESS, PROCESS__SHARE,
2269a6f76f23SDavid Howells 					  NULL);
2270a6f76f23SDavid Howells 			if (rc)
2271a6f76f23SDavid Howells 				return -EPERM;
22721da177e4SLinus Torvalds 		}
22731da177e4SLinus Torvalds 
2274a6f76f23SDavid Howells 		/* Make sure that anyone attempting to ptrace over a task that
2275a6f76f23SDavid Howells 		 * changes its SID has the appropriate permit */
2276a6f76f23SDavid Howells 		if (bprm->unsafe &
2277a6f76f23SDavid Howells 		    (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2278a6f76f23SDavid Howells 			struct task_struct *tracer;
2279a6f76f23SDavid Howells 			struct task_security_struct *sec;
2280a6f76f23SDavid Howells 			u32 ptsid = 0;
2281a6f76f23SDavid Howells 
2282a6f76f23SDavid Howells 			rcu_read_lock();
228306d98473STejun Heo 			tracer = ptrace_parent(current);
2284a6f76f23SDavid Howells 			if (likely(tracer != NULL)) {
2285a6f76f23SDavid Howells 				sec = __task_cred(tracer)->security;
2286a6f76f23SDavid Howells 				ptsid = sec->sid;
2287a6f76f23SDavid Howells 			}
2288a6f76f23SDavid Howells 			rcu_read_unlock();
2289a6f76f23SDavid Howells 
2290a6f76f23SDavid Howells 			if (ptsid != 0) {
2291a6f76f23SDavid Howells 				rc = avc_has_perm(ptsid, new_tsec->sid,
2292a6f76f23SDavid Howells 						  SECCLASS_PROCESS,
2293a6f76f23SDavid Howells 						  PROCESS__PTRACE, NULL);
2294a6f76f23SDavid Howells 				if (rc)
2295a6f76f23SDavid Howells 					return -EPERM;
2296a6f76f23SDavid Howells 			}
2297a6f76f23SDavid Howells 		}
2298a6f76f23SDavid Howells 
2299a6f76f23SDavid Howells 		/* Clear any possibly unsafe personality bits on exec: */
2300a6f76f23SDavid Howells 		bprm->per_clear |= PER_CLEAR_ON_SETID;
2301a6f76f23SDavid Howells 	}
2302a6f76f23SDavid Howells 
23031da177e4SLinus Torvalds 	return 0;
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm)
23071da177e4SLinus Torvalds {
23085fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
2309275bb41eSDavid Howells 	u32 sid, osid;
23101da177e4SLinus Torvalds 	int atsecure = 0;
23111da177e4SLinus Torvalds 
2312275bb41eSDavid Howells 	sid = tsec->sid;
2313275bb41eSDavid Howells 	osid = tsec->osid;
2314275bb41eSDavid Howells 
2315275bb41eSDavid Howells 	if (osid != sid) {
23161da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
23171da177e4SLinus Torvalds 		   the noatsecure permission is granted between
23181da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
2319275bb41eSDavid Howells 		atsecure = avc_has_perm(osid, sid,
23201da177e4SLinus Torvalds 					SECCLASS_PROCESS,
23211da177e4SLinus Torvalds 					PROCESS__NOATSECURE, NULL);
23221da177e4SLinus Torvalds 	}
23231da177e4SLinus Torvalds 
2324200ac532SEric Paris 	return (atsecure || cap_bprm_secureexec(bprm));
23251da177e4SLinus Torvalds }
23261da177e4SLinus Torvalds 
2327c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd)
2328c3c073f8SAl Viro {
2329c3c073f8SAl Viro 	return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2330c3c073f8SAl Viro }
2331c3c073f8SAl Viro 
23321da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
2333745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred,
2334745ca247SDavid Howells 					    struct files_struct *files)
23351da177e4SLinus Torvalds {
23361da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2337b20c8122SStephen Smalley 	struct tty_struct *tty;
233824ec839cSPeter Zijlstra 	int drop_tty = 0;
2339c3c073f8SAl Viro 	unsigned n;
23401da177e4SLinus Torvalds 
234124ec839cSPeter Zijlstra 	tty = get_current_tty();
23421da177e4SLinus Torvalds 	if (tty) {
2343ee2ffa0dSNick Piggin 		spin_lock(&tty_files_lock);
234437dd0bd0SEric Paris 		if (!list_empty(&tty->tty_files)) {
2345d996b62aSNick Piggin 			struct tty_file_private *file_priv;
234637dd0bd0SEric Paris 
23471da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
234813f8e981SDavid Howells 			   Use file_path_has_perm on the tty path directly
234913f8e981SDavid Howells 			   rather than using file_has_perm, as this particular
235013f8e981SDavid Howells 			   open file may belong to another process and we are
235113f8e981SDavid Howells 			   only interested in the inode-based check here. */
2352d996b62aSNick Piggin 			file_priv = list_first_entry(&tty->tty_files,
2353d996b62aSNick Piggin 						struct tty_file_private, list);
2354d996b62aSNick Piggin 			file = file_priv->file;
235513f8e981SDavid Howells 			if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
235624ec839cSPeter Zijlstra 				drop_tty = 1;
23571da177e4SLinus Torvalds 		}
2358ee2ffa0dSNick Piggin 		spin_unlock(&tty_files_lock);
2359452a00d2SAlan Cox 		tty_kref_put(tty);
23601da177e4SLinus Torvalds 	}
236198a27ba4SEric W. Biederman 	/* Reset controlling tty. */
236298a27ba4SEric W. Biederman 	if (drop_tty)
236398a27ba4SEric W. Biederman 		no_tty();
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
2366c3c073f8SAl Viro 	n = iterate_fd(files, 0, match_file, cred);
2367c3c073f8SAl Viro 	if (!n) /* none found? */
2368c3c073f8SAl Viro 		return;
23691da177e4SLinus Torvalds 
2370c3c073f8SAl Viro 	devnull = dentry_open(&selinux_null, O_RDWR, cred);
237145525b26SAl Viro 	if (IS_ERR(devnull))
237245525b26SAl Viro 		devnull = NULL;
2373c3c073f8SAl Viro 	/* replace all the matching ones with this */
2374c3c073f8SAl Viro 	do {
237545525b26SAl Viro 		replace_fd(n - 1, devnull, 0);
2376c3c073f8SAl Viro 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
237745525b26SAl Viro 	if (devnull)
2378c3c073f8SAl Viro 		fput(devnull);
23791da177e4SLinus Torvalds }
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds /*
2382a6f76f23SDavid Howells  * Prepare a process for imminent new credential changes due to exec
23831da177e4SLinus Torvalds  */
2384a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
23851da177e4SLinus Torvalds {
2386a6f76f23SDavid Howells 	struct task_security_struct *new_tsec;
23871da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
23881da177e4SLinus Torvalds 	int rc, i;
23891da177e4SLinus Torvalds 
2390a6f76f23SDavid Howells 	new_tsec = bprm->cred->security;
2391a6f76f23SDavid Howells 	if (new_tsec->sid == new_tsec->osid)
23921da177e4SLinus Torvalds 		return;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
2395a6f76f23SDavid Howells 	flush_unauthorized_files(bprm->cred, current->files);
23961da177e4SLinus Torvalds 
2397a6f76f23SDavid Howells 	/* Always clear parent death signal on SID transitions. */
2398a6f76f23SDavid Howells 	current->pdeath_signal = 0;
2399a6f76f23SDavid Howells 
2400a6f76f23SDavid Howells 	/* Check whether the new SID can inherit resource limits from the old
2401a6f76f23SDavid Howells 	 * SID.  If not, reset all soft limits to the lower of the current
2402a6f76f23SDavid Howells 	 * task's hard limit and the init task's soft limit.
2403a6f76f23SDavid Howells 	 *
2404a6f76f23SDavid Howells 	 * Note that the setting of hard limits (even to lower them) can be
2405a6f76f23SDavid Howells 	 * controlled by the setrlimit check.  The inclusion of the init task's
2406a6f76f23SDavid Howells 	 * soft limit into the computation is to avoid resetting soft limits
2407a6f76f23SDavid Howells 	 * higher than the default soft limit for cases where the default is
2408a6f76f23SDavid Howells 	 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2409a6f76f23SDavid Howells 	 */
2410a6f76f23SDavid Howells 	rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2411a6f76f23SDavid Howells 			  PROCESS__RLIMITINH, NULL);
2412a6f76f23SDavid Howells 	if (rc) {
2413eb2d55a3SOleg Nesterov 		/* protect against do_prlimit() */
2414eb2d55a3SOleg Nesterov 		task_lock(current);
2415a6f76f23SDavid Howells 		for (i = 0; i < RLIM_NLIMITS; i++) {
2416a6f76f23SDavid Howells 			rlim = current->signal->rlim + i;
2417a6f76f23SDavid Howells 			initrlim = init_task.signal->rlim + i;
2418a6f76f23SDavid Howells 			rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2419a6f76f23SDavid Howells 		}
2420eb2d55a3SOleg Nesterov 		task_unlock(current);
2421eb2d55a3SOleg Nesterov 		update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2422a6f76f23SDavid Howells 	}
2423a6f76f23SDavid Howells }
2424a6f76f23SDavid Howells 
2425a6f76f23SDavid Howells /*
2426a6f76f23SDavid Howells  * Clean up the process immediately after the installation of new credentials
2427a6f76f23SDavid Howells  * due to exec
2428a6f76f23SDavid Howells  */
2429a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2430a6f76f23SDavid Howells {
2431a6f76f23SDavid Howells 	const struct task_security_struct *tsec = current_security();
2432a6f76f23SDavid Howells 	struct itimerval itimer;
2433a6f76f23SDavid Howells 	u32 osid, sid;
2434a6f76f23SDavid Howells 	int rc, i;
2435a6f76f23SDavid Howells 
2436a6f76f23SDavid Howells 	osid = tsec->osid;
2437a6f76f23SDavid Howells 	sid = tsec->sid;
2438a6f76f23SDavid Howells 
2439a6f76f23SDavid Howells 	if (sid == osid)
2440a6f76f23SDavid Howells 		return;
2441a6f76f23SDavid Howells 
2442a6f76f23SDavid Howells 	/* Check whether the new SID can inherit signal state from the old SID.
2443a6f76f23SDavid Howells 	 * If not, clear itimers to avoid subsequent signal generation and
2444a6f76f23SDavid Howells 	 * flush and unblock signals.
2445a6f76f23SDavid Howells 	 *
2446a6f76f23SDavid Howells 	 * This must occur _after_ the task SID has been updated so that any
2447a6f76f23SDavid Howells 	 * kill done after the flush will be checked against the new SID.
2448a6f76f23SDavid Howells 	 */
2449a6f76f23SDavid Howells 	rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
24501da177e4SLinus Torvalds 	if (rc) {
24511da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
24521da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
24531da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
24541da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
24553bcac026SDavid Howells 		if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
24563bcac026SDavid Howells 			__flush_signals(current);
24571da177e4SLinus Torvalds 			flush_signal_handlers(current, 1);
24581da177e4SLinus Torvalds 			sigemptyset(&current->blocked);
24593bcac026SDavid Howells 		}
24601da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
24611da177e4SLinus Torvalds 	}
24621da177e4SLinus Torvalds 
2463a6f76f23SDavid Howells 	/* Wake up the parent if it is waiting so that it can recheck
2464a6f76f23SDavid Howells 	 * wait permission to the new task SID. */
2465ecd6de3cSOleg Nesterov 	read_lock(&tasklist_lock);
24660b7570e7SOleg Nesterov 	__wake_up_parent(current, current->real_parent);
2467ecd6de3cSOleg Nesterov 	read_unlock(&tasklist_lock);
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds /* superblock security operations */
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
24731da177e4SLinus Torvalds {
24741da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
24781da177e4SLinus Torvalds {
24791da177e4SLinus Torvalds 	superblock_free_security(sb);
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
24831da177e4SLinus Torvalds {
24841da177e4SLinus Torvalds 	if (plen > olen)
24851da177e4SLinus Torvalds 		return 0;
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
24911da177e4SLinus Torvalds {
2492832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2493832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2494832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
249511689d47SDavid P. Quigley 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
249611689d47SDavid P. Quigley 		match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
24971da177e4SLinus Torvalds }
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
25001da177e4SLinus Torvalds {
25011da177e4SLinus Torvalds 	if (!*first) {
25021da177e4SLinus Torvalds 		**to = ',';
25031da177e4SLinus Torvalds 		*to += 1;
25043528a953SCory Olmo 	} else
25051da177e4SLinus Torvalds 		*first = 0;
25061da177e4SLinus Torvalds 	memcpy(*to, from, len);
25071da177e4SLinus Torvalds 	*to += len;
25081da177e4SLinus Torvalds }
25091da177e4SLinus Torvalds 
25103528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
25113528a953SCory Olmo 				       int len)
25123528a953SCory Olmo {
25133528a953SCory Olmo 	int current_size = 0;
25143528a953SCory Olmo 
25153528a953SCory Olmo 	if (!*first) {
25163528a953SCory Olmo 		**to = '|';
25173528a953SCory Olmo 		*to += 1;
2518828dfe1dSEric Paris 	} else
25193528a953SCory Olmo 		*first = 0;
25203528a953SCory Olmo 
25213528a953SCory Olmo 	while (current_size < len) {
25223528a953SCory Olmo 		if (*from != '"') {
25233528a953SCory Olmo 			**to = *from;
25243528a953SCory Olmo 			*to += 1;
25253528a953SCory Olmo 		}
25263528a953SCory Olmo 		from += 1;
25273528a953SCory Olmo 		current_size += 1;
25283528a953SCory Olmo 	}
25293528a953SCory Olmo }
25303528a953SCory Olmo 
2531e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
25321da177e4SLinus Torvalds {
25331da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
25341da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
25351da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
25363528a953SCory Olmo 	int open_quote = 0;
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	in_curr = orig;
25391da177e4SLinus Torvalds 	sec_curr = copy;
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
25421da177e4SLinus Torvalds 	if (!nosec) {
25431da177e4SLinus Torvalds 		rc = -ENOMEM;
25441da177e4SLinus Torvalds 		goto out;
25451da177e4SLinus Torvalds 	}
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	nosec_save = nosec;
25481da177e4SLinus Torvalds 	fnosec = fsec = 1;
25491da177e4SLinus Torvalds 	in_save = in_end = orig;
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	do {
25523528a953SCory Olmo 		if (*in_end == '"')
25533528a953SCory Olmo 			open_quote = !open_quote;
25543528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
25553528a953SCory Olmo 				*in_end == '\0') {
25561da177e4SLinus Torvalds 			int len = in_end - in_curr;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
25593528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
25601da177e4SLinus Torvalds 			else
25611da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 			in_curr = in_end + 1;
25641da177e4SLinus Torvalds 		}
25651da177e4SLinus Torvalds 	} while (*in_end++);
25661da177e4SLinus Torvalds 
25676931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2568da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
25691da177e4SLinus Torvalds out:
25701da177e4SLinus Torvalds 	return rc;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds 
2573026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data)
2574026eb167SEric Paris {
2575026eb167SEric Paris 	int rc, i, *flags;
2576026eb167SEric Paris 	struct security_mnt_opts opts;
2577026eb167SEric Paris 	char *secdata, **mount_options;
2578026eb167SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
2579026eb167SEric Paris 
2580026eb167SEric Paris 	if (!(sbsec->flags & SE_SBINITIALIZED))
2581026eb167SEric Paris 		return 0;
2582026eb167SEric Paris 
2583026eb167SEric Paris 	if (!data)
2584026eb167SEric Paris 		return 0;
2585026eb167SEric Paris 
2586026eb167SEric Paris 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2587026eb167SEric Paris 		return 0;
2588026eb167SEric Paris 
2589026eb167SEric Paris 	security_init_mnt_opts(&opts);
2590026eb167SEric Paris 	secdata = alloc_secdata();
2591026eb167SEric Paris 	if (!secdata)
2592026eb167SEric Paris 		return -ENOMEM;
2593026eb167SEric Paris 	rc = selinux_sb_copy_data(data, secdata);
2594026eb167SEric Paris 	if (rc)
2595026eb167SEric Paris 		goto out_free_secdata;
2596026eb167SEric Paris 
2597026eb167SEric Paris 	rc = selinux_parse_opts_str(secdata, &opts);
2598026eb167SEric Paris 	if (rc)
2599026eb167SEric Paris 		goto out_free_secdata;
2600026eb167SEric Paris 
2601026eb167SEric Paris 	mount_options = opts.mnt_opts;
2602026eb167SEric Paris 	flags = opts.mnt_opts_flags;
2603026eb167SEric Paris 
2604026eb167SEric Paris 	for (i = 0; i < opts.num_mnt_opts; i++) {
2605026eb167SEric Paris 		u32 sid;
2606026eb167SEric Paris 		size_t len;
2607026eb167SEric Paris 
260812f348b9SEric Paris 		if (flags[i] == SBLABEL_MNT)
2609026eb167SEric Paris 			continue;
2610026eb167SEric Paris 		len = strlen(mount_options[i]);
261152a4c640SNikolay Aleksandrov 		rc = security_context_to_sid(mount_options[i], len, &sid,
261252a4c640SNikolay Aleksandrov 					     GFP_KERNEL);
2613026eb167SEric Paris 		if (rc) {
2614026eb167SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
261529b1deb2SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
261629b1deb2SLinus Torvalds 			       mount_options[i], sb->s_id, sb->s_type->name, rc);
2617026eb167SEric Paris 			goto out_free_opts;
2618026eb167SEric Paris 		}
2619026eb167SEric Paris 		rc = -EINVAL;
2620026eb167SEric Paris 		switch (flags[i]) {
2621026eb167SEric Paris 		case FSCONTEXT_MNT:
2622026eb167SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2623026eb167SEric Paris 				goto out_bad_option;
2624026eb167SEric Paris 			break;
2625026eb167SEric Paris 		case CONTEXT_MNT:
2626026eb167SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2627026eb167SEric Paris 				goto out_bad_option;
2628026eb167SEric Paris 			break;
2629026eb167SEric Paris 		case ROOTCONTEXT_MNT: {
2630026eb167SEric Paris 			struct inode_security_struct *root_isec;
2631026eb167SEric Paris 			root_isec = sb->s_root->d_inode->i_security;
2632026eb167SEric Paris 
2633026eb167SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2634026eb167SEric Paris 				goto out_bad_option;
2635026eb167SEric Paris 			break;
2636026eb167SEric Paris 		}
2637026eb167SEric Paris 		case DEFCONTEXT_MNT:
2638026eb167SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2639026eb167SEric Paris 				goto out_bad_option;
2640026eb167SEric Paris 			break;
2641026eb167SEric Paris 		default:
2642026eb167SEric Paris 			goto out_free_opts;
2643026eb167SEric Paris 		}
2644026eb167SEric Paris 	}
2645026eb167SEric Paris 
2646026eb167SEric Paris 	rc = 0;
2647026eb167SEric Paris out_free_opts:
2648026eb167SEric Paris 	security_free_mnt_opts(&opts);
2649026eb167SEric Paris out_free_secdata:
2650026eb167SEric Paris 	free_secdata(secdata);
2651026eb167SEric Paris 	return rc;
2652026eb167SEric Paris out_bad_option:
2653026eb167SEric Paris 	printk(KERN_WARNING "SELinux: unable to change security options "
265429b1deb2SLinus Torvalds 	       "during remount (dev %s, type=%s)\n", sb->s_id,
265529b1deb2SLinus Torvalds 	       sb->s_type->name);
2656026eb167SEric Paris 	goto out_free_opts;
2657026eb167SEric Paris }
2658026eb167SEric Paris 
265912204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
26601da177e4SLinus Torvalds {
266188e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26622bf49690SThomas Liu 	struct common_audit_data ad;
26631da177e4SLinus Torvalds 	int rc;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
26661da177e4SLinus Torvalds 	if (rc)
26671da177e4SLinus Torvalds 		return rc;
26681da177e4SLinus Torvalds 
266974192246SJames Morris 	/* Allow all mounts performed by the kernel */
267074192246SJames Morris 	if (flags & MS_KERNMOUNT)
267174192246SJames Morris 		return 0;
267274192246SJames Morris 
267350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2674a269434dSEric Paris 	ad.u.dentry = sb->s_root;
267588e67f3bSDavid Howells 	return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds 
2678726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
26791da177e4SLinus Torvalds {
268088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26812bf49690SThomas Liu 	struct common_audit_data ad;
26821da177e4SLinus Torvalds 
268350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
2684a269434dSEric Paris 	ad.u.dentry = dentry->d_sb->s_root;
268588e67f3bSDavid Howells 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
2688808d4e3cSAl Viro static int selinux_mount(const char *dev_name,
2689b5266eb4SAl Viro 			 struct path *path,
2690808d4e3cSAl Viro 			 const char *type,
26911da177e4SLinus Torvalds 			 unsigned long flags,
26921da177e4SLinus Torvalds 			 void *data)
26931da177e4SLinus Torvalds {
269488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2697d8c9584eSAl Viro 		return superblock_has_perm(cred, path->dentry->d_sb,
26981da177e4SLinus Torvalds 					   FILESYSTEM__REMOUNT, NULL);
26991da177e4SLinus Torvalds 	else
27002875fa00SEric Paris 		return path_has_perm(cred, path, FILE__MOUNTON);
27011da177e4SLinus Torvalds }
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
27041da177e4SLinus Torvalds {
270588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
27061da177e4SLinus Torvalds 
270788e67f3bSDavid Howells 	return superblock_has_perm(cred, mnt->mnt_sb,
27081da177e4SLinus Torvalds 				   FILESYSTEM__UNMOUNT, NULL);
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds /* inode security operations */
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
27141da177e4SLinus Torvalds {
27151da177e4SLinus Torvalds 	return inode_alloc_security(inode);
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds 	inode_free_security(inode);
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
2723d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2724d47be3dfSDavid Quigley 					struct qstr *name, void **ctx,
2725d47be3dfSDavid Quigley 					u32 *ctxlen)
2726d47be3dfSDavid Quigley {
2727d47be3dfSDavid Quigley 	const struct cred *cred = current_cred();
2728d47be3dfSDavid Quigley 	struct task_security_struct *tsec;
2729d47be3dfSDavid Quigley 	struct inode_security_struct *dsec;
2730d47be3dfSDavid Quigley 	struct superblock_security_struct *sbsec;
2731d47be3dfSDavid Quigley 	struct inode *dir = dentry->d_parent->d_inode;
2732d47be3dfSDavid Quigley 	u32 newsid;
2733d47be3dfSDavid Quigley 	int rc;
2734d47be3dfSDavid Quigley 
2735d47be3dfSDavid Quigley 	tsec = cred->security;
2736d47be3dfSDavid Quigley 	dsec = dir->i_security;
2737d47be3dfSDavid Quigley 	sbsec = dir->i_sb->s_security;
2738d47be3dfSDavid Quigley 
2739d47be3dfSDavid Quigley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2740d47be3dfSDavid Quigley 		newsid = tsec->create_sid;
2741d47be3dfSDavid Quigley 	} else {
2742d47be3dfSDavid Quigley 		rc = security_transition_sid(tsec->sid, dsec->sid,
2743d47be3dfSDavid Quigley 					     inode_mode_to_security_class(mode),
2744d47be3dfSDavid Quigley 					     name,
2745d47be3dfSDavid Quigley 					     &newsid);
2746d47be3dfSDavid Quigley 		if (rc) {
2747d47be3dfSDavid Quigley 			printk(KERN_WARNING
2748d47be3dfSDavid Quigley 				"%s: security_transition_sid failed, rc=%d\n",
2749d47be3dfSDavid Quigley 			       __func__, -rc);
2750d47be3dfSDavid Quigley 			return rc;
2751d47be3dfSDavid Quigley 		}
2752d47be3dfSDavid Quigley 	}
2753d47be3dfSDavid Quigley 
2754d47be3dfSDavid Quigley 	return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2755d47be3dfSDavid Quigley }
2756d47be3dfSDavid Quigley 
27575e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
27589548906bSTetsuo Handa 				       const struct qstr *qstr,
27599548906bSTetsuo Handa 				       const char **name,
27602a7dba39SEric Paris 				       void **value, size_t *len)
27615e41ff9eSStephen Smalley {
27625fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
27635e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
27645e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2765275bb41eSDavid Howells 	u32 sid, newsid, clen;
27665e41ff9eSStephen Smalley 	int rc;
27679548906bSTetsuo Handa 	char *context;
27685e41ff9eSStephen Smalley 
27695e41ff9eSStephen Smalley 	dsec = dir->i_security;
27705e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
27715e41ff9eSStephen Smalley 
2772275bb41eSDavid Howells 	sid = tsec->sid;
27735e41ff9eSStephen Smalley 	newsid = tsec->create_sid;
2774275bb41eSDavid Howells 
2775415103f9SEric Paris 	if ((sbsec->flags & SE_SBINITIALIZED) &&
2776415103f9SEric Paris 	    (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2777415103f9SEric Paris 		newsid = sbsec->mntpoint_sid;
277812f348b9SEric Paris 	else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
2779275bb41eSDavid Howells 		rc = security_transition_sid(sid, dsec->sid,
27805e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
2781652bb9b0SEric Paris 					     qstr, &newsid);
27825e41ff9eSStephen Smalley 		if (rc) {
27835e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
27845e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
27855e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2786dd6f953aSHarvey Harrison 			       __func__,
27875e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
27885e41ff9eSStephen Smalley 			return rc;
27895e41ff9eSStephen Smalley 		}
27905e41ff9eSStephen Smalley 	}
27915e41ff9eSStephen Smalley 
2792296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
27930d90a7ecSDavid P. Quigley 	if (sbsec->flags & SE_SBINITIALIZED) {
2794296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2795296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2796296fddf7SEric Paris 		isec->sid = newsid;
2797296fddf7SEric Paris 		isec->initialized = 1;
2798296fddf7SEric Paris 	}
27995e41ff9eSStephen Smalley 
280012f348b9SEric Paris 	if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
280125a74f3bSStephen Smalley 		return -EOPNOTSUPP;
280225a74f3bSStephen Smalley 
28039548906bSTetsuo Handa 	if (name)
28049548906bSTetsuo Handa 		*name = XATTR_SELINUX_SUFFIX;
28055e41ff9eSStephen Smalley 
2806570bc1c2SStephen Smalley 	if (value && len) {
280712b29f34SStephen Smalley 		rc = security_sid_to_context_force(newsid, &context, &clen);
28089548906bSTetsuo Handa 		if (rc)
28095e41ff9eSStephen Smalley 			return rc;
28105e41ff9eSStephen Smalley 		*value = context;
2811570bc1c2SStephen Smalley 		*len = clen;
2812570bc1c2SStephen Smalley 	}
28135e41ff9eSStephen Smalley 
28145e41ff9eSStephen Smalley 	return 0;
28155e41ff9eSStephen Smalley }
28165e41ff9eSStephen Smalley 
28174acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
28181da177e4SLinus Torvalds {
28191da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
28201da177e4SLinus Torvalds }
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28231da177e4SLinus Torvalds {
28241da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
28251da177e4SLinus Torvalds }
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
28281da177e4SLinus Torvalds {
28291da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
28331da177e4SLinus Torvalds {
28341da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds 
283718bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
28381da177e4SLinus Torvalds {
28391da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
28401da177e4SLinus Torvalds }
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
28431da177e4SLinus Torvalds {
28441da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
28451da177e4SLinus Torvalds }
28461da177e4SLinus Torvalds 
28471a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
28481da177e4SLinus Torvalds {
28491da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
28501da177e4SLinus Torvalds }
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
28531da177e4SLinus Torvalds 				struct inode *new_inode, struct dentry *new_dentry)
28541da177e4SLinus Torvalds {
28551da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
28591da177e4SLinus Torvalds {
286088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
286188e67f3bSDavid Howells 
28622875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
28631da177e4SLinus Torvalds }
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
28661da177e4SLinus Torvalds {
286788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
28681da177e4SLinus Torvalds 
28692875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__READ);
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
2872d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode,
2873d4cf970dSEric Paris 					   u32 perms, u32 audited, u32 denied,
2874626b9740SStephen Smalley 					   int result,
2875d4cf970dSEric Paris 					   unsigned flags)
2876d4cf970dSEric Paris {
2877d4cf970dSEric Paris 	struct common_audit_data ad;
2878d4cf970dSEric Paris 	struct inode_security_struct *isec = inode->i_security;
2879d4cf970dSEric Paris 	int rc;
2880d4cf970dSEric Paris 
288150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_INODE;
2882d4cf970dSEric Paris 	ad.u.inode = inode;
2883d4cf970dSEric Paris 
2884d4cf970dSEric Paris 	rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2885626b9740SStephen Smalley 			    audited, denied, result, &ad, flags);
2886d4cf970dSEric Paris 	if (rc)
2887d4cf970dSEric Paris 		return rc;
2888d4cf970dSEric Paris 	return 0;
2889d4cf970dSEric Paris }
2890d4cf970dSEric Paris 
2891e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask)
28921da177e4SLinus Torvalds {
289388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2894b782e0a6SEric Paris 	u32 perms;
2895b782e0a6SEric Paris 	bool from_access;
2896cf1dd1daSAl Viro 	unsigned flags = mask & MAY_NOT_BLOCK;
28972e334057SEric Paris 	struct inode_security_struct *isec;
28982e334057SEric Paris 	u32 sid;
28992e334057SEric Paris 	struct av_decision avd;
29002e334057SEric Paris 	int rc, rc2;
29012e334057SEric Paris 	u32 audited, denied;
29021da177e4SLinus Torvalds 
2903b782e0a6SEric Paris 	from_access = mask & MAY_ACCESS;
2904d09ca739SEric Paris 	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2905d09ca739SEric Paris 
29061da177e4SLinus Torvalds 	/* No permission to check.  Existence test. */
2907b782e0a6SEric Paris 	if (!mask)
29081da177e4SLinus Torvalds 		return 0;
29091da177e4SLinus Torvalds 
29102e334057SEric Paris 	validate_creds(cred);
2911b782e0a6SEric Paris 
29122e334057SEric Paris 	if (unlikely(IS_PRIVATE(inode)))
29132e334057SEric Paris 		return 0;
2914b782e0a6SEric Paris 
2915b782e0a6SEric Paris 	perms = file_mask_to_av(inode->i_mode, mask);
2916b782e0a6SEric Paris 
29172e334057SEric Paris 	sid = cred_sid(cred);
29182e334057SEric Paris 	isec = inode->i_security;
29192e334057SEric Paris 
29202e334057SEric Paris 	rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
29212e334057SEric Paris 	audited = avc_audit_required(perms, &avd, rc,
29222e334057SEric Paris 				     from_access ? FILE__AUDIT_ACCESS : 0,
29232e334057SEric Paris 				     &denied);
29242e334057SEric Paris 	if (likely(!audited))
29252e334057SEric Paris 		return rc;
29262e334057SEric Paris 
2927626b9740SStephen Smalley 	rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
29282e334057SEric Paris 	if (rc2)
29292e334057SEric Paris 		return rc2;
29302e334057SEric Paris 	return rc;
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
29341da177e4SLinus Torvalds {
293588e67f3bSDavid Howells 	const struct cred *cred = current_cred();
2936bc6a6008SAmerigo Wang 	unsigned int ia_valid = iattr->ia_valid;
293795dbf739SEric Paris 	__u32 av = FILE__WRITE;
29381da177e4SLinus Torvalds 
2939bc6a6008SAmerigo Wang 	/* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2940bc6a6008SAmerigo Wang 	if (ia_valid & ATTR_FORCE) {
2941bc6a6008SAmerigo Wang 		ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2942bc6a6008SAmerigo Wang 			      ATTR_FORCE);
2943bc6a6008SAmerigo Wang 		if (!ia_valid)
29441da177e4SLinus Torvalds 			return 0;
2945bc6a6008SAmerigo Wang 	}
29461da177e4SLinus Torvalds 
2947bc6a6008SAmerigo Wang 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2948bc6a6008SAmerigo Wang 			ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
29492875fa00SEric Paris 		return dentry_has_perm(cred, dentry, FILE__SETATTR);
29501da177e4SLinus Torvalds 
29513d2195c3SEric Paris 	if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
295295dbf739SEric Paris 		av |= FILE__OPEN;
295395dbf739SEric Paris 
295495dbf739SEric Paris 	return dentry_has_perm(cred, dentry, av);
29551da177e4SLinus Torvalds }
29561da177e4SLinus Torvalds 
2957*3f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path)
29581da177e4SLinus Torvalds {
2959*3f7036a0SAl Viro 	return path_has_perm(current_cred(), path, FILE__GETATTR);
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
29628f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2963b5376771SSerge E. Hallyn {
296488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
296588e67f3bSDavid Howells 
2966b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2967b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2968b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2969b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2970b5376771SSerge E. Hallyn 				return -EPERM;
2971b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2972b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2973b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2974b5376771SSerge E. Hallyn 			return -EPERM;
2975b5376771SSerge E. Hallyn 		}
2976b5376771SSerge E. Hallyn 	}
2977b5376771SSerge E. Hallyn 
2978b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2979b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
29802875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__SETATTR);
2981b5376771SSerge E. Hallyn }
2982b5376771SSerge E. Hallyn 
29838f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
29848f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
29851da177e4SLinus Torvalds {
29861da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
29871da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
29881da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
29892bf49690SThomas Liu 	struct common_audit_data ad;
2990275bb41eSDavid Howells 	u32 newsid, sid = current_sid();
29911da177e4SLinus Torvalds 	int rc = 0;
29921da177e4SLinus Torvalds 
2993b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2994b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
299712f348b9SEric Paris 	if (!(sbsec->flags & SBLABEL_MNT))
29981da177e4SLinus Torvalds 		return -EOPNOTSUPP;
29991da177e4SLinus Torvalds 
30002e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
30011da177e4SLinus Torvalds 		return -EPERM;
30021da177e4SLinus Torvalds 
300350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_DENTRY;
3004a269434dSEric Paris 	ad.u.dentry = dentry;
30051da177e4SLinus Torvalds 
3006275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, isec->sclass,
30071da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
30081da177e4SLinus Torvalds 	if (rc)
30091da177e4SLinus Torvalds 		return rc;
30101da177e4SLinus Torvalds 
301152a4c640SNikolay Aleksandrov 	rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
301212b29f34SStephen Smalley 	if (rc == -EINVAL) {
3013d6ea83ecSEric Paris 		if (!capable(CAP_MAC_ADMIN)) {
3014d6ea83ecSEric Paris 			struct audit_buffer *ab;
3015d6ea83ecSEric Paris 			size_t audit_size;
3016d6ea83ecSEric Paris 			const char *str;
3017d6ea83ecSEric Paris 
3018d6ea83ecSEric Paris 			/* We strip a nul only if it is at the end, otherwise the
3019d6ea83ecSEric Paris 			 * context contains a nul and we should audit that */
3020e3fea3f7SAl Viro 			if (value) {
3021d6ea83ecSEric Paris 				str = value;
3022d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
3023d6ea83ecSEric Paris 					audit_size = size - 1;
3024d6ea83ecSEric Paris 				else
3025d6ea83ecSEric Paris 					audit_size = size;
3026e3fea3f7SAl Viro 			} else {
3027e3fea3f7SAl Viro 				str = "";
3028e3fea3f7SAl Viro 				audit_size = 0;
3029e3fea3f7SAl Viro 			}
3030d6ea83ecSEric Paris 			ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
3031d6ea83ecSEric Paris 			audit_log_format(ab, "op=setxattr invalid_context=");
3032d6ea83ecSEric Paris 			audit_log_n_untrustedstring(ab, value, audit_size);
3033d6ea83ecSEric Paris 			audit_log_end(ab);
3034d6ea83ecSEric Paris 
303512b29f34SStephen Smalley 			return rc;
3036d6ea83ecSEric Paris 		}
303712b29f34SStephen Smalley 		rc = security_context_to_sid_force(value, size, &newsid);
303812b29f34SStephen Smalley 	}
30391da177e4SLinus Torvalds 	if (rc)
30401da177e4SLinus Torvalds 		return rc;
30411da177e4SLinus Torvalds 
3042275bb41eSDavid Howells 	rc = avc_has_perm(sid, newsid, isec->sclass,
30431da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
30441da177e4SLinus Torvalds 	if (rc)
30451da177e4SLinus Torvalds 		return rc;
30461da177e4SLinus Torvalds 
3047275bb41eSDavid Howells 	rc = security_validate_transition(isec->sid, newsid, sid,
30481da177e4SLinus Torvalds 					  isec->sclass);
30491da177e4SLinus Torvalds 	if (rc)
30501da177e4SLinus Torvalds 		return rc;
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	return avc_has_perm(newsid,
30531da177e4SLinus Torvalds 			    sbsec->sid,
30541da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
30551da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
30561da177e4SLinus Torvalds 			    &ad);
30571da177e4SLinus Torvalds }
30581da177e4SLinus Torvalds 
30598f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
30608f0cfa52SDavid Howells 					const void *value, size_t size,
30618f0cfa52SDavid Howells 					int flags)
30621da177e4SLinus Torvalds {
30631da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
30641da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
30651da177e4SLinus Torvalds 	u32 newsid;
30661da177e4SLinus Torvalds 	int rc;
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
30691da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
30701da177e4SLinus Torvalds 		return;
30711da177e4SLinus Torvalds 	}
30721da177e4SLinus Torvalds 
307312b29f34SStephen Smalley 	rc = security_context_to_sid_force(value, size, &newsid);
30741da177e4SLinus Torvalds 	if (rc) {
307512b29f34SStephen Smalley 		printk(KERN_ERR "SELinux:  unable to map context to SID"
307612b29f34SStephen Smalley 		       "for (%s, %lu), rc=%d\n",
307712b29f34SStephen Smalley 		       inode->i_sb->s_id, inode->i_ino, -rc);
30781da177e4SLinus Torvalds 		return;
30791da177e4SLinus Torvalds 	}
30801da177e4SLinus Torvalds 
3081aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
30821da177e4SLinus Torvalds 	isec->sid = newsid;
3083aa9c2669SDavid Quigley 	isec->initialized = 1;
3084aa9c2669SDavid Quigley 
30851da177e4SLinus Torvalds 	return;
30861da177e4SLinus Torvalds }
30871da177e4SLinus Torvalds 
30888f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
30891da177e4SLinus Torvalds {
309088e67f3bSDavid Howells 	const struct cred *cred = current_cred();
309188e67f3bSDavid Howells 
30922875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
30931da177e4SLinus Torvalds }
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry)
30961da177e4SLinus Torvalds {
309788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
309888e67f3bSDavid Howells 
30992875fa00SEric Paris 	return dentry_has_perm(cred, dentry, FILE__GETATTR);
31001da177e4SLinus Torvalds }
31011da177e4SLinus Torvalds 
31028f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
31031da177e4SLinus Torvalds {
3104b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
3105b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
31081da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
31091da177e4SLinus Torvalds 	return -EACCES;
31101da177e4SLinus Torvalds }
31111da177e4SLinus Torvalds 
3112d381d8a9SJames Morris /*
3113abc69bb6SStephen Smalley  * Copy the inode security context value to the user.
3114d381d8a9SJames Morris  *
3115d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
3116d381d8a9SJames Morris  */
311742492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
31181da177e4SLinus Torvalds {
311942492594SDavid P. Quigley 	u32 size;
312042492594SDavid P. Quigley 	int error;
312142492594SDavid P. Quigley 	char *context = NULL;
31221da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
31231da177e4SLinus Torvalds 
31248c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
31258c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
31261da177e4SLinus Torvalds 
3127abc69bb6SStephen Smalley 	/*
3128abc69bb6SStephen Smalley 	 * If the caller has CAP_MAC_ADMIN, then get the raw context
3129abc69bb6SStephen Smalley 	 * value even if it is not defined by current policy; otherwise,
3130abc69bb6SStephen Smalley 	 * use the in-core value under current policy.
3131abc69bb6SStephen Smalley 	 * Use the non-auditing forms of the permission checks since
3132abc69bb6SStephen Smalley 	 * getxattr may be called by unprivileged processes commonly
3133abc69bb6SStephen Smalley 	 * and lack of permission just means that we fall back to the
3134abc69bb6SStephen Smalley 	 * in-core context value, not a denial.
3135abc69bb6SStephen Smalley 	 */
31366a9de491SEric Paris 	error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
31373699c53cSDavid Howells 				SECURITY_CAP_NOAUDIT);
3138abc69bb6SStephen Smalley 	if (!error)
3139abc69bb6SStephen Smalley 		error = security_sid_to_context_force(isec->sid, &context,
3140abc69bb6SStephen Smalley 						      &size);
3141abc69bb6SStephen Smalley 	else
314242492594SDavid P. Quigley 		error = security_sid_to_context(isec->sid, &context, &size);
314342492594SDavid P. Quigley 	if (error)
314442492594SDavid P. Quigley 		return error;
314542492594SDavid P. Quigley 	error = size;
314642492594SDavid P. Quigley 	if (alloc) {
314742492594SDavid P. Quigley 		*buffer = context;
314842492594SDavid P. Quigley 		goto out_nofree;
314942492594SDavid P. Quigley 	}
315042492594SDavid P. Quigley 	kfree(context);
315142492594SDavid P. Quigley out_nofree:
315242492594SDavid P. Quigley 	return error;
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
31561da177e4SLinus Torvalds 				     const void *value, size_t size, int flags)
31571da177e4SLinus Torvalds {
31581da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
31591da177e4SLinus Torvalds 	u32 newsid;
31601da177e4SLinus Torvalds 	int rc;
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
31631da177e4SLinus Torvalds 		return -EOPNOTSUPP;
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 	if (!value || !size)
31661da177e4SLinus Torvalds 		return -EACCES;
31671da177e4SLinus Torvalds 
316852a4c640SNikolay Aleksandrov 	rc = security_context_to_sid((void *)value, size, &newsid, GFP_KERNEL);
31691da177e4SLinus Torvalds 	if (rc)
31701da177e4SLinus Torvalds 		return rc;
31711da177e4SLinus Torvalds 
3172aa9c2669SDavid Quigley 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
31731da177e4SLinus Torvalds 	isec->sid = newsid;
3174ddd29ec6SDavid P. Quigley 	isec->initialized = 1;
31751da177e4SLinus Torvalds 	return 0;
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
31791da177e4SLinus Torvalds {
31801da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
31811da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
31821da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
31831da177e4SLinus Torvalds 	return len;
31841da177e4SLinus Torvalds }
31851da177e4SLinus Torvalds 
3186713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3187713a04aeSAhmed S. Darwish {
3188713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
3189713a04aeSAhmed S. Darwish 	*secid = isec->sid;
3190713a04aeSAhmed S. Darwish }
3191713a04aeSAhmed S. Darwish 
31921da177e4SLinus Torvalds /* file security operations */
31931da177e4SLinus Torvalds 
3194788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
31951da177e4SLinus Torvalds {
319688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3197496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
32001da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
32011da177e4SLinus Torvalds 		mask |= MAY_APPEND;
32021da177e4SLinus Torvalds 
3203389fb800SPaul Moore 	return file_has_perm(cred, file,
32041da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
32051da177e4SLinus Torvalds }
32061da177e4SLinus Torvalds 
3207788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
3208788e7dd4SYuichi Nakamura {
3209496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
321020dda18bSStephen Smalley 	struct file_security_struct *fsec = file->f_security;
321120dda18bSStephen Smalley 	struct inode_security_struct *isec = inode->i_security;
321220dda18bSStephen Smalley 	u32 sid = current_sid();
321320dda18bSStephen Smalley 
3214389fb800SPaul Moore 	if (!mask)
3215788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
3216788e7dd4SYuichi Nakamura 		return 0;
3217788e7dd4SYuichi Nakamura 
321820dda18bSStephen Smalley 	if (sid == fsec->sid && fsec->isid == isec->sid &&
321920dda18bSStephen Smalley 	    fsec->pseqno == avc_policy_seqno())
322083d49856SEric Paris 		/* No change since file_open check. */
322120dda18bSStephen Smalley 		return 0;
322220dda18bSStephen Smalley 
3223788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
3224788e7dd4SYuichi Nakamura }
3225788e7dd4SYuichi Nakamura 
32261da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
32271da177e4SLinus Torvalds {
32281da177e4SLinus Torvalds 	return file_alloc_security(file);
32291da177e4SLinus Torvalds }
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	file_free_security(file);
32341da177e4SLinus Torvalds }
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
32371da177e4SLinus Torvalds 			      unsigned long arg)
32381da177e4SLinus Torvalds {
323988e67f3bSDavid Howells 	const struct cred *cred = current_cred();
32400b24dcb7SEric Paris 	int error = 0;
32411da177e4SLinus Torvalds 
32420b24dcb7SEric Paris 	switch (cmd) {
32430b24dcb7SEric Paris 	case FIONREAD:
32440b24dcb7SEric Paris 	/* fall through */
32450b24dcb7SEric Paris 	case FIBMAP:
32460b24dcb7SEric Paris 	/* fall through */
32470b24dcb7SEric Paris 	case FIGETBSZ:
32480b24dcb7SEric Paris 	/* fall through */
32492f99c369SAl Viro 	case FS_IOC_GETFLAGS:
32500b24dcb7SEric Paris 	/* fall through */
32512f99c369SAl Viro 	case FS_IOC_GETVERSION:
32520b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__GETATTR);
32530b24dcb7SEric Paris 		break;
32541da177e4SLinus Torvalds 
32552f99c369SAl Viro 	case FS_IOC_SETFLAGS:
32560b24dcb7SEric Paris 	/* fall through */
32572f99c369SAl Viro 	case FS_IOC_SETVERSION:
32580b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__SETATTR);
32590b24dcb7SEric Paris 		break;
32600b24dcb7SEric Paris 
32610b24dcb7SEric Paris 	/* sys_ioctl() checks */
32620b24dcb7SEric Paris 	case FIONBIO:
32630b24dcb7SEric Paris 	/* fall through */
32640b24dcb7SEric Paris 	case FIOASYNC:
32650b24dcb7SEric Paris 		error = file_has_perm(cred, file, 0);
32660b24dcb7SEric Paris 		break;
32670b24dcb7SEric Paris 
32680b24dcb7SEric Paris 	case KDSKBENT:
32690b24dcb7SEric Paris 	case KDSKBSENT:
32706a9de491SEric Paris 		error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
32710b24dcb7SEric Paris 					    SECURITY_CAP_AUDIT);
32720b24dcb7SEric Paris 		break;
32730b24dcb7SEric Paris 
32740b24dcb7SEric Paris 	/* default case assumes that the command will go
32750b24dcb7SEric Paris 	 * to the file's ioctl() function.
32760b24dcb7SEric Paris 	 */
32770b24dcb7SEric Paris 	default:
32780b24dcb7SEric Paris 		error = file_has_perm(cred, file, FILE__IOCTL);
32790b24dcb7SEric Paris 	}
32800b24dcb7SEric Paris 	return error;
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
3283fcaaade1SStephen Smalley static int default_noexec;
3284fcaaade1SStephen Smalley 
32851da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
32861da177e4SLinus Torvalds {
328788e67f3bSDavid Howells 	const struct cred *cred = current_cred();
3288d84f4f99SDavid Howells 	int rc = 0;
328988e67f3bSDavid Howells 
3290fcaaade1SStephen Smalley 	if (default_noexec &&
3291fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
32921da177e4SLinus Torvalds 		/*
32931da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
32941da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
32951da177e4SLinus Torvalds 		 * This has an additional check.
32961da177e4SLinus Torvalds 		 */
3297d84f4f99SDavid Howells 		rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
32981da177e4SLinus Torvalds 		if (rc)
3299d84f4f99SDavid Howells 			goto error;
33001da177e4SLinus Torvalds 	}
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 	if (file) {
33031da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
33041da177e4SLinus Torvalds 		u32 av = FILE__READ;
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
33071da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
33081da177e4SLinus Torvalds 			av |= FILE__WRITE;
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
33111da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
33121da177e4SLinus Torvalds 
331388e67f3bSDavid Howells 		return file_has_perm(cred, file, av);
33141da177e4SLinus Torvalds 	}
3315d84f4f99SDavid Howells 
3316d84f4f99SDavid Howells error:
3317d84f4f99SDavid Howells 	return rc;
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds 
3320e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr)
33211da177e4SLinus Torvalds {
332298883bfdSPaul Moore 	int rc;
332384336d1aSEric Paris 
332484336d1aSEric Paris 	/* do DAC check on address space usage */
332598883bfdSPaul Moore 	rc = cap_mmap_addr(addr);
332698883bfdSPaul Moore 	if (rc)
332798883bfdSPaul Moore 		return rc;
332898883bfdSPaul Moore 
332998883bfdSPaul Moore 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
333098883bfdSPaul Moore 		u32 sid = current_sid();
333198883bfdSPaul Moore 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
333298883bfdSPaul Moore 				  MEMPROTECT__MMAP_ZERO, NULL);
333398883bfdSPaul Moore 	}
333498883bfdSPaul Moore 
333598883bfdSPaul Moore 	return rc;
3336e5467859SAl Viro }
33371da177e4SLinus Torvalds 
3338e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3339e5467859SAl Viro 			     unsigned long prot, unsigned long flags)
3340e5467859SAl Viro {
33411da177e4SLinus Torvalds 	if (selinux_checkreqprot)
33421da177e4SLinus Torvalds 		prot = reqprot;
33431da177e4SLinus Torvalds 
33441da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
33451da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
33461da177e4SLinus Torvalds }
33471da177e4SLinus Torvalds 
33481da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
33491da177e4SLinus Torvalds 				 unsigned long reqprot,
33501da177e4SLinus Torvalds 				 unsigned long prot)
33511da177e4SLinus Torvalds {
335288e67f3bSDavid Howells 	const struct cred *cred = current_cred();
33531da177e4SLinus Torvalds 
33541da177e4SLinus Torvalds 	if (selinux_checkreqprot)
33551da177e4SLinus Torvalds 		prot = reqprot;
33561da177e4SLinus Torvalds 
3357fcaaade1SStephen Smalley 	if (default_noexec &&
3358fcaaade1SStephen Smalley 	    (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3359d541bbeeSJames Morris 		int rc = 0;
3360db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
3361db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
3362d84f4f99SDavid Howells 			rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3363db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
33646b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
33656b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
33663b11a1deSDavid Howells 			rc = current_has_perm(current, PROCESS__EXECSTACK);
3367db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
3368db4c9641SStephen Smalley 			/*
3369db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
3370db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
3371db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
3372db4c9641SStephen Smalley 			 * modified content.  This typically should only
3373db4c9641SStephen Smalley 			 * occur for text relocations.
3374db4c9641SStephen Smalley 			 */
3375d84f4f99SDavid Howells 			rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3376db4c9641SStephen Smalley 		}
33776b992197SLorenzo Hernandez García-Hierro 		if (rc)
33786b992197SLorenzo Hernandez García-Hierro 			return rc;
33796b992197SLorenzo Hernandez García-Hierro 	}
33801da177e4SLinus Torvalds 
33811da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
33821da177e4SLinus Torvalds }
33831da177e4SLinus Torvalds 
33841da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
33851da177e4SLinus Torvalds {
338688e67f3bSDavid Howells 	const struct cred *cred = current_cred();
338788e67f3bSDavid Howells 
338888e67f3bSDavid Howells 	return file_has_perm(cred, file, FILE__LOCK);
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
33921da177e4SLinus Torvalds 			      unsigned long arg)
33931da177e4SLinus Torvalds {
339488e67f3bSDavid Howells 	const struct cred *cred = current_cred();
33951da177e4SLinus Torvalds 	int err = 0;
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 	switch (cmd) {
33981da177e4SLinus Torvalds 	case F_SETFL:
33991da177e4SLinus Torvalds 		if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
340088e67f3bSDavid Howells 			err = file_has_perm(cred, file, FILE__WRITE);
34011da177e4SLinus Torvalds 			break;
34021da177e4SLinus Torvalds 		}
34031da177e4SLinus Torvalds 		/* fall through */
34041da177e4SLinus Torvalds 	case F_SETOWN:
34051da177e4SLinus Torvalds 	case F_SETSIG:
34061da177e4SLinus Torvalds 	case F_GETFL:
34071da177e4SLinus Torvalds 	case F_GETOWN:
34081da177e4SLinus Torvalds 	case F_GETSIG:
34091d151c33SCyrill Gorcunov 	case F_GETOWNER_UIDS:
34101da177e4SLinus Torvalds 		/* Just check FD__USE permission */
341188e67f3bSDavid Howells 		err = file_has_perm(cred, file, 0);
34121da177e4SLinus Torvalds 		break;
34131da177e4SLinus Torvalds 	case F_GETLK:
34141da177e4SLinus Torvalds 	case F_SETLK:
34151da177e4SLinus Torvalds 	case F_SETLKW:
34160d3f7a2dSJeff Layton 	case F_OFD_GETLK:
34170d3f7a2dSJeff Layton 	case F_OFD_SETLK:
34180d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
34191da177e4SLinus Torvalds #if BITS_PER_LONG == 32
34201da177e4SLinus Torvalds 	case F_GETLK64:
34211da177e4SLinus Torvalds 	case F_SETLK64:
34221da177e4SLinus Torvalds 	case F_SETLKW64:
34231da177e4SLinus Torvalds #endif
342488e67f3bSDavid Howells 		err = file_has_perm(cred, file, FILE__LOCK);
34251da177e4SLinus Torvalds 		break;
34261da177e4SLinus Torvalds 	}
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 	return err;
34291da177e4SLinus Torvalds }
34301da177e4SLinus Torvalds 
3431e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file)
34321da177e4SLinus Torvalds {
34331da177e4SLinus Torvalds 	struct file_security_struct *fsec;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	fsec = file->f_security;
3436275bb41eSDavid Howells 	fsec->fown_sid = current_sid();
34371da177e4SLinus Torvalds }
34381da177e4SLinus Torvalds 
34391da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
34401da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
34411da177e4SLinus Torvalds {
34421da177e4SLinus Torvalds 	struct file *file;
344365c90bcaSStephen Smalley 	u32 sid = task_sid(tsk);
34441da177e4SLinus Torvalds 	u32 perm;
34451da177e4SLinus Torvalds 	struct file_security_struct *fsec;
34461da177e4SLinus Torvalds 
34471da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3448b385a144SRobert P. J. Day 	file = container_of(fown, struct file, f_owner);
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 	fsec = file->f_security;
34511da177e4SLinus Torvalds 
34521da177e4SLinus Torvalds 	if (!signum)
34531da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
34541da177e4SLinus Torvalds 	else
34551da177e4SLinus Torvalds 		perm = signal_to_av(signum);
34561da177e4SLinus Torvalds 
3457275bb41eSDavid Howells 	return avc_has_perm(fsec->fown_sid, sid,
34581da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
34591da177e4SLinus Torvalds }
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
34621da177e4SLinus Torvalds {
346388e67f3bSDavid Howells 	const struct cred *cred = current_cred();
346488e67f3bSDavid Howells 
346588e67f3bSDavid Howells 	return file_has_perm(cred, file, file_to_av(file));
34661da177e4SLinus Torvalds }
34671da177e4SLinus Torvalds 
346883d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred)
3469788e7dd4SYuichi Nakamura {
3470788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3471788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3472d84f4f99SDavid Howells 
3473788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3474496ad9aaSAl Viro 	isec = file_inode(file)->i_security;
3475788e7dd4SYuichi Nakamura 	/*
3476788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3477788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3478788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3479788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3480788e7dd4SYuichi Nakamura 	 * struct as its SID.
3481788e7dd4SYuichi Nakamura 	 */
3482788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3483788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3484788e7dd4SYuichi Nakamura 	/*
3485788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3486788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3487788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3488788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3489788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3490788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3491788e7dd4SYuichi Nakamura 	 */
349213f8e981SDavid Howells 	return file_path_has_perm(cred, file, open_file_to_av(file));
3493788e7dd4SYuichi Nakamura }
3494788e7dd4SYuichi Nakamura 
34951da177e4SLinus Torvalds /* task security operations */
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
34981da177e4SLinus Torvalds {
34993b11a1deSDavid Howells 	return current_has_perm(current, PROCESS__FORK);
35001da177e4SLinus Torvalds }
35011da177e4SLinus Torvalds 
3502f1752eecSDavid Howells /*
3503ee18d64cSDavid Howells  * allocate the SELinux part of blank credentials
3504ee18d64cSDavid Howells  */
3505ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3506ee18d64cSDavid Howells {
3507ee18d64cSDavid Howells 	struct task_security_struct *tsec;
3508ee18d64cSDavid Howells 
3509ee18d64cSDavid Howells 	tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3510ee18d64cSDavid Howells 	if (!tsec)
3511ee18d64cSDavid Howells 		return -ENOMEM;
3512ee18d64cSDavid Howells 
3513ee18d64cSDavid Howells 	cred->security = tsec;
3514ee18d64cSDavid Howells 	return 0;
3515ee18d64cSDavid Howells }
3516ee18d64cSDavid Howells 
3517ee18d64cSDavid Howells /*
3518f1752eecSDavid Howells  * detach and free the LSM part of a set of credentials
3519f1752eecSDavid Howells  */
3520f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred)
35211da177e4SLinus Torvalds {
3522f1752eecSDavid Howells 	struct task_security_struct *tsec = cred->security;
3523e0e81739SDavid Howells 
35242edeaa34STetsuo Handa 	/*
35252edeaa34STetsuo Handa 	 * cred->security == NULL if security_cred_alloc_blank() or
35262edeaa34STetsuo Handa 	 * security_prepare_creds() returned an error.
35272edeaa34STetsuo Handa 	 */
35282edeaa34STetsuo Handa 	BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3529e0e81739SDavid Howells 	cred->security = (void *) 0x7UL;
3530f1752eecSDavid Howells 	kfree(tsec);
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
3533d84f4f99SDavid Howells /*
3534d84f4f99SDavid Howells  * prepare a new set of credentials for modification
3535d84f4f99SDavid Howells  */
3536d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3537d84f4f99SDavid Howells 				gfp_t gfp)
3538d84f4f99SDavid Howells {
3539d84f4f99SDavid Howells 	const struct task_security_struct *old_tsec;
3540d84f4f99SDavid Howells 	struct task_security_struct *tsec;
3541d84f4f99SDavid Howells 
3542d84f4f99SDavid Howells 	old_tsec = old->security;
3543d84f4f99SDavid Howells 
3544d84f4f99SDavid Howells 	tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3545d84f4f99SDavid Howells 	if (!tsec)
3546d84f4f99SDavid Howells 		return -ENOMEM;
3547d84f4f99SDavid Howells 
3548d84f4f99SDavid Howells 	new->security = tsec;
3549d84f4f99SDavid Howells 	return 0;
3550d84f4f99SDavid Howells }
3551d84f4f99SDavid Howells 
3552d84f4f99SDavid Howells /*
3553ee18d64cSDavid Howells  * transfer the SELinux data to a blank set of creds
3554ee18d64cSDavid Howells  */
3555ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3556ee18d64cSDavid Howells {
3557ee18d64cSDavid Howells 	const struct task_security_struct *old_tsec = old->security;
3558ee18d64cSDavid Howells 	struct task_security_struct *tsec = new->security;
3559ee18d64cSDavid Howells 
3560ee18d64cSDavid Howells 	*tsec = *old_tsec;
3561ee18d64cSDavid Howells }
3562ee18d64cSDavid Howells 
3563ee18d64cSDavid Howells /*
35643a3b7ce9SDavid Howells  * set the security data for a kernel service
35653a3b7ce9SDavid Howells  * - all the creation contexts are set to unlabelled
35663a3b7ce9SDavid Howells  */
35673a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid)
35683a3b7ce9SDavid Howells {
35693a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
35703a3b7ce9SDavid Howells 	u32 sid = current_sid();
35713a3b7ce9SDavid Howells 	int ret;
35723a3b7ce9SDavid Howells 
35733a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, secid,
35743a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
35753a3b7ce9SDavid Howells 			   KERNEL_SERVICE__USE_AS_OVERRIDE,
35763a3b7ce9SDavid Howells 			   NULL);
35773a3b7ce9SDavid Howells 	if (ret == 0) {
35783a3b7ce9SDavid Howells 		tsec->sid = secid;
35793a3b7ce9SDavid Howells 		tsec->create_sid = 0;
35803a3b7ce9SDavid Howells 		tsec->keycreate_sid = 0;
35813a3b7ce9SDavid Howells 		tsec->sockcreate_sid = 0;
35823a3b7ce9SDavid Howells 	}
35833a3b7ce9SDavid Howells 	return ret;
35843a3b7ce9SDavid Howells }
35853a3b7ce9SDavid Howells 
35863a3b7ce9SDavid Howells /*
35873a3b7ce9SDavid Howells  * set the file creation context in a security record to the same as the
35883a3b7ce9SDavid Howells  * objective context of the specified inode
35893a3b7ce9SDavid Howells  */
35903a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
35913a3b7ce9SDavid Howells {
35923a3b7ce9SDavid Howells 	struct inode_security_struct *isec = inode->i_security;
35933a3b7ce9SDavid Howells 	struct task_security_struct *tsec = new->security;
35943a3b7ce9SDavid Howells 	u32 sid = current_sid();
35953a3b7ce9SDavid Howells 	int ret;
35963a3b7ce9SDavid Howells 
35973a3b7ce9SDavid Howells 	ret = avc_has_perm(sid, isec->sid,
35983a3b7ce9SDavid Howells 			   SECCLASS_KERNEL_SERVICE,
35993a3b7ce9SDavid Howells 			   KERNEL_SERVICE__CREATE_FILES_AS,
36003a3b7ce9SDavid Howells 			   NULL);
36013a3b7ce9SDavid Howells 
36023a3b7ce9SDavid Howells 	if (ret == 0)
36033a3b7ce9SDavid Howells 		tsec->create_sid = isec->sid;
3604ef57471aSDavid Howells 	return ret;
36053a3b7ce9SDavid Howells }
36063a3b7ce9SDavid Howells 
3607dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name)
360825354c4fSEric Paris {
3609dd8dbf2eSEric Paris 	u32 sid;
3610dd8dbf2eSEric Paris 	struct common_audit_data ad;
3611dd8dbf2eSEric Paris 
3612dd8dbf2eSEric Paris 	sid = task_sid(current);
3613dd8dbf2eSEric Paris 
361450c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_KMOD;
3615dd8dbf2eSEric Paris 	ad.u.kmod_name = kmod_name;
3616dd8dbf2eSEric Paris 
3617dd8dbf2eSEric Paris 	return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3618dd8dbf2eSEric Paris 			    SYSTEM__MODULE_REQUEST, &ad);
361925354c4fSEric Paris }
362025354c4fSEric Paris 
36211da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
36221da177e4SLinus Torvalds {
36233b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETPGID);
36241da177e4SLinus Torvalds }
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
36271da177e4SLinus Torvalds {
36283b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETPGID);
36291da177e4SLinus Torvalds }
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
36321da177e4SLinus Torvalds {
36333b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSESSION);
36341da177e4SLinus Torvalds }
36351da177e4SLinus Torvalds 
3636f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3637f9008e4cSDavid Quigley {
3638275bb41eSDavid Howells 	*secid = task_sid(p);
3639f9008e4cSDavid Quigley }
3640f9008e4cSDavid Quigley 
36411da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
36421da177e4SLinus Torvalds {
36431da177e4SLinus Torvalds 	int rc;
36441da177e4SLinus Torvalds 
3645200ac532SEric Paris 	rc = cap_task_setnice(p, nice);
36461da177e4SLinus Torvalds 	if (rc)
36471da177e4SLinus Torvalds 		return rc;
36481da177e4SLinus Torvalds 
36493b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
36501da177e4SLinus Torvalds }
36511da177e4SLinus Torvalds 
365203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
365303e68060SJames Morris {
3654b5376771SSerge E. Hallyn 	int rc;
3655b5376771SSerge E. Hallyn 
3656200ac532SEric Paris 	rc = cap_task_setioprio(p, ioprio);
3657b5376771SSerge E. Hallyn 	if (rc)
3658b5376771SSerge E. Hallyn 		return rc;
3659b5376771SSerge E. Hallyn 
36603b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
366103e68060SJames Morris }
366203e68060SJames Morris 
3663a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3664a1836a42SDavid Quigley {
36653b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
3666a1836a42SDavid Quigley }
3667a1836a42SDavid Quigley 
36688fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
36698fd00b4dSJiri Slaby 		struct rlimit *new_rlim)
36701da177e4SLinus Torvalds {
36718fd00b4dSJiri Slaby 	struct rlimit *old_rlim = p->signal->rlim + resource;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
36741da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
36751da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
3676d84f4f99SDavid Howells 	   upon context transitions.  See selinux_bprm_committing_creds. */
36771da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
36788fd00b4dSJiri Slaby 		return current_has_perm(p, PROCESS__SETRLIMIT);
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds 	return 0;
36811da177e4SLinus Torvalds }
36821da177e4SLinus Torvalds 
3683b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p)
36841da177e4SLinus Torvalds {
3685b5376771SSerge E. Hallyn 	int rc;
3686b5376771SSerge E. Hallyn 
3687b0ae1981SKOSAKI Motohiro 	rc = cap_task_setscheduler(p);
3688b5376771SSerge E. Hallyn 	if (rc)
3689b5376771SSerge E. Hallyn 		return rc;
3690b5376771SSerge E. Hallyn 
36913b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
36921da177e4SLinus Torvalds }
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
36951da177e4SLinus Torvalds {
36963b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__GETSCHED);
36971da177e4SLinus Torvalds }
36981da177e4SLinus Torvalds 
369935601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
370035601547SDavid Quigley {
37013b11a1deSDavid Howells 	return current_has_perm(p, PROCESS__SETSCHED);
370235601547SDavid Quigley }
370335601547SDavid Quigley 
3704f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3705f9008e4cSDavid Quigley 				int sig, u32 secid)
37061da177e4SLinus Torvalds {
37071da177e4SLinus Torvalds 	u32 perm;
37081da177e4SLinus Torvalds 	int rc;
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 	if (!sig)
37111da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
37121da177e4SLinus Torvalds 	else
37131da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3714f9008e4cSDavid Quigley 	if (secid)
3715275bb41eSDavid Howells 		rc = avc_has_perm(secid, task_sid(p),
3716275bb41eSDavid Howells 				  SECCLASS_PROCESS, perm, NULL);
3717f9008e4cSDavid Quigley 	else
37183b11a1deSDavid Howells 		rc = current_has_perm(p, perm);
3719f9008e4cSDavid Quigley 	return rc;
37201da177e4SLinus Torvalds }
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
37231da177e4SLinus Torvalds {
37248a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
37251da177e4SLinus Torvalds }
37261da177e4SLinus Torvalds 
37271da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
37281da177e4SLinus Torvalds 				  struct inode *inode)
37291da177e4SLinus Torvalds {
37301da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
3731275bb41eSDavid Howells 	u32 sid = task_sid(p);
37321da177e4SLinus Torvalds 
3733275bb41eSDavid Howells 	isec->sid = sid;
37341da177e4SLinus Torvalds 	isec->initialized = 1;
37351da177e4SLinus Torvalds }
37361da177e4SLinus Torvalds 
37371da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
373867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
37392bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
37401da177e4SLinus Torvalds {
37411da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
37421da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
37431da177e4SLinus Torvalds 
3744bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
37451da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
37461da177e4SLinus Torvalds 	if (ih == NULL)
37471da177e4SLinus Torvalds 		goto out;
37481da177e4SLinus Torvalds 
37491da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
37501da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
37511da177e4SLinus Torvalds 		goto out;
37521da177e4SLinus Torvalds 
375348c62af6SEric Paris 	ad->u.net->v4info.saddr = ih->saddr;
375448c62af6SEric Paris 	ad->u.net->v4info.daddr = ih->daddr;
37551da177e4SLinus Torvalds 	ret = 0;
37561da177e4SLinus Torvalds 
375767f83cbfSVenkat Yekkirala 	if (proto)
375867f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
375967f83cbfSVenkat Yekkirala 
37601da177e4SLinus Torvalds 	switch (ih->protocol) {
37611da177e4SLinus Torvalds 	case IPPROTO_TCP: {
37621da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
37651da177e4SLinus Torvalds 			break;
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds 		offset += ihlen;
37681da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
37691da177e4SLinus Torvalds 		if (th == NULL)
37701da177e4SLinus Torvalds 			break;
37711da177e4SLinus Torvalds 
377248c62af6SEric Paris 		ad->u.net->sport = th->source;
377348c62af6SEric Paris 		ad->u.net->dport = th->dest;
37741da177e4SLinus Torvalds 		break;
37751da177e4SLinus Torvalds 	}
37761da177e4SLinus Torvalds 
37771da177e4SLinus Torvalds 	case IPPROTO_UDP: {
37781da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
37791da177e4SLinus Torvalds 
37801da177e4SLinus Torvalds 		if (ntohs(ih->frag_off) & IP_OFFSET)
37811da177e4SLinus Torvalds 			break;
37821da177e4SLinus Torvalds 
37831da177e4SLinus Torvalds 		offset += ihlen;
37841da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
37851da177e4SLinus Torvalds 		if (uh == NULL)
37861da177e4SLinus Torvalds 			break;
37871da177e4SLinus Torvalds 
378848c62af6SEric Paris 		ad->u.net->sport = uh->source;
378948c62af6SEric Paris 		ad->u.net->dport = uh->dest;
37901da177e4SLinus Torvalds 		break;
37911da177e4SLinus Torvalds 	}
37921da177e4SLinus Torvalds 
37932ee92d46SJames Morris 	case IPPROTO_DCCP: {
37942ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
37952ee92d46SJames Morris 
37962ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
37972ee92d46SJames Morris 			break;
37982ee92d46SJames Morris 
37992ee92d46SJames Morris 		offset += ihlen;
38002ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
38012ee92d46SJames Morris 		if (dh == NULL)
38022ee92d46SJames Morris 			break;
38032ee92d46SJames Morris 
380448c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
380548c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
38062ee92d46SJames Morris 		break;
38072ee92d46SJames Morris 	}
38082ee92d46SJames Morris 
38091da177e4SLinus Torvalds 	default:
38101da177e4SLinus Torvalds 		break;
38111da177e4SLinus Torvalds 	}
38121da177e4SLinus Torvalds out:
38131da177e4SLinus Torvalds 	return ret;
38141da177e4SLinus Torvalds }
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
381967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
38202bf49690SThomas Liu 			struct common_audit_data *ad, u8 *proto)
38211da177e4SLinus Torvalds {
38221da177e4SLinus Torvalds 	u8 nexthdr;
38231da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
38241da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
382575f2811cSJesse Gross 	__be16 frag_off;
38261da177e4SLinus Torvalds 
3827bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
38281da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
38291da177e4SLinus Torvalds 	if (ip6 == NULL)
38301da177e4SLinus Torvalds 		goto out;
38311da177e4SLinus Torvalds 
383248c62af6SEric Paris 	ad->u.net->v6info.saddr = ip6->saddr;
383348c62af6SEric Paris 	ad->u.net->v6info.daddr = ip6->daddr;
38341da177e4SLinus Torvalds 	ret = 0;
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
38371da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
383875f2811cSJesse Gross 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
38391da177e4SLinus Torvalds 	if (offset < 0)
38401da177e4SLinus Torvalds 		goto out;
38411da177e4SLinus Torvalds 
384267f83cbfSVenkat Yekkirala 	if (proto)
384367f83cbfSVenkat Yekkirala 		*proto = nexthdr;
384467f83cbfSVenkat Yekkirala 
38451da177e4SLinus Torvalds 	switch (nexthdr) {
38461da177e4SLinus Torvalds 	case IPPROTO_TCP: {
38471da177e4SLinus Torvalds 		struct tcphdr _tcph, *th;
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
38501da177e4SLinus Torvalds 		if (th == NULL)
38511da177e4SLinus Torvalds 			break;
38521da177e4SLinus Torvalds 
385348c62af6SEric Paris 		ad->u.net->sport = th->source;
385448c62af6SEric Paris 		ad->u.net->dport = th->dest;
38551da177e4SLinus Torvalds 		break;
38561da177e4SLinus Torvalds 	}
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 	case IPPROTO_UDP: {
38591da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
38621da177e4SLinus Torvalds 		if (uh == NULL)
38631da177e4SLinus Torvalds 			break;
38641da177e4SLinus Torvalds 
386548c62af6SEric Paris 		ad->u.net->sport = uh->source;
386648c62af6SEric Paris 		ad->u.net->dport = uh->dest;
38671da177e4SLinus Torvalds 		break;
38681da177e4SLinus Torvalds 	}
38691da177e4SLinus Torvalds 
38702ee92d46SJames Morris 	case IPPROTO_DCCP: {
38712ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
38722ee92d46SJames Morris 
38732ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
38742ee92d46SJames Morris 		if (dh == NULL)
38752ee92d46SJames Morris 			break;
38762ee92d46SJames Morris 
387748c62af6SEric Paris 		ad->u.net->sport = dh->dccph_sport;
387848c62af6SEric Paris 		ad->u.net->dport = dh->dccph_dport;
38792ee92d46SJames Morris 		break;
38802ee92d46SJames Morris 	}
38812ee92d46SJames Morris 
38821da177e4SLinus Torvalds 	/* includes fragments */
38831da177e4SLinus Torvalds 	default:
38841da177e4SLinus Torvalds 		break;
38851da177e4SLinus Torvalds 	}
38861da177e4SLinus Torvalds out:
38871da177e4SLinus Torvalds 	return ret;
38881da177e4SLinus Torvalds }
38891da177e4SLinus Torvalds 
38901da177e4SLinus Torvalds #endif /* IPV6 */
38911da177e4SLinus Torvalds 
38922bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3893cf9481e2SDavid Howells 			     char **_addrp, int src, u8 *proto)
38941da177e4SLinus Torvalds {
3895cf9481e2SDavid Howells 	char *addrp;
3896cf9481e2SDavid Howells 	int ret;
38971da177e4SLinus Torvalds 
389848c62af6SEric Paris 	switch (ad->u.net->family) {
38991da177e4SLinus Torvalds 	case PF_INET:
390067f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
3901cf9481e2SDavid Howells 		if (ret)
3902cf9481e2SDavid Howells 			goto parse_error;
390348c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v4info.saddr :
390448c62af6SEric Paris 				       &ad->u.net->v4info.daddr);
3905cf9481e2SDavid Howells 		goto okay;
39061da177e4SLinus Torvalds 
39071da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
39081da177e4SLinus Torvalds 	case PF_INET6:
390967f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
3910cf9481e2SDavid Howells 		if (ret)
3911cf9481e2SDavid Howells 			goto parse_error;
391248c62af6SEric Paris 		addrp = (char *)(src ? &ad->u.net->v6info.saddr :
391348c62af6SEric Paris 				       &ad->u.net->v6info.daddr);
3914cf9481e2SDavid Howells 		goto okay;
39151da177e4SLinus Torvalds #endif	/* IPV6 */
39161da177e4SLinus Torvalds 	default:
3917cf9481e2SDavid Howells 		addrp = NULL;
3918cf9481e2SDavid Howells 		goto okay;
39191da177e4SLinus Torvalds 	}
39201da177e4SLinus Torvalds 
3921cf9481e2SDavid Howells parse_error:
392271f1cb05SPaul Moore 	printk(KERN_WARNING
392371f1cb05SPaul Moore 	       "SELinux: failure in selinux_parse_skb(),"
392471f1cb05SPaul Moore 	       " unable to parse packet\n");
39251da177e4SLinus Torvalds 	return ret;
3926cf9481e2SDavid Howells 
3927cf9481e2SDavid Howells okay:
3928cf9481e2SDavid Howells 	if (_addrp)
3929cf9481e2SDavid Howells 		*_addrp = addrp;
3930cf9481e2SDavid Howells 	return 0;
39311da177e4SLinus Torvalds }
39321da177e4SLinus Torvalds 
39334f6a993fSPaul Moore /**
3934220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
39354f6a993fSPaul Moore  * @skb: the packet
393675e22910SPaul Moore  * @family: protocol family
3937220deb96SPaul Moore  * @sid: the packet's peer label SID
39384f6a993fSPaul Moore  *
39394f6a993fSPaul Moore  * Description:
3940220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3941220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3942220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3943220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3944220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3945220deb96SPaul Moore  * peer labels.
39464f6a993fSPaul Moore  *
39474f6a993fSPaul Moore  */
3948220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
39494f6a993fSPaul Moore {
395071f1cb05SPaul Moore 	int err;
39514f6a993fSPaul Moore 	u32 xfrm_sid;
39524f6a993fSPaul Moore 	u32 nlbl_sid;
3953220deb96SPaul Moore 	u32 nlbl_type;
39544f6a993fSPaul Moore 
3955817eff71SPaul Moore 	err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
3956bed4d7efSPaul Moore 	if (unlikely(err))
3957bed4d7efSPaul Moore 		return -EACCES;
3958bed4d7efSPaul Moore 	err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3959bed4d7efSPaul Moore 	if (unlikely(err))
3960bed4d7efSPaul Moore 		return -EACCES;
3961220deb96SPaul Moore 
396271f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
396371f1cb05SPaul Moore 	if (unlikely(err)) {
396471f1cb05SPaul Moore 		printk(KERN_WARNING
396571f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
396671f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3967220deb96SPaul Moore 		return -EACCES;
396871f1cb05SPaul Moore 	}
3969220deb96SPaul Moore 
3970220deb96SPaul Moore 	return 0;
39714f6a993fSPaul Moore }
39724f6a993fSPaul Moore 
3973446b8024SPaul Moore /**
3974446b8024SPaul Moore  * selinux_conn_sid - Determine the child socket label for a connection
3975446b8024SPaul Moore  * @sk_sid: the parent socket's SID
3976446b8024SPaul Moore  * @skb_sid: the packet's SID
3977446b8024SPaul Moore  * @conn_sid: the resulting connection SID
3978446b8024SPaul Moore  *
3979446b8024SPaul Moore  * If @skb_sid is valid then the user:role:type information from @sk_sid is
3980446b8024SPaul Moore  * combined with the MLS information from @skb_sid in order to create
3981446b8024SPaul Moore  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
3982446b8024SPaul Moore  * of @sk_sid.  Returns zero on success, negative values on failure.
3983446b8024SPaul Moore  *
3984446b8024SPaul Moore  */
3985446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3986446b8024SPaul Moore {
3987446b8024SPaul Moore 	int err = 0;
3988446b8024SPaul Moore 
3989446b8024SPaul Moore 	if (skb_sid != SECSID_NULL)
3990446b8024SPaul Moore 		err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3991446b8024SPaul Moore 	else
3992446b8024SPaul Moore 		*conn_sid = sk_sid;
3993446b8024SPaul Moore 
3994446b8024SPaul Moore 	return err;
3995446b8024SPaul Moore }
3996446b8024SPaul Moore 
39971da177e4SLinus Torvalds /* socket security operations */
3998d4f2d978SPaul Moore 
39992ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec,
40002ad18bdfSHarry Ciao 				 u16 secclass, u32 *socksid)
4001d4f2d978SPaul Moore {
40022ad18bdfSHarry Ciao 	if (tsec->sockcreate_sid > SECSID_NULL) {
40032ad18bdfSHarry Ciao 		*socksid = tsec->sockcreate_sid;
40042ad18bdfSHarry Ciao 		return 0;
40052ad18bdfSHarry Ciao 	}
40062ad18bdfSHarry Ciao 
40072ad18bdfSHarry Ciao 	return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
40082ad18bdfSHarry Ciao 				       socksid);
4009d4f2d978SPaul Moore }
4010d4f2d978SPaul Moore 
4011253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
40121da177e4SLinus Torvalds {
4013253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
40142bf49690SThomas Liu 	struct common_audit_data ad;
401548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4016253bfae6SPaul Moore 	u32 tsid = task_sid(task);
40171da177e4SLinus Torvalds 
4018253bfae6SPaul Moore 	if (sksec->sid == SECINITSID_KERNEL)
4019253bfae6SPaul Moore 		return 0;
40201da177e4SLinus Torvalds 
402150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
402248c62af6SEric Paris 	ad.u.net = &net;
402348c62af6SEric Paris 	ad.u.net->sk = sk;
40241da177e4SLinus Torvalds 
4025253bfae6SPaul Moore 	return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
40261da177e4SLinus Torvalds }
40271da177e4SLinus Torvalds 
40281da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
40291da177e4SLinus Torvalds 				 int protocol, int kern)
40301da177e4SLinus Torvalds {
40315fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4032d4f2d978SPaul Moore 	u32 newsid;
4033275bb41eSDavid Howells 	u16 secclass;
40342ad18bdfSHarry Ciao 	int rc;
40351da177e4SLinus Torvalds 
40361da177e4SLinus Torvalds 	if (kern)
4037d4f2d978SPaul Moore 		return 0;
40381da177e4SLinus Torvalds 
4039275bb41eSDavid Howells 	secclass = socket_type_to_security_class(family, type, protocol);
40402ad18bdfSHarry Ciao 	rc = socket_sockcreate_sid(tsec, secclass, &newsid);
40412ad18bdfSHarry Ciao 	if (rc)
40422ad18bdfSHarry Ciao 		return rc;
40432ad18bdfSHarry Ciao 
4044d4f2d978SPaul Moore 	return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
40451da177e4SLinus Torvalds }
40461da177e4SLinus Torvalds 
40477420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
40481da177e4SLinus Torvalds 				      int type, int protocol, int kern)
40491da177e4SLinus Torvalds {
40505fb49870SPaul Moore 	const struct task_security_struct *tsec = current_security();
4051d4f2d978SPaul Moore 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
4052892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
4053275bb41eSDavid Howells 	int err = 0;
4054275bb41eSDavid Howells 
40552ad18bdfSHarry Ciao 	isec->sclass = socket_type_to_security_class(family, type, protocol);
40562ad18bdfSHarry Ciao 
4057275bb41eSDavid Howells 	if (kern)
4058275bb41eSDavid Howells 		isec->sid = SECINITSID_KERNEL;
40592ad18bdfSHarry Ciao 	else {
40602ad18bdfSHarry Ciao 		err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
40612ad18bdfSHarry Ciao 		if (err)
40622ad18bdfSHarry Ciao 			return err;
40632ad18bdfSHarry Ciao 	}
4064275bb41eSDavid Howells 
40651da177e4SLinus Torvalds 	isec->initialized = 1;
40661da177e4SLinus Torvalds 
4067892c141eSVenkat Yekkirala 	if (sock->sk) {
4068892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
4069892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
4070220deb96SPaul Moore 		sksec->sclass = isec->sclass;
4071389fb800SPaul Moore 		err = selinux_netlbl_socket_post_create(sock->sk, family);
4072892c141eSVenkat Yekkirala 	}
4073892c141eSVenkat Yekkirala 
40747420ed23SVenkat Yekkirala 	return err;
40751da177e4SLinus Torvalds }
40761da177e4SLinus Torvalds 
40771da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
40781da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
40791da177e4SLinus Torvalds    permission check between the socket and the port number. */
40801da177e4SLinus Torvalds 
40811da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
40821da177e4SLinus Torvalds {
4083253bfae6SPaul Moore 	struct sock *sk = sock->sk;
40841da177e4SLinus Torvalds 	u16 family;
40851da177e4SLinus Torvalds 	int err;
40861da177e4SLinus Torvalds 
4087253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__BIND);
40881da177e4SLinus Torvalds 	if (err)
40891da177e4SLinus Torvalds 		goto out;
40901da177e4SLinus Torvalds 
40911da177e4SLinus Torvalds 	/*
40921da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
409313402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
409413402580SJames Morris 	 * check the first address now.
40951da177e4SLinus Torvalds 	 */
4096253bfae6SPaul Moore 	family = sk->sk_family;
40971da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
40981da177e4SLinus Torvalds 		char *addrp;
4099253bfae6SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
41002bf49690SThomas Liu 		struct common_audit_data ad;
410148c62af6SEric Paris 		struct lsm_network_audit net = {0,};
41021da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
41031da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
41041da177e4SLinus Torvalds 		unsigned short snum;
4105e399f982SJames Morris 		u32 sid, node_perm;
41061da177e4SLinus Torvalds 
41071da177e4SLinus Torvalds 		if (family == PF_INET) {
41081da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
41091da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
41101da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
41111da177e4SLinus Torvalds 		} else {
41121da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
41131da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
41141da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
41151da177e4SLinus Torvalds 		}
41161da177e4SLinus Torvalds 
4117227b60f5SStephen Hemminger 		if (snum) {
4118227b60f5SStephen Hemminger 			int low, high;
4119227b60f5SStephen Hemminger 
41200bbf87d8SEric W. Biederman 			inet_get_local_port_range(sock_net(sk), &low, &high);
4121227b60f5SStephen Hemminger 
4122227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
41233e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
41243e112172SPaul Moore 						      snum, &sid);
41251da177e4SLinus Torvalds 				if (err)
41261da177e4SLinus Torvalds 					goto out;
412750c205f5SEric Paris 				ad.type = LSM_AUDIT_DATA_NET;
412848c62af6SEric Paris 				ad.u.net = &net;
412948c62af6SEric Paris 				ad.u.net->sport = htons(snum);
413048c62af6SEric Paris 				ad.u.net->family = family;
4131253bfae6SPaul Moore 				err = avc_has_perm(sksec->sid, sid,
4132253bfae6SPaul Moore 						   sksec->sclass,
41331da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
41341da177e4SLinus Torvalds 				if (err)
41351da177e4SLinus Torvalds 					goto out;
41361da177e4SLinus Torvalds 			}
4137227b60f5SStephen Hemminger 		}
41381da177e4SLinus Torvalds 
4139253bfae6SPaul Moore 		switch (sksec->sclass) {
414013402580SJames Morris 		case SECCLASS_TCP_SOCKET:
41411da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
41421da177e4SLinus Torvalds 			break;
41431da177e4SLinus Torvalds 
414413402580SJames Morris 		case SECCLASS_UDP_SOCKET:
41451da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
41461da177e4SLinus Torvalds 			break;
41471da177e4SLinus Torvalds 
41482ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
41492ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
41502ee92d46SJames Morris 			break;
41512ee92d46SJames Morris 
41521da177e4SLinus Torvalds 		default:
41531da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
41541da177e4SLinus Torvalds 			break;
41551da177e4SLinus Torvalds 		}
41561da177e4SLinus Torvalds 
4157224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
41581da177e4SLinus Torvalds 		if (err)
41591da177e4SLinus Torvalds 			goto out;
41601da177e4SLinus Torvalds 
416150c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
416248c62af6SEric Paris 		ad.u.net = &net;
416348c62af6SEric Paris 		ad.u.net->sport = htons(snum);
416448c62af6SEric Paris 		ad.u.net->family = family;
41651da177e4SLinus Torvalds 
41661da177e4SLinus Torvalds 		if (family == PF_INET)
416748c62af6SEric Paris 			ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
41681da177e4SLinus Torvalds 		else
416948c62af6SEric Paris 			ad.u.net->v6info.saddr = addr6->sin6_addr;
41701da177e4SLinus Torvalds 
4171253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid,
4172253bfae6SPaul Moore 				   sksec->sclass, node_perm, &ad);
41731da177e4SLinus Torvalds 		if (err)
41741da177e4SLinus Torvalds 			goto out;
41751da177e4SLinus Torvalds 	}
41761da177e4SLinus Torvalds out:
41771da177e4SLinus Torvalds 	return err;
41781da177e4SLinus Torvalds }
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
41811da177e4SLinus Torvalds {
4182014ab19aSPaul Moore 	struct sock *sk = sock->sk;
4183253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
41841da177e4SLinus Torvalds 	int err;
41851da177e4SLinus Torvalds 
4186253bfae6SPaul Moore 	err = sock_has_perm(current, sk, SOCKET__CONNECT);
41871da177e4SLinus Torvalds 	if (err)
41881da177e4SLinus Torvalds 		return err;
41891da177e4SLinus Torvalds 
41901da177e4SLinus Torvalds 	/*
41912ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
41921da177e4SLinus Torvalds 	 */
4193253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4194253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_DCCP_SOCKET) {
41952bf49690SThomas Liu 		struct common_audit_data ad;
419648c62af6SEric Paris 		struct lsm_network_audit net = {0,};
41971da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
41981da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
41991da177e4SLinus Torvalds 		unsigned short snum;
42002ee92d46SJames Morris 		u32 sid, perm;
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
42031da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
4204911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
42051da177e4SLinus Torvalds 				return -EINVAL;
42061da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
42071da177e4SLinus Torvalds 		} else {
42081da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
4209911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
42101da177e4SLinus Torvalds 				return -EINVAL;
42111da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
42121da177e4SLinus Torvalds 		}
42131da177e4SLinus Torvalds 
42143e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
42151da177e4SLinus Torvalds 		if (err)
42161da177e4SLinus Torvalds 			goto out;
42171da177e4SLinus Torvalds 
4218253bfae6SPaul Moore 		perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
42192ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
42202ee92d46SJames Morris 
422150c205f5SEric Paris 		ad.type = LSM_AUDIT_DATA_NET;
422248c62af6SEric Paris 		ad.u.net = &net;
422348c62af6SEric Paris 		ad.u.net->dport = htons(snum);
422448c62af6SEric Paris 		ad.u.net->family = sk->sk_family;
4225253bfae6SPaul Moore 		err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
42261da177e4SLinus Torvalds 		if (err)
42271da177e4SLinus Torvalds 			goto out;
42281da177e4SLinus Torvalds 	}
42291da177e4SLinus Torvalds 
4230014ab19aSPaul Moore 	err = selinux_netlbl_socket_connect(sk, address);
4231014ab19aSPaul Moore 
42321da177e4SLinus Torvalds out:
42331da177e4SLinus Torvalds 	return err;
42341da177e4SLinus Torvalds }
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
42371da177e4SLinus Torvalds {
4238253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
42391da177e4SLinus Torvalds }
42401da177e4SLinus Torvalds 
42411da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
42421da177e4SLinus Torvalds {
42431da177e4SLinus Torvalds 	int err;
42441da177e4SLinus Torvalds 	struct inode_security_struct *isec;
42451da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
42461da177e4SLinus Torvalds 
4247253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
42481da177e4SLinus Torvalds 	if (err)
42491da177e4SLinus Torvalds 		return err;
42501da177e4SLinus Torvalds 
42511da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
42521da177e4SLinus Torvalds 
42531da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
42541da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
42551da177e4SLinus Torvalds 	newisec->sid = isec->sid;
42561da177e4SLinus Torvalds 	newisec->initialized = 1;
42571da177e4SLinus Torvalds 
42581da177e4SLinus Torvalds 	return 0;
42591da177e4SLinus Torvalds }
42601da177e4SLinus Torvalds 
42611da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
42621da177e4SLinus Torvalds 				  int size)
42631da177e4SLinus Torvalds {
4264253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__WRITE);
42651da177e4SLinus Torvalds }
42661da177e4SLinus Torvalds 
42671da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
42681da177e4SLinus Torvalds 				  int size, int flags)
42691da177e4SLinus Torvalds {
4270253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__READ);
42711da177e4SLinus Torvalds }
42721da177e4SLinus Torvalds 
42731da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
42741da177e4SLinus Torvalds {
4275253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
42761da177e4SLinus Torvalds }
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
42791da177e4SLinus Torvalds {
4280253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
42811da177e4SLinus Torvalds }
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
42841da177e4SLinus Torvalds {
4285f8687afeSPaul Moore 	int err;
4286f8687afeSPaul Moore 
4287253bfae6SPaul Moore 	err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4288f8687afeSPaul Moore 	if (err)
4289f8687afeSPaul Moore 		return err;
4290f8687afeSPaul Moore 
4291f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
42921da177e4SLinus Torvalds }
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
42951da177e4SLinus Torvalds 				     int optname)
42961da177e4SLinus Torvalds {
4297253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
42981da177e4SLinus Torvalds }
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
43011da177e4SLinus Torvalds {
4302253bfae6SPaul Moore 	return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
43031da177e4SLinus Torvalds }
43041da177e4SLinus Torvalds 
43053610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock,
43063610cda5SDavid S. Miller 					      struct sock *other,
43071da177e4SLinus Torvalds 					      struct sock *newsk)
43081da177e4SLinus Torvalds {
43093610cda5SDavid S. Miller 	struct sk_security_struct *sksec_sock = sock->sk_security;
43103610cda5SDavid S. Miller 	struct sk_security_struct *sksec_other = other->sk_security;
43114d1e2451SPaul Moore 	struct sk_security_struct *sksec_new = newsk->sk_security;
43122bf49690SThomas Liu 	struct common_audit_data ad;
431348c62af6SEric Paris 	struct lsm_network_audit net = {0,};
43141da177e4SLinus Torvalds 	int err;
43151da177e4SLinus Torvalds 
431650c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
431748c62af6SEric Paris 	ad.u.net = &net;
431848c62af6SEric Paris 	ad.u.net->sk = other;
43191da177e4SLinus Torvalds 
43204d1e2451SPaul Moore 	err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
43214d1e2451SPaul Moore 			   sksec_other->sclass,
43221da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
43231da177e4SLinus Torvalds 	if (err)
43241da177e4SLinus Torvalds 		return err;
43251da177e4SLinus Torvalds 
43261da177e4SLinus Torvalds 	/* server child socket */
43274d1e2451SPaul Moore 	sksec_new->peer_sid = sksec_sock->sid;
43284d1e2451SPaul Moore 	err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
43294d1e2451SPaul Moore 				    &sksec_new->sid);
43304d1e2451SPaul Moore 	if (err)
43314237c75cSVenkat Yekkirala 		return err;
43324d1e2451SPaul Moore 
43334d1e2451SPaul Moore 	/* connecting socket */
43344d1e2451SPaul Moore 	sksec_sock->peer_sid = sksec_new->sid;
43354d1e2451SPaul Moore 
43364d1e2451SPaul Moore 	return 0;
43371da177e4SLinus Torvalds }
43381da177e4SLinus Torvalds 
43391da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
43401da177e4SLinus Torvalds 					struct socket *other)
43411da177e4SLinus Torvalds {
4342253bfae6SPaul Moore 	struct sk_security_struct *ssec = sock->sk->sk_security;
4343253bfae6SPaul Moore 	struct sk_security_struct *osec = other->sk->sk_security;
43442bf49690SThomas Liu 	struct common_audit_data ad;
434548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
43461da177e4SLinus Torvalds 
434750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
434848c62af6SEric Paris 	ad.u.net = &net;
434948c62af6SEric Paris 	ad.u.net->sk = other->sk;
43501da177e4SLinus Torvalds 
4351253bfae6SPaul Moore 	return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4352253bfae6SPaul Moore 			    &ad);
43531da177e4SLinus Torvalds }
43541da177e4SLinus Torvalds 
4355cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4356cbe0d6e8SPaul Moore 				    char *addrp, u16 family, u32 peer_sid,
43572bf49690SThomas Liu 				    struct common_audit_data *ad)
4358effad8dfSPaul Moore {
4359effad8dfSPaul Moore 	int err;
4360effad8dfSPaul Moore 	u32 if_sid;
4361effad8dfSPaul Moore 	u32 node_sid;
4362effad8dfSPaul Moore 
4363cbe0d6e8SPaul Moore 	err = sel_netif_sid(ns, ifindex, &if_sid);
4364effad8dfSPaul Moore 	if (err)
4365effad8dfSPaul Moore 		return err;
4366effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
4367effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
4368effad8dfSPaul Moore 	if (err)
4369effad8dfSPaul Moore 		return err;
4370effad8dfSPaul Moore 
4371effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
4372effad8dfSPaul Moore 	if (err)
4373effad8dfSPaul Moore 		return err;
4374effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
4375effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
4376effad8dfSPaul Moore }
4377effad8dfSPaul Moore 
4378220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4379d8395c87SPaul Moore 				       u16 family)
4380220deb96SPaul Moore {
4381277d342fSPaul Moore 	int err = 0;
4382220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4383220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
43842bf49690SThomas Liu 	struct common_audit_data ad;
438548c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4386d8395c87SPaul Moore 	char *addrp;
4387d8395c87SPaul Moore 
438850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
438948c62af6SEric Paris 	ad.u.net = &net;
439048c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
439148c62af6SEric Paris 	ad.u.net->family = family;
4392d8395c87SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4393d8395c87SPaul Moore 	if (err)
4394d8395c87SPaul Moore 		return err;
4395220deb96SPaul Moore 
439658bfbb51SPaul Moore 	if (selinux_secmark_enabled()) {
4397220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4398d8395c87SPaul Moore 				   PACKET__RECV, &ad);
4399220deb96SPaul Moore 		if (err)
4400220deb96SPaul Moore 			return err;
440158bfbb51SPaul Moore 	}
4402220deb96SPaul Moore 
4403d8395c87SPaul Moore 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4404220deb96SPaul Moore 	if (err)
4405220deb96SPaul Moore 		return err;
4406d8395c87SPaul Moore 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4407220deb96SPaul Moore 
44084e5ab4cbSJames Morris 	return err;
44094e5ab4cbSJames Morris }
4410d28d1e08STrent Jaeger 
44114e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
44124e5ab4cbSJames Morris {
4413220deb96SPaul Moore 	int err;
44144237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4415220deb96SPaul Moore 	u16 family = sk->sk_family;
4416220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
44172bf49690SThomas Liu 	struct common_audit_data ad;
441848c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4419220deb96SPaul Moore 	char *addrp;
4420d8395c87SPaul Moore 	u8 secmark_active;
4421d8395c87SPaul Moore 	u8 peerlbl_active;
44224e5ab4cbSJames Morris 
44234e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4424220deb96SPaul Moore 		return 0;
44254e5ab4cbSJames Morris 
44264e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
442787fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
44284e5ab4cbSJames Morris 		family = PF_INET;
44294e5ab4cbSJames Morris 
4430d8395c87SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4431d8395c87SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4432d8395c87SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4433d8395c87SPaul Moore 	 * as fast and as clean as possible. */
443458bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4435d8395c87SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, family);
4436d8395c87SPaul Moore 
4437d8395c87SPaul Moore 	secmark_active = selinux_secmark_enabled();
44382be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4439d8395c87SPaul Moore 	if (!secmark_active && !peerlbl_active)
4440d8395c87SPaul Moore 		return 0;
4441d8395c87SPaul Moore 
444250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
444348c62af6SEric Paris 	ad.u.net = &net;
444448c62af6SEric Paris 	ad.u.net->netif = skb->skb_iif;
444548c62af6SEric Paris 	ad.u.net->family = family;
4446224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
44474e5ab4cbSJames Morris 	if (err)
4448220deb96SPaul Moore 		return err;
44494e5ab4cbSJames Morris 
4450d8395c87SPaul Moore 	if (peerlbl_active) {
4451d621d35eSPaul Moore 		u32 peer_sid;
4452220deb96SPaul Moore 
4453220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4454220deb96SPaul Moore 		if (err)
4455220deb96SPaul Moore 			return err;
4456cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4457cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4458dfaebe98SPaul Moore 		if (err) {
4459dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
4460effad8dfSPaul Moore 			return err;
4461dfaebe98SPaul Moore 		}
4462d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4463d621d35eSPaul Moore 				   PEER__RECV, &ad);
446446d01d63SChad Hanson 		if (err) {
4465dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 0);
446646d01d63SChad Hanson 			return err;
446746d01d63SChad Hanson 		}
4468d621d35eSPaul Moore 	}
4469d621d35eSPaul Moore 
4470d8395c87SPaul Moore 	if (secmark_active) {
4471effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4472effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4473effad8dfSPaul Moore 		if (err)
4474effad8dfSPaul Moore 			return err;
4475effad8dfSPaul Moore 	}
4476effad8dfSPaul Moore 
4477d621d35eSPaul Moore 	return err;
44781da177e4SLinus Torvalds }
44791da177e4SLinus Torvalds 
44802c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
44811da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
44821da177e4SLinus Torvalds {
44831da177e4SLinus Torvalds 	int err = 0;
44841da177e4SLinus Torvalds 	char *scontext;
44851da177e4SLinus Torvalds 	u32 scontext_len;
4486253bfae6SPaul Moore 	struct sk_security_struct *sksec = sock->sk->sk_security;
44873de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
44881da177e4SLinus Torvalds 
4489253bfae6SPaul Moore 	if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4490253bfae6SPaul Moore 	    sksec->sclass == SECCLASS_TCP_SOCKET)
4491dd3e7836SEric Paris 		peer_sid = sksec->peer_sid;
4492253bfae6SPaul Moore 	if (peer_sid == SECSID_NULL)
4493253bfae6SPaul Moore 		return -ENOPROTOOPT;
44941da177e4SLinus Torvalds 
44952c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
44961da177e4SLinus Torvalds 	if (err)
4497253bfae6SPaul Moore 		return err;
44981da177e4SLinus Torvalds 
44991da177e4SLinus Torvalds 	if (scontext_len > len) {
45001da177e4SLinus Torvalds 		err = -ERANGE;
45011da177e4SLinus Torvalds 		goto out_len;
45021da177e4SLinus Torvalds 	}
45031da177e4SLinus Torvalds 
45041da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
45051da177e4SLinus Torvalds 		err = -EFAULT;
45061da177e4SLinus Torvalds 
45071da177e4SLinus Torvalds out_len:
45081da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
45091da177e4SLinus Torvalds 		err = -EFAULT;
45101da177e4SLinus Torvalds 	kfree(scontext);
45111da177e4SLinus Torvalds 	return err;
45121da177e4SLinus Torvalds }
45131da177e4SLinus Torvalds 
4514dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
45152c7946a7SCatherine Zhang {
4516dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
451775e22910SPaul Moore 	u16 family;
4518877ce7c1SCatherine Zhang 
4519aa862900SPaul Moore 	if (skb && skb->protocol == htons(ETH_P_IP))
4520aa862900SPaul Moore 		family = PF_INET;
4521aa862900SPaul Moore 	else if (skb && skb->protocol == htons(ETH_P_IPV6))
4522aa862900SPaul Moore 		family = PF_INET6;
4523aa862900SPaul Moore 	else if (sock)
452475e22910SPaul Moore 		family = sock->sk->sk_family;
452575e22910SPaul Moore 	else
452675e22910SPaul Moore 		goto out;
452775e22910SPaul Moore 
452875e22910SPaul Moore 	if (sock && family == PF_UNIX)
4529713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
45303de4bab5SPaul Moore 	else if (skb)
4531220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
45322c7946a7SCatherine Zhang 
453375e22910SPaul Moore out:
4534dc49c1f9SCatherine Zhang 	*secid = peer_secid;
453575e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
453675e22910SPaul Moore 		return -EINVAL;
453775e22910SPaul Moore 	return 0;
45382c7946a7SCatherine Zhang }
45392c7946a7SCatherine Zhang 
45407d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
45411da177e4SLinus Torvalds {
454284914b7eSPaul Moore 	struct sk_security_struct *sksec;
454384914b7eSPaul Moore 
454484914b7eSPaul Moore 	sksec = kzalloc(sizeof(*sksec), priority);
454584914b7eSPaul Moore 	if (!sksec)
454684914b7eSPaul Moore 		return -ENOMEM;
454784914b7eSPaul Moore 
454884914b7eSPaul Moore 	sksec->peer_sid = SECINITSID_UNLABELED;
454984914b7eSPaul Moore 	sksec->sid = SECINITSID_UNLABELED;
455084914b7eSPaul Moore 	selinux_netlbl_sk_security_reset(sksec);
455184914b7eSPaul Moore 	sk->sk_security = sksec;
455284914b7eSPaul Moore 
455384914b7eSPaul Moore 	return 0;
45541da177e4SLinus Torvalds }
45551da177e4SLinus Torvalds 
45561da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
45571da177e4SLinus Torvalds {
455884914b7eSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
455984914b7eSPaul Moore 
456084914b7eSPaul Moore 	sk->sk_security = NULL;
456184914b7eSPaul Moore 	selinux_netlbl_sk_security_free(sksec);
456284914b7eSPaul Moore 	kfree(sksec);
45631da177e4SLinus Torvalds }
45641da177e4SLinus Torvalds 
4565892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4566892c141eSVenkat Yekkirala {
4567dd3e7836SEric Paris 	struct sk_security_struct *sksec = sk->sk_security;
4568dd3e7836SEric Paris 	struct sk_security_struct *newsksec = newsk->sk_security;
4569892c141eSVenkat Yekkirala 
4570dd3e7836SEric Paris 	newsksec->sid = sksec->sid;
4571dd3e7836SEric Paris 	newsksec->peer_sid = sksec->peer_sid;
4572dd3e7836SEric Paris 	newsksec->sclass = sksec->sclass;
457399f59ed0SPaul Moore 
4574dd3e7836SEric Paris 	selinux_netlbl_sk_security_reset(newsksec);
4575892c141eSVenkat Yekkirala }
4576892c141eSVenkat Yekkirala 
4577beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4578d28d1e08STrent Jaeger {
4579d28d1e08STrent Jaeger 	if (!sk)
4580beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4581892c141eSVenkat Yekkirala 	else {
4582892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4583d28d1e08STrent Jaeger 
4584beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4585892c141eSVenkat Yekkirala 	}
4586d28d1e08STrent Jaeger }
4587d28d1e08STrent Jaeger 
45889a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent)
45894237c75cSVenkat Yekkirala {
45904237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
45914237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
45924237c75cSVenkat Yekkirala 
45932873ead7SPaul Moore 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
45942873ead7SPaul Moore 	    sk->sk_family == PF_UNIX)
45954237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4596220deb96SPaul Moore 	sksec->sclass = isec->sclass;
45974237c75cSVenkat Yekkirala }
45984237c75cSVenkat Yekkirala 
45999a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
46004237c75cSVenkat Yekkirala 				     struct request_sock *req)
46014237c75cSVenkat Yekkirala {
46024237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
46034237c75cSVenkat Yekkirala 	int err;
46040b1f24e6SPaul Moore 	u16 family = req->rsk_ops->family;
4605446b8024SPaul Moore 	u32 connsid;
46064237c75cSVenkat Yekkirala 	u32 peersid;
46074237c75cSVenkat Yekkirala 
4608aa862900SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4609220deb96SPaul Moore 	if (err)
4610220deb96SPaul Moore 		return err;
4611446b8024SPaul Moore 	err = selinux_conn_sid(sksec->sid, peersid, &connsid);
46124237c75cSVenkat Yekkirala 	if (err)
46134237c75cSVenkat Yekkirala 		return err;
4614446b8024SPaul Moore 	req->secid = connsid;
46156b877699SVenkat Yekkirala 	req->peer_secid = peersid;
4616389fb800SPaul Moore 
4617389fb800SPaul Moore 	return selinux_netlbl_inet_conn_request(req, family);
46184237c75cSVenkat Yekkirala }
46194237c75cSVenkat Yekkirala 
46209a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
46219a673e56SAdrian Bunk 				   const struct request_sock *req)
46224237c75cSVenkat Yekkirala {
46234237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
46244237c75cSVenkat Yekkirala 
46254237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
46266b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
46274237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
46284237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
46294237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
46304237c75cSVenkat Yekkirala 	   time it will have been created and available. */
463199f59ed0SPaul Moore 
46329f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
46339f2ad665SPaul Moore 	 * thread with access to newsksec */
4634389fb800SPaul Moore 	selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
46354237c75cSVenkat Yekkirala }
46364237c75cSVenkat Yekkirala 
4637014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
46386b877699SVenkat Yekkirala {
4639aa862900SPaul Moore 	u16 family = sk->sk_family;
46406b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
46416b877699SVenkat Yekkirala 
4642aa862900SPaul Moore 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
4643aa862900SPaul Moore 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4644aa862900SPaul Moore 		family = PF_INET;
4645aa862900SPaul Moore 
4646aa862900SPaul Moore 	selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
46476b877699SVenkat Yekkirala }
46486b877699SVenkat Yekkirala 
4649ca10b9e9SEric Dumazet static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4650ca10b9e9SEric Dumazet {
4651ca10b9e9SEric Dumazet 	skb_set_owner_w(skb, sk);
4652ca10b9e9SEric Dumazet }
4653ca10b9e9SEric Dumazet 
46542606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid)
46552606fd1fSEric Paris {
46562606fd1fSEric Paris 	const struct task_security_struct *__tsec;
46572606fd1fSEric Paris 	u32 tsid;
46582606fd1fSEric Paris 
46592606fd1fSEric Paris 	__tsec = current_security();
46602606fd1fSEric Paris 	tsid = __tsec->sid;
46612606fd1fSEric Paris 
46622606fd1fSEric Paris 	return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
46632606fd1fSEric Paris }
46642606fd1fSEric Paris 
46652606fd1fSEric Paris static void selinux_secmark_refcount_inc(void)
46662606fd1fSEric Paris {
46672606fd1fSEric Paris 	atomic_inc(&selinux_secmark_refcount);
46682606fd1fSEric Paris }
46692606fd1fSEric Paris 
46702606fd1fSEric Paris static void selinux_secmark_refcount_dec(void)
46712606fd1fSEric Paris {
46722606fd1fSEric Paris 	atomic_dec(&selinux_secmark_refcount);
46732606fd1fSEric Paris }
46742606fd1fSEric Paris 
46759a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
46769a673e56SAdrian Bunk 				      struct flowi *fl)
46774237c75cSVenkat Yekkirala {
46781d28f42cSDavid S. Miller 	fl->flowi_secid = req->secid;
46794237c75cSVenkat Yekkirala }
46804237c75cSVenkat Yekkirala 
46815dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security)
46825dbbaf2dSPaul Moore {
46835dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec;
46845dbbaf2dSPaul Moore 
46855dbbaf2dSPaul Moore 	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
46865dbbaf2dSPaul Moore 	if (!tunsec)
46875dbbaf2dSPaul Moore 		return -ENOMEM;
46885dbbaf2dSPaul Moore 	tunsec->sid = current_sid();
46895dbbaf2dSPaul Moore 
46905dbbaf2dSPaul Moore 	*security = tunsec;
46915dbbaf2dSPaul Moore 	return 0;
46925dbbaf2dSPaul Moore }
46935dbbaf2dSPaul Moore 
46945dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security)
46955dbbaf2dSPaul Moore {
46965dbbaf2dSPaul Moore 	kfree(security);
46975dbbaf2dSPaul Moore }
46985dbbaf2dSPaul Moore 
4699ed6d76e4SPaul Moore static int selinux_tun_dev_create(void)
4700ed6d76e4SPaul Moore {
4701ed6d76e4SPaul Moore 	u32 sid = current_sid();
4702ed6d76e4SPaul Moore 
4703ed6d76e4SPaul Moore 	/* we aren't taking into account the "sockcreate" SID since the socket
4704ed6d76e4SPaul Moore 	 * that is being created here is not a socket in the traditional sense,
4705ed6d76e4SPaul Moore 	 * instead it is a private sock, accessible only to the kernel, and
4706ed6d76e4SPaul Moore 	 * representing a wide range of network traffic spanning multiple
4707ed6d76e4SPaul Moore 	 * connections unlike traditional sockets - check the TUN driver to
4708ed6d76e4SPaul Moore 	 * get a better understanding of why this socket is special */
4709ed6d76e4SPaul Moore 
4710ed6d76e4SPaul Moore 	return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4711ed6d76e4SPaul Moore 			    NULL);
4712ed6d76e4SPaul Moore }
4713ed6d76e4SPaul Moore 
47145dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security)
4715ed6d76e4SPaul Moore {
47165dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
47175dbbaf2dSPaul Moore 
47185dbbaf2dSPaul Moore 	return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
47195dbbaf2dSPaul Moore 			    TUN_SOCKET__ATTACH_QUEUE, NULL);
47205dbbaf2dSPaul Moore }
47215dbbaf2dSPaul Moore 
47225dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security)
47235dbbaf2dSPaul Moore {
47245dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4725ed6d76e4SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4726ed6d76e4SPaul Moore 
4727ed6d76e4SPaul Moore 	/* we don't currently perform any NetLabel based labeling here and it
4728ed6d76e4SPaul Moore 	 * isn't clear that we would want to do so anyway; while we could apply
4729ed6d76e4SPaul Moore 	 * labeling without the support of the TUN user the resulting labeled
4730ed6d76e4SPaul Moore 	 * traffic from the other end of the connection would almost certainly
4731ed6d76e4SPaul Moore 	 * cause confusion to the TUN user that had no idea network labeling
4732ed6d76e4SPaul Moore 	 * protocols were being used */
4733ed6d76e4SPaul Moore 
47345dbbaf2dSPaul Moore 	sksec->sid = tunsec->sid;
4735ed6d76e4SPaul Moore 	sksec->sclass = SECCLASS_TUN_SOCKET;
47365dbbaf2dSPaul Moore 
47375dbbaf2dSPaul Moore 	return 0;
4738ed6d76e4SPaul Moore }
4739ed6d76e4SPaul Moore 
47405dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security)
4741ed6d76e4SPaul Moore {
47425dbbaf2dSPaul Moore 	struct tun_security_struct *tunsec = security;
4743ed6d76e4SPaul Moore 	u32 sid = current_sid();
4744ed6d76e4SPaul Moore 	int err;
4745ed6d76e4SPaul Moore 
47465dbbaf2dSPaul Moore 	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4747ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELFROM, NULL);
4748ed6d76e4SPaul Moore 	if (err)
4749ed6d76e4SPaul Moore 		return err;
4750ed6d76e4SPaul Moore 	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4751ed6d76e4SPaul Moore 			   TUN_SOCKET__RELABELTO, NULL);
4752ed6d76e4SPaul Moore 	if (err)
4753ed6d76e4SPaul Moore 		return err;
47545dbbaf2dSPaul Moore 	tunsec->sid = sid;
4755ed6d76e4SPaul Moore 
4756ed6d76e4SPaul Moore 	return 0;
4757ed6d76e4SPaul Moore }
4758ed6d76e4SPaul Moore 
47591da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
47601da177e4SLinus Torvalds {
47611da177e4SLinus Torvalds 	int err = 0;
47621da177e4SLinus Torvalds 	u32 perm;
47631da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4764253bfae6SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
47651da177e4SLinus Torvalds 
476677954983SHong zhi guo 	if (skb->len < NLMSG_HDRLEN) {
47671da177e4SLinus Torvalds 		err = -EINVAL;
47681da177e4SLinus Torvalds 		goto out;
47691da177e4SLinus Torvalds 	}
4770b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
47711da177e4SLinus Torvalds 
4772253bfae6SPaul Moore 	err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
47731da177e4SLinus Torvalds 	if (err) {
47741da177e4SLinus Torvalds 		if (err == -EINVAL) {
4775d950f84cSRichard Guy Briggs 			printk(KERN_WARNING
4776d950f84cSRichard Guy Briggs 			       "SELinux: unrecognized netlink message:"
4777e173fb26SRichard Guy Briggs 			       " protocol=%hu nlmsg_type=%hu sclass=%hu\n",
4778e173fb26SRichard Guy Briggs 			       sk->sk_protocol, nlh->nlmsg_type, sksec->sclass);
477939c9aedeSEric Paris 			if (!selinux_enforcing || security_get_allow_unknown())
47801da177e4SLinus Torvalds 				err = 0;
47811da177e4SLinus Torvalds 		}
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 		/* Ignore */
47841da177e4SLinus Torvalds 		if (err == -ENOENT)
47851da177e4SLinus Torvalds 			err = 0;
47861da177e4SLinus Torvalds 		goto out;
47871da177e4SLinus Torvalds 	}
47881da177e4SLinus Torvalds 
4789253bfae6SPaul Moore 	err = sock_has_perm(current, sk, perm);
47901da177e4SLinus Torvalds out:
47911da177e4SLinus Torvalds 	return err;
47921da177e4SLinus Torvalds }
47931da177e4SLinus Torvalds 
47941da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
47951da177e4SLinus Torvalds 
4796cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb,
4797cbe0d6e8SPaul Moore 				       const struct net_device *indev,
4798effad8dfSPaul Moore 				       u16 family)
47991da177e4SLinus Torvalds {
4800dfaebe98SPaul Moore 	int err;
4801effad8dfSPaul Moore 	char *addrp;
4802effad8dfSPaul Moore 	u32 peer_sid;
48032bf49690SThomas Liu 	struct common_audit_data ad;
480448c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4805effad8dfSPaul Moore 	u8 secmark_active;
4806948bf85cSPaul Moore 	u8 netlbl_active;
4807effad8dfSPaul Moore 	u8 peerlbl_active;
48084237c75cSVenkat Yekkirala 
4809effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4810effad8dfSPaul Moore 		return NF_ACCEPT;
48114237c75cSVenkat Yekkirala 
4812effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4813948bf85cSPaul Moore 	netlbl_active = netlbl_enabled();
48142be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4815effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4816effad8dfSPaul Moore 		return NF_ACCEPT;
48174237c75cSVenkat Yekkirala 
4818d8395c87SPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4819d8395c87SPaul Moore 		return NF_DROP;
4820d8395c87SPaul Moore 
482150c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
482248c62af6SEric Paris 	ad.u.net = &net;
4823cbe0d6e8SPaul Moore 	ad.u.net->netif = indev->ifindex;
482448c62af6SEric Paris 	ad.u.net->family = family;
4825effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4826effad8dfSPaul Moore 		return NF_DROP;
48271da177e4SLinus Torvalds 
4828dfaebe98SPaul Moore 	if (peerlbl_active) {
4829cbe0d6e8SPaul Moore 		err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
4830cbe0d6e8SPaul Moore 					       addrp, family, peer_sid, &ad);
4831dfaebe98SPaul Moore 		if (err) {
4832dfaebe98SPaul Moore 			selinux_netlbl_err(skb, err, 1);
4833effad8dfSPaul Moore 			return NF_DROP;
4834dfaebe98SPaul Moore 		}
4835dfaebe98SPaul Moore 	}
4836effad8dfSPaul Moore 
4837effad8dfSPaul Moore 	if (secmark_active)
4838effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4839effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4840effad8dfSPaul Moore 			return NF_DROP;
4841effad8dfSPaul Moore 
4842948bf85cSPaul Moore 	if (netlbl_active)
4843948bf85cSPaul Moore 		/* we do this in the FORWARD path and not the POST_ROUTING
4844948bf85cSPaul Moore 		 * path because we want to make sure we apply the necessary
4845948bf85cSPaul Moore 		 * labeling before IPsec is applied so we can leverage AH
4846948bf85cSPaul Moore 		 * protection */
4847948bf85cSPaul Moore 		if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4848948bf85cSPaul Moore 			return NF_DROP;
4849948bf85cSPaul Moore 
4850effad8dfSPaul Moore 	return NF_ACCEPT;
4851effad8dfSPaul Moore }
4852effad8dfSPaul Moore 
4853795aa6efSPatrick McHardy static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
4854effad8dfSPaul Moore 					 struct sk_buff *skb,
4855effad8dfSPaul Moore 					 const struct net_device *in,
4856effad8dfSPaul Moore 					 const struct net_device *out,
4857effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4858effad8dfSPaul Moore {
4859cbe0d6e8SPaul Moore 	return selinux_ip_forward(skb, in, PF_INET);
4860effad8dfSPaul Moore }
4861effad8dfSPaul Moore 
4862effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4863795aa6efSPatrick McHardy static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
4864effad8dfSPaul Moore 					 struct sk_buff *skb,
4865effad8dfSPaul Moore 					 const struct net_device *in,
4866effad8dfSPaul Moore 					 const struct net_device *out,
4867effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4868effad8dfSPaul Moore {
4869cbe0d6e8SPaul Moore 	return selinux_ip_forward(skb, in, PF_INET6);
4870effad8dfSPaul Moore }
4871effad8dfSPaul Moore #endif	/* IPV6 */
4872effad8dfSPaul Moore 
4873948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb,
4874948bf85cSPaul Moore 				      u16 family)
4875948bf85cSPaul Moore {
487647180068SPaul Moore 	struct sock *sk;
4877948bf85cSPaul Moore 	u32 sid;
4878948bf85cSPaul Moore 
4879948bf85cSPaul Moore 	if (!netlbl_enabled())
4880948bf85cSPaul Moore 		return NF_ACCEPT;
4881948bf85cSPaul Moore 
4882948bf85cSPaul Moore 	/* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4883948bf85cSPaul Moore 	 * because we want to make sure we apply the necessary labeling
4884948bf85cSPaul Moore 	 * before IPsec is applied so we can leverage AH protection */
488547180068SPaul Moore 	sk = skb->sk;
488647180068SPaul Moore 	if (sk) {
488747180068SPaul Moore 		struct sk_security_struct *sksec;
488847180068SPaul Moore 
488947180068SPaul Moore 		if (sk->sk_state == TCP_LISTEN)
489047180068SPaul Moore 			/* if the socket is the listening state then this
489147180068SPaul Moore 			 * packet is a SYN-ACK packet which means it needs to
489247180068SPaul Moore 			 * be labeled based on the connection/request_sock and
489347180068SPaul Moore 			 * not the parent socket.  unfortunately, we can't
489447180068SPaul Moore 			 * lookup the request_sock yet as it isn't queued on
489547180068SPaul Moore 			 * the parent socket until after the SYN-ACK is sent.
489647180068SPaul Moore 			 * the "solution" is to simply pass the packet as-is
489747180068SPaul Moore 			 * as any IP option based labeling should be copied
489847180068SPaul Moore 			 * from the initial connection request (in the IP
489947180068SPaul Moore 			 * layer).  it is far from ideal, but until we get a
490047180068SPaul Moore 			 * security label in the packet itself this is the
490147180068SPaul Moore 			 * best we can do. */
490247180068SPaul Moore 			return NF_ACCEPT;
490347180068SPaul Moore 
490447180068SPaul Moore 		/* standard practice, label using the parent socket */
490547180068SPaul Moore 		sksec = sk->sk_security;
4906948bf85cSPaul Moore 		sid = sksec->sid;
4907948bf85cSPaul Moore 	} else
4908948bf85cSPaul Moore 		sid = SECINITSID_KERNEL;
4909948bf85cSPaul Moore 	if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4910948bf85cSPaul Moore 		return NF_DROP;
4911948bf85cSPaul Moore 
4912948bf85cSPaul Moore 	return NF_ACCEPT;
4913948bf85cSPaul Moore }
4914948bf85cSPaul Moore 
4915795aa6efSPatrick McHardy static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
4916948bf85cSPaul Moore 					struct sk_buff *skb,
4917948bf85cSPaul Moore 					const struct net_device *in,
4918948bf85cSPaul Moore 					const struct net_device *out,
4919948bf85cSPaul Moore 					int (*okfn)(struct sk_buff *))
4920948bf85cSPaul Moore {
4921948bf85cSPaul Moore 	return selinux_ip_output(skb, PF_INET);
4922948bf85cSPaul Moore }
4923948bf85cSPaul Moore 
4924effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4925effad8dfSPaul Moore 						int ifindex,
4926d8395c87SPaul Moore 						u16 family)
49274e5ab4cbSJames Morris {
4928effad8dfSPaul Moore 	struct sock *sk = skb->sk;
49294237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
49302bf49690SThomas Liu 	struct common_audit_data ad;
493148c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4932d8395c87SPaul Moore 	char *addrp;
4933d8395c87SPaul Moore 	u8 proto;
49344e5ab4cbSJames Morris 
4935effad8dfSPaul Moore 	if (sk == NULL)
4936effad8dfSPaul Moore 		return NF_ACCEPT;
49374237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
49384e5ab4cbSJames Morris 
493950c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
494048c62af6SEric Paris 	ad.u.net = &net;
494148c62af6SEric Paris 	ad.u.net->netif = ifindex;
494248c62af6SEric Paris 	ad.u.net->family = family;
4943d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4944d8395c87SPaul Moore 		return NF_DROP;
4945d8395c87SPaul Moore 
494658bfbb51SPaul Moore 	if (selinux_secmark_enabled())
4947effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4948d8395c87SPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, &ad))
49492fe66ec2SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
49501da177e4SLinus Torvalds 
4951d8395c87SPaul Moore 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
49522fe66ec2SEric Paris 		return NF_DROP_ERR(-ECONNREFUSED);
4953effad8dfSPaul Moore 
4954effad8dfSPaul Moore 	return NF_ACCEPT;
4955effad8dfSPaul Moore }
4956effad8dfSPaul Moore 
4957cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb,
4958cbe0d6e8SPaul Moore 					 const struct net_device *outdev,
4959effad8dfSPaul Moore 					 u16 family)
4960effad8dfSPaul Moore {
4961effad8dfSPaul Moore 	u32 secmark_perm;
4962effad8dfSPaul Moore 	u32 peer_sid;
4963cbe0d6e8SPaul Moore 	int ifindex = outdev->ifindex;
4964effad8dfSPaul Moore 	struct sock *sk;
49652bf49690SThomas Liu 	struct common_audit_data ad;
496648c62af6SEric Paris 	struct lsm_network_audit net = {0,};
4967effad8dfSPaul Moore 	char *addrp;
4968effad8dfSPaul Moore 	u8 secmark_active;
4969effad8dfSPaul Moore 	u8 peerlbl_active;
4970effad8dfSPaul Moore 
4971effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4972effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4973effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4974effad8dfSPaul Moore 	 * as fast and as clean as possible. */
497558bfbb51SPaul Moore 	if (!selinux_policycap_netpeer)
4976d8395c87SPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, family);
4977c0828e50SPaul Moore 
4978effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
49792be4d74fSChris PeBenito 	peerlbl_active = selinux_peerlbl_enabled();
4980effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4981effad8dfSPaul Moore 		return NF_ACCEPT;
4982effad8dfSPaul Moore 
4983effad8dfSPaul Moore 	sk = skb->sk;
4984c0828e50SPaul Moore 
4985effad8dfSPaul Moore #ifdef CONFIG_XFRM
4986effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4987effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4988effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4989effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4990effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4991c0828e50SPaul Moore 	 *       is NULL, in this case go ahead and apply access control.
4992c0828e50SPaul Moore 	 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4993c0828e50SPaul Moore 	 *       TCP listening state we cannot wait until the XFRM processing
4994c0828e50SPaul Moore 	 *       is done as we will miss out on the SA label if we do;
4995c0828e50SPaul Moore 	 *       unfortunately, this means more work, but it is only once per
4996c0828e50SPaul Moore 	 *       connection. */
4997c0828e50SPaul Moore 	if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4998c0828e50SPaul Moore 	    !(sk != NULL && sk->sk_state == TCP_LISTEN))
4999effad8dfSPaul Moore 		return NF_ACCEPT;
5000effad8dfSPaul Moore #endif
5001effad8dfSPaul Moore 
5002d8395c87SPaul Moore 	if (sk == NULL) {
5003446b8024SPaul Moore 		/* Without an associated socket the packet is either coming
5004446b8024SPaul Moore 		 * from the kernel or it is being forwarded; check the packet
5005446b8024SPaul Moore 		 * to determine which and if the packet is being forwarded
5006446b8024SPaul Moore 		 * query the packet directly to determine the security label. */
50074a7ab3dcSSteffen Klassert 		if (skb->skb_iif) {
5008d8395c87SPaul Moore 			secmark_perm = PACKET__FORWARD_OUT;
5009d8395c87SPaul Moore 			if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
501004f6d70fSEric Paris 				return NF_DROP;
50114a7ab3dcSSteffen Klassert 		} else {
50124a7ab3dcSSteffen Klassert 			secmark_perm = PACKET__SEND;
5013d8395c87SPaul Moore 			peer_sid = SECINITSID_KERNEL;
50144a7ab3dcSSteffen Klassert 		}
5015446b8024SPaul Moore 	} else if (sk->sk_state == TCP_LISTEN) {
5016446b8024SPaul Moore 		/* Locally generated packet but the associated socket is in the
5017446b8024SPaul Moore 		 * listening state which means this is a SYN-ACK packet.  In
5018446b8024SPaul Moore 		 * this particular case the correct security label is assigned
5019446b8024SPaul Moore 		 * to the connection/request_sock but unfortunately we can't
5020446b8024SPaul Moore 		 * query the request_sock as it isn't queued on the parent
5021446b8024SPaul Moore 		 * socket until after the SYN-ACK packet is sent; the only
5022446b8024SPaul Moore 		 * viable choice is to regenerate the label like we do in
5023446b8024SPaul Moore 		 * selinux_inet_conn_request().  See also selinux_ip_output()
5024446b8024SPaul Moore 		 * for similar problems. */
5025446b8024SPaul Moore 		u32 skb_sid;
5026446b8024SPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5027446b8024SPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5028446b8024SPaul Moore 			return NF_DROP;
5029c0828e50SPaul Moore 		/* At this point, if the returned skb peerlbl is SECSID_NULL
5030c0828e50SPaul Moore 		 * and the packet has been through at least one XFRM
5031c0828e50SPaul Moore 		 * transformation then we must be dealing with the "final"
5032c0828e50SPaul Moore 		 * form of labeled IPsec packet; since we've already applied
5033c0828e50SPaul Moore 		 * all of our access controls on this packet we can safely
5034c0828e50SPaul Moore 		 * pass the packet. */
5035c0828e50SPaul Moore 		if (skb_sid == SECSID_NULL) {
5036c0828e50SPaul Moore 			switch (family) {
5037c0828e50SPaul Moore 			case PF_INET:
5038c0828e50SPaul Moore 				if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5039c0828e50SPaul Moore 					return NF_ACCEPT;
5040c0828e50SPaul Moore 				break;
5041c0828e50SPaul Moore 			case PF_INET6:
5042c0828e50SPaul Moore 				if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5043c0828e50SPaul Moore 					return NF_ACCEPT;
5044a7a91a19SPaul Moore 				break;
5045c0828e50SPaul Moore 			default:
5046c0828e50SPaul Moore 				return NF_DROP_ERR(-ECONNREFUSED);
5047c0828e50SPaul Moore 			}
5048c0828e50SPaul Moore 		}
5049446b8024SPaul Moore 		if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5050446b8024SPaul Moore 			return NF_DROP;
5051446b8024SPaul Moore 		secmark_perm = PACKET__SEND;
5052d8395c87SPaul Moore 	} else {
5053446b8024SPaul Moore 		/* Locally generated packet, fetch the security label from the
5054446b8024SPaul Moore 		 * associated socket. */
5055effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
5056effad8dfSPaul Moore 		peer_sid = sksec->sid;
5057effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
5058effad8dfSPaul Moore 	}
5059effad8dfSPaul Moore 
506050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_NET;
506148c62af6SEric Paris 	ad.u.net = &net;
506248c62af6SEric Paris 	ad.u.net->netif = ifindex;
506348c62af6SEric Paris 	ad.u.net->family = family;
5064d8395c87SPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
506504f6d70fSEric Paris 		return NF_DROP;
5066d8395c87SPaul Moore 
5067effad8dfSPaul Moore 	if (secmark_active)
5068effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
5069effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
50701f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5071effad8dfSPaul Moore 
5072effad8dfSPaul Moore 	if (peerlbl_active) {
5073effad8dfSPaul Moore 		u32 if_sid;
5074effad8dfSPaul Moore 		u32 node_sid;
5075effad8dfSPaul Moore 
5076cbe0d6e8SPaul Moore 		if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
507704f6d70fSEric Paris 			return NF_DROP;
5078effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
5079effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
50801f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5081effad8dfSPaul Moore 
5082effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
508304f6d70fSEric Paris 			return NF_DROP;
5084effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
5085effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
50861f1aaf82SEric Paris 			return NF_DROP_ERR(-ECONNREFUSED);
5087effad8dfSPaul Moore 	}
5088effad8dfSPaul Moore 
5089effad8dfSPaul Moore 	return NF_ACCEPT;
5090effad8dfSPaul Moore }
5091effad8dfSPaul Moore 
5092795aa6efSPatrick McHardy static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
5093a224be76SDavid S. Miller 					   struct sk_buff *skb,
50941da177e4SLinus Torvalds 					   const struct net_device *in,
50951da177e4SLinus Torvalds 					   const struct net_device *out,
50961da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
50971da177e4SLinus Torvalds {
5098cbe0d6e8SPaul Moore 	return selinux_ip_postroute(skb, out, PF_INET);
50991da177e4SLinus Torvalds }
51001da177e4SLinus Torvalds 
51011da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5102795aa6efSPatrick McHardy static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
5103a224be76SDavid S. Miller 					   struct sk_buff *skb,
51041da177e4SLinus Torvalds 					   const struct net_device *in,
51051da177e4SLinus Torvalds 					   const struct net_device *out,
51061da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
51071da177e4SLinus Torvalds {
5108cbe0d6e8SPaul Moore 	return selinux_ip_postroute(skb, out, PF_INET6);
51091da177e4SLinus Torvalds }
51101da177e4SLinus Torvalds #endif	/* IPV6 */
51111da177e4SLinus Torvalds 
51121da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
51131da177e4SLinus Torvalds 
51141da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
51151da177e4SLinus Torvalds {
51161da177e4SLinus Torvalds 	int err;
51171da177e4SLinus Torvalds 
5118200ac532SEric Paris 	err = cap_netlink_send(sk, skb);
51191da177e4SLinus Torvalds 	if (err)
51201da177e4SLinus Torvalds 		return err;
51211da177e4SLinus Torvalds 
5122941fc5b2SStephen Smalley 	return selinux_nlmsg_perm(sk, skb);
51231da177e4SLinus Torvalds }
51241da177e4SLinus Torvalds 
51251da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
51261da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
51271da177e4SLinus Torvalds 			      u16 sclass)
51281da177e4SLinus Torvalds {
51291da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
5130275bb41eSDavid Howells 	u32 sid;
51311da177e4SLinus Torvalds 
513289d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
51331da177e4SLinus Torvalds 	if (!isec)
51341da177e4SLinus Torvalds 		return -ENOMEM;
51351da177e4SLinus Torvalds 
5136275bb41eSDavid Howells 	sid = task_sid(task);
51371da177e4SLinus Torvalds 	isec->sclass = sclass;
5138275bb41eSDavid Howells 	isec->sid = sid;
51391da177e4SLinus Torvalds 	perm->security = isec;
51401da177e4SLinus Torvalds 
51411da177e4SLinus Torvalds 	return 0;
51421da177e4SLinus Torvalds }
51431da177e4SLinus Torvalds 
51441da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
51451da177e4SLinus Torvalds {
51461da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
51471da177e4SLinus Torvalds 	perm->security = NULL;
51481da177e4SLinus Torvalds 	kfree(isec);
51491da177e4SLinus Torvalds }
51501da177e4SLinus Torvalds 
51511da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
51521da177e4SLinus Torvalds {
51531da177e4SLinus Torvalds 	struct msg_security_struct *msec;
51541da177e4SLinus Torvalds 
515589d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
51561da177e4SLinus Torvalds 	if (!msec)
51571da177e4SLinus Torvalds 		return -ENOMEM;
51581da177e4SLinus Torvalds 
51591da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
51601da177e4SLinus Torvalds 	msg->security = msec;
51611da177e4SLinus Torvalds 
51621da177e4SLinus Torvalds 	return 0;
51631da177e4SLinus Torvalds }
51641da177e4SLinus Torvalds 
51651da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
51661da177e4SLinus Torvalds {
51671da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
51681da177e4SLinus Torvalds 
51691da177e4SLinus Torvalds 	msg->security = NULL;
51701da177e4SLinus Torvalds 	kfree(msec);
51711da177e4SLinus Torvalds }
51721da177e4SLinus Torvalds 
51731da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
51746af963f1SStephen Smalley 			u32 perms)
51751da177e4SLinus Torvalds {
51761da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
51772bf49690SThomas Liu 	struct common_audit_data ad;
5178275bb41eSDavid Howells 	u32 sid = current_sid();
51791da177e4SLinus Torvalds 
51801da177e4SLinus Torvalds 	isec = ipc_perms->security;
51811da177e4SLinus Torvalds 
518250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
51831da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
51841da177e4SLinus Torvalds 
5185275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
51861da177e4SLinus Torvalds }
51871da177e4SLinus Torvalds 
51881da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
51891da177e4SLinus Torvalds {
51901da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
51911da177e4SLinus Torvalds }
51921da177e4SLinus Torvalds 
51931da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
51941da177e4SLinus Torvalds {
51951da177e4SLinus Torvalds 	msg_msg_free_security(msg);
51961da177e4SLinus Torvalds }
51971da177e4SLinus Torvalds 
51981da177e4SLinus Torvalds /* message queue security operations */
51991da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
52001da177e4SLinus Torvalds {
52011da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52022bf49690SThomas Liu 	struct common_audit_data ad;
5203275bb41eSDavid Howells 	u32 sid = current_sid();
52041da177e4SLinus Torvalds 	int rc;
52051da177e4SLinus Torvalds 
52061da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
52071da177e4SLinus Torvalds 	if (rc)
52081da177e4SLinus Torvalds 		return rc;
52091da177e4SLinus Torvalds 
52101da177e4SLinus Torvalds 	isec = msq->q_perm.security;
52111da177e4SLinus Torvalds 
521250c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52131da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
52141da177e4SLinus Torvalds 
5215275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
52161da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
52171da177e4SLinus Torvalds 	if (rc) {
52181da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
52191da177e4SLinus Torvalds 		return rc;
52201da177e4SLinus Torvalds 	}
52211da177e4SLinus Torvalds 	return 0;
52221da177e4SLinus Torvalds }
52231da177e4SLinus Torvalds 
52241da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
52251da177e4SLinus Torvalds {
52261da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
52271da177e4SLinus Torvalds }
52281da177e4SLinus Torvalds 
52291da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
52301da177e4SLinus Torvalds {
52311da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52322bf49690SThomas Liu 	struct common_audit_data ad;
5233275bb41eSDavid Howells 	u32 sid = current_sid();
52341da177e4SLinus Torvalds 
52351da177e4SLinus Torvalds 	isec = msq->q_perm.security;
52361da177e4SLinus Torvalds 
523750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52381da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
52391da177e4SLinus Torvalds 
5240275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
52411da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
52421da177e4SLinus Torvalds }
52431da177e4SLinus Torvalds 
52441da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
52451da177e4SLinus Torvalds {
52461da177e4SLinus Torvalds 	int err;
52471da177e4SLinus Torvalds 	int perms;
52481da177e4SLinus Torvalds 
52491da177e4SLinus Torvalds 	switch (cmd) {
52501da177e4SLinus Torvalds 	case IPC_INFO:
52511da177e4SLinus Torvalds 	case MSG_INFO:
52521da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
52531da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
52541da177e4SLinus Torvalds 	case IPC_STAT:
52551da177e4SLinus Torvalds 	case MSG_STAT:
52561da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
52571da177e4SLinus Torvalds 		break;
52581da177e4SLinus Torvalds 	case IPC_SET:
52591da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
52601da177e4SLinus Torvalds 		break;
52611da177e4SLinus Torvalds 	case IPC_RMID:
52621da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
52631da177e4SLinus Torvalds 		break;
52641da177e4SLinus Torvalds 	default:
52651da177e4SLinus Torvalds 		return 0;
52661da177e4SLinus Torvalds 	}
52671da177e4SLinus Torvalds 
52686af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
52691da177e4SLinus Torvalds 	return err;
52701da177e4SLinus Torvalds }
52711da177e4SLinus Torvalds 
52721da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
52731da177e4SLinus Torvalds {
52741da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
52751da177e4SLinus Torvalds 	struct msg_security_struct *msec;
52762bf49690SThomas Liu 	struct common_audit_data ad;
5277275bb41eSDavid Howells 	u32 sid = current_sid();
52781da177e4SLinus Torvalds 	int rc;
52791da177e4SLinus Torvalds 
52801da177e4SLinus Torvalds 	isec = msq->q_perm.security;
52811da177e4SLinus Torvalds 	msec = msg->security;
52821da177e4SLinus Torvalds 
52831da177e4SLinus Torvalds 	/*
52841da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
52851da177e4SLinus Torvalds 	 */
52861da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
52871da177e4SLinus Torvalds 		/*
52881da177e4SLinus Torvalds 		 * Compute new sid based on current process and
52891da177e4SLinus Torvalds 		 * message queue this message will be stored in
52901da177e4SLinus Torvalds 		 */
5291275bb41eSDavid Howells 		rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5292652bb9b0SEric Paris 					     NULL, &msec->sid);
52931da177e4SLinus Torvalds 		if (rc)
52941da177e4SLinus Torvalds 			return rc;
52951da177e4SLinus Torvalds 	}
52961da177e4SLinus Torvalds 
529750c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
52981da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
52991da177e4SLinus Torvalds 
53001da177e4SLinus Torvalds 	/* Can this process write to the queue? */
5301275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
53021da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
53031da177e4SLinus Torvalds 	if (!rc)
53041da177e4SLinus Torvalds 		/* Can this process send the message */
5305275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5306275bb41eSDavid Howells 				  MSG__SEND, &ad);
53071da177e4SLinus Torvalds 	if (!rc)
53081da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
5309275bb41eSDavid Howells 		rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5310275bb41eSDavid Howells 				  MSGQ__ENQUEUE, &ad);
53111da177e4SLinus Torvalds 
53121da177e4SLinus Torvalds 	return rc;
53131da177e4SLinus Torvalds }
53141da177e4SLinus Torvalds 
53151da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
53161da177e4SLinus Torvalds 				    struct task_struct *target,
53171da177e4SLinus Torvalds 				    long type, int mode)
53181da177e4SLinus Torvalds {
53191da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53201da177e4SLinus Torvalds 	struct msg_security_struct *msec;
53212bf49690SThomas Liu 	struct common_audit_data ad;
5322275bb41eSDavid Howells 	u32 sid = task_sid(target);
53231da177e4SLinus Torvalds 	int rc;
53241da177e4SLinus Torvalds 
53251da177e4SLinus Torvalds 	isec = msq->q_perm.security;
53261da177e4SLinus Torvalds 	msec = msg->security;
53271da177e4SLinus Torvalds 
532850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53291da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
53301da177e4SLinus Torvalds 
5331275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid,
53321da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
53331da177e4SLinus Torvalds 	if (!rc)
5334275bb41eSDavid Howells 		rc = avc_has_perm(sid, msec->sid,
53351da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
53361da177e4SLinus Torvalds 	return rc;
53371da177e4SLinus Torvalds }
53381da177e4SLinus Torvalds 
53391da177e4SLinus Torvalds /* Shared Memory security operations */
53401da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
53411da177e4SLinus Torvalds {
53421da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53432bf49690SThomas Liu 	struct common_audit_data ad;
5344275bb41eSDavid Howells 	u32 sid = current_sid();
53451da177e4SLinus Torvalds 	int rc;
53461da177e4SLinus Torvalds 
53471da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
53481da177e4SLinus Torvalds 	if (rc)
53491da177e4SLinus Torvalds 		return rc;
53501da177e4SLinus Torvalds 
53511da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
53521da177e4SLinus Torvalds 
535350c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53541da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
53551da177e4SLinus Torvalds 
5356275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
53571da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
53581da177e4SLinus Torvalds 	if (rc) {
53591da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
53601da177e4SLinus Torvalds 		return rc;
53611da177e4SLinus Torvalds 	}
53621da177e4SLinus Torvalds 	return 0;
53631da177e4SLinus Torvalds }
53641da177e4SLinus Torvalds 
53651da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
53661da177e4SLinus Torvalds {
53671da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
53681da177e4SLinus Torvalds }
53691da177e4SLinus Torvalds 
53701da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
53711da177e4SLinus Torvalds {
53721da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
53732bf49690SThomas Liu 	struct common_audit_data ad;
5374275bb41eSDavid Howells 	u32 sid = current_sid();
53751da177e4SLinus Torvalds 
53761da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
53771da177e4SLinus Torvalds 
537850c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
53791da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
53801da177e4SLinus Torvalds 
5381275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
53821da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
53831da177e4SLinus Torvalds }
53841da177e4SLinus Torvalds 
53851da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
53861da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
53871da177e4SLinus Torvalds {
53881da177e4SLinus Torvalds 	int perms;
53891da177e4SLinus Torvalds 	int err;
53901da177e4SLinus Torvalds 
53911da177e4SLinus Torvalds 	switch (cmd) {
53921da177e4SLinus Torvalds 	case IPC_INFO:
53931da177e4SLinus Torvalds 	case SHM_INFO:
53941da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
53951da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
53961da177e4SLinus Torvalds 	case IPC_STAT:
53971da177e4SLinus Torvalds 	case SHM_STAT:
53981da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
53991da177e4SLinus Torvalds 		break;
54001da177e4SLinus Torvalds 	case IPC_SET:
54011da177e4SLinus Torvalds 		perms = SHM__SETATTR;
54021da177e4SLinus Torvalds 		break;
54031da177e4SLinus Torvalds 	case SHM_LOCK:
54041da177e4SLinus Torvalds 	case SHM_UNLOCK:
54051da177e4SLinus Torvalds 		perms = SHM__LOCK;
54061da177e4SLinus Torvalds 		break;
54071da177e4SLinus Torvalds 	case IPC_RMID:
54081da177e4SLinus Torvalds 		perms = SHM__DESTROY;
54091da177e4SLinus Torvalds 		break;
54101da177e4SLinus Torvalds 	default:
54111da177e4SLinus Torvalds 		return 0;
54121da177e4SLinus Torvalds 	}
54131da177e4SLinus Torvalds 
54146af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
54151da177e4SLinus Torvalds 	return err;
54161da177e4SLinus Torvalds }
54171da177e4SLinus Torvalds 
54181da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
54191da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
54201da177e4SLinus Torvalds {
54211da177e4SLinus Torvalds 	u32 perms;
54221da177e4SLinus Torvalds 
54231da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
54241da177e4SLinus Torvalds 		perms = SHM__READ;
54251da177e4SLinus Torvalds 	else
54261da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
54271da177e4SLinus Torvalds 
54286af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
54291da177e4SLinus Torvalds }
54301da177e4SLinus Torvalds 
54311da177e4SLinus Torvalds /* Semaphore security operations */
54321da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
54331da177e4SLinus Torvalds {
54341da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
54352bf49690SThomas Liu 	struct common_audit_data ad;
5436275bb41eSDavid Howells 	u32 sid = current_sid();
54371da177e4SLinus Torvalds 	int rc;
54381da177e4SLinus Torvalds 
54391da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
54401da177e4SLinus Torvalds 	if (rc)
54411da177e4SLinus Torvalds 		return rc;
54421da177e4SLinus Torvalds 
54431da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
54441da177e4SLinus Torvalds 
544550c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
54461da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
54471da177e4SLinus Torvalds 
5448275bb41eSDavid Howells 	rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
54491da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
54501da177e4SLinus Torvalds 	if (rc) {
54511da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
54521da177e4SLinus Torvalds 		return rc;
54531da177e4SLinus Torvalds 	}
54541da177e4SLinus Torvalds 	return 0;
54551da177e4SLinus Torvalds }
54561da177e4SLinus Torvalds 
54571da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
54581da177e4SLinus Torvalds {
54591da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
54601da177e4SLinus Torvalds }
54611da177e4SLinus Torvalds 
54621da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
54631da177e4SLinus Torvalds {
54641da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
54652bf49690SThomas Liu 	struct common_audit_data ad;
5466275bb41eSDavid Howells 	u32 sid = current_sid();
54671da177e4SLinus Torvalds 
54681da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
54691da177e4SLinus Torvalds 
547050c205f5SEric Paris 	ad.type = LSM_AUDIT_DATA_IPC;
54711da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
54721da177e4SLinus Torvalds 
5473275bb41eSDavid Howells 	return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
54741da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
54751da177e4SLinus Torvalds }
54761da177e4SLinus Torvalds 
54771da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
54781da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
54791da177e4SLinus Torvalds {
54801da177e4SLinus Torvalds 	int err;
54811da177e4SLinus Torvalds 	u32 perms;
54821da177e4SLinus Torvalds 
54831da177e4SLinus Torvalds 	switch (cmd) {
54841da177e4SLinus Torvalds 	case IPC_INFO:
54851da177e4SLinus Torvalds 	case SEM_INFO:
54861da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
54871da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
54881da177e4SLinus Torvalds 	case GETPID:
54891da177e4SLinus Torvalds 	case GETNCNT:
54901da177e4SLinus Torvalds 	case GETZCNT:
54911da177e4SLinus Torvalds 		perms = SEM__GETATTR;
54921da177e4SLinus Torvalds 		break;
54931da177e4SLinus Torvalds 	case GETVAL:
54941da177e4SLinus Torvalds 	case GETALL:
54951da177e4SLinus Torvalds 		perms = SEM__READ;
54961da177e4SLinus Torvalds 		break;
54971da177e4SLinus Torvalds 	case SETVAL:
54981da177e4SLinus Torvalds 	case SETALL:
54991da177e4SLinus Torvalds 		perms = SEM__WRITE;
55001da177e4SLinus Torvalds 		break;
55011da177e4SLinus Torvalds 	case IPC_RMID:
55021da177e4SLinus Torvalds 		perms = SEM__DESTROY;
55031da177e4SLinus Torvalds 		break;
55041da177e4SLinus Torvalds 	case IPC_SET:
55051da177e4SLinus Torvalds 		perms = SEM__SETATTR;
55061da177e4SLinus Torvalds 		break;
55071da177e4SLinus Torvalds 	case IPC_STAT:
55081da177e4SLinus Torvalds 	case SEM_STAT:
55091da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
55101da177e4SLinus Torvalds 		break;
55111da177e4SLinus Torvalds 	default:
55121da177e4SLinus Torvalds 		return 0;
55131da177e4SLinus Torvalds 	}
55141da177e4SLinus Torvalds 
55156af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
55161da177e4SLinus Torvalds 	return err;
55171da177e4SLinus Torvalds }
55181da177e4SLinus Torvalds 
55191da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
55201da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
55211da177e4SLinus Torvalds {
55221da177e4SLinus Torvalds 	u32 perms;
55231da177e4SLinus Torvalds 
55241da177e4SLinus Torvalds 	if (alter)
55251da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
55261da177e4SLinus Torvalds 	else
55271da177e4SLinus Torvalds 		perms = SEM__READ;
55281da177e4SLinus Torvalds 
55296af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
55301da177e4SLinus Torvalds }
55311da177e4SLinus Torvalds 
55321da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
55331da177e4SLinus Torvalds {
55341da177e4SLinus Torvalds 	u32 av = 0;
55351da177e4SLinus Torvalds 
55361da177e4SLinus Torvalds 	av = 0;
55371da177e4SLinus Torvalds 	if (flag & S_IRUGO)
55381da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
55391da177e4SLinus Torvalds 	if (flag & S_IWUGO)
55401da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
55411da177e4SLinus Torvalds 
55421da177e4SLinus Torvalds 	if (av == 0)
55431da177e4SLinus Torvalds 		return 0;
55441da177e4SLinus Torvalds 
55456af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
55461da177e4SLinus Torvalds }
55471da177e4SLinus Torvalds 
5548713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5549713a04aeSAhmed S. Darwish {
5550713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5551713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5552713a04aeSAhmed S. Darwish }
5553713a04aeSAhmed S. Darwish 
55541da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
55551da177e4SLinus Torvalds {
55561da177e4SLinus Torvalds 	if (inode)
55571da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
55581da177e4SLinus Torvalds }
55591da177e4SLinus Torvalds 
55601da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
556104ff9708SAl Viro 			       char *name, char **value)
55621da177e4SLinus Torvalds {
5563275bb41eSDavid Howells 	const struct task_security_struct *__tsec;
55648c8570fbSDustin Kirkland 	u32 sid;
55651da177e4SLinus Torvalds 	int error;
556604ff9708SAl Viro 	unsigned len;
55671da177e4SLinus Torvalds 
55681da177e4SLinus Torvalds 	if (current != p) {
55693b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__GETATTR);
55701da177e4SLinus Torvalds 		if (error)
55711da177e4SLinus Torvalds 			return error;
55721da177e4SLinus Torvalds 	}
55731da177e4SLinus Torvalds 
5574275bb41eSDavid Howells 	rcu_read_lock();
5575275bb41eSDavid Howells 	__tsec = __task_cred(p)->security;
55761da177e4SLinus Torvalds 
55771da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
5578275bb41eSDavid Howells 		sid = __tsec->sid;
55791da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
5580275bb41eSDavid Howells 		sid = __tsec->osid;
55811da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
5582275bb41eSDavid Howells 		sid = __tsec->exec_sid;
55831da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
5584275bb41eSDavid Howells 		sid = __tsec->create_sid;
55854eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
5586275bb41eSDavid Howells 		sid = __tsec->keycreate_sid;
558742c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
5588275bb41eSDavid Howells 		sid = __tsec->sockcreate_sid;
55891da177e4SLinus Torvalds 	else
5590275bb41eSDavid Howells 		goto invalid;
5591275bb41eSDavid Howells 	rcu_read_unlock();
55921da177e4SLinus Torvalds 
55931da177e4SLinus Torvalds 	if (!sid)
55941da177e4SLinus Torvalds 		return 0;
55951da177e4SLinus Torvalds 
559604ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
559704ff9708SAl Viro 	if (error)
559804ff9708SAl Viro 		return error;
559904ff9708SAl Viro 	return len;
5600275bb41eSDavid Howells 
5601275bb41eSDavid Howells invalid:
5602275bb41eSDavid Howells 	rcu_read_unlock();
5603275bb41eSDavid Howells 	return -EINVAL;
56041da177e4SLinus Torvalds }
56051da177e4SLinus Torvalds 
56061da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
56071da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
56081da177e4SLinus Torvalds {
56091da177e4SLinus Torvalds 	struct task_security_struct *tsec;
56100356357cSRoland McGrath 	struct task_struct *tracer;
5611d84f4f99SDavid Howells 	struct cred *new;
5612d84f4f99SDavid Howells 	u32 sid = 0, ptsid;
56131da177e4SLinus Torvalds 	int error;
56141da177e4SLinus Torvalds 	char *str = value;
56151da177e4SLinus Torvalds 
56161da177e4SLinus Torvalds 	if (current != p) {
56171da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
56181da177e4SLinus Torvalds 		   security attributes. */
56191da177e4SLinus Torvalds 		return -EACCES;
56201da177e4SLinus Torvalds 	}
56211da177e4SLinus Torvalds 
56221da177e4SLinus Torvalds 	/*
56231da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
56241da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
56251da177e4SLinus Torvalds 	 * above restriction is ever removed.
56261da177e4SLinus Torvalds 	 */
56271da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
56283b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETEXEC);
56291da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
56303b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETFSCREATE);
56314eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
56323b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETKEYCREATE);
563342c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
56343b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETSOCKCREATE);
56351da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
56363b11a1deSDavid Howells 		error = current_has_perm(p, PROCESS__SETCURRENT);
56371da177e4SLinus Torvalds 	else
56381da177e4SLinus Torvalds 		error = -EINVAL;
56391da177e4SLinus Torvalds 	if (error)
56401da177e4SLinus Torvalds 		return error;
56411da177e4SLinus Torvalds 
56421da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
56431da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
56441da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
56451da177e4SLinus Torvalds 			str[size-1] = 0;
56461da177e4SLinus Torvalds 			size--;
56471da177e4SLinus Torvalds 		}
564852a4c640SNikolay Aleksandrov 		error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
564912b29f34SStephen Smalley 		if (error == -EINVAL && !strcmp(name, "fscreate")) {
5650d6ea83ecSEric Paris 			if (!capable(CAP_MAC_ADMIN)) {
5651d6ea83ecSEric Paris 				struct audit_buffer *ab;
5652d6ea83ecSEric Paris 				size_t audit_size;
5653d6ea83ecSEric Paris 
5654d6ea83ecSEric Paris 				/* We strip a nul only if it is at the end, otherwise the
5655d6ea83ecSEric Paris 				 * context contains a nul and we should audit that */
5656d6ea83ecSEric Paris 				if (str[size - 1] == '\0')
5657d6ea83ecSEric Paris 					audit_size = size - 1;
5658d6ea83ecSEric Paris 				else
5659d6ea83ecSEric Paris 					audit_size = size;
5660d6ea83ecSEric Paris 				ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5661d6ea83ecSEric Paris 				audit_log_format(ab, "op=fscreate invalid_context=");
5662d6ea83ecSEric Paris 				audit_log_n_untrustedstring(ab, value, audit_size);
5663d6ea83ecSEric Paris 				audit_log_end(ab);
5664d6ea83ecSEric Paris 
566512b29f34SStephen Smalley 				return error;
5666d6ea83ecSEric Paris 			}
566712b29f34SStephen Smalley 			error = security_context_to_sid_force(value, size,
566812b29f34SStephen Smalley 							      &sid);
566912b29f34SStephen Smalley 		}
56701da177e4SLinus Torvalds 		if (error)
56711da177e4SLinus Torvalds 			return error;
56721da177e4SLinus Torvalds 	}
56731da177e4SLinus Torvalds 
5674d84f4f99SDavid Howells 	new = prepare_creds();
5675d84f4f99SDavid Howells 	if (!new)
5676d84f4f99SDavid Howells 		return -ENOMEM;
5677d84f4f99SDavid Howells 
56781da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
56791da177e4SLinus Torvalds 	   performed during the actual operation (execve,
56801da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
5681d84f4f99SDavid Howells 	   operation.  See selinux_bprm_set_creds for the execve
56821da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
56831da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
5684d84f4f99SDavid Howells 	tsec = new->security;
5685d84f4f99SDavid Howells 	if (!strcmp(name, "exec")) {
56861da177e4SLinus Torvalds 		tsec->exec_sid = sid;
5687d84f4f99SDavid Howells 	} else if (!strcmp(name, "fscreate")) {
56881da177e4SLinus Torvalds 		tsec->create_sid = sid;
5689d84f4f99SDavid Howells 	} else if (!strcmp(name, "keycreate")) {
56904eb582cfSMichael LeMay 		error = may_create_key(sid, p);
56914eb582cfSMichael LeMay 		if (error)
5692d84f4f99SDavid Howells 			goto abort_change;
56934eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
5694d84f4f99SDavid Howells 	} else if (!strcmp(name, "sockcreate")) {
569542c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
5696d84f4f99SDavid Howells 	} else if (!strcmp(name, "current")) {
5697d84f4f99SDavid Howells 		error = -EINVAL;
56981da177e4SLinus Torvalds 		if (sid == 0)
5699d84f4f99SDavid Howells 			goto abort_change;
5700d9250deaSKaiGai Kohei 
5701d84f4f99SDavid Howells 		/* Only allow single threaded processes to change context */
5702d84f4f99SDavid Howells 		error = -EPERM;
57035bb459bbSOleg Nesterov 		if (!current_is_single_threaded()) {
5704d84f4f99SDavid Howells 			error = security_bounded_transition(tsec->sid, sid);
5705d84f4f99SDavid Howells 			if (error)
5706d84f4f99SDavid Howells 				goto abort_change;
57071da177e4SLinus Torvalds 		}
57081da177e4SLinus Torvalds 
57091da177e4SLinus Torvalds 		/* Check permissions for the transition. */
57101da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
57111da177e4SLinus Torvalds 				     PROCESS__DYNTRANSITION, NULL);
57121da177e4SLinus Torvalds 		if (error)
5713d84f4f99SDavid Howells 			goto abort_change;
57141da177e4SLinus Torvalds 
57151da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
57161da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
5717d84f4f99SDavid Howells 		ptsid = 0;
5718c0c14395SOleg Nesterov 		rcu_read_lock();
571906d98473STejun Heo 		tracer = ptrace_parent(p);
5720d84f4f99SDavid Howells 		if (tracer)
5721d84f4f99SDavid Howells 			ptsid = task_sid(tracer);
5722c0c14395SOleg Nesterov 		rcu_read_unlock();
57231da177e4SLinus Torvalds 
5724d84f4f99SDavid Howells 		if (tracer) {
5725d84f4f99SDavid Howells 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5726d84f4f99SDavid Howells 					     PROCESS__PTRACE, NULL);
5727d84f4f99SDavid Howells 			if (error)
5728d84f4f99SDavid Howells 				goto abort_change;
5729d84f4f99SDavid Howells 		}
5730d84f4f99SDavid Howells 
5731d84f4f99SDavid Howells 		tsec->sid = sid;
5732d84f4f99SDavid Howells 	} else {
5733d84f4f99SDavid Howells 		error = -EINVAL;
5734d84f4f99SDavid Howells 		goto abort_change;
5735d84f4f99SDavid Howells 	}
5736d84f4f99SDavid Howells 
5737d84f4f99SDavid Howells 	commit_creds(new);
57381da177e4SLinus Torvalds 	return size;
5739d84f4f99SDavid Howells 
5740d84f4f99SDavid Howells abort_change:
5741d84f4f99SDavid Howells 	abort_creds(new);
5742d84f4f99SDavid Howells 	return error;
57431da177e4SLinus Torvalds }
57441da177e4SLinus Torvalds 
5745746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name)
5746746df9b5SDavid Quigley {
5747746df9b5SDavid Quigley 	return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5748746df9b5SDavid Quigley }
5749746df9b5SDavid Quigley 
5750dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5751dc49c1f9SCatherine Zhang {
5752dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5753dc49c1f9SCatherine Zhang }
5754dc49c1f9SCatherine Zhang 
57557bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
575663cb3449SDavid Howells {
575752a4c640SNikolay Aleksandrov 	return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
575863cb3449SDavid Howells }
575963cb3449SDavid Howells 
5760dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5761dc49c1f9SCatherine Zhang {
5762dc49c1f9SCatherine Zhang 	kfree(secdata);
5763dc49c1f9SCatherine Zhang }
5764dc49c1f9SCatherine Zhang 
57651ee65e37SDavid P. Quigley /*
57661ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
57671ee65e37SDavid P. Quigley  */
57681ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
57691ee65e37SDavid P. Quigley {
57701ee65e37SDavid P. Quigley 	return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
57711ee65e37SDavid P. Quigley }
57721ee65e37SDavid P. Quigley 
57731ee65e37SDavid P. Quigley /*
57741ee65e37SDavid P. Quigley  *	called with inode->i_mutex locked
57751ee65e37SDavid P. Quigley  */
57761ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
57771ee65e37SDavid P. Quigley {
57781ee65e37SDavid P. Quigley 	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
57791ee65e37SDavid P. Quigley }
57801ee65e37SDavid P. Quigley 
57811ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
57821ee65e37SDavid P. Quigley {
57831ee65e37SDavid P. Quigley 	int len = 0;
57841ee65e37SDavid P. Quigley 	len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
57851ee65e37SDavid P. Quigley 						ctx, true);
57861ee65e37SDavid P. Quigley 	if (len < 0)
57871ee65e37SDavid P. Quigley 		return len;
57881ee65e37SDavid P. Quigley 	*ctxlen = len;
57891ee65e37SDavid P. Quigley 	return 0;
57901ee65e37SDavid P. Quigley }
5791d720024eSMichael LeMay #ifdef CONFIG_KEYS
5792d720024eSMichael LeMay 
5793d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred,
57947e047ef5SDavid Howells 			     unsigned long flags)
5795d720024eSMichael LeMay {
5796d84f4f99SDavid Howells 	const struct task_security_struct *tsec;
5797d720024eSMichael LeMay 	struct key_security_struct *ksec;
5798d720024eSMichael LeMay 
5799d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5800d720024eSMichael LeMay 	if (!ksec)
5801d720024eSMichael LeMay 		return -ENOMEM;
5802d720024eSMichael LeMay 
5803d84f4f99SDavid Howells 	tsec = cred->security;
5804d84f4f99SDavid Howells 	if (tsec->keycreate_sid)
5805d84f4f99SDavid Howells 		ksec->sid = tsec->keycreate_sid;
58064eb582cfSMichael LeMay 	else
5807d84f4f99SDavid Howells 		ksec->sid = tsec->sid;
5808d720024eSMichael LeMay 
5809275bb41eSDavid Howells 	k->security = ksec;
5810d720024eSMichael LeMay 	return 0;
5811d720024eSMichael LeMay }
5812d720024eSMichael LeMay 
5813d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5814d720024eSMichael LeMay {
5815d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5816d720024eSMichael LeMay 
5817d720024eSMichael LeMay 	k->security = NULL;
5818d720024eSMichael LeMay 	kfree(ksec);
5819d720024eSMichael LeMay }
5820d720024eSMichael LeMay 
5821d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5822d84f4f99SDavid Howells 				  const struct cred *cred,
5823f5895943SDavid Howells 				  unsigned perm)
5824d720024eSMichael LeMay {
5825d720024eSMichael LeMay 	struct key *key;
5826d720024eSMichael LeMay 	struct key_security_struct *ksec;
5827275bb41eSDavid Howells 	u32 sid;
5828d720024eSMichael LeMay 
5829d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5830d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5831d720024eSMichael LeMay 	   appear to be created. */
5832d720024eSMichael LeMay 	if (perm == 0)
5833d720024eSMichael LeMay 		return 0;
5834d720024eSMichael LeMay 
5835d84f4f99SDavid Howells 	sid = cred_sid(cred);
5836275bb41eSDavid Howells 
5837275bb41eSDavid Howells 	key = key_ref_to_ptr(key_ref);
5838275bb41eSDavid Howells 	ksec = key->security;
5839275bb41eSDavid Howells 
5840275bb41eSDavid Howells 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5841d720024eSMichael LeMay }
5842d720024eSMichael LeMay 
584370a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer)
584470a5bb72SDavid Howells {
584570a5bb72SDavid Howells 	struct key_security_struct *ksec = key->security;
584670a5bb72SDavid Howells 	char *context = NULL;
584770a5bb72SDavid Howells 	unsigned len;
584870a5bb72SDavid Howells 	int rc;
584970a5bb72SDavid Howells 
585070a5bb72SDavid Howells 	rc = security_sid_to_context(ksec->sid, &context, &len);
585170a5bb72SDavid Howells 	if (!rc)
585270a5bb72SDavid Howells 		rc = len;
585370a5bb72SDavid Howells 	*_buffer = context;
585470a5bb72SDavid Howells 	return rc;
585570a5bb72SDavid Howells }
585670a5bb72SDavid Howells 
5857d720024eSMichael LeMay #endif
5858d720024eSMichael LeMay 
58591da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5860076c54c5SAhmed S. Darwish 	.name =				"selinux",
5861076c54c5SAhmed S. Darwish 
586279af7307SStephen Smalley 	.binder_set_context_mgr =	selinux_binder_set_context_mgr,
586379af7307SStephen Smalley 	.binder_transaction =		selinux_binder_transaction,
586479af7307SStephen Smalley 	.binder_transfer_binder =	selinux_binder_transfer_binder,
586579af7307SStephen Smalley 	.binder_transfer_file =		selinux_binder_transfer_file,
586679af7307SStephen Smalley 
58679e48858fSIngo Molnar 	.ptrace_access_check =		selinux_ptrace_access_check,
58685cd9c58fSDavid Howells 	.ptrace_traceme =		selinux_ptrace_traceme,
58691da177e4SLinus Torvalds 	.capget =			selinux_capget,
5870d84f4f99SDavid Howells 	.capset =			selinux_capset,
58711da177e4SLinus Torvalds 	.capable =			selinux_capable,
58721da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
58731da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
58741da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
58751da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
58761da177e4SLinus Torvalds 
58771da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
58781da177e4SLinus Torvalds 
5879a6f76f23SDavid Howells 	.bprm_set_creds =		selinux_bprm_set_creds,
5880a6f76f23SDavid Howells 	.bprm_committing_creds =	selinux_bprm_committing_creds,
5881a6f76f23SDavid Howells 	.bprm_committed_creds =		selinux_bprm_committed_creds,
58821da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
58831da177e4SLinus Torvalds 
58841da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
58851da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
58861da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
5887026eb167SEric Paris 	.sb_remount =			selinux_sb_remount,
58881da177e4SLinus Torvalds 	.sb_kern_mount =		selinux_sb_kern_mount,
58892069f457SEric Paris 	.sb_show_options =		selinux_sb_show_options,
58901da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
58911da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
58921da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5893c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5894c9180a57SEric Paris 	.sb_clone_mnt_opts =		selinux_sb_clone_mnt_opts,
5895e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5896e0007529SEric Paris 
5897d47be3dfSDavid Quigley 	.dentry_init_security =		selinux_dentry_init_security,
58981da177e4SLinus Torvalds 
58991da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
59001da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
59015e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
59021da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
59031da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
59041da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
59051da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
59061da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
59071da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
59081da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
59091da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
59101da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
59111da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
59121da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
59131da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
59141da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
59151da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
59161da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
59171da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
59181da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
59191da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
59201da177e4SLinus Torvalds 	.inode_getsecurity =		selinux_inode_getsecurity,
59211da177e4SLinus Torvalds 	.inode_setsecurity =		selinux_inode_setsecurity,
59221da177e4SLinus Torvalds 	.inode_listsecurity =		selinux_inode_listsecurity,
5923713a04aeSAhmed S. Darwish 	.inode_getsecid =		selinux_inode_getsecid,
59241da177e4SLinus Torvalds 
59251da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
59261da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
59271da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
59281da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
5929e5467859SAl Viro 	.mmap_file =			selinux_mmap_file,
5930e5467859SAl Viro 	.mmap_addr =			selinux_mmap_addr,
59311da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
59321da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
59331da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
59341da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
59351da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
59361da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
59371da177e4SLinus Torvalds 
593883d49856SEric Paris 	.file_open =			selinux_file_open,
5939788e7dd4SYuichi Nakamura 
59401da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
5941ee18d64cSDavid Howells 	.cred_alloc_blank =		selinux_cred_alloc_blank,
5942f1752eecSDavid Howells 	.cred_free =			selinux_cred_free,
5943d84f4f99SDavid Howells 	.cred_prepare =			selinux_cred_prepare,
5944ee18d64cSDavid Howells 	.cred_transfer =		selinux_cred_transfer,
59453a3b7ce9SDavid Howells 	.kernel_act_as =		selinux_kernel_act_as,
59463a3b7ce9SDavid Howells 	.kernel_create_files_as =	selinux_kernel_create_files_as,
594725354c4fSEric Paris 	.kernel_module_request =	selinux_kernel_module_request,
59481da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
59491da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
59501da177e4SLinus Torvalds 	.task_getsid =			selinux_task_getsid,
5951f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
59521da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
595303e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5954a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
59551da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
59561da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
59571da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
595835601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
59591da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
59601da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
59611da177e4SLinus Torvalds 	.task_to_inode =		selinux_task_to_inode,
59621da177e4SLinus Torvalds 
59631da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5964713a04aeSAhmed S. Darwish 	.ipc_getsecid =			selinux_ipc_getsecid,
59651da177e4SLinus Torvalds 
59661da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
59671da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
59681da177e4SLinus Torvalds 
59691da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
59701da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
59711da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
59721da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
59731da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
59741da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
59751da177e4SLinus Torvalds 
59761da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
59771da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
59781da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
59791da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
59801da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
59811da177e4SLinus Torvalds 
59821da177e4SLinus Torvalds 	.sem_alloc_security =		selinux_sem_alloc_security,
59831da177e4SLinus Torvalds 	.sem_free_security =		selinux_sem_free_security,
59841da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
59851da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
59861da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
59871da177e4SLinus Torvalds 
59881da177e4SLinus Torvalds 	.d_instantiate =		selinux_d_instantiate,
59891da177e4SLinus Torvalds 
59901da177e4SLinus Torvalds 	.getprocattr =			selinux_getprocattr,
59911da177e4SLinus Torvalds 	.setprocattr =			selinux_setprocattr,
59921da177e4SLinus Torvalds 
5993746df9b5SDavid Quigley 	.ismaclabel =			selinux_ismaclabel,
5994dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
599563cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5996dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
59971ee65e37SDavid P. Quigley 	.inode_notifysecctx =		selinux_inode_notifysecctx,
59981ee65e37SDavid P. Quigley 	.inode_setsecctx =		selinux_inode_setsecctx,
59991ee65e37SDavid P. Quigley 	.inode_getsecctx =		selinux_inode_getsecctx,
6000dc49c1f9SCatherine Zhang 
60011da177e4SLinus Torvalds 	.unix_stream_connect =		selinux_socket_unix_stream_connect,
60021da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
60031da177e4SLinus Torvalds 
60041da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
60051da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
60061da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
60071da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
60081da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
60091da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
60101da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
60111da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
60121da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
60131da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
60141da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
60151da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
60161da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
60171da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
60182c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
60192c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
60201da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
60211da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
6022892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
6023beb8d13bSVenkat Yekkirala 	.sk_getsecid =			selinux_sk_getsecid,
60244237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
60254237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
60264237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
60276b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
60282606fd1fSEric Paris 	.secmark_relabel_packet =	selinux_secmark_relabel_packet,
60292606fd1fSEric Paris 	.secmark_refcount_inc =		selinux_secmark_refcount_inc,
60302606fd1fSEric Paris 	.secmark_refcount_dec =		selinux_secmark_refcount_dec,
60314237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
60325dbbaf2dSPaul Moore 	.tun_dev_alloc_security =	selinux_tun_dev_alloc_security,
60335dbbaf2dSPaul Moore 	.tun_dev_free_security =	selinux_tun_dev_free_security,
6034ed6d76e4SPaul Moore 	.tun_dev_create =		selinux_tun_dev_create,
60355dbbaf2dSPaul Moore 	.tun_dev_attach_queue =		selinux_tun_dev_attach_queue,
6036ed6d76e4SPaul Moore 	.tun_dev_attach =		selinux_tun_dev_attach,
60375dbbaf2dSPaul Moore 	.tun_dev_open =			selinux_tun_dev_open,
6038ca10b9e9SEric Dumazet 	.skb_owned_by =			selinux_skb_owned_by,
6039d28d1e08STrent Jaeger 
6040d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
6041d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
6042d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
6043d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
6044c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
60452e5aa866SPaul Moore 	.xfrm_state_alloc =		selinux_xfrm_state_alloc,
60462e5aa866SPaul Moore 	.xfrm_state_alloc_acquire =	selinux_xfrm_state_alloc_acquire,
6047d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
6048c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
6049d28d1e08STrent Jaeger 	.xfrm_policy_lookup =		selinux_xfrm_policy_lookup,
6050e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
6051e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
60521da177e4SLinus Torvalds #endif
6053d720024eSMichael LeMay 
6054d720024eSMichael LeMay #ifdef CONFIG_KEYS
6055d720024eSMichael LeMay 	.key_alloc =			selinux_key_alloc,
6056d720024eSMichael LeMay 	.key_free =			selinux_key_free,
6057d720024eSMichael LeMay 	.key_permission =		selinux_key_permission,
605870a5bb72SDavid Howells 	.key_getsecurity =		selinux_key_getsecurity,
6059d720024eSMichael LeMay #endif
60609d57a7f9SAhmed S. Darwish 
60619d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
60629d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
60639d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
60649d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
60659d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
60669d57a7f9SAhmed S. Darwish #endif
60671da177e4SLinus Torvalds };
60681da177e4SLinus Torvalds 
60691da177e4SLinus Torvalds static __init int selinux_init(void)
60701da177e4SLinus Torvalds {
6071076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
6072076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
6073076c54c5SAhmed S. Darwish 		return 0;
6074076c54c5SAhmed S. Darwish 	}
6075076c54c5SAhmed S. Darwish 
60761da177e4SLinus Torvalds 	if (!selinux_enabled) {
60771da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
60781da177e4SLinus Torvalds 		return 0;
60791da177e4SLinus Torvalds 	}
60801da177e4SLinus Torvalds 
60811da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
60821da177e4SLinus Torvalds 
60831da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
6084d84f4f99SDavid Howells 	cred_init_security();
60851da177e4SLinus Torvalds 
6086fcaaade1SStephen Smalley 	default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6087fcaaade1SStephen Smalley 
60887cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
60897cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
609020c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
60911da177e4SLinus Torvalds 	avc_init();
60921da177e4SLinus Torvalds 
60931da177e4SLinus Torvalds 	if (register_security(&selinux_ops))
60941da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
60951da177e4SLinus Torvalds 
6096615e51fdSPaul Moore 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6097615e51fdSPaul Moore 		panic("SELinux: Unable to register AVC netcache callback\n");
6098615e51fdSPaul Moore 
6099828dfe1dSEric Paris 	if (selinux_enforcing)
6100fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
6101828dfe1dSEric Paris 	else
6102fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
6103d720024eSMichael LeMay 
61041da177e4SLinus Torvalds 	return 0;
61051da177e4SLinus Torvalds }
61061da177e4SLinus Torvalds 
6107e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused)
6108e8c26255SAl Viro {
6109e8c26255SAl Viro 	superblock_doinit(sb, NULL);
6110e8c26255SAl Viro }
6111e8c26255SAl Viro 
61121da177e4SLinus Torvalds void selinux_complete_init(void)
61131da177e4SLinus Torvalds {
6114fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
61151da177e4SLinus Torvalds 
61161da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
6117fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
6118e8c26255SAl Viro 	iterate_supers(delayed_superblock_init, NULL);
61191da177e4SLinus Torvalds }
61201da177e4SLinus Torvalds 
61211da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
61221da177e4SLinus Torvalds    all processes and objects when they are created. */
61231da177e4SLinus Torvalds security_initcall(selinux_init);
61241da177e4SLinus Torvalds 
6125c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
61261da177e4SLinus Torvalds 
612725db6beaSJiri Pirko static struct nf_hook_ops selinux_nf_ops[] = {
6128effad8dfSPaul Moore 	{
6129effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
61301da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
61312597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
61326e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
61331da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
6134effad8dfSPaul Moore 	},
6135effad8dfSPaul Moore 	{
6136effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
6137effad8dfSPaul Moore 		.owner =	THIS_MODULE,
61382597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
6139effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
6140effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
6141948bf85cSPaul Moore 	},
6142948bf85cSPaul Moore 	{
6143948bf85cSPaul Moore 		.hook =		selinux_ipv4_output,
6144948bf85cSPaul Moore 		.owner =	THIS_MODULE,
61452597a834SAlban Crequy 		.pf =		NFPROTO_IPV4,
6146948bf85cSPaul Moore 		.hooknum =	NF_INET_LOCAL_OUT,
6147948bf85cSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
614825db6beaSJiri Pirko 	},
61491da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6150effad8dfSPaul Moore 	{
6151effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
61521da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
61532597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
61546e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
61551da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
6156effad8dfSPaul Moore 	},
6157effad8dfSPaul Moore 	{
6158effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
6159effad8dfSPaul Moore 		.owner =	THIS_MODULE,
61602597a834SAlban Crequy 		.pf =		NFPROTO_IPV6,
6161effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
6162effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
616325db6beaSJiri Pirko 	},
61641da177e4SLinus Torvalds #endif	/* IPV6 */
616525db6beaSJiri Pirko };
61661da177e4SLinus Torvalds 
61671da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
61681da177e4SLinus Torvalds {
616925db6beaSJiri Pirko 	int err;
61701da177e4SLinus Torvalds 
61711da177e4SLinus Torvalds 	if (!selinux_enabled)
617225db6beaSJiri Pirko 		return 0;
61731da177e4SLinus Torvalds 
6174fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
61751da177e4SLinus Torvalds 
617625db6beaSJiri Pirko 	err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
61771da177e4SLinus Torvalds 	if (err)
617825db6beaSJiri Pirko 		panic("SELinux: nf_register_hooks: error %d\n", err);
61791da177e4SLinus Torvalds 
618025db6beaSJiri Pirko 	return 0;
61811da177e4SLinus Torvalds }
61821da177e4SLinus Torvalds 
61831da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
61841da177e4SLinus Torvalds 
61851da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
61861da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
61871da177e4SLinus Torvalds {
6188fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
61891da177e4SLinus Torvalds 
619025db6beaSJiri Pirko 	nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
61911da177e4SLinus Torvalds }
61921da177e4SLinus Torvalds #endif
61931da177e4SLinus Torvalds 
6194c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
61951da177e4SLinus Torvalds 
61961da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
61971da177e4SLinus Torvalds #define selinux_nf_ip_exit()
61981da177e4SLinus Torvalds #endif
61991da177e4SLinus Torvalds 
6200c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
62011da177e4SLinus Torvalds 
62021da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6203828dfe1dSEric Paris static int selinux_disabled;
6204828dfe1dSEric Paris 
62051da177e4SLinus Torvalds int selinux_disable(void)
62061da177e4SLinus Torvalds {
62071da177e4SLinus Torvalds 	if (ss_initialized) {
62081da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
62091da177e4SLinus Torvalds 		return -EINVAL;
62101da177e4SLinus Torvalds 	}
62111da177e4SLinus Torvalds 
62121da177e4SLinus Torvalds 	if (selinux_disabled) {
62131da177e4SLinus Torvalds 		/* Only do this once. */
62141da177e4SLinus Torvalds 		return -EINVAL;
62151da177e4SLinus Torvalds 	}
62161da177e4SLinus Torvalds 
62171da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
62181da177e4SLinus Torvalds 
62191da177e4SLinus Torvalds 	selinux_disabled = 1;
622030d55280SStephen Smalley 	selinux_enabled = 0;
62211da177e4SLinus Torvalds 
6222189b3b1cSwzt.wzt@gmail.com 	reset_security_ops();
62231da177e4SLinus Torvalds 
6224af8ff049SEric Paris 	/* Try to destroy the avc node cache */
6225af8ff049SEric Paris 	avc_disable();
6226af8ff049SEric Paris 
62271da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
62281da177e4SLinus Torvalds 	selinux_nf_ip_exit();
62291da177e4SLinus Torvalds 
62301da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
62311da177e4SLinus Torvalds 	exit_sel_fs();
62321da177e4SLinus Torvalds 
62331da177e4SLinus Torvalds 	return 0;
62341da177e4SLinus Torvalds }
62351da177e4SLinus Torvalds #endif
6236