xref: /openbmc/linux/security/selinux/hooks.c (revision 744ba35e455b0d5cf4f85208a8ca0edcc9976b95)
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.
121da177e4SLinus Torvalds  *  Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
131da177e4SLinus Torvalds  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
141da177e4SLinus Torvalds  *                          <dgoeddel@trustedcs.com>
15effad8dfSPaul Moore  *  Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
16effad8dfSPaul Moore  *                Paul Moore <paul.moore@hp.com>
17788e7dd4SYuichi Nakamura  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
18788e7dd4SYuichi Nakamura  *                     Yuichi Nakamura <ynakam@hitachisoft.jp>
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
211da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
221da177e4SLinus Torvalds  *      as published by the Free Software Foundation.
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/ptrace.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/sched.h>
301da177e4SLinus Torvalds #include <linux/security.h>
311da177e4SLinus Torvalds #include <linux/xattr.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/unistd.h>
341da177e4SLinus Torvalds #include <linux/mm.h>
351da177e4SLinus Torvalds #include <linux/mman.h>
361da177e4SLinus Torvalds #include <linux/slab.h>
371da177e4SLinus Torvalds #include <linux/pagemap.h>
381da177e4SLinus Torvalds #include <linux/swap.h>
391da177e4SLinus Torvalds #include <linux/spinlock.h>
401da177e4SLinus Torvalds #include <linux/syscalls.h>
411da177e4SLinus Torvalds #include <linux/file.h>
421da177e4SLinus Torvalds #include <linux/namei.h>
431da177e4SLinus Torvalds #include <linux/mount.h>
441da177e4SLinus Torvalds #include <linux/ext2_fs.h>
451da177e4SLinus Torvalds #include <linux/proc_fs.h>
461da177e4SLinus Torvalds #include <linux/kd.h>
471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
491da177e4SLinus Torvalds #include <linux/tty.h>
501da177e4SLinus Torvalds #include <net/icmp.h>
51227b60f5SStephen Hemminger #include <net/ip.h>		/* for local_port_range[] */
521da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
53220deb96SPaul Moore #include <net/net_namespace.h>
54d621d35eSPaul Moore #include <net/netlabel.h>
551da177e4SLinus Torvalds #include <asm/uaccess.h>
561da177e4SLinus Torvalds #include <asm/ioctls.h>
57d621d35eSPaul Moore #include <asm/atomic.h>
581da177e4SLinus Torvalds #include <linux/bitops.h>
591da177e4SLinus Torvalds #include <linux/interrupt.h>
601da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
611da177e4SLinus Torvalds #include <linux/netlink.h>
621da177e4SLinus Torvalds #include <linux/tcp.h>
631da177e4SLinus Torvalds #include <linux/udp.h>
642ee92d46SJames Morris #include <linux/dccp.h>
651da177e4SLinus Torvalds #include <linux/quota.h>
661da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
671da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
681da177e4SLinus Torvalds #include <linux/parser.h>
691da177e4SLinus Torvalds #include <linux/nfs_mount.h>
701da177e4SLinus Torvalds #include <net/ipv6.h>
711da177e4SLinus Torvalds #include <linux/hugetlb.h>
721da177e4SLinus Torvalds #include <linux/personality.h>
731da177e4SLinus Torvalds #include <linux/sysctl.h>
741da177e4SLinus Torvalds #include <linux/audit.h>
756931dfc9SEric Paris #include <linux/string.h>
76877ce7c1SCatherine Zhang #include <linux/selinux.h>
7723970741SEric Paris #include <linux/mutex.h>
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #include "avc.h"
801da177e4SLinus Torvalds #include "objsec.h"
811da177e4SLinus Torvalds #include "netif.h"
82224dfbd8SPaul Moore #include "netnode.h"
833e112172SPaul Moore #include "netport.h"
84d28d1e08STrent Jaeger #include "xfrm.h"
85c60475bfSPaul Moore #include "netlabel.h"
869d57a7f9SAhmed S. Darwish #include "audit.h"
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux"
891da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
901da177e4SLinus Torvalds 
91c9180a57SEric Paris #define NUM_SEL_MNT_OPTS 4
92c9180a57SEric Paris 
931da177e4SLinus Torvalds extern unsigned int policydb_loaded_version;
941da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
954e5ab4cbSJames Morris extern int selinux_compat_net;
9620510f2fSJames Morris extern struct security_operations *security_ops;
971da177e4SLinus Torvalds 
98d621d35eSPaul Moore /* SECMARK reference count */
99d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
100d621d35eSPaul Moore 
1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1021da177e4SLinus Torvalds int selinux_enforcing = 0;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	selinux_enforcing = simple_strtol(str,NULL,0);
1071da177e4SLinus Torvalds 	return 1;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
1101da177e4SLinus Torvalds #endif
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1131da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	selinux_enabled = simple_strtol(str, NULL, 0);
1181da177e4SLinus Torvalds 	return 1;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
12130d55280SStephen Smalley #else
12230d55280SStephen Smalley int selinux_enabled = 1;
1231da177e4SLinus Torvalds #endif
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* Original (dummy) security module. */
1261da177e4SLinus Torvalds static struct security_operations *original_ops = NULL;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* Minimal support for a secondary security module,
1291da177e4SLinus Torvalds    just to allow the use of the dummy or capability modules.
1301da177e4SLinus Torvalds    The owlsm module can alternatively be used as a secondary
1311da177e4SLinus Torvalds    module as long as CONFIG_OWLSM_FD is not enabled. */
1321da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized
1351da177e4SLinus Torvalds    before the policy was loaded. */
1361da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head);
1371da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock);
1381da177e4SLinus Torvalds 
139e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache;
1407cae7e26SJames Morris 
141d621d35eSPaul Moore /**
142d621d35eSPaul Moore  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
143d621d35eSPaul Moore  *
144d621d35eSPaul Moore  * Description:
145d621d35eSPaul Moore  * This function checks the SECMARK reference counter to see if any SECMARK
146d621d35eSPaul Moore  * targets are currently configured, if the reference counter is greater than
147d621d35eSPaul Moore  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
148d621d35eSPaul Moore  * enabled, false (0) if SECMARK is disabled.
149d621d35eSPaul Moore  *
150d621d35eSPaul Moore  */
151d621d35eSPaul Moore static int selinux_secmark_enabled(void)
152d621d35eSPaul Moore {
153d621d35eSPaul Moore 	return (atomic_read(&selinux_secmark_refcount) > 0);
154d621d35eSPaul Moore }
155d621d35eSPaul Moore 
1561da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1611da177e4SLinus Torvalds 
16289d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1631da177e4SLinus Torvalds 	if (!tsec)
1641da177e4SLinus Torvalds 		return -ENOMEM;
1651da177e4SLinus Torvalds 
1660356357cSRoland McGrath 	tsec->osid = tsec->sid = SECINITSID_UNLABELED;
1671da177e4SLinus Torvalds 	task->security = tsec;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void task_free_security(struct task_struct *task)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct task_security_struct *tsec = task->security;
1751da177e4SLinus Torvalds 	task->security = NULL;
1761da177e4SLinus Torvalds 	kfree(tsec);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
1821da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1831da177e4SLinus Torvalds 
184a02fe132SJosef Bacik 	isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
1851da177e4SLinus Torvalds 	if (!isec)
1861da177e4SLinus Torvalds 		return -ENOMEM;
1871da177e4SLinus Torvalds 
18823970741SEric Paris 	mutex_init(&isec->lock);
1891da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
1901da177e4SLinus Torvalds 	isec->inode = inode;
1911da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
1921da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
1931da177e4SLinus Torvalds 	isec->task_sid = tsec->sid;
1941da177e4SLinus Torvalds 	inode->i_security = isec;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	return 0;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2021da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2051da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2061da177e4SLinus Torvalds 		list_del_init(&isec->list);
2071da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	inode->i_security = NULL;
2107cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
2161da177e4SLinus Torvalds 	struct file_security_struct *fsec;
2171da177e4SLinus Torvalds 
21826d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2191da177e4SLinus Torvalds 	if (!fsec)
2201da177e4SLinus Torvalds 		return -ENOMEM;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	fsec->sid = tsec->sid;
2231da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
2241da177e4SLinus Torvalds 	file->f_security = fsec;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	return 0;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static void file_free_security(struct file *file)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2321da177e4SLinus Torvalds 	file->f_security = NULL;
2331da177e4SLinus Torvalds 	kfree(fsec);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2391da177e4SLinus Torvalds 
24089d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2411da177e4SLinus Torvalds 	if (!sbsec)
2421da177e4SLinus Torvalds 		return -ENOMEM;
2431da177e4SLinus Torvalds 
244bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2451da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->list);
2461da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2471da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2481da177e4SLinus Torvalds 	sbsec->sb = sb;
2491da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2501da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
251c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2521da177e4SLinus Torvalds 	sb->s_security = sbsec;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	return 0;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
2621da177e4SLinus Torvalds 	if (!list_empty(&sbsec->list))
2631da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
2641da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	sb->s_security = NULL;
2671da177e4SLinus Torvalds 	kfree(sbsec);
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2707d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
2731da177e4SLinus Torvalds 
27489d155efSJames Morris 	ssec = kzalloc(sizeof(*ssec), priority);
2751da177e4SLinus Torvalds 	if (!ssec)
2761da177e4SLinus Torvalds 		return -ENOMEM;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	ssec->peer_sid = SECINITSID_UNLABELED;
279892c141eSVenkat Yekkirala 	ssec->sid = SECINITSID_UNLABELED;
2801da177e4SLinus Torvalds 	sk->sk_security = ssec;
2811da177e4SLinus Torvalds 
282f74af6e8SPaul Moore 	selinux_netlbl_sk_security_reset(ssec, family);
28399f59ed0SPaul Moore 
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static void sk_free_security(struct sock *sk)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct sk_security_struct *ssec = sk->sk_security;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	sk->sk_security = NULL;
2921da177e4SLinus Torvalds 	kfree(ssec);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /* The security server must be initialized before
2961da177e4SLinus Torvalds    any labeling or access decisions can be provided. */
2971da177e4SLinus Torvalds extern int ss_initialized;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static char *labeling_behaviors[6] = {
3021da177e4SLinus Torvalds 	"uses xattr",
3031da177e4SLinus Torvalds 	"uses transition SIDs",
3041da177e4SLinus Torvalds 	"uses task SIDs",
3051da177e4SLinus Torvalds 	"uses genfs_contexts",
3061da177e4SLinus Torvalds 	"not configured for labeling",
3071da177e4SLinus Torvalds 	"uses mountpoint labeling",
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds enum {
31831e87930SEric Paris 	Opt_error = -1,
3191da177e4SLinus Torvalds 	Opt_context = 1,
3201da177e4SLinus Torvalds 	Opt_fscontext = 2,
321c9180a57SEric Paris 	Opt_defcontext = 3,
322c9180a57SEric Paris 	Opt_rootcontext = 4,
3231da177e4SLinus Torvalds };
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static match_table_t tokens = {
326832cbd9aSEric Paris 	{Opt_context, CONTEXT_STR "%s"},
327832cbd9aSEric Paris 	{Opt_fscontext, FSCONTEXT_STR "%s"},
328832cbd9aSEric Paris 	{Opt_defcontext, DEFCONTEXT_STR "%s"},
329832cbd9aSEric Paris 	{Opt_rootcontext, ROOTCONTEXT_STR "%s"},
33031e87930SEric Paris 	{Opt_error, NULL},
3311da177e4SLinus Torvalds };
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3341da177e4SLinus Torvalds 
335c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
336c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
337c312feb2SEric Paris 			struct task_security_struct *tsec)
338c312feb2SEric Paris {
339c312feb2SEric Paris 	int rc;
340c312feb2SEric Paris 
341c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
342c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
343c312feb2SEric Paris 	if (rc)
344c312feb2SEric Paris 		return rc;
345c312feb2SEric Paris 
346c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
347c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
348c312feb2SEric Paris 	return rc;
349c312feb2SEric Paris }
350c312feb2SEric Paris 
3510808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3520808925eSEric Paris 			struct superblock_security_struct *sbsec,
3530808925eSEric Paris 			struct task_security_struct *tsec)
3540808925eSEric Paris {
3550808925eSEric Paris 	int rc;
3560808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3570808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3580808925eSEric Paris 	if (rc)
3590808925eSEric Paris 		return rc;
3600808925eSEric Paris 
3610808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3620808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3630808925eSEric Paris 	return rc;
3640808925eSEric Paris }
3650808925eSEric Paris 
366c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3691da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
370c9180a57SEric Paris 	struct inode *root_inode = root->d_inode;
3711da177e4SLinus Torvalds 	int rc = 0;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
3741da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
3751da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
3761da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
3771da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
3781da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
379c9180a57SEric Paris 		if (!root_inode->i_op->getxattr) {
3801da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
3811da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
3821da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
3831da177e4SLinus Torvalds 			goto out;
3841da177e4SLinus Torvalds 		}
385c9180a57SEric Paris 		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
3861da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
3871da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
3881da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3891da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
3901da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
3911da177e4SLinus Torvalds 			else
3921da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
3931da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
3941da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
3951da177e4SLinus Torvalds 			goto out;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	sbsec->initialized = 1;
4001da177e4SLinus Torvalds 
401c9180a57SEric Paris 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
402fadcdb45SEric Paris 		printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
4031da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
404c9180a57SEric Paris 	else
405fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
4061da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
4071da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/* Initialize the root inode. */
410c9180a57SEric Paris 	rc = inode_doinit_with_dentry(root_inode, root);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
4131da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
4141da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
4151da177e4SLinus Torvalds 	   populates itself. */
4161da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
4171da177e4SLinus Torvalds next_inode:
4181da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
4191da177e4SLinus Torvalds 		struct inode_security_struct *isec =
4201da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
4211da177e4SLinus Torvalds 					   struct inode_security_struct, list);
4221da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
4231da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
4241da177e4SLinus Torvalds 		inode = igrab(inode);
4251da177e4SLinus Torvalds 		if (inode) {
4261da177e4SLinus Torvalds 			if (!IS_PRIVATE(inode))
4271da177e4SLinus Torvalds 				inode_doinit(inode);
4281da177e4SLinus Torvalds 			iput(inode);
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
4311da177e4SLinus Torvalds 		list_del_init(&isec->list);
4321da177e4SLinus Torvalds 		goto next_inode;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
4351da177e4SLinus Torvalds out:
436c9180a57SEric Paris 	return rc;
437c9180a57SEric Paris }
438c9180a57SEric Paris 
439c9180a57SEric Paris /*
440c9180a57SEric Paris  * This function should allow an FS to ask what it's mount security
441c9180a57SEric Paris  * options were so it can use those later for submounts, displaying
442c9180a57SEric Paris  * mount options, or whatever.
443c9180a57SEric Paris  */
444c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb,
445e0007529SEric Paris 				struct security_mnt_opts *opts)
446c9180a57SEric Paris {
447c9180a57SEric Paris 	int rc = 0, i;
448c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
449c9180a57SEric Paris 	char *context = NULL;
450c9180a57SEric Paris 	u32 len;
451c9180a57SEric Paris 	char tmp;
452c9180a57SEric Paris 
453e0007529SEric Paris 	security_init_mnt_opts(opts);
454c9180a57SEric Paris 
455c9180a57SEric Paris 	if (!sbsec->initialized)
456c9180a57SEric Paris 		return -EINVAL;
457c9180a57SEric Paris 
458c9180a57SEric Paris 	if (!ss_initialized)
459c9180a57SEric Paris 		return -EINVAL;
460c9180a57SEric Paris 
461c9180a57SEric Paris 	/*
462c9180a57SEric Paris 	 * if we ever use sbsec flags for anything other than tracking mount
463c9180a57SEric Paris 	 * settings this is going to need a mask
464c9180a57SEric Paris 	 */
465c9180a57SEric Paris 	tmp = sbsec->flags;
466c9180a57SEric Paris 	/* count the number of mount options for this sb */
467c9180a57SEric Paris 	for (i = 0; i < 8; i++) {
468c9180a57SEric Paris 		if (tmp & 0x01)
469e0007529SEric Paris 			opts->num_mnt_opts++;
470c9180a57SEric Paris 		tmp >>= 1;
471c9180a57SEric Paris 	}
472c9180a57SEric Paris 
473e0007529SEric Paris 	opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
474e0007529SEric Paris 	if (!opts->mnt_opts) {
475c9180a57SEric Paris 		rc = -ENOMEM;
476c9180a57SEric Paris 		goto out_free;
477c9180a57SEric Paris 	}
478c9180a57SEric Paris 
479e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
480e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
481c9180a57SEric Paris 		rc = -ENOMEM;
482c9180a57SEric Paris 		goto out_free;
483c9180a57SEric Paris 	}
484c9180a57SEric Paris 
485c9180a57SEric Paris 	i = 0;
486c9180a57SEric Paris 	if (sbsec->flags & FSCONTEXT_MNT) {
487c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->sid, &context, &len);
488c9180a57SEric Paris 		if (rc)
489c9180a57SEric Paris 			goto out_free;
490e0007529SEric Paris 		opts->mnt_opts[i] = context;
491e0007529SEric Paris 		opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
492c9180a57SEric Paris 	}
493c9180a57SEric Paris 	if (sbsec->flags & CONTEXT_MNT) {
494c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
495c9180a57SEric Paris 		if (rc)
496c9180a57SEric Paris 			goto out_free;
497e0007529SEric Paris 		opts->mnt_opts[i] = context;
498e0007529SEric Paris 		opts->mnt_opts_flags[i++] = CONTEXT_MNT;
499c9180a57SEric Paris 	}
500c9180a57SEric Paris 	if (sbsec->flags & DEFCONTEXT_MNT) {
501c9180a57SEric Paris 		rc = security_sid_to_context(sbsec->def_sid, &context, &len);
502c9180a57SEric Paris 		if (rc)
503c9180a57SEric Paris 			goto out_free;
504e0007529SEric Paris 		opts->mnt_opts[i] = context;
505e0007529SEric Paris 		opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
506c9180a57SEric Paris 	}
507c9180a57SEric Paris 	if (sbsec->flags & ROOTCONTEXT_MNT) {
508c9180a57SEric Paris 		struct inode *root = sbsec->sb->s_root->d_inode;
509c9180a57SEric Paris 		struct inode_security_struct *isec = root->i_security;
510c9180a57SEric Paris 
511c9180a57SEric Paris 		rc = security_sid_to_context(isec->sid, &context, &len);
512c9180a57SEric Paris 		if (rc)
513c9180a57SEric Paris 			goto out_free;
514e0007529SEric Paris 		opts->mnt_opts[i] = context;
515e0007529SEric Paris 		opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
516c9180a57SEric Paris 	}
517c9180a57SEric Paris 
518e0007529SEric Paris 	BUG_ON(i != opts->num_mnt_opts);
519c9180a57SEric Paris 
520c9180a57SEric Paris 	return 0;
521c9180a57SEric Paris 
522c9180a57SEric Paris out_free:
523e0007529SEric Paris 	security_free_mnt_opts(opts);
524c9180a57SEric Paris 	return rc;
525c9180a57SEric Paris }
526c9180a57SEric Paris 
527c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag,
528c9180a57SEric Paris 		      u32 old_sid, u32 new_sid)
529c9180a57SEric Paris {
530c9180a57SEric Paris 	/* check if the old mount command had the same options */
531c9180a57SEric Paris 	if (sbsec->initialized)
532c9180a57SEric Paris 		if (!(sbsec->flags & flag) ||
533c9180a57SEric Paris 		    (old_sid != new_sid))
534c9180a57SEric Paris 			return 1;
535c9180a57SEric Paris 
536c9180a57SEric Paris 	/* check if we were passed the same options twice,
537c9180a57SEric Paris 	 * aka someone passed context=a,context=b
538c9180a57SEric Paris 	 */
539c9180a57SEric Paris 	if (!sbsec->initialized)
540c9180a57SEric Paris 		if (sbsec->flags & flag)
541c9180a57SEric Paris 			return 1;
542c9180a57SEric Paris 	return 0;
543c9180a57SEric Paris }
544e0007529SEric Paris 
545c9180a57SEric Paris /*
546c9180a57SEric Paris  * Allow filesystems with binary mount data to explicitly set mount point
547c9180a57SEric Paris  * labeling information.
548c9180a57SEric Paris  */
549e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb,
550e0007529SEric Paris 				struct security_mnt_opts *opts)
551c9180a57SEric Paris {
552c9180a57SEric Paris 	int rc = 0, i;
553c9180a57SEric Paris 	struct task_security_struct *tsec = current->security;
554c9180a57SEric Paris 	struct superblock_security_struct *sbsec = sb->s_security;
555c9180a57SEric Paris 	const char *name = sb->s_type->name;
556c9180a57SEric Paris 	struct inode *inode = sbsec->sb->s_root->d_inode;
557c9180a57SEric Paris 	struct inode_security_struct *root_isec = inode->i_security;
558c9180a57SEric Paris 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
559c9180a57SEric Paris 	u32 defcontext_sid = 0;
560e0007529SEric Paris 	char **mount_options = opts->mnt_opts;
561e0007529SEric Paris 	int *flags = opts->mnt_opts_flags;
562e0007529SEric Paris 	int num_opts = opts->num_mnt_opts;
563c9180a57SEric Paris 
564c9180a57SEric Paris 	mutex_lock(&sbsec->lock);
565c9180a57SEric Paris 
566c9180a57SEric Paris 	if (!ss_initialized) {
567c9180a57SEric Paris 		if (!num_opts) {
568c9180a57SEric Paris 			/* Defer initialization until selinux_complete_init,
569c9180a57SEric Paris 			   after the initial policy is loaded and the security
570c9180a57SEric Paris 			   server is ready to handle calls. */
571c9180a57SEric Paris 			spin_lock(&sb_security_lock);
572c9180a57SEric Paris 			if (list_empty(&sbsec->list))
573c9180a57SEric Paris 				list_add(&sbsec->list, &superblock_security_head);
574c9180a57SEric Paris 			spin_unlock(&sb_security_lock);
575c9180a57SEric Paris 			goto out;
576c9180a57SEric Paris 		}
577c9180a57SEric Paris 		rc = -EINVAL;
578*744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: Unable to set superblock options "
579*744ba35eSEric Paris 			"before the security server is initialized\n");
580c9180a57SEric Paris 		goto out;
581c9180a57SEric Paris 	}
582c9180a57SEric Paris 
583c9180a57SEric Paris 	/*
584e0007529SEric Paris 	 * Binary mount data FS will come through this function twice.  Once
585e0007529SEric Paris 	 * from an explicit call and once from the generic calls from the vfs.
586e0007529SEric Paris 	 * Since the generic VFS calls will not contain any security mount data
587e0007529SEric Paris 	 * we need to skip the double mount verification.
588e0007529SEric Paris 	 *
589e0007529SEric Paris 	 * This does open a hole in which we will not notice if the first
590e0007529SEric Paris 	 * mount using this sb set explict options and a second mount using
591e0007529SEric Paris 	 * this sb does not set any security options.  (The first options
592e0007529SEric Paris 	 * will be used for both mounts)
593e0007529SEric Paris 	 */
594e0007529SEric Paris 	if (sbsec->initialized && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
595e0007529SEric Paris 	    && (num_opts == 0))
596e0007529SEric Paris 	        goto out;
597e0007529SEric Paris 
598e0007529SEric Paris 	/*
599c9180a57SEric Paris 	 * parse the mount options, check if they are valid sids.
600c9180a57SEric Paris 	 * also check if someone is trying to mount the same sb more
601c9180a57SEric Paris 	 * than once with different security options.
602c9180a57SEric Paris 	 */
603c9180a57SEric Paris 	for (i = 0; i < num_opts; i++) {
604c9180a57SEric Paris 		u32 sid;
605c9180a57SEric Paris 		rc = security_context_to_sid(mount_options[i],
606c9180a57SEric Paris 					     strlen(mount_options[i]), &sid);
607c9180a57SEric Paris 		if (rc) {
608c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
609c9180a57SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
610c9180a57SEric Paris 			       mount_options[i], sb->s_id, name, rc);
611c9180a57SEric Paris 			goto out;
612c9180a57SEric Paris 		}
613c9180a57SEric Paris 		switch (flags[i]) {
614c9180a57SEric Paris 		case FSCONTEXT_MNT:
615c9180a57SEric Paris 			fscontext_sid = sid;
616c9180a57SEric Paris 
617c9180a57SEric Paris 			if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
618c9180a57SEric Paris 					fscontext_sid))
619c9180a57SEric Paris 				goto out_double_mount;
620c9180a57SEric Paris 
621c9180a57SEric Paris 			sbsec->flags |= FSCONTEXT_MNT;
622c9180a57SEric Paris 			break;
623c9180a57SEric Paris 		case CONTEXT_MNT:
624c9180a57SEric Paris 			context_sid = sid;
625c9180a57SEric Paris 
626c9180a57SEric Paris 			if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
627c9180a57SEric Paris 					context_sid))
628c9180a57SEric Paris 				goto out_double_mount;
629c9180a57SEric Paris 
630c9180a57SEric Paris 			sbsec->flags |= CONTEXT_MNT;
631c9180a57SEric Paris 			break;
632c9180a57SEric Paris 		case ROOTCONTEXT_MNT:
633c9180a57SEric Paris 			rootcontext_sid = sid;
634c9180a57SEric Paris 
635c9180a57SEric Paris 			if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
636c9180a57SEric Paris 					rootcontext_sid))
637c9180a57SEric Paris 				goto out_double_mount;
638c9180a57SEric Paris 
639c9180a57SEric Paris 			sbsec->flags |= ROOTCONTEXT_MNT;
640c9180a57SEric Paris 
641c9180a57SEric Paris 			break;
642c9180a57SEric Paris 		case DEFCONTEXT_MNT:
643c9180a57SEric Paris 			defcontext_sid = sid;
644c9180a57SEric Paris 
645c9180a57SEric Paris 			if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
646c9180a57SEric Paris 					defcontext_sid))
647c9180a57SEric Paris 				goto out_double_mount;
648c9180a57SEric Paris 
649c9180a57SEric Paris 			sbsec->flags |= DEFCONTEXT_MNT;
650c9180a57SEric Paris 
651c9180a57SEric Paris 			break;
652c9180a57SEric Paris 		default:
653c9180a57SEric Paris 			rc = -EINVAL;
654c9180a57SEric Paris 			goto out;
655c9180a57SEric Paris 		}
656c9180a57SEric Paris 	}
657c9180a57SEric Paris 
658c9180a57SEric Paris 	if (sbsec->initialized) {
659c9180a57SEric Paris 		/* previously mounted with options, but not on this attempt? */
660c9180a57SEric Paris 		if (sbsec->flags && !num_opts)
661c9180a57SEric Paris 			goto out_double_mount;
662c9180a57SEric Paris 		rc = 0;
663c9180a57SEric Paris 		goto out;
664c9180a57SEric Paris 	}
665c9180a57SEric Paris 
666c9180a57SEric Paris 	if (strcmp(sb->s_type->name, "proc") == 0)
667c9180a57SEric Paris 		sbsec->proc = 1;
668c9180a57SEric Paris 
669c9180a57SEric Paris 	/* Determine the labeling behavior to use for this filesystem type. */
670c9180a57SEric Paris 	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
671c9180a57SEric Paris 	if (rc) {
672c9180a57SEric Paris 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
673dd6f953aSHarvey Harrison 		       __func__, sb->s_type->name, rc);
674c9180a57SEric Paris 		goto out;
675c9180a57SEric Paris 	}
676c9180a57SEric Paris 
677c9180a57SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
678c9180a57SEric Paris 	if (fscontext_sid) {
679c9180a57SEric Paris 
680c9180a57SEric Paris 		rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, tsec);
681c9180a57SEric Paris 		if (rc)
682c9180a57SEric Paris 			goto out;
683c9180a57SEric Paris 
684c9180a57SEric Paris 		sbsec->sid = fscontext_sid;
685c9180a57SEric Paris 	}
686c9180a57SEric Paris 
687c9180a57SEric Paris 	/*
688c9180a57SEric Paris 	 * Switch to using mount point labeling behavior.
689c9180a57SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
690c9180a57SEric Paris 	 * the superblock context if not already set.
691c9180a57SEric Paris 	 */
692c9180a57SEric Paris 	if (context_sid) {
693c9180a57SEric Paris 		if (!fscontext_sid) {
694c9180a57SEric Paris 			rc = may_context_mount_sb_relabel(context_sid, sbsec, tsec);
695c9180a57SEric Paris 			if (rc)
696c9180a57SEric Paris 				goto out;
697c9180a57SEric Paris 			sbsec->sid = context_sid;
698c9180a57SEric Paris 		} else {
699c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(context_sid, sbsec, tsec);
700c9180a57SEric Paris 			if (rc)
701c9180a57SEric Paris 				goto out;
702c9180a57SEric Paris 		}
703c9180a57SEric Paris 		if (!rootcontext_sid)
704c9180a57SEric Paris 			rootcontext_sid = context_sid;
705c9180a57SEric Paris 
706c9180a57SEric Paris 		sbsec->mntpoint_sid = context_sid;
707c9180a57SEric Paris 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
708c9180a57SEric Paris 	}
709c9180a57SEric Paris 
710c9180a57SEric Paris 	if (rootcontext_sid) {
711c9180a57SEric Paris 		rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, tsec);
712c9180a57SEric Paris 		if (rc)
713c9180a57SEric Paris 			goto out;
714c9180a57SEric Paris 
715c9180a57SEric Paris 		root_isec->sid = rootcontext_sid;
716c9180a57SEric Paris 		root_isec->initialized = 1;
717c9180a57SEric Paris 	}
718c9180a57SEric Paris 
719c9180a57SEric Paris 	if (defcontext_sid) {
720c9180a57SEric Paris 		if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
721c9180a57SEric Paris 			rc = -EINVAL;
722c9180a57SEric Paris 			printk(KERN_WARNING "SELinux: defcontext option is "
723c9180a57SEric Paris 			       "invalid for this filesystem type\n");
724c9180a57SEric Paris 			goto out;
725c9180a57SEric Paris 		}
726c9180a57SEric Paris 
727c9180a57SEric Paris 		if (defcontext_sid != sbsec->def_sid) {
728c9180a57SEric Paris 			rc = may_context_mount_inode_relabel(defcontext_sid,
729c9180a57SEric Paris 							     sbsec, tsec);
730c9180a57SEric Paris 			if (rc)
731c9180a57SEric Paris 				goto out;
732c9180a57SEric Paris 		}
733c9180a57SEric Paris 
734c9180a57SEric Paris 		sbsec->def_sid = defcontext_sid;
735c9180a57SEric Paris 	}
736c9180a57SEric Paris 
737c9180a57SEric Paris 	rc = sb_finish_set_opts(sb);
738c9180a57SEric Paris out:
739bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
7401da177e4SLinus Torvalds 	return rc;
741c9180a57SEric Paris out_double_mount:
742c9180a57SEric Paris 	rc = -EINVAL;
743c9180a57SEric Paris 	printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
744c9180a57SEric Paris 	       "security settings for (dev %s, type %s)\n", sb->s_id, name);
745c9180a57SEric Paris 	goto out;
746c9180a57SEric Paris }
747c9180a57SEric Paris 
748c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
749c9180a57SEric Paris 					struct super_block *newsb)
750c9180a57SEric Paris {
751c9180a57SEric Paris 	const struct superblock_security_struct *oldsbsec = oldsb->s_security;
752c9180a57SEric Paris 	struct superblock_security_struct *newsbsec = newsb->s_security;
753c9180a57SEric Paris 
754c9180a57SEric Paris 	int set_fscontext =	(oldsbsec->flags & FSCONTEXT_MNT);
755c9180a57SEric Paris 	int set_context =	(oldsbsec->flags & CONTEXT_MNT);
756c9180a57SEric Paris 	int set_rootcontext =	(oldsbsec->flags & ROOTCONTEXT_MNT);
757c9180a57SEric Paris 
758c9180a57SEric Paris 	/* we can't error, we can't save the info, this shouldn't get called
759c9180a57SEric Paris 	 * this early in the boot process. */
760c9180a57SEric Paris 	BUG_ON(!ss_initialized);
761c9180a57SEric Paris 
762c9180a57SEric Paris 	/* how can we clone if the old one wasn't set up?? */
763c9180a57SEric Paris 	BUG_ON(!oldsbsec->initialized);
764c9180a57SEric Paris 
7655a552617SEric Paris 	/* if fs is reusing a sb, just let its options stand... */
7665a552617SEric Paris 	if (newsbsec->initialized)
7675a552617SEric Paris 		return;
7685a552617SEric Paris 
769c9180a57SEric Paris 	mutex_lock(&newsbsec->lock);
770c9180a57SEric Paris 
771c9180a57SEric Paris 	newsbsec->flags = oldsbsec->flags;
772c9180a57SEric Paris 
773c9180a57SEric Paris 	newsbsec->sid = oldsbsec->sid;
774c9180a57SEric Paris 	newsbsec->def_sid = oldsbsec->def_sid;
775c9180a57SEric Paris 	newsbsec->behavior = oldsbsec->behavior;
776c9180a57SEric Paris 
777c9180a57SEric Paris 	if (set_context) {
778c9180a57SEric Paris 		u32 sid = oldsbsec->mntpoint_sid;
779c9180a57SEric Paris 
780c9180a57SEric Paris 		if (!set_fscontext)
781c9180a57SEric Paris 			newsbsec->sid = sid;
782c9180a57SEric Paris 		if (!set_rootcontext) {
783c9180a57SEric Paris 			struct inode *newinode = newsb->s_root->d_inode;
784c9180a57SEric Paris 			struct inode_security_struct *newisec = newinode->i_security;
785c9180a57SEric Paris 			newisec->sid = sid;
786c9180a57SEric Paris 		}
787c9180a57SEric Paris 		newsbsec->mntpoint_sid = sid;
788c9180a57SEric Paris 	}
789c9180a57SEric Paris 	if (set_rootcontext) {
790c9180a57SEric Paris 		const struct inode *oldinode = oldsb->s_root->d_inode;
791c9180a57SEric Paris 		const struct inode_security_struct *oldisec = oldinode->i_security;
792c9180a57SEric Paris 		struct inode *newinode = newsb->s_root->d_inode;
793c9180a57SEric Paris 		struct inode_security_struct *newisec = newinode->i_security;
794c9180a57SEric Paris 
795c9180a57SEric Paris 		newisec->sid = oldisec->sid;
796c9180a57SEric Paris 	}
797c9180a57SEric Paris 
798c9180a57SEric Paris 	sb_finish_set_opts(newsb);
799c9180a57SEric Paris 	mutex_unlock(&newsbsec->lock);
800c9180a57SEric Paris }
801c9180a57SEric Paris 
8022e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options,
8032e1479d9SAdrian Bunk 				  struct security_mnt_opts *opts)
804c9180a57SEric Paris {
805e0007529SEric Paris 	char *p;
806c9180a57SEric Paris 	char *context = NULL, *defcontext = NULL;
807c9180a57SEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
808e0007529SEric Paris 	int rc, num_mnt_opts = 0;
809c9180a57SEric Paris 
810e0007529SEric Paris 	opts->num_mnt_opts = 0;
811c9180a57SEric Paris 
812c9180a57SEric Paris 	/* Standard string-based options. */
813c9180a57SEric Paris 	while ((p = strsep(&options, "|")) != NULL) {
814c9180a57SEric Paris 		int token;
815c9180a57SEric Paris 		substring_t args[MAX_OPT_ARGS];
816c9180a57SEric Paris 
817c9180a57SEric Paris 		if (!*p)
818c9180a57SEric Paris 			continue;
819c9180a57SEric Paris 
820c9180a57SEric Paris 		token = match_token(p, tokens, args);
821c9180a57SEric Paris 
822c9180a57SEric Paris 		switch (token) {
823c9180a57SEric Paris 		case Opt_context:
824c9180a57SEric Paris 			if (context || defcontext) {
825c9180a57SEric Paris 				rc = -EINVAL;
826c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
827c9180a57SEric Paris 				goto out_err;
828c9180a57SEric Paris 			}
829c9180a57SEric Paris 			context = match_strdup(&args[0]);
830c9180a57SEric Paris 			if (!context) {
831c9180a57SEric Paris 				rc = -ENOMEM;
832c9180a57SEric Paris 				goto out_err;
833c9180a57SEric Paris 			}
834c9180a57SEric Paris 			break;
835c9180a57SEric Paris 
836c9180a57SEric Paris 		case Opt_fscontext:
837c9180a57SEric Paris 			if (fscontext) {
838c9180a57SEric Paris 				rc = -EINVAL;
839c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
840c9180a57SEric Paris 				goto out_err;
841c9180a57SEric Paris 			}
842c9180a57SEric Paris 			fscontext = match_strdup(&args[0]);
843c9180a57SEric Paris 			if (!fscontext) {
844c9180a57SEric Paris 				rc = -ENOMEM;
845c9180a57SEric Paris 				goto out_err;
846c9180a57SEric Paris 			}
847c9180a57SEric Paris 			break;
848c9180a57SEric Paris 
849c9180a57SEric Paris 		case Opt_rootcontext:
850c9180a57SEric Paris 			if (rootcontext) {
851c9180a57SEric Paris 				rc = -EINVAL;
852c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
853c9180a57SEric Paris 				goto out_err;
854c9180a57SEric Paris 			}
855c9180a57SEric Paris 			rootcontext = match_strdup(&args[0]);
856c9180a57SEric Paris 			if (!rootcontext) {
857c9180a57SEric Paris 				rc = -ENOMEM;
858c9180a57SEric Paris 				goto out_err;
859c9180a57SEric Paris 			}
860c9180a57SEric Paris 			break;
861c9180a57SEric Paris 
862c9180a57SEric Paris 		case Opt_defcontext:
863c9180a57SEric Paris 			if (context || defcontext) {
864c9180a57SEric Paris 				rc = -EINVAL;
865c9180a57SEric Paris 				printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
866c9180a57SEric Paris 				goto out_err;
867c9180a57SEric Paris 			}
868c9180a57SEric Paris 			defcontext = match_strdup(&args[0]);
869c9180a57SEric Paris 			if (!defcontext) {
870c9180a57SEric Paris 				rc = -ENOMEM;
871c9180a57SEric Paris 				goto out_err;
872c9180a57SEric Paris 			}
873c9180a57SEric Paris 			break;
874c9180a57SEric Paris 
875c9180a57SEric Paris 		default:
876c9180a57SEric Paris 			rc = -EINVAL;
877c9180a57SEric Paris 			printk(KERN_WARNING "SELinux:  unknown mount option\n");
878c9180a57SEric Paris 			goto out_err;
879c9180a57SEric Paris 
880c9180a57SEric Paris 		}
881c9180a57SEric Paris 	}
882c9180a57SEric Paris 
883e0007529SEric Paris 	rc = -ENOMEM;
884e0007529SEric Paris 	opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
885e0007529SEric Paris 	if (!opts->mnt_opts)
886e0007529SEric Paris 		goto out_err;
887e0007529SEric Paris 
888e0007529SEric Paris 	opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
889e0007529SEric Paris 	if (!opts->mnt_opts_flags) {
890e0007529SEric Paris 		kfree(opts->mnt_opts);
891e0007529SEric Paris 		goto out_err;
892c9180a57SEric Paris 	}
893c9180a57SEric Paris 
894e0007529SEric Paris 	if (fscontext) {
895e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = fscontext;
896e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
897e0007529SEric Paris 	}
898e0007529SEric Paris 	if (context) {
899e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = context;
900e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
901e0007529SEric Paris 	}
902e0007529SEric Paris 	if (rootcontext) {
903e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = rootcontext;
904e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
905e0007529SEric Paris 	}
906e0007529SEric Paris 	if (defcontext) {
907e0007529SEric Paris 		opts->mnt_opts[num_mnt_opts] = defcontext;
908e0007529SEric Paris 		opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
909e0007529SEric Paris 	}
910e0007529SEric Paris 
911e0007529SEric Paris 	opts->num_mnt_opts = num_mnt_opts;
912e0007529SEric Paris 	return 0;
913e0007529SEric Paris 
914c9180a57SEric Paris out_err:
915c9180a57SEric Paris 	kfree(context);
916c9180a57SEric Paris 	kfree(defcontext);
917c9180a57SEric Paris 	kfree(fscontext);
918c9180a57SEric Paris 	kfree(rootcontext);
919c9180a57SEric Paris 	return rc;
9201da177e4SLinus Torvalds }
921e0007529SEric Paris /*
922e0007529SEric Paris  * string mount options parsing and call set the sbsec
923e0007529SEric Paris  */
924e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data)
925e0007529SEric Paris {
926e0007529SEric Paris 	int rc = 0;
927e0007529SEric Paris 	char *options = data;
928e0007529SEric Paris 	struct security_mnt_opts opts;
929e0007529SEric Paris 
930e0007529SEric Paris 	security_init_mnt_opts(&opts);
931e0007529SEric Paris 
932e0007529SEric Paris 	if (!data)
933e0007529SEric Paris 		goto out;
934e0007529SEric Paris 
935e0007529SEric Paris 	BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
936e0007529SEric Paris 
937e0007529SEric Paris 	rc = selinux_parse_opts_str(options, &opts);
938e0007529SEric Paris 	if (rc)
939e0007529SEric Paris 		goto out_err;
940e0007529SEric Paris 
941e0007529SEric Paris out:
942e0007529SEric Paris 	rc = selinux_set_mnt_opts(sb, &opts);
943e0007529SEric Paris 
944e0007529SEric Paris out_err:
945e0007529SEric Paris 	security_free_mnt_opts(&opts);
946e0007529SEric Paris 	return rc;
947e0007529SEric Paris }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
9521da177e4SLinus Torvalds 	case S_IFSOCK:
9531da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
9541da177e4SLinus Torvalds 	case S_IFLNK:
9551da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
9561da177e4SLinus Torvalds 	case S_IFREG:
9571da177e4SLinus Torvalds 		return SECCLASS_FILE;
9581da177e4SLinus Torvalds 	case S_IFBLK:
9591da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
9601da177e4SLinus Torvalds 	case S_IFDIR:
9611da177e4SLinus Torvalds 		return SECCLASS_DIR;
9621da177e4SLinus Torvalds 	case S_IFCHR:
9631da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
9641da177e4SLinus Torvalds 	case S_IFIFO:
9651da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	return SECCLASS_FILE;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
97213402580SJames Morris static inline int default_protocol_stream(int protocol)
97313402580SJames Morris {
97413402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
97513402580SJames Morris }
97613402580SJames Morris 
97713402580SJames Morris static inline int default_protocol_dgram(int protocol)
97813402580SJames Morris {
97913402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
98013402580SJames Morris }
98113402580SJames Morris 
9821da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	switch (family) {
9851da177e4SLinus Torvalds 	case PF_UNIX:
9861da177e4SLinus Torvalds 		switch (type) {
9871da177e4SLinus Torvalds 		case SOCK_STREAM:
9881da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
9891da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
9901da177e4SLinus Torvalds 		case SOCK_DGRAM:
9911da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
9921da177e4SLinus Torvalds 		}
9931da177e4SLinus Torvalds 		break;
9941da177e4SLinus Torvalds 	case PF_INET:
9951da177e4SLinus Torvalds 	case PF_INET6:
9961da177e4SLinus Torvalds 		switch (type) {
9971da177e4SLinus Torvalds 		case SOCK_STREAM:
99813402580SJames Morris 			if (default_protocol_stream(protocol))
9991da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
100013402580SJames Morris 			else
100113402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
10021da177e4SLinus Torvalds 		case SOCK_DGRAM:
100313402580SJames Morris 			if (default_protocol_dgram(protocol))
10041da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
100513402580SJames Morris 			else
100613402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
10072ee92d46SJames Morris 		case SOCK_DCCP:
10082ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
100913402580SJames Morris 		default:
10101da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
10111da177e4SLinus Torvalds 		}
10121da177e4SLinus Torvalds 		break;
10131da177e4SLinus Torvalds 	case PF_NETLINK:
10141da177e4SLinus Torvalds 		switch (protocol) {
10151da177e4SLinus Torvalds 		case NETLINK_ROUTE:
10161da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
10171da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
10181da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
1019216efaaaSJames Morris 		case NETLINK_INET_DIAG:
10201da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
10211da177e4SLinus Torvalds 		case NETLINK_NFLOG:
10221da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
10231da177e4SLinus Torvalds 		case NETLINK_XFRM:
10241da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
10251da177e4SLinus Torvalds 		case NETLINK_SELINUX:
10261da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
10271da177e4SLinus Torvalds 		case NETLINK_AUDIT:
10281da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
10291da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
10301da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
10311da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
10321da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
10330c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
10340c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
10351da177e4SLinus Torvalds 		default:
10361da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
10371da177e4SLinus Torvalds 		}
10381da177e4SLinus Torvalds 	case PF_PACKET:
10391da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
10401da177e4SLinus Torvalds 	case PF_KEY:
10411da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
10423e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
10433e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10501da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
10511da177e4SLinus Torvalds 				u16 tclass,
10521da177e4SLinus Torvalds 				u32 *sid)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	int buflen, rc;
10551da177e4SLinus Torvalds 	char *buffer, *path, *end;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	buffer = (char*)__get_free_page(GFP_KERNEL);
10581da177e4SLinus Torvalds 	if (!buffer)
10591da177e4SLinus Torvalds 		return -ENOMEM;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	buflen = PAGE_SIZE;
10621da177e4SLinus Torvalds 	end = buffer+buflen;
10631da177e4SLinus Torvalds 	*--end = '\0';
10641da177e4SLinus Torvalds 	buflen--;
10651da177e4SLinus Torvalds 	path = end-1;
10661da177e4SLinus Torvalds 	*path = '/';
10671da177e4SLinus Torvalds 	while (de && de != de->parent) {
10681da177e4SLinus Torvalds 		buflen -= de->namelen + 1;
10691da177e4SLinus Torvalds 		if (buflen < 0)
10701da177e4SLinus Torvalds 			break;
10711da177e4SLinus Torvalds 		end -= de->namelen;
10721da177e4SLinus Torvalds 		memcpy(end, de->name, de->namelen);
10731da177e4SLinus Torvalds 		*--end = '/';
10741da177e4SLinus Torvalds 		path = end;
10751da177e4SLinus Torvalds 		de = de->parent;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 	rc = security_genfs_sid("proc", path, tclass, sid);
10781da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
10791da177e4SLinus Torvalds 	return rc;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds #else
10821da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
10831da177e4SLinus Torvalds 				u16 tclass,
10841da177e4SLinus Torvalds 				u32 *sid)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	return -EINVAL;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds #endif
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
10911da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
10941da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
10951da177e4SLinus Torvalds 	u32 sid;
10961da177e4SLinus Torvalds 	struct dentry *dentry;
10971da177e4SLinus Torvalds #define INITCONTEXTLEN 255
10981da177e4SLinus Torvalds 	char *context = NULL;
10991da177e4SLinus Torvalds 	unsigned len = 0;
11001da177e4SLinus Torvalds 	int rc = 0;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	if (isec->initialized)
11031da177e4SLinus Torvalds 		goto out;
11041da177e4SLinus Torvalds 
110523970741SEric Paris 	mutex_lock(&isec->lock);
11061da177e4SLinus Torvalds 	if (isec->initialized)
110723970741SEric Paris 		goto out_unlock;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
11101da177e4SLinus Torvalds 	if (!sbsec->initialized) {
11111da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
11121da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
11131da177e4SLinus Torvalds 		   server is ready to handle calls. */
11141da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
11151da177e4SLinus Torvalds 		if (list_empty(&isec->list))
11161da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
11171da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
111823970741SEric Paris 		goto out_unlock;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	switch (sbsec->behavior) {
11221da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
11231da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
11241da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
11251da177e4SLinus Torvalds 			break;
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
11291da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
11301da177e4SLinus Torvalds 		if (opt_dentry) {
11311da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
11321da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
11331da177e4SLinus Torvalds 		} else {
11341da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
11351da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 		if (!dentry) {
1138*744ba35eSEric Paris 			printk(KERN_WARNING "SELinux: %s:  no dentry for dev=%s "
1139dd6f953aSHarvey Harrison 			       "ino=%ld\n", __func__, inode->i_sb->s_id,
11401da177e4SLinus Torvalds 			       inode->i_ino);
114123970741SEric Paris 			goto out_unlock;
11421da177e4SLinus Torvalds 		}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
1145869ab514SStephen Smalley 		context = kmalloc(len, GFP_NOFS);
11461da177e4SLinus Torvalds 		if (!context) {
11471da177e4SLinus Torvalds 			rc = -ENOMEM;
11481da177e4SLinus Torvalds 			dput(dentry);
114923970741SEric Paris 			goto out_unlock;
11501da177e4SLinus Torvalds 		}
11511da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
11521da177e4SLinus Torvalds 					   context, len);
11531da177e4SLinus Torvalds 		if (rc == -ERANGE) {
11541da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
11551da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
11561da177e4SLinus Torvalds 						   NULL, 0);
11571da177e4SLinus Torvalds 			if (rc < 0) {
11581da177e4SLinus Torvalds 				dput(dentry);
115923970741SEric Paris 				goto out_unlock;
11601da177e4SLinus Torvalds 			}
11611da177e4SLinus Torvalds 			kfree(context);
11621da177e4SLinus Torvalds 			len = rc;
1163869ab514SStephen Smalley 			context = kmalloc(len, GFP_NOFS);
11641da177e4SLinus Torvalds 			if (!context) {
11651da177e4SLinus Torvalds 				rc = -ENOMEM;
11661da177e4SLinus Torvalds 				dput(dentry);
116723970741SEric Paris 				goto out_unlock;
11681da177e4SLinus Torvalds 			}
11691da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
11701da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
11711da177e4SLinus Torvalds 						   context, len);
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 		dput(dentry);
11741da177e4SLinus Torvalds 		if (rc < 0) {
11751da177e4SLinus Torvalds 			if (rc != -ENODATA) {
1176*744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  getxattr returned "
1177dd6f953aSHarvey Harrison 				       "%d for dev=%s ino=%ld\n", __func__,
11781da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
11791da177e4SLinus Torvalds 				kfree(context);
118023970741SEric Paris 				goto out_unlock;
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
11831da177e4SLinus Torvalds 			sid = sbsec->def_sid;
11841da177e4SLinus Torvalds 			rc = 0;
11851da177e4SLinus Torvalds 		} else {
1186f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
1187869ab514SStephen Smalley 							     sbsec->def_sid,
1188869ab514SStephen Smalley 							     GFP_NOFS);
11891da177e4SLinus Torvalds 			if (rc) {
1190*744ba35eSEric Paris 				printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
11911da177e4SLinus Torvalds 				       "returned %d for dev=%s ino=%ld\n",
1192dd6f953aSHarvey Harrison 				       __func__, context, -rc,
11931da177e4SLinus Torvalds 				       inode->i_sb->s_id, inode->i_ino);
11941da177e4SLinus Torvalds 				kfree(context);
11951da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
11961da177e4SLinus Torvalds 				rc = 0;
11971da177e4SLinus Torvalds 				break;
11981da177e4SLinus Torvalds 			}
11991da177e4SLinus Torvalds 		}
12001da177e4SLinus Torvalds 		kfree(context);
12011da177e4SLinus Torvalds 		isec->sid = sid;
12021da177e4SLinus Torvalds 		break;
12031da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
12041da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
12051da177e4SLinus Torvalds 		break;
12061da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
12071da177e4SLinus Torvalds 		/* Default to the fs SID. */
12081da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
12111da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
12121da177e4SLinus Torvalds 		rc = security_transition_sid(isec->task_sid,
12131da177e4SLinus Torvalds 					     sbsec->sid,
12141da177e4SLinus Torvalds 					     isec->sclass,
12151da177e4SLinus Torvalds 					     &sid);
12161da177e4SLinus Torvalds 		if (rc)
121723970741SEric Paris 			goto out_unlock;
12181da177e4SLinus Torvalds 		isec->sid = sid;
12191da177e4SLinus Torvalds 		break;
1220c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
1221c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
1222c312feb2SEric Paris 		break;
12231da177e4SLinus Torvalds 	default:
1224c312feb2SEric Paris 		/* Default to the fs superblock SID. */
12251da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 		if (sbsec->proc) {
12281da177e4SLinus Torvalds 			struct proc_inode *proci = PROC_I(inode);
12291da177e4SLinus Torvalds 			if (proci->pde) {
12301da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
12311da177e4SLinus Torvalds 				rc = selinux_proc_get_sid(proci->pde,
12321da177e4SLinus Torvalds 							  isec->sclass,
12331da177e4SLinus Torvalds 							  &sid);
12341da177e4SLinus Torvalds 				if (rc)
123523970741SEric Paris 					goto out_unlock;
12361da177e4SLinus Torvalds 				isec->sid = sid;
12371da177e4SLinus Torvalds 			}
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 		break;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	isec->initialized = 1;
12431da177e4SLinus Torvalds 
124423970741SEric Paris out_unlock:
124523970741SEric Paris 	mutex_unlock(&isec->lock);
12461da177e4SLinus Torvalds out:
12471da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
12481da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
12491da177e4SLinus Torvalds 	return rc;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
12531da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
12541da177e4SLinus Torvalds {
12551da177e4SLinus Torvalds 	u32 perm = 0;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	switch (sig) {
12581da177e4SLinus Torvalds 	case SIGCHLD:
12591da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
12601da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
12611da177e4SLinus Torvalds 		break;
12621da177e4SLinus Torvalds 	case SIGKILL:
12631da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
12641da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
12651da177e4SLinus Torvalds 		break;
12661da177e4SLinus Torvalds 	case SIGSTOP:
12671da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
12681da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
12691da177e4SLinus Torvalds 		break;
12701da177e4SLinus Torvalds 	default:
12711da177e4SLinus Torvalds 		/* All other signals. */
12721da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
12731da177e4SLinus Torvalds 		break;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	return perm;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks,
12801da177e4SLinus Torvalds    fork check, ptrace check, etc. */
12811da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1,
12821da177e4SLinus Torvalds 			 struct task_struct *tsk2,
12831da177e4SLinus Torvalds 			 u32 perms)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	tsec1 = tsk1->security;
12881da177e4SLinus Torvalds 	tsec2 = tsk2->security;
12891da177e4SLinus Torvalds 	return avc_has_perm(tsec1->sid, tsec2->sid,
12901da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perms, NULL);
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
1293b68e418cSStephen Smalley #if CAP_LAST_CAP > 63
1294b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63.
1295b68e418cSStephen Smalley #endif
1296b68e418cSStephen Smalley 
12971da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
12981da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk,
12991da177e4SLinus Torvalds 			       int cap)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct task_security_struct *tsec;
13021da177e4SLinus Torvalds 	struct avc_audit_data ad;
1303b68e418cSStephen Smalley 	u16 sclass;
1304b68e418cSStephen Smalley 	u32 av = CAP_TO_MASK(cap);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	tsec = tsk->security;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,CAP);
13091da177e4SLinus Torvalds 	ad.tsk = tsk;
13101da177e4SLinus Torvalds 	ad.u.cap = cap;
13111da177e4SLinus Torvalds 
1312b68e418cSStephen Smalley 	switch (CAP_TO_INDEX(cap)) {
1313b68e418cSStephen Smalley 	case 0:
1314b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY;
1315b68e418cSStephen Smalley 		break;
1316b68e418cSStephen Smalley 	case 1:
1317b68e418cSStephen Smalley 		sclass = SECCLASS_CAPABILITY2;
1318b68e418cSStephen Smalley 		break;
1319b68e418cSStephen Smalley 	default:
1320b68e418cSStephen Smalley 		printk(KERN_ERR
1321b68e418cSStephen Smalley 		       "SELinux:  out of range capability %d\n", cap);
1322b68e418cSStephen Smalley 		BUG();
1323b68e418cSStephen Smalley 	}
1324b68e418cSStephen Smalley 	return avc_has_perm(tsec->sid, tsec->sid, sclass, av, &ad);
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
13281da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
13291da177e4SLinus Torvalds 			   u32 perms)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	struct task_security_struct *tsec;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	tsec = tsk->security;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, SECINITSID_KERNEL,
13361da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
13401da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
13411da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
13421da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk,
13431da177e4SLinus Torvalds 			  struct inode *inode,
13441da177e4SLinus Torvalds 			  u32 perms,
13451da177e4SLinus Torvalds 			  struct avc_audit_data *adp)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct task_security_struct *tsec;
13481da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13491da177e4SLinus Torvalds 	struct avc_audit_data ad;
13501da177e4SLinus Torvalds 
1351bbaca6c2SStephen Smalley 	if (unlikely (IS_PRIVATE (inode)))
1352bbaca6c2SStephen Smalley 		return 0;
1353bbaca6c2SStephen Smalley 
13541da177e4SLinus Torvalds 	tsec = tsk->security;
13551da177e4SLinus Torvalds 	isec = inode->i_security;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (!adp) {
13581da177e4SLinus Torvalds 		adp = &ad;
13591da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, FS);
13601da177e4SLinus Torvalds 		ad.u.fs.inode = inode;
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp);
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
13671da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
13681da177e4SLinus Torvalds    pathname if needed. */
13691da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk,
13701da177e4SLinus Torvalds 				  struct vfsmount *mnt,
13711da177e4SLinus Torvalds 				  struct dentry *dentry,
13721da177e4SLinus Torvalds 				  u32 av)
13731da177e4SLinus Torvalds {
13741da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13751da177e4SLinus Torvalds 	struct avc_audit_data ad;
13761da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
137744707fdfSJan Blunck 	ad.u.fs.path.mnt = mnt;
137844707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
13791da177e4SLinus Torvalds 	return inode_has_perm(tsk, inode, av, &ad);
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
13831da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
13841da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
13851da177e4SLinus Torvalds    check a particular permission to the file.
13861da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
13871da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
13881da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
13891da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
1390858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk,
13911da177e4SLinus Torvalds 				struct file *file,
13921da177e4SLinus Torvalds 				u32 av)
13931da177e4SLinus Torvalds {
13941da177e4SLinus Torvalds 	struct task_security_struct *tsec = tsk->security;
13951da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
139644707fdfSJan Blunck 	struct inode *inode = file->f_path.dentry->d_inode;
13971da177e4SLinus Torvalds 	struct avc_audit_data ad;
13981da177e4SLinus Torvalds 	int rc;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
140144707fdfSJan Blunck 	ad.u.fs.path = file->f_path;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	if (tsec->sid != fsec->sid) {
14041da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, fsec->sid,
14051da177e4SLinus Torvalds 				  SECCLASS_FD,
14061da177e4SLinus Torvalds 				  FD__USE,
14071da177e4SLinus Torvalds 				  &ad);
14081da177e4SLinus Torvalds 		if (rc)
14091da177e4SLinus Torvalds 			return rc;
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
14131da177e4SLinus Torvalds 	if (av)
14141da177e4SLinus Torvalds 		return inode_has_perm(tsk, inode, av, &ad);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	return 0;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds /* Check whether a task can create a file. */
14201da177e4SLinus Torvalds static int may_create(struct inode *dir,
14211da177e4SLinus Torvalds 		      struct dentry *dentry,
14221da177e4SLinus Torvalds 		      u16 tclass)
14231da177e4SLinus Torvalds {
14241da177e4SLinus Torvalds 	struct task_security_struct *tsec;
14251da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
14261da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
14271da177e4SLinus Torvalds 	u32 newsid;
14281da177e4SLinus Torvalds 	struct avc_audit_data ad;
14291da177e4SLinus Torvalds 	int rc;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	tsec = current->security;
14321da177e4SLinus Torvalds 	dsec = dir->i_security;
14331da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
143644707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR,
14391da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
14401da177e4SLinus Torvalds 			  &ad);
14411da177e4SLinus Torvalds 	if (rc)
14421da177e4SLinus Torvalds 		return rc;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
14451da177e4SLinus Torvalds 		newsid = tsec->create_sid;
14461da177e4SLinus Torvalds 	} else {
14471da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, dsec->sid, tclass,
14481da177e4SLinus Torvalds 					     &newsid);
14491da177e4SLinus Torvalds 		if (rc)
14501da177e4SLinus Torvalds 			return rc;
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad);
14541da177e4SLinus Torvalds 	if (rc)
14551da177e4SLinus Torvalds 		return rc;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
14581da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
14591da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14624eb582cfSMichael LeMay /* Check whether a task can create a key. */
14634eb582cfSMichael LeMay static int may_create_key(u32 ksid,
14644eb582cfSMichael LeMay 			  struct task_struct *ctx)
14654eb582cfSMichael LeMay {
14664eb582cfSMichael LeMay 	struct task_security_struct *tsec;
14674eb582cfSMichael LeMay 
14684eb582cfSMichael LeMay 	tsec = ctx->security;
14694eb582cfSMichael LeMay 
14704eb582cfSMichael LeMay 	return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
14714eb582cfSMichael LeMay }
14724eb582cfSMichael LeMay 
14731da177e4SLinus Torvalds #define MAY_LINK   0
14741da177e4SLinus Torvalds #define MAY_UNLINK 1
14751da177e4SLinus Torvalds #define MAY_RMDIR  2
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
14781da177e4SLinus Torvalds static int may_link(struct inode *dir,
14791da177e4SLinus Torvalds 		    struct dentry *dentry,
14801da177e4SLinus Torvalds 		    int kind)
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds {
14831da177e4SLinus Torvalds 	struct task_security_struct *tsec;
14841da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
14851da177e4SLinus Torvalds 	struct avc_audit_data ad;
14861da177e4SLinus Torvalds 	u32 av;
14871da177e4SLinus Torvalds 	int rc;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	tsec = current->security;
14901da177e4SLinus Torvalds 	dsec = dir->i_security;
14911da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
149444707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	av = DIR__SEARCH;
14971da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
14981da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad);
14991da177e4SLinus Torvalds 	if (rc)
15001da177e4SLinus Torvalds 		return rc;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	switch (kind) {
15031da177e4SLinus Torvalds 	case MAY_LINK:
15041da177e4SLinus Torvalds 		av = FILE__LINK;
15051da177e4SLinus Torvalds 		break;
15061da177e4SLinus Torvalds 	case MAY_UNLINK:
15071da177e4SLinus Torvalds 		av = FILE__UNLINK;
15081da177e4SLinus Torvalds 		break;
15091da177e4SLinus Torvalds 	case MAY_RMDIR:
15101da177e4SLinus Torvalds 		av = DIR__RMDIR;
15111da177e4SLinus Torvalds 		break;
15121da177e4SLinus Torvalds 	default:
1513*744ba35eSEric Paris 		printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
1514*744ba35eSEric Paris 			__func__, kind);
15151da177e4SLinus Torvalds 		return 0;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad);
15191da177e4SLinus Torvalds 	return rc;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
15231da177e4SLinus Torvalds 			     struct dentry *old_dentry,
15241da177e4SLinus Torvalds 			     struct inode *new_dir,
15251da177e4SLinus Torvalds 			     struct dentry *new_dentry)
15261da177e4SLinus Torvalds {
15271da177e4SLinus Torvalds 	struct task_security_struct *tsec;
15281da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
15291da177e4SLinus Torvalds 	struct avc_audit_data ad;
15301da177e4SLinus Torvalds 	u32 av;
15311da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
15321da177e4SLinus Torvalds 	int rc;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	tsec = current->security;
15351da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
15361da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
15371da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
15381da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
15411da177e4SLinus Torvalds 
154244707fdfSJan Blunck 	ad.u.fs.path.dentry = old_dentry;
15431da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR,
15441da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
15451da177e4SLinus Torvalds 	if (rc)
15461da177e4SLinus Torvalds 		return rc;
15471da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_isec->sid,
15481da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
15491da177e4SLinus Torvalds 	if (rc)
15501da177e4SLinus Torvalds 		return rc;
15511da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
15521da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, old_isec->sid,
15531da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
15541da177e4SLinus Torvalds 		if (rc)
15551da177e4SLinus Torvalds 			return rc;
15561da177e4SLinus Torvalds 	}
15571da177e4SLinus Torvalds 
155844707fdfSJan Blunck 	ad.u.fs.path.dentry = new_dentry;
15591da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
15601da177e4SLinus Torvalds 	if (new_dentry->d_inode)
15611da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
15621da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
15631da177e4SLinus Torvalds 	if (rc)
15641da177e4SLinus Torvalds 		return rc;
15651da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
15661da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
15671da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
15681da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, new_isec->sid,
15691da177e4SLinus Torvalds 				  new_isec->sclass,
15701da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
15711da177e4SLinus Torvalds 		if (rc)
15721da177e4SLinus Torvalds 			return rc;
15731da177e4SLinus Torvalds 	}
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	return 0;
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
15791da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk,
15801da177e4SLinus Torvalds 			       struct super_block *sb,
15811da177e4SLinus Torvalds 			       u32 perms,
15821da177e4SLinus Torvalds 			       struct avc_audit_data *ad)
15831da177e4SLinus Torvalds {
15841da177e4SLinus Torvalds 	struct task_security_struct *tsec;
15851da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	tsec = tsk->security;
15881da177e4SLinus Torvalds 	sbsec = sb->s_security;
15891da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
15901da177e4SLinus Torvalds 			    perms, ad);
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
15941da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	u32 av = 0;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	if ((mode & S_IFMT) != S_IFDIR) {
15991da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
16001da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
16011da177e4SLinus Torvalds 		if (mask & MAY_READ)
16021da177e4SLinus Torvalds 			av |= FILE__READ;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
16051da177e4SLinus Torvalds 			av |= FILE__APPEND;
16061da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
16071da177e4SLinus Torvalds 			av |= FILE__WRITE;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	} else {
16101da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
16111da177e4SLinus Torvalds 			av |= DIR__SEARCH;
16121da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
16131da177e4SLinus Torvalds 			av |= DIR__WRITE;
16141da177e4SLinus Torvalds 		if (mask & MAY_READ)
16151da177e4SLinus Torvalds 			av |= DIR__READ;
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	return av;
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
1621b0c636b9SEric Paris /*
1622b0c636b9SEric Paris  * Convert a file mask to an access vector and include the correct open
1623b0c636b9SEric Paris  * open permission.
1624b0c636b9SEric Paris  */
1625b0c636b9SEric Paris static inline u32 open_file_mask_to_av(int mode, int mask)
1626b0c636b9SEric Paris {
1627b0c636b9SEric Paris 	u32 av = file_mask_to_av(mode, mask);
1628b0c636b9SEric Paris 
1629b0c636b9SEric Paris 	if (selinux_policycap_openperm) {
1630b0c636b9SEric Paris 		/*
1631b0c636b9SEric Paris 		 * lnk files and socks do not really have an 'open'
1632b0c636b9SEric Paris 		 */
1633b0c636b9SEric Paris 		if (S_ISREG(mode))
1634b0c636b9SEric Paris 			av |= FILE__OPEN;
1635b0c636b9SEric Paris 		else if (S_ISCHR(mode))
1636b0c636b9SEric Paris 			av |= CHR_FILE__OPEN;
1637b0c636b9SEric Paris 		else if (S_ISBLK(mode))
1638b0c636b9SEric Paris 			av |= BLK_FILE__OPEN;
1639b0c636b9SEric Paris 		else if (S_ISFIFO(mode))
1640b0c636b9SEric Paris 			av |= FIFO_FILE__OPEN;
1641b0c636b9SEric Paris 		else if (S_ISDIR(mode))
1642b0c636b9SEric Paris 			av |= DIR__OPEN;
1643b0c636b9SEric Paris 		else
1644*744ba35eSEric Paris 			printk(KERN_ERR "SELinux: WARNING: inside %s with "
1645*744ba35eSEric Paris 				"unknown mode:%x\n", __func__, mode);
1646b0c636b9SEric Paris 	}
1647b0c636b9SEric Paris 	return av;
1648b0c636b9SEric Paris }
1649b0c636b9SEric Paris 
16501da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
16511da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
16521da177e4SLinus Torvalds {
16531da177e4SLinus Torvalds 	u32 av = 0;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
16561da177e4SLinus Torvalds 		av |= FILE__READ;
16571da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
16581da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
16591da177e4SLinus Torvalds 			av |= FILE__APPEND;
16601da177e4SLinus Torvalds 		else
16611da177e4SLinus Torvalds 			av |= FILE__WRITE;
16621da177e4SLinus Torvalds 	}
16630794c66dSStephen Smalley 	if (!av) {
16640794c66dSStephen Smalley 		/*
16650794c66dSStephen Smalley 		 * Special file opened with flags 3 for ioctl-only use.
16660794c66dSStephen Smalley 		 */
16670794c66dSStephen Smalley 		av = FILE__IOCTL;
16680794c66dSStephen Smalley 	}
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	return av;
16711da177e4SLinus Torvalds }
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds /* Hook functions begin here. */
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	int rc;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	rc = secondary_ops->ptrace(parent,child);
16801da177e4SLinus Torvalds 	if (rc)
16811da177e4SLinus Torvalds 		return rc;
16821da177e4SLinus Torvalds 
16830356357cSRoland McGrath 	return task_has_perm(parent, child, PROCESS__PTRACE);
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
16871da177e4SLinus Torvalds                           kernel_cap_t *inheritable, kernel_cap_t *permitted)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	int error;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	error = task_has_perm(current, target, PROCESS__GETCAP);
16921da177e4SLinus Torvalds 	if (error)
16931da177e4SLinus Torvalds 		return error;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	return secondary_ops->capget(target, effective, inheritable, permitted);
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective,
16991da177e4SLinus Torvalds                                 kernel_cap_t *inheritable, kernel_cap_t *permitted)
17001da177e4SLinus Torvalds {
17011da177e4SLinus Torvalds 	int error;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	error = secondary_ops->capset_check(target, effective, inheritable, permitted);
17041da177e4SLinus Torvalds 	if (error)
17051da177e4SLinus Torvalds 		return error;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	return task_has_perm(current, target, PROCESS__SETCAP);
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective,
17111da177e4SLinus Torvalds                                kernel_cap_t *inheritable, kernel_cap_t *permitted)
17121da177e4SLinus Torvalds {
17131da177e4SLinus Torvalds 	secondary_ops->capset_set(target, effective, inheritable, permitted);
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap)
17171da177e4SLinus Torvalds {
17181da177e4SLinus Torvalds 	int rc;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	rc = secondary_ops->capable(tsk, cap);
17211da177e4SLinus Torvalds 	if (rc)
17221da177e4SLinus Torvalds 		return rc;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	return task_has_capability(tsk,cap);
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17273fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid)
17283fbfa981SEric W. Biederman {
17293fbfa981SEric W. Biederman 	int buflen, rc;
17303fbfa981SEric W. Biederman 	char *buffer, *path, *end;
17313fbfa981SEric W. Biederman 
17323fbfa981SEric W. Biederman 	rc = -ENOMEM;
17333fbfa981SEric W. Biederman 	buffer = (char*)__get_free_page(GFP_KERNEL);
17343fbfa981SEric W. Biederman 	if (!buffer)
17353fbfa981SEric W. Biederman 		goto out;
17363fbfa981SEric W. Biederman 
17373fbfa981SEric W. Biederman 	buflen = PAGE_SIZE;
17383fbfa981SEric W. Biederman 	end = buffer+buflen;
17393fbfa981SEric W. Biederman 	*--end = '\0';
17403fbfa981SEric W. Biederman 	buflen--;
17413fbfa981SEric W. Biederman 	path = end-1;
17423fbfa981SEric W. Biederman 	*path = '/';
17433fbfa981SEric W. Biederman 	while (table) {
17443fbfa981SEric W. Biederman 		const char *name = table->procname;
17453fbfa981SEric W. Biederman 		size_t namelen = strlen(name);
17463fbfa981SEric W. Biederman 		buflen -= namelen + 1;
17473fbfa981SEric W. Biederman 		if (buflen < 0)
17483fbfa981SEric W. Biederman 			goto out_free;
17493fbfa981SEric W. Biederman 		end -= namelen;
17503fbfa981SEric W. Biederman 		memcpy(end, name, namelen);
17513fbfa981SEric W. Biederman 		*--end = '/';
17523fbfa981SEric W. Biederman 		path = end;
17533fbfa981SEric W. Biederman 		table = table->parent;
17543fbfa981SEric W. Biederman 	}
1755b599fdfdSEric W. Biederman 	buflen -= 4;
1756b599fdfdSEric W. Biederman 	if (buflen < 0)
1757b599fdfdSEric W. Biederman 		goto out_free;
1758b599fdfdSEric W. Biederman 	end -= 4;
1759b599fdfdSEric W. Biederman 	memcpy(end, "/sys", 4);
1760b599fdfdSEric W. Biederman 	path = end;
17613fbfa981SEric W. Biederman 	rc = security_genfs_sid("proc", path, tclass, sid);
17623fbfa981SEric W. Biederman out_free:
17633fbfa981SEric W. Biederman 	free_page((unsigned long)buffer);
17643fbfa981SEric W. Biederman out:
17653fbfa981SEric W. Biederman 	return rc;
17663fbfa981SEric W. Biederman }
17673fbfa981SEric W. Biederman 
17681da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	int error = 0;
17711da177e4SLinus Torvalds 	u32 av;
17721da177e4SLinus Torvalds 	struct task_security_struct *tsec;
17731da177e4SLinus Torvalds 	u32 tsid;
17741da177e4SLinus Torvalds 	int rc;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	rc = secondary_ops->sysctl(table, op);
17771da177e4SLinus Torvalds 	if (rc)
17781da177e4SLinus Torvalds 		return rc;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	tsec = current->security;
17811da177e4SLinus Torvalds 
17823fbfa981SEric W. Biederman 	rc = selinux_sysctl_get_sid(table, (op == 0001) ?
17831da177e4SLinus Torvalds 				    SECCLASS_DIR : SECCLASS_FILE, &tsid);
17841da177e4SLinus Torvalds 	if (rc) {
17851da177e4SLinus Torvalds 		/* Default to the well-defined sysctl SID. */
17861da177e4SLinus Torvalds 		tsid = SECINITSID_SYSCTL;
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	/* The op values are "defined" in sysctl.c, thereby creating
17901da177e4SLinus Torvalds 	 * a bad coupling between this module and sysctl.c */
17911da177e4SLinus Torvalds 	if(op == 001) {
17921da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, tsid,
17931da177e4SLinus Torvalds 				     SECCLASS_DIR, DIR__SEARCH, NULL);
17941da177e4SLinus Torvalds 	} else {
17951da177e4SLinus Torvalds 		av = 0;
17961da177e4SLinus Torvalds 		if (op & 004)
17971da177e4SLinus Torvalds 			av |= FILE__READ;
17981da177e4SLinus Torvalds 		if (op & 002)
17991da177e4SLinus Torvalds 			av |= FILE__WRITE;
18001da177e4SLinus Torvalds 		if (av)
18011da177e4SLinus Torvalds 			error = avc_has_perm(tsec->sid, tsid,
18021da177e4SLinus Torvalds 					     SECCLASS_FILE, av, NULL);
18031da177e4SLinus Torvalds         }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	return error;
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	int rc = 0;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	if (!sb)
18131da177e4SLinus Torvalds 		return 0;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	switch (cmds) {
18161da177e4SLinus Torvalds 		case Q_SYNC:
18171da177e4SLinus Torvalds 		case Q_QUOTAON:
18181da177e4SLinus Torvalds 		case Q_QUOTAOFF:
18191da177e4SLinus Torvalds 	        case Q_SETINFO:
18201da177e4SLinus Torvalds 		case Q_SETQUOTA:
18211da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
18221da177e4SLinus Torvalds 						 sb,
18231da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAMOD, NULL);
18241da177e4SLinus Torvalds 			break;
18251da177e4SLinus Torvalds 	        case Q_GETFMT:
18261da177e4SLinus Torvalds 	        case Q_GETINFO:
18271da177e4SLinus Torvalds 		case Q_GETQUOTA:
18281da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
18291da177e4SLinus Torvalds 						 sb,
18301da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAGET, NULL);
18311da177e4SLinus Torvalds 			break;
18321da177e4SLinus Torvalds 		default:
18331da177e4SLinus Torvalds 			rc = 0;  /* let the kernel handle invalid cmds */
18341da177e4SLinus Torvalds 			break;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 	return rc;
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
18401da177e4SLinus Torvalds {
18411da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON);
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds static int selinux_syslog(int type)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds 	int rc;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	rc = secondary_ops->syslog(type);
18491da177e4SLinus Torvalds 	if (rc)
18501da177e4SLinus Torvalds 		return rc;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	switch (type) {
18531da177e4SLinus Torvalds 		case 3:         /* Read last kernel messages */
18541da177e4SLinus Torvalds 		case 10:        /* Return size of the log buffer */
18551da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_READ);
18561da177e4SLinus Torvalds 			break;
18571da177e4SLinus Torvalds 		case 6:         /* Disable logging to console */
18581da177e4SLinus Torvalds 		case 7:         /* Enable logging to console */
18591da177e4SLinus Torvalds 		case 8:		/* Set level of messages printed to console */
18601da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
18611da177e4SLinus Torvalds 			break;
18621da177e4SLinus Torvalds 		case 0:         /* Close log */
18631da177e4SLinus Torvalds 		case 1:         /* Open log */
18641da177e4SLinus Torvalds 		case 2:         /* Read from log */
18651da177e4SLinus Torvalds 		case 4:         /* Read/clear last kernel messages */
18661da177e4SLinus Torvalds 		case 5:         /* Clear ring buffer */
18671da177e4SLinus Torvalds 		default:
18681da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
18691da177e4SLinus Torvalds 			break;
18701da177e4SLinus Torvalds 	}
18711da177e4SLinus Torvalds 	return rc;
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /*
18751da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
18761da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
18771da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  * Note that secondary_ops->capable and task_has_perm_noaudit return 0
18801da177e4SLinus Torvalds  * if the capability is granted, but __vm_enough_memory requires 1 if
18811da177e4SLinus Torvalds  * the capability is granted.
18821da177e4SLinus Torvalds  *
18831da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
18841da177e4SLinus Torvalds  * processes that allocate mappings.
18851da177e4SLinus Torvalds  */
188634b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
18871da177e4SLinus Torvalds {
18881da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
18891da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 	rc = secondary_ops->capable(current, CAP_SYS_ADMIN);
18921da177e4SLinus Torvalds 	if (rc == 0)
18931da177e4SLinus Torvalds 		rc = avc_has_perm_noaudit(tsec->sid, tsec->sid,
18941da177e4SLinus Torvalds 					  SECCLASS_CAPABILITY,
18951da177e4SLinus Torvalds 					  CAP_TO_MASK(CAP_SYS_ADMIN),
18962c3c05dbSStephen Smalley 					  0,
18971da177e4SLinus Torvalds 					  NULL);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	if (rc == 0)
19001da177e4SLinus Torvalds 		cap_sys_admin = 1;
19011da177e4SLinus Torvalds 
190234b4e4aaSAlan Cox 	return __vm_enough_memory(mm, pages, cap_sys_admin);
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
19050356357cSRoland McGrath /**
19060356357cSRoland McGrath  * task_tracer_task - return the task that is tracing the given task
19070356357cSRoland McGrath  * @task:		task to consider
19080356357cSRoland McGrath  *
19090356357cSRoland McGrath  * Returns NULL if noone is tracing @task, or the &struct task_struct
19100356357cSRoland McGrath  * pointer to its tracer.
19110356357cSRoland McGrath  *
19120356357cSRoland McGrath  * Must be called under rcu_read_lock().
19130356357cSRoland McGrath  */
19140356357cSRoland McGrath static struct task_struct *task_tracer_task(struct task_struct *task)
19150356357cSRoland McGrath {
19160356357cSRoland McGrath 	if (task->ptrace & PT_PTRACED)
19170356357cSRoland McGrath 		return rcu_dereference(task->parent);
19180356357cSRoland McGrath 	return NULL;
19190356357cSRoland McGrath }
19200356357cSRoland McGrath 
19211da177e4SLinus Torvalds /* binprm security operations */
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm)
19241da177e4SLinus Torvalds {
19251da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
19261da177e4SLinus Torvalds 
192789d155efSJames Morris 	bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL);
19281da177e4SLinus Torvalds 	if (!bsec)
19291da177e4SLinus Torvalds 		return -ENOMEM;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	bsec->sid = SECINITSID_UNLABELED;
19321da177e4SLinus Torvalds 	bsec->set = 0;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	bprm->security = bsec;
19351da177e4SLinus Torvalds 	return 0;
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm)
19391da177e4SLinus Torvalds {
19401da177e4SLinus Torvalds 	struct task_security_struct *tsec;
19413d5ff529SJosef Sipek 	struct inode *inode = bprm->file->f_path.dentry->d_inode;
19421da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19431da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
19441da177e4SLinus Torvalds 	u32 newsid;
19451da177e4SLinus Torvalds 	struct avc_audit_data ad;
19461da177e4SLinus Torvalds 	int rc;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	rc = secondary_ops->bprm_set_security(bprm);
19491da177e4SLinus Torvalds 	if (rc)
19501da177e4SLinus Torvalds 		return rc;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	bsec = bprm->security;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	if (bsec->set)
19551da177e4SLinus Torvalds 		return 0;
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 	tsec = current->security;
19581da177e4SLinus Torvalds 	isec = inode->i_security;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	/* Default to the current task SID. */
19611da177e4SLinus Torvalds 	bsec->sid = tsec->sid;
19621da177e4SLinus Torvalds 
196328eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
19641da177e4SLinus Torvalds 	tsec->create_sid = 0;
196528eba5bfSMichael LeMay 	tsec->keycreate_sid = 0;
196642c3e03eSEric Paris 	tsec->sockcreate_sid = 0;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	if (tsec->exec_sid) {
19691da177e4SLinus Torvalds 		newsid = tsec->exec_sid;
19701da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
19711da177e4SLinus Torvalds 		tsec->exec_sid = 0;
19721da177e4SLinus Torvalds 	} else {
19731da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
19741da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, isec->sid,
19751da177e4SLinus Torvalds 		                             SECCLASS_PROCESS, &newsid);
19761da177e4SLinus Torvalds 		if (rc)
19771da177e4SLinus Torvalds 			return rc;
19781da177e4SLinus Torvalds 	}
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
198144707fdfSJan Blunck 	ad.u.fs.path = bprm->file->f_path;
19821da177e4SLinus Torvalds 
19833d5ff529SJosef Sipek 	if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
19841da177e4SLinus Torvalds 		newsid = tsec->sid;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds         if (tsec->sid == newsid) {
19871da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, isec->sid,
19881da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
19891da177e4SLinus Torvalds 		if (rc)
19901da177e4SLinus Torvalds 			return rc;
19911da177e4SLinus Torvalds 	} else {
19921da177e4SLinus Torvalds 		/* Check permissions for the transition. */
19931da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, newsid,
19941da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
19951da177e4SLinus Torvalds 		if (rc)
19961da177e4SLinus Torvalds 			return rc;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 		rc = avc_has_perm(newsid, isec->sid,
19991da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
20001da177e4SLinus Torvalds 		if (rc)
20011da177e4SLinus Torvalds 			return rc;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 		/* Clear any possibly unsafe personality bits on exec: */
20041da177e4SLinus Torvalds 		current->personality &= ~PER_CLEAR_ON_SETID;
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 		/* Set the security field to the new SID. */
20071da177e4SLinus Torvalds 		bsec->sid = newsid;
20081da177e4SLinus Torvalds 	}
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	bsec->set = 1;
20111da177e4SLinus Torvalds 	return 0;
20121da177e4SLinus Torvalds }
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm)
20151da177e4SLinus Torvalds {
20161da177e4SLinus Torvalds 	return secondary_ops->bprm_check_security(bprm);
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm)
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
20231da177e4SLinus Torvalds 	int atsecure = 0;
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	if (tsec->osid != tsec->sid) {
20261da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
20271da177e4SLinus Torvalds 		   the noatsecure permission is granted between
20281da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
20291da177e4SLinus Torvalds 		atsecure = avc_has_perm(tsec->osid, tsec->sid,
20301da177e4SLinus Torvalds 					 SECCLASS_PROCESS,
20311da177e4SLinus Torvalds 					 PROCESS__NOATSECURE, NULL);
20321da177e4SLinus Torvalds 	}
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	return (atsecure || secondary_ops->bprm_secureexec(bprm));
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm)
20381da177e4SLinus Torvalds {
20399a5f04bfSJesper Juhl 	kfree(bprm->security);
20401da177e4SLinus Torvalds 	bprm->security = NULL;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount;
20441da177e4SLinus Torvalds extern struct dentry *selinux_null;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
20471da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	struct avc_audit_data ad;
20501da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
2051b20c8122SStephen Smalley 	struct tty_struct *tty;
2052badf1662SDipankar Sarma 	struct fdtable *fdt;
20531da177e4SLinus Torvalds 	long j = -1;
205424ec839cSPeter Zijlstra 	int drop_tty = 0;
20551da177e4SLinus Torvalds 
2056b20c8122SStephen Smalley 	mutex_lock(&tty_mutex);
205724ec839cSPeter Zijlstra 	tty = get_current_tty();
20581da177e4SLinus Torvalds 	if (tty) {
20591da177e4SLinus Torvalds 		file_list_lock();
20602f512016SEric Dumazet 		file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);
20611da177e4SLinus Torvalds 		if (file) {
20621da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
20631da177e4SLinus Torvalds 			   Use inode_has_perm on the tty inode directly rather
20641da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
20651da177e4SLinus Torvalds 			   file may belong to another process and we are only
20661da177e4SLinus Torvalds 			   interested in the inode-based check here. */
20673d5ff529SJosef Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
20681da177e4SLinus Torvalds 			if (inode_has_perm(current, inode,
20691da177e4SLinus Torvalds 					   FILE__READ | FILE__WRITE, NULL)) {
207024ec839cSPeter Zijlstra 				drop_tty = 1;
20711da177e4SLinus Torvalds 			}
20721da177e4SLinus Torvalds 		}
20731da177e4SLinus Torvalds 		file_list_unlock();
20741da177e4SLinus Torvalds 	}
2075b20c8122SStephen Smalley 	mutex_unlock(&tty_mutex);
207698a27ba4SEric W. Biederman 	/* Reset controlling tty. */
207798a27ba4SEric W. Biederman 	if (drop_tty)
207898a27ba4SEric W. Biederman 		no_tty();
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	spin_lock(&files->file_lock);
20851da177e4SLinus Torvalds 	for (;;) {
20861da177e4SLinus Torvalds 		unsigned long set, i;
20871da177e4SLinus Torvalds 		int fd;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 		j++;
20901da177e4SLinus Torvalds 		i = j * __NFDBITS;
2091badf1662SDipankar Sarma 		fdt = files_fdtable(files);
2092bbea9f69SVadim Lobanov 		if (i >= fdt->max_fds)
20931da177e4SLinus Torvalds 			break;
2094badf1662SDipankar Sarma 		set = fdt->open_fds->fds_bits[j];
20951da177e4SLinus Torvalds 		if (!set)
20961da177e4SLinus Torvalds 			continue;
20971da177e4SLinus Torvalds 		spin_unlock(&files->file_lock);
20981da177e4SLinus Torvalds 		for ( ; set ; i++,set >>= 1) {
20991da177e4SLinus Torvalds 			if (set & 1) {
21001da177e4SLinus Torvalds 				file = fget(i);
21011da177e4SLinus Torvalds 				if (!file)
21021da177e4SLinus Torvalds 					continue;
21031da177e4SLinus Torvalds 				if (file_has_perm(current,
21041da177e4SLinus Torvalds 						  file,
21051da177e4SLinus Torvalds 						  file_to_av(file))) {
21061da177e4SLinus Torvalds 					sys_close(i);
21071da177e4SLinus Torvalds 					fd = get_unused_fd();
21081da177e4SLinus Torvalds 					if (fd != i) {
21091da177e4SLinus Torvalds 						if (fd >= 0)
21101da177e4SLinus Torvalds 							put_unused_fd(fd);
21111da177e4SLinus Torvalds 						fput(file);
21121da177e4SLinus Torvalds 						continue;
21131da177e4SLinus Torvalds 					}
21141da177e4SLinus Torvalds 					if (devnull) {
2115095975daSNick Piggin 						get_file(devnull);
21161da177e4SLinus Torvalds 					} else {
21171da177e4SLinus Torvalds 						devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR);
2118fc5d81e6SAkinobu Mita 						if (IS_ERR(devnull)) {
2119fc5d81e6SAkinobu Mita 							devnull = NULL;
21201da177e4SLinus Torvalds 							put_unused_fd(fd);
21211da177e4SLinus Torvalds 							fput(file);
21221da177e4SLinus Torvalds 							continue;
21231da177e4SLinus Torvalds 						}
21241da177e4SLinus Torvalds 					}
21251da177e4SLinus Torvalds 					fd_install(fd, devnull);
21261da177e4SLinus Torvalds 				}
21271da177e4SLinus Torvalds 				fput(file);
21281da177e4SLinus Torvalds 			}
21291da177e4SLinus Torvalds 		}
21301da177e4SLinus Torvalds 		spin_lock(&files->file_lock);
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	}
21331da177e4SLinus Torvalds 	spin_unlock(&files->file_lock);
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
21371da177e4SLinus Torvalds {
21381da177e4SLinus Torvalds 	struct task_security_struct *tsec;
21391da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
21401da177e4SLinus Torvalds 	u32 sid;
21411da177e4SLinus Torvalds 	int rc;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	secondary_ops->bprm_apply_creds(bprm, unsafe);
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	tsec = current->security;
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	bsec = bprm->security;
21481da177e4SLinus Torvalds 	sid = bsec->sid;
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
21511da177e4SLinus Torvalds 	bsec->unsafe = 0;
21521da177e4SLinus Torvalds 	if (tsec->sid != sid) {
21531da177e4SLinus Torvalds 		/* Check for shared state.  If not ok, leave SID
21541da177e4SLinus Torvalds 		   unchanged and kill. */
21551da177e4SLinus Torvalds 		if (unsafe & LSM_UNSAFE_SHARE) {
21561da177e4SLinus Torvalds 			rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
21571da177e4SLinus Torvalds 					PROCESS__SHARE, NULL);
21581da177e4SLinus Torvalds 			if (rc) {
21591da177e4SLinus Torvalds 				bsec->unsafe = 1;
21601da177e4SLinus Torvalds 				return;
21611da177e4SLinus Torvalds 			}
21621da177e4SLinus Torvalds 		}
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
21651da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and kill. */
21661da177e4SLinus Torvalds 		if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
21670356357cSRoland McGrath 			struct task_struct *tracer;
21680356357cSRoland McGrath 			struct task_security_struct *sec;
21690356357cSRoland McGrath 			u32 ptsid = 0;
21700356357cSRoland McGrath 
21710356357cSRoland McGrath 			rcu_read_lock();
21720356357cSRoland McGrath 			tracer = task_tracer_task(current);
21730356357cSRoland McGrath 			if (likely(tracer != NULL)) {
21740356357cSRoland McGrath 				sec = tracer->security;
21750356357cSRoland McGrath 				ptsid = sec->sid;
21760356357cSRoland McGrath 			}
21770356357cSRoland McGrath 			rcu_read_unlock();
21780356357cSRoland McGrath 
21790356357cSRoland McGrath 			if (ptsid != 0) {
21800356357cSRoland McGrath 				rc = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
21810356357cSRoland McGrath 						  PROCESS__PTRACE, NULL);
21821da177e4SLinus Torvalds 				if (rc) {
21831da177e4SLinus Torvalds 					bsec->unsafe = 1;
21841da177e4SLinus Torvalds 					return;
21851da177e4SLinus Torvalds 				}
21861da177e4SLinus Torvalds 			}
21870356357cSRoland McGrath 		}
21881da177e4SLinus Torvalds 		tsec->sid = sid;
21891da177e4SLinus Torvalds 	}
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds /*
21931da177e4SLinus Torvalds  * called after apply_creds without the task lock held
21941da177e4SLinus Torvalds  */
21951da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm)
21961da177e4SLinus Torvalds {
21971da177e4SLinus Torvalds 	struct task_security_struct *tsec;
21981da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
21991da177e4SLinus Torvalds 	struct itimerval itimer;
22001da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
22011da177e4SLinus Torvalds 	int rc, i;
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds 	tsec = current->security;
22041da177e4SLinus Torvalds 	bsec = bprm->security;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	if (bsec->unsafe) {
22071da177e4SLinus Torvalds 		force_sig_specific(SIGKILL, current);
22081da177e4SLinus Torvalds 		return;
22091da177e4SLinus Torvalds 	}
22101da177e4SLinus Torvalds 	if (tsec->osid == tsec->sid)
22111da177e4SLinus Torvalds 		return;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
22141da177e4SLinus Torvalds 	flush_unauthorized_files(current->files);
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/* Check whether the new SID can inherit signal state
22171da177e4SLinus Torvalds 	   from the old SID.  If not, clear itimers to avoid
22181da177e4SLinus Torvalds 	   subsequent signal generation and flush and unblock
22191da177e4SLinus Torvalds 	   signals. This must occur _after_ the task SID has
22201da177e4SLinus Torvalds 	  been updated so that any kill done after the flush
22211da177e4SLinus Torvalds 	  will be checked against the new SID. */
22221da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
22231da177e4SLinus Torvalds 			  PROCESS__SIGINH, NULL);
22241da177e4SLinus Torvalds 	if (rc) {
22251da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
22261da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
22271da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
22281da177e4SLinus Torvalds 		flush_signals(current);
22291da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
22301da177e4SLinus Torvalds 		flush_signal_handlers(current, 1);
22311da177e4SLinus Torvalds 		sigemptyset(&current->blocked);
22321da177e4SLinus Torvalds 		recalc_sigpending();
22331da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
22364ac212adSStephen Smalley 	/* Always clear parent death signal on SID transitions. */
22374ac212adSStephen Smalley 	current->pdeath_signal = 0;
22384ac212adSStephen Smalley 
22391da177e4SLinus Torvalds 	/* Check whether the new SID can inherit resource limits
22401da177e4SLinus Torvalds 	   from the old SID.  If not, reset all soft limits to
22411da177e4SLinus Torvalds 	   the lower of the current task's hard limit and the init
22421da177e4SLinus Torvalds 	   task's soft limit.  Note that the setting of hard limits
22431da177e4SLinus Torvalds 	   (even to lower them) can be controlled by the setrlimit
22441da177e4SLinus Torvalds 	   check. The inclusion of the init task's soft limit into
22451da177e4SLinus Torvalds 	   the computation is to avoid resetting soft limits higher
22461da177e4SLinus Torvalds 	   than the default soft limit for cases where the default
22471da177e4SLinus Torvalds 	   is lower than the hard limit, e.g. RLIMIT_CORE or
22481da177e4SLinus Torvalds 	   RLIMIT_STACK.*/
22491da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
22501da177e4SLinus Torvalds 			  PROCESS__RLIMITINH, NULL);
22511da177e4SLinus Torvalds 	if (rc) {
22521da177e4SLinus Torvalds 		for (i = 0; i < RLIM_NLIMITS; i++) {
22531da177e4SLinus Torvalds 			rlim = current->signal->rlim + i;
22541da177e4SLinus Torvalds 			initrlim = init_task.signal->rlim+i;
22551da177e4SLinus Torvalds 			rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur);
22561da177e4SLinus Torvalds 		}
22571da177e4SLinus Torvalds 		if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
22581da177e4SLinus Torvalds 			/*
22591da177e4SLinus Torvalds 			 * This will cause RLIMIT_CPU calculations
22601da177e4SLinus Torvalds 			 * to be refigured.
22611da177e4SLinus Torvalds 			 */
22621da177e4SLinus Torvalds 			current->it_prof_expires = jiffies_to_cputime(1);
22631da177e4SLinus Torvalds 		}
22641da177e4SLinus Torvalds 	}
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	/* Wake up the parent if it is waiting so that it can
22671da177e4SLinus Torvalds 	   recheck wait permission to the new task SID. */
22681da177e4SLinus Torvalds 	wake_up_interruptible(&current->parent->signal->wait_chldexit);
22691da177e4SLinus Torvalds }
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds /* superblock security operations */
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
22741da177e4SLinus Torvalds {
22751da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	superblock_free_security(sb);
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
22841da177e4SLinus Torvalds {
22851da177e4SLinus Torvalds 	if (plen > olen)
22861da177e4SLinus Torvalds 		return 0;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
22921da177e4SLinus Torvalds {
2293832cbd9aSEric Paris 	return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2294832cbd9aSEric Paris 		match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2295832cbd9aSEric Paris 		match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
2296832cbd9aSEric Paris 		match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len));
22971da177e4SLinus Torvalds }
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
23001da177e4SLinus Torvalds {
23011da177e4SLinus Torvalds 	if (!*first) {
23021da177e4SLinus Torvalds 		**to = ',';
23031da177e4SLinus Torvalds 		*to += 1;
23043528a953SCory Olmo 	} else
23051da177e4SLinus Torvalds 		*first = 0;
23061da177e4SLinus Torvalds 	memcpy(*to, from, len);
23071da177e4SLinus Torvalds 	*to += len;
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
23103528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
23113528a953SCory Olmo 		                       int len)
23123528a953SCory Olmo {
23133528a953SCory Olmo 	int current_size = 0;
23143528a953SCory Olmo 
23153528a953SCory Olmo 	if (!*first) {
23163528a953SCory Olmo 		**to = '|';
23173528a953SCory Olmo 		*to += 1;
23183528a953SCory Olmo 	}
23193528a953SCory Olmo 	else
23203528a953SCory Olmo 		*first = 0;
23213528a953SCory Olmo 
23223528a953SCory Olmo 	while (current_size < len) {
23233528a953SCory Olmo 		if (*from != '"') {
23243528a953SCory Olmo 			**to = *from;
23253528a953SCory Olmo 			*to += 1;
23263528a953SCory Olmo 		}
23273528a953SCory Olmo 		from += 1;
23283528a953SCory Olmo 		current_size += 1;
23293528a953SCory Olmo 	}
23303528a953SCory Olmo }
23313528a953SCory Olmo 
2332e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy)
23331da177e4SLinus Torvalds {
23341da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
23351da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
23361da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
23373528a953SCory Olmo 	int open_quote = 0;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	in_curr = orig;
23401da177e4SLinus Torvalds 	sec_curr = copy;
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
23431da177e4SLinus Torvalds 	if (!nosec) {
23441da177e4SLinus Torvalds 		rc = -ENOMEM;
23451da177e4SLinus Torvalds 		goto out;
23461da177e4SLinus Torvalds 	}
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	nosec_save = nosec;
23491da177e4SLinus Torvalds 	fnosec = fsec = 1;
23501da177e4SLinus Torvalds 	in_save = in_end = orig;
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	do {
23533528a953SCory Olmo 		if (*in_end == '"')
23543528a953SCory Olmo 			open_quote = !open_quote;
23553528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
23563528a953SCory Olmo 				*in_end == '\0') {
23571da177e4SLinus Torvalds 			int len = in_end - in_curr;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
23603528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
23611da177e4SLinus Torvalds 			else
23621da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 			in_curr = in_end + 1;
23651da177e4SLinus Torvalds 		}
23661da177e4SLinus Torvalds 	} while (*in_end++);
23671da177e4SLinus Torvalds 
23686931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2369da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
23701da177e4SLinus Torvalds out:
23711da177e4SLinus Torvalds 	return rc;
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data)
23751da177e4SLinus Torvalds {
23761da177e4SLinus Torvalds 	struct avc_audit_data ad;
23771da177e4SLinus Torvalds 	int rc;
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
23801da177e4SLinus Torvalds 	if (rc)
23811da177e4SLinus Torvalds 		return rc;
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
238444707fdfSJan Blunck 	ad.u.fs.path.dentry = sb->s_root;
23851da177e4SLinus Torvalds 	return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad);
23861da177e4SLinus Torvalds }
23871da177e4SLinus Torvalds 
2388726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
23891da177e4SLinus Torvalds {
23901da177e4SLinus Torvalds 	struct avc_audit_data ad;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
239344707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry->d_sb->s_root;
2394726c3342SDavid Howells 	return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds static int selinux_mount(char * dev_name,
23981da177e4SLinus Torvalds                          struct nameidata *nd,
23991da177e4SLinus Torvalds                          char * type,
24001da177e4SLinus Torvalds                          unsigned long flags,
24011da177e4SLinus Torvalds                          void * data)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	int rc;
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data);
24061da177e4SLinus Torvalds 	if (rc)
24071da177e4SLinus Torvalds 		return rc;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
24104ac91378SJan Blunck 		return superblock_has_perm(current, nd->path.mnt->mnt_sb,
24111da177e4SLinus Torvalds 		                           FILESYSTEM__REMOUNT, NULL);
24121da177e4SLinus Torvalds 	else
24134ac91378SJan Blunck 		return dentry_has_perm(current, nd->path.mnt, nd->path.dentry,
24141da177e4SLinus Torvalds 		                       FILE__MOUNTON);
24151da177e4SLinus Torvalds }
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
24181da177e4SLinus Torvalds {
24191da177e4SLinus Torvalds 	int rc;
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	rc = secondary_ops->sb_umount(mnt, flags);
24221da177e4SLinus Torvalds 	if (rc)
24231da177e4SLinus Torvalds 		return rc;
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	return superblock_has_perm(current,mnt->mnt_sb,
24261da177e4SLinus Torvalds 	                           FILESYSTEM__UNMOUNT,NULL);
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds /* inode security operations */
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
24321da177e4SLinus Torvalds {
24331da177e4SLinus Torvalds 	return inode_alloc_security(inode);
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	inode_free_security(inode);
24391da177e4SLinus Torvalds }
24401da177e4SLinus Torvalds 
24415e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
24425e41ff9eSStephen Smalley 				       char **name, void **value,
24435e41ff9eSStephen Smalley 				       size_t *len)
24445e41ff9eSStephen Smalley {
24455e41ff9eSStephen Smalley 	struct task_security_struct *tsec;
24465e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
24475e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2448570bc1c2SStephen Smalley 	u32 newsid, clen;
24495e41ff9eSStephen Smalley 	int rc;
2450570bc1c2SStephen Smalley 	char *namep = NULL, *context;
24515e41ff9eSStephen Smalley 
24525e41ff9eSStephen Smalley 	tsec = current->security;
24535e41ff9eSStephen Smalley 	dsec = dir->i_security;
24545e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
24555e41ff9eSStephen Smalley 
24565e41ff9eSStephen Smalley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
24575e41ff9eSStephen Smalley 		newsid = tsec->create_sid;
24585e41ff9eSStephen Smalley 	} else {
24595e41ff9eSStephen Smalley 		rc = security_transition_sid(tsec->sid, dsec->sid,
24605e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
24615e41ff9eSStephen Smalley 					     &newsid);
24625e41ff9eSStephen Smalley 		if (rc) {
24635e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
24645e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
24655e41ff9eSStephen Smalley 			       "ino=%ld)\n",
2466dd6f953aSHarvey Harrison 			       __func__,
24675e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
24685e41ff9eSStephen Smalley 			return rc;
24695e41ff9eSStephen Smalley 		}
24705e41ff9eSStephen Smalley 	}
24715e41ff9eSStephen Smalley 
2472296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
2473296fddf7SEric Paris 	if (sbsec->initialized) {
2474296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2475296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2476296fddf7SEric Paris 		isec->sid = newsid;
2477296fddf7SEric Paris 		isec->initialized = 1;
2478296fddf7SEric Paris 	}
24795e41ff9eSStephen Smalley 
24808aad3875SStephen Smalley 	if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
248125a74f3bSStephen Smalley 		return -EOPNOTSUPP;
248225a74f3bSStephen Smalley 
2483570bc1c2SStephen Smalley 	if (name) {
2484a02fe132SJosef Bacik 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
24855e41ff9eSStephen Smalley 		if (!namep)
24865e41ff9eSStephen Smalley 			return -ENOMEM;
24875e41ff9eSStephen Smalley 		*name = namep;
2488570bc1c2SStephen Smalley 	}
24895e41ff9eSStephen Smalley 
2490570bc1c2SStephen Smalley 	if (value && len) {
2491570bc1c2SStephen Smalley 		rc = security_sid_to_context(newsid, &context, &clen);
24925e41ff9eSStephen Smalley 		if (rc) {
24935e41ff9eSStephen Smalley 			kfree(namep);
24945e41ff9eSStephen Smalley 			return rc;
24955e41ff9eSStephen Smalley 		}
24965e41ff9eSStephen Smalley 		*value = context;
2497570bc1c2SStephen Smalley 		*len = clen;
2498570bc1c2SStephen Smalley 	}
24995e41ff9eSStephen Smalley 
25005e41ff9eSStephen Smalley 	return 0;
25015e41ff9eSStephen Smalley }
25025e41ff9eSStephen Smalley 
25031da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
25041da177e4SLinus Torvalds {
25051da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
25091da177e4SLinus Torvalds {
25101da177e4SLinus Torvalds 	int rc;
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds 	rc = secondary_ops->inode_link(old_dentry,dir,new_dentry);
25131da177e4SLinus Torvalds 	if (rc)
25141da177e4SLinus Torvalds 		return rc;
25151da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
25191da177e4SLinus Torvalds {
25201da177e4SLinus Torvalds 	int rc;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	rc = secondary_ops->inode_unlink(dir, dentry);
25231da177e4SLinus Torvalds 	if (rc)
25241da177e4SLinus Torvalds 		return rc;
25251da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
25261da177e4SLinus Torvalds }
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
25291da177e4SLinus Torvalds {
25301da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
25341da177e4SLinus Torvalds {
25351da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
25361da177e4SLinus Torvalds }
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
25391da177e4SLinus Torvalds {
25401da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
25441da177e4SLinus Torvalds {
25451da177e4SLinus Torvalds 	int rc;
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	rc = secondary_ops->inode_mknod(dir, dentry, mode, dev);
25481da177e4SLinus Torvalds 	if (rc)
25491da177e4SLinus Torvalds 		return rc;
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
25551da177e4SLinus Torvalds                                 struct inode *new_inode, struct dentry *new_dentry)
25561da177e4SLinus Torvalds {
25571da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
25661da177e4SLinus Torvalds {
25671da177e4SLinus Torvalds 	int rc;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	rc = secondary_ops->inode_follow_link(dentry,nameidata);
25701da177e4SLinus Torvalds 	if (rc)
25711da177e4SLinus Torvalds 		return rc;
25721da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask,
25761da177e4SLinus Torvalds 				    struct nameidata *nd)
25771da177e4SLinus Torvalds {
25781da177e4SLinus Torvalds 	int rc;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	rc = secondary_ops->inode_permission(inode, mask, nd);
25811da177e4SLinus Torvalds 	if (rc)
25821da177e4SLinus Torvalds 		return rc;
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	if (!mask) {
25851da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
25861da177e4SLinus Torvalds 		return 0;
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 	return inode_has_perm(current, inode,
2590b0c636b9SEric Paris 			       open_file_mask_to_av(inode->i_mode, mask), NULL);
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	int rc;
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	rc = secondary_ops->inode_setattr(dentry, iattr);
25981da177e4SLinus Torvalds 	if (rc)
25991da177e4SLinus Torvalds 		return rc;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	if (iattr->ia_valid & ATTR_FORCE)
26021da177e4SLinus Torvalds 		return 0;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
26051da177e4SLinus Torvalds 			       ATTR_ATIME_SET | ATTR_MTIME_SET))
26061da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__WRITE);
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
26121da177e4SLinus Torvalds {
26131da177e4SLinus Torvalds 	return dentry_has_perm(current, mnt, dentry, FILE__GETATTR);
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
2616b5376771SSerge E. Hallyn static int selinux_inode_setotherxattr(struct dentry *dentry, char *name)
2617b5376771SSerge E. Hallyn {
2618b5376771SSerge E. Hallyn 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
2619b5376771SSerge E. Hallyn 		     sizeof XATTR_SECURITY_PREFIX - 1)) {
2620b5376771SSerge E. Hallyn 		if (!strcmp(name, XATTR_NAME_CAPS)) {
2621b5376771SSerge E. Hallyn 			if (!capable(CAP_SETFCAP))
2622b5376771SSerge E. Hallyn 				return -EPERM;
2623b5376771SSerge E. Hallyn 		} else if (!capable(CAP_SYS_ADMIN)) {
2624b5376771SSerge E. Hallyn 			/* A different attribute in the security namespace.
2625b5376771SSerge E. Hallyn 			   Restrict to administrator. */
2626b5376771SSerge E. Hallyn 			return -EPERM;
2627b5376771SSerge E. Hallyn 		}
2628b5376771SSerge E. Hallyn 	}
2629b5376771SSerge E. Hallyn 
2630b5376771SSerge E. Hallyn 	/* Not an attribute we recognize, so just check the
2631b5376771SSerge E. Hallyn 	   ordinary setattr permission. */
2632b5376771SSerge E. Hallyn 	return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
2633b5376771SSerge E. Hallyn }
2634b5376771SSerge E. Hallyn 
26351da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags)
26361da177e4SLinus Torvalds {
26371da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
26381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
26391da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
26401da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
26411da177e4SLinus Torvalds 	struct avc_audit_data ad;
26421da177e4SLinus Torvalds 	u32 newsid;
26431da177e4SLinus Torvalds 	int rc = 0;
26441da177e4SLinus Torvalds 
2645b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2646b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
26491da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
26501da177e4SLinus Torvalds 		return -EOPNOTSUPP;
26511da177e4SLinus Torvalds 
26523bd858abSSatyam Sharma 	if (!is_owner_or_cap(inode))
26531da177e4SLinus Torvalds 		return -EPERM;
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
265644707fdfSJan Blunck 	ad.u.fs.path.dentry = dentry;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass,
26591da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
26601da177e4SLinus Torvalds 	if (rc)
26611da177e4SLinus Torvalds 		return rc;
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
26641da177e4SLinus Torvalds 	if (rc)
26651da177e4SLinus Torvalds 		return rc;
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, isec->sclass,
26681da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
26691da177e4SLinus Torvalds 	if (rc)
26701da177e4SLinus Torvalds 		return rc;
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	rc = security_validate_transition(isec->sid, newsid, tsec->sid,
26731da177e4SLinus Torvalds 	                                  isec->sclass);
26741da177e4SLinus Torvalds 	if (rc)
26751da177e4SLinus Torvalds 		return rc;
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds 	return avc_has_perm(newsid,
26781da177e4SLinus Torvalds 			    sbsec->sid,
26791da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
26801da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
26811da177e4SLinus Torvalds 			    &ad);
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name,
26851da177e4SLinus Torvalds                                         void *value, size_t size, int flags)
26861da177e4SLinus Torvalds {
26871da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
26881da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
26891da177e4SLinus Torvalds 	u32 newsid;
26901da177e4SLinus Torvalds 	int rc;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
26931da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
26941da177e4SLinus Torvalds 		return;
26951da177e4SLinus Torvalds 	}
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
26981da177e4SLinus Torvalds 	if (rc) {
26991da177e4SLinus Torvalds 		printk(KERN_WARNING "%s:  unable to obtain SID for context "
2700dd6f953aSHarvey Harrison 		       "%s, rc=%d\n", __func__, (char *)value, -rc);
27011da177e4SLinus Torvalds 		return;
27021da177e4SLinus Torvalds 	}
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	isec->sid = newsid;
27051da177e4SLinus Torvalds 	return;
27061da177e4SLinus Torvalds }
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name)
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry)
27141da177e4SLinus Torvalds {
27151da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name)
27191da177e4SLinus Torvalds {
2720b5376771SSerge E. Hallyn 	if (strcmp(name, XATTR_NAME_SELINUX))
2721b5376771SSerge E. Hallyn 		return selinux_inode_setotherxattr(dentry, name);
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
27241da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
27251da177e4SLinus Torvalds 	return -EACCES;
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
2728d381d8a9SJames Morris /*
2729d381d8a9SJames Morris  * Copy the in-core inode security context value to the user.  If the
2730d381d8a9SJames Morris  * getxattr() prior to this succeeded, check to see if we need to
2731d381d8a9SJames Morris  * canonicalize the value to be finally returned to the user.
2732d381d8a9SJames Morris  *
2733d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2734d381d8a9SJames Morris  */
273542492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
27361da177e4SLinus Torvalds {
273742492594SDavid P. Quigley 	u32 size;
273842492594SDavid P. Quigley 	int error;
273942492594SDavid P. Quigley 	char *context = NULL;
27401da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27411da177e4SLinus Torvalds 
27428c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
27438c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
27441da177e4SLinus Torvalds 
274542492594SDavid P. Quigley 	error = security_sid_to_context(isec->sid, &context, &size);
274642492594SDavid P. Quigley 	if (error)
274742492594SDavid P. Quigley 		return error;
274842492594SDavid P. Quigley 	error = size;
274942492594SDavid P. Quigley 	if (alloc) {
275042492594SDavid P. Quigley 		*buffer = context;
275142492594SDavid P. Quigley 		goto out_nofree;
275242492594SDavid P. Quigley 	}
275342492594SDavid P. Quigley 	kfree(context);
275442492594SDavid P. Quigley out_nofree:
275542492594SDavid P. Quigley 	return error;
27561da177e4SLinus Torvalds }
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
27591da177e4SLinus Torvalds                                      const void *value, size_t size, int flags)
27601da177e4SLinus Torvalds {
27611da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27621da177e4SLinus Torvalds 	u32 newsid;
27631da177e4SLinus Torvalds 	int rc;
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
27661da177e4SLinus Torvalds 		return -EOPNOTSUPP;
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	if (!value || !size)
27691da177e4SLinus Torvalds 		return -EACCES;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	rc = security_context_to_sid((void*)value, size, &newsid);
27721da177e4SLinus Torvalds 	if (rc)
27731da177e4SLinus Torvalds 		return rc;
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds 	isec->sid = newsid;
27761da177e4SLinus Torvalds 	return 0;
27771da177e4SLinus Torvalds }
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
27801da177e4SLinus Torvalds {
27811da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
27821da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
27831da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
27841da177e4SLinus Torvalds 	return len;
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
2787b5376771SSerge E. Hallyn static int selinux_inode_need_killpriv(struct dentry *dentry)
2788b5376771SSerge E. Hallyn {
2789b5376771SSerge E. Hallyn 	return secondary_ops->inode_need_killpriv(dentry);
2790b5376771SSerge E. Hallyn }
2791b5376771SSerge E. Hallyn 
2792b5376771SSerge E. Hallyn static int selinux_inode_killpriv(struct dentry *dentry)
2793b5376771SSerge E. Hallyn {
2794b5376771SSerge E. Hallyn 	return secondary_ops->inode_killpriv(dentry);
2795b5376771SSerge E. Hallyn }
2796b5376771SSerge E. Hallyn 
2797713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2798713a04aeSAhmed S. Darwish {
2799713a04aeSAhmed S. Darwish 	struct inode_security_struct *isec = inode->i_security;
2800713a04aeSAhmed S. Darwish 	*secid = isec->sid;
2801713a04aeSAhmed S. Darwish }
2802713a04aeSAhmed S. Darwish 
28031da177e4SLinus Torvalds /* file security operations */
28041da177e4SLinus Torvalds 
2805788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask)
28061da177e4SLinus Torvalds {
28077420ed23SVenkat Yekkirala 	int rc;
28083d5ff529SJosef Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	if (!mask) {
28111da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
28121da177e4SLinus Torvalds 		return 0;
28131da177e4SLinus Torvalds 	}
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
28161da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
28171da177e4SLinus Torvalds 		mask |= MAY_APPEND;
28181da177e4SLinus Torvalds 
28197420ed23SVenkat Yekkirala 	rc = file_has_perm(current, file,
28201da177e4SLinus Torvalds 			   file_mask_to_av(inode->i_mode, mask));
28217420ed23SVenkat Yekkirala 	if (rc)
28227420ed23SVenkat Yekkirala 		return rc;
28237420ed23SVenkat Yekkirala 
28247420ed23SVenkat Yekkirala 	return selinux_netlbl_inode_permission(inode, mask);
28251da177e4SLinus Torvalds }
28261da177e4SLinus Torvalds 
2827788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask)
2828788e7dd4SYuichi Nakamura {
2829788e7dd4SYuichi Nakamura 	struct inode *inode = file->f_path.dentry->d_inode;
2830788e7dd4SYuichi Nakamura 	struct task_security_struct *tsec = current->security;
2831788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec = file->f_security;
2832788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec = inode->i_security;
2833788e7dd4SYuichi Nakamura 
2834788e7dd4SYuichi Nakamura 	if (!mask) {
2835788e7dd4SYuichi Nakamura 		/* No permission to check.  Existence test. */
2836788e7dd4SYuichi Nakamura 		return 0;
2837788e7dd4SYuichi Nakamura 	}
2838788e7dd4SYuichi Nakamura 
2839788e7dd4SYuichi Nakamura 	if (tsec->sid == fsec->sid && fsec->isid == isec->sid
2840788e7dd4SYuichi Nakamura 	    && fsec->pseqno == avc_policy_seqno())
2841788e7dd4SYuichi Nakamura 		return selinux_netlbl_inode_permission(inode, mask);
2842788e7dd4SYuichi Nakamura 
2843788e7dd4SYuichi Nakamura 	return selinux_revalidate_file_permission(file, mask);
2844788e7dd4SYuichi Nakamura }
2845788e7dd4SYuichi Nakamura 
28461da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
28471da177e4SLinus Torvalds {
28481da177e4SLinus Torvalds 	return file_alloc_security(file);
28491da177e4SLinus Torvalds }
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
28521da177e4SLinus Torvalds {
28531da177e4SLinus Torvalds 	file_free_security(file);
28541da177e4SLinus Torvalds }
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
28571da177e4SLinus Torvalds 			      unsigned long arg)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	int error = 0;
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 	switch (cmd) {
28621da177e4SLinus Torvalds 		case FIONREAD:
28631da177e4SLinus Torvalds 		/* fall through */
28641da177e4SLinus Torvalds 		case FIBMAP:
28651da177e4SLinus Torvalds 		/* fall through */
28661da177e4SLinus Torvalds 		case FIGETBSZ:
28671da177e4SLinus Torvalds 		/* fall through */
28681da177e4SLinus Torvalds 		case EXT2_IOC_GETFLAGS:
28691da177e4SLinus Torvalds 		/* fall through */
28701da177e4SLinus Torvalds 		case EXT2_IOC_GETVERSION:
28711da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__GETATTR);
28721da177e4SLinus Torvalds 			break;
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 		case EXT2_IOC_SETFLAGS:
28751da177e4SLinus Torvalds 		/* fall through */
28761da177e4SLinus Torvalds 		case EXT2_IOC_SETVERSION:
28771da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__SETATTR);
28781da177e4SLinus Torvalds 			break;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 		/* sys_ioctl() checks */
28811da177e4SLinus Torvalds 		case FIONBIO:
28821da177e4SLinus Torvalds 		/* fall through */
28831da177e4SLinus Torvalds 		case FIOASYNC:
28841da177e4SLinus Torvalds 			error = file_has_perm(current, file, 0);
28851da177e4SLinus Torvalds 			break;
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	        case KDSKBENT:
28881da177e4SLinus Torvalds 	        case KDSKBSENT:
28891da177e4SLinus Torvalds 			error = task_has_capability(current,CAP_SYS_TTY_CONFIG);
28901da177e4SLinus Torvalds 			break;
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 		/* default case assumes that the command will go
28931da177e4SLinus Torvalds 		 * to the file's ioctl() function.
28941da177e4SLinus Torvalds 		 */
28951da177e4SLinus Torvalds 		default:
28961da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__IOCTL);
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	}
28991da177e4SLinus Torvalds 	return error;
29001da177e4SLinus Torvalds }
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
29031da177e4SLinus Torvalds {
29041da177e4SLinus Torvalds #ifndef CONFIG_PPC32
29051da177e4SLinus Torvalds 	if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
29061da177e4SLinus Torvalds 		/*
29071da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
29081da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
29091da177e4SLinus Torvalds 		 * This has an additional check.
29101da177e4SLinus Torvalds 		 */
29111da177e4SLinus Torvalds 		int rc = task_has_perm(current, current, PROCESS__EXECMEM);
29121da177e4SLinus Torvalds 		if (rc)
29131da177e4SLinus Torvalds 			return rc;
29141da177e4SLinus Torvalds 	}
29151da177e4SLinus Torvalds #endif
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 	if (file) {
29181da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
29191da177e4SLinus Torvalds 		u32 av = FILE__READ;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
29221da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
29231da177e4SLinus Torvalds 			av |= FILE__WRITE;
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
29261da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
29271da177e4SLinus Torvalds 
29281da177e4SLinus Torvalds 		return file_has_perm(current, file, av);
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds 	return 0;
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot,
2934ed032189SEric Paris 			     unsigned long prot, unsigned long flags,
2935ed032189SEric Paris 			     unsigned long addr, unsigned long addr_only)
29361da177e4SLinus Torvalds {
2937ed032189SEric Paris 	int rc = 0;
2938ed032189SEric Paris 	u32 sid = ((struct task_security_struct*)(current->security))->sid;
29391da177e4SLinus Torvalds 
2940ed032189SEric Paris 	if (addr < mmap_min_addr)
2941ed032189SEric Paris 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
2942ed032189SEric Paris 				  MEMPROTECT__MMAP_ZERO, NULL);
2943ed032189SEric Paris 	if (rc || addr_only)
29441da177e4SLinus Torvalds 		return rc;
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 	if (selinux_checkreqprot)
29471da177e4SLinus Torvalds 		prot = reqprot;
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
29501da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
29541da177e4SLinus Torvalds 				 unsigned long reqprot,
29551da177e4SLinus Torvalds 				 unsigned long prot)
29561da177e4SLinus Torvalds {
29571da177e4SLinus Torvalds 	int rc;
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds 	rc = secondary_ops->file_mprotect(vma, reqprot, prot);
29601da177e4SLinus Torvalds 	if (rc)
29611da177e4SLinus Torvalds 		return rc;
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	if (selinux_checkreqprot)
29641da177e4SLinus Torvalds 		prot = reqprot;
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds #ifndef CONFIG_PPC32
2967db4c9641SStephen Smalley 	if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
2968db4c9641SStephen Smalley 		rc = 0;
2969db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
2970db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
2971db4c9641SStephen Smalley 			rc = task_has_perm(current, current,
2972db4c9641SStephen Smalley 					   PROCESS__EXECHEAP);
2973db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
29746b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
29756b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
29766b992197SLorenzo Hernandez García-Hierro 			rc = task_has_perm(current, current, PROCESS__EXECSTACK);
2977db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
2978db4c9641SStephen Smalley 			/*
2979db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
2980db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
2981db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
2982db4c9641SStephen Smalley 			 * modified content.  This typically should only
2983db4c9641SStephen Smalley 			 * occur for text relocations.
2984db4c9641SStephen Smalley 			 */
2985db4c9641SStephen Smalley 			rc = file_has_perm(current, vma->vm_file,
2986db4c9641SStephen Smalley 					   FILE__EXECMOD);
2987db4c9641SStephen Smalley 		}
29886b992197SLorenzo Hernandez García-Hierro 		if (rc)
29896b992197SLorenzo Hernandez García-Hierro 			return rc;
29906b992197SLorenzo Hernandez García-Hierro 	}
29911da177e4SLinus Torvalds #endif
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
29971da177e4SLinus Torvalds {
29981da177e4SLinus Torvalds 	return file_has_perm(current, file, FILE__LOCK);
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
30021da177e4SLinus Torvalds 			      unsigned long arg)
30031da177e4SLinus Torvalds {
30041da177e4SLinus Torvalds 	int err = 0;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	switch (cmd) {
30071da177e4SLinus Torvalds 	        case F_SETFL:
30083d5ff529SJosef Sipek 			if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
30091da177e4SLinus Torvalds 				err = -EINVAL;
30101da177e4SLinus Torvalds 				break;
30111da177e4SLinus Torvalds 			}
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 			if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
30141da177e4SLinus Torvalds 				err = file_has_perm(current, file,FILE__WRITE);
30151da177e4SLinus Torvalds 				break;
30161da177e4SLinus Torvalds 			}
30171da177e4SLinus Torvalds 			/* fall through */
30181da177e4SLinus Torvalds 	        case F_SETOWN:
30191da177e4SLinus Torvalds 	        case F_SETSIG:
30201da177e4SLinus Torvalds 	        case F_GETFL:
30211da177e4SLinus Torvalds 	        case F_GETOWN:
30221da177e4SLinus Torvalds 	        case F_GETSIG:
30231da177e4SLinus Torvalds 			/* Just check FD__USE permission */
30241da177e4SLinus Torvalds 			err = file_has_perm(current, file, 0);
30251da177e4SLinus Torvalds 			break;
30261da177e4SLinus Torvalds 		case F_GETLK:
30271da177e4SLinus Torvalds 		case F_SETLK:
30281da177e4SLinus Torvalds 	        case F_SETLKW:
30291da177e4SLinus Torvalds #if BITS_PER_LONG == 32
30301da177e4SLinus Torvalds 	        case F_GETLK64:
30311da177e4SLinus Torvalds 		case F_SETLK64:
30321da177e4SLinus Torvalds 	        case F_SETLKW64:
30331da177e4SLinus Torvalds #endif
30343d5ff529SJosef Sipek 			if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
30351da177e4SLinus Torvalds 				err = -EINVAL;
30361da177e4SLinus Torvalds 				break;
30371da177e4SLinus Torvalds 			}
30381da177e4SLinus Torvalds 			err = file_has_perm(current, file, FILE__LOCK);
30391da177e4SLinus Torvalds 			break;
30401da177e4SLinus Torvalds 	}
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 	return err;
30431da177e4SLinus Torvalds }
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
30461da177e4SLinus Torvalds {
30471da177e4SLinus Torvalds 	struct task_security_struct *tsec;
30481da177e4SLinus Torvalds 	struct file_security_struct *fsec;
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds 	tsec = current->security;
30511da177e4SLinus Torvalds 	fsec = file->f_security;
30521da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	return 0;
30551da177e4SLinus Torvalds }
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
30581da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
30591da177e4SLinus Torvalds {
30601da177e4SLinus Torvalds         struct file *file;
30611da177e4SLinus Torvalds 	u32 perm;
30621da177e4SLinus Torvalds 	struct task_security_struct *tsec;
30631da177e4SLinus Torvalds 	struct file_security_struct *fsec;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
3066b385a144SRobert P. J. Day         file = container_of(fown, struct file, f_owner);
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 	tsec = tsk->security;
30691da177e4SLinus Torvalds 	fsec = file->f_security;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	if (!signum)
30721da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
30731da177e4SLinus Torvalds 	else
30741da177e4SLinus Torvalds 		perm = signal_to_av(signum);
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 	return avc_has_perm(fsec->fown_sid, tsec->sid,
30771da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
30811da177e4SLinus Torvalds {
30821da177e4SLinus Torvalds 	return file_has_perm(current, file, file_to_av(file));
30831da177e4SLinus Torvalds }
30841da177e4SLinus Torvalds 
3085788e7dd4SYuichi Nakamura static int selinux_dentry_open(struct file *file)
3086788e7dd4SYuichi Nakamura {
3087788e7dd4SYuichi Nakamura 	struct file_security_struct *fsec;
3088788e7dd4SYuichi Nakamura 	struct inode *inode;
3089788e7dd4SYuichi Nakamura 	struct inode_security_struct *isec;
3090788e7dd4SYuichi Nakamura 	inode = file->f_path.dentry->d_inode;
3091788e7dd4SYuichi Nakamura 	fsec = file->f_security;
3092788e7dd4SYuichi Nakamura 	isec = inode->i_security;
3093788e7dd4SYuichi Nakamura 	/*
3094788e7dd4SYuichi Nakamura 	 * Save inode label and policy sequence number
3095788e7dd4SYuichi Nakamura 	 * at open-time so that selinux_file_permission
3096788e7dd4SYuichi Nakamura 	 * can determine whether revalidation is necessary.
3097788e7dd4SYuichi Nakamura 	 * Task label is already saved in the file security
3098788e7dd4SYuichi Nakamura 	 * struct as its SID.
3099788e7dd4SYuichi Nakamura 	 */
3100788e7dd4SYuichi Nakamura 	fsec->isid = isec->sid;
3101788e7dd4SYuichi Nakamura 	fsec->pseqno = avc_policy_seqno();
3102788e7dd4SYuichi Nakamura 	/*
3103788e7dd4SYuichi Nakamura 	 * Since the inode label or policy seqno may have changed
3104788e7dd4SYuichi Nakamura 	 * between the selinux_inode_permission check and the saving
3105788e7dd4SYuichi Nakamura 	 * of state above, recheck that access is still permitted.
3106788e7dd4SYuichi Nakamura 	 * Otherwise, access might never be revalidated against the
3107788e7dd4SYuichi Nakamura 	 * new inode label or new policy.
3108788e7dd4SYuichi Nakamura 	 * This check is not redundant - do not remove.
3109788e7dd4SYuichi Nakamura 	 */
3110788e7dd4SYuichi Nakamura 	return inode_has_perm(current, inode, file_to_av(file), NULL);
3111788e7dd4SYuichi Nakamura }
3112788e7dd4SYuichi Nakamura 
31131da177e4SLinus Torvalds /* task security operations */
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
31161da177e4SLinus Torvalds {
31171da177e4SLinus Torvalds 	int rc;
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds 	rc = secondary_ops->task_create(clone_flags);
31201da177e4SLinus Torvalds 	if (rc)
31211da177e4SLinus Torvalds 		return rc;
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds 	return task_has_perm(current, current, PROCESS__FORK);
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk)
31271da177e4SLinus Torvalds {
31281da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
31291da177e4SLinus Torvalds 	int rc;
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 	tsec1 = current->security;
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 	rc = task_alloc_security(tsk);
31341da177e4SLinus Torvalds 	if (rc)
31351da177e4SLinus Torvalds 		return rc;
31361da177e4SLinus Torvalds 	tsec2 = tsk->security;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 	tsec2->osid = tsec1->osid;
31391da177e4SLinus Torvalds 	tsec2->sid = tsec1->sid;
31401da177e4SLinus Torvalds 
314128eba5bfSMichael LeMay 	/* Retain the exec, fs, key, and sock SIDs across fork */
31421da177e4SLinus Torvalds 	tsec2->exec_sid = tsec1->exec_sid;
31431da177e4SLinus Torvalds 	tsec2->create_sid = tsec1->create_sid;
314428eba5bfSMichael LeMay 	tsec2->keycreate_sid = tsec1->keycreate_sid;
314542c3e03eSEric Paris 	tsec2->sockcreate_sid = tsec1->sockcreate_sid;
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	return 0;
31481da177e4SLinus Torvalds }
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk)
31511da177e4SLinus Torvalds {
31521da177e4SLinus Torvalds 	task_free_security(tsk);
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
31561da177e4SLinus Torvalds {
31571da177e4SLinus Torvalds 	/* Since setuid only affects the current process, and
31581da177e4SLinus Torvalds 	   since the SELinux controls are not based on the Linux
31591da177e4SLinus Torvalds 	   identity attributes, SELinux does not need to control
31601da177e4SLinus Torvalds 	   this operation.  However, SELinux does control the use
31611da177e4SLinus Torvalds 	   of the CAP_SETUID and CAP_SETGID capabilities using the
31621da177e4SLinus Torvalds 	   capable hook. */
31631da177e4SLinus Torvalds 	return 0;
31641da177e4SLinus Torvalds }
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
31671da177e4SLinus Torvalds {
31681da177e4SLinus Torvalds 	return secondary_ops->task_post_setuid(id0,id1,id2,flags);
31691da177e4SLinus Torvalds }
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
31721da177e4SLinus Torvalds {
31731da177e4SLinus Torvalds 	/* See the comment for setuid above. */
31741da177e4SLinus Torvalds 	return 0;
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
31781da177e4SLinus Torvalds {
31791da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETPGID);
31801da177e4SLinus Torvalds }
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
31831da177e4SLinus Torvalds {
31841da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETPGID);
31851da177e4SLinus Torvalds }
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
31881da177e4SLinus Torvalds {
31891da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSESSION);
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
3192f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3193f9008e4cSDavid Quigley {
3194713a04aeSAhmed S. Darwish 	struct task_security_struct *tsec = p->security;
3195713a04aeSAhmed S. Darwish 	*secid = tsec->sid;
3196f9008e4cSDavid Quigley }
3197f9008e4cSDavid Quigley 
31981da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info)
31991da177e4SLinus Torvalds {
32001da177e4SLinus Torvalds 	/* See the comment for setuid above. */
32011da177e4SLinus Torvalds 	return 0;
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
32051da177e4SLinus Torvalds {
32061da177e4SLinus Torvalds 	int rc;
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	rc = secondary_ops->task_setnice(p, nice);
32091da177e4SLinus Torvalds 	if (rc)
32101da177e4SLinus Torvalds 		return rc;
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds 	return task_has_perm(current,p, PROCESS__SETSCHED);
32131da177e4SLinus Torvalds }
32141da177e4SLinus Torvalds 
321503e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
321603e68060SJames Morris {
3217b5376771SSerge E. Hallyn 	int rc;
3218b5376771SSerge E. Hallyn 
3219b5376771SSerge E. Hallyn 	rc = secondary_ops->task_setioprio(p, ioprio);
3220b5376771SSerge E. Hallyn 	if (rc)
3221b5376771SSerge E. Hallyn 		return rc;
3222b5376771SSerge E. Hallyn 
322303e68060SJames Morris 	return task_has_perm(current, p, PROCESS__SETSCHED);
322403e68060SJames Morris }
322503e68060SJames Morris 
3226a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
3227a1836a42SDavid Quigley {
3228a1836a42SDavid Quigley 	return task_has_perm(current, p, PROCESS__GETSCHED);
3229a1836a42SDavid Quigley }
3230a1836a42SDavid Quigley 
32311da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	struct rlimit *old_rlim = current->signal->rlim + resource;
32341da177e4SLinus Torvalds 	int rc;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	rc = secondary_ops->task_setrlimit(resource, new_rlim);
32371da177e4SLinus Torvalds 	if (rc)
32381da177e4SLinus Torvalds 		return rc;
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
32411da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
32421da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
32431da177e4SLinus Torvalds 	   upon context transitions. See selinux_bprm_apply_creds. */
32441da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
32451da177e4SLinus Torvalds 		return task_has_perm(current, current, PROCESS__SETRLIMIT);
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 	return 0;
32481da177e4SLinus Torvalds }
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
32511da177e4SLinus Torvalds {
3252b5376771SSerge E. Hallyn 	int rc;
3253b5376771SSerge E. Hallyn 
3254b5376771SSerge E. Hallyn 	rc = secondary_ops->task_setscheduler(p, policy, lp);
3255b5376771SSerge E. Hallyn 	if (rc)
3256b5376771SSerge E. Hallyn 		return rc;
3257b5376771SSerge E. Hallyn 
32581da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETSCHED);
32591da177e4SLinus Torvalds }
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
32621da177e4SLinus Torvalds {
32631da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSCHED);
32641da177e4SLinus Torvalds }
32651da177e4SLinus Torvalds 
326635601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
326735601547SDavid Quigley {
326835601547SDavid Quigley 	return task_has_perm(current, p, PROCESS__SETSCHED);
326935601547SDavid Quigley }
327035601547SDavid Quigley 
3271f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3272f9008e4cSDavid Quigley 				int sig, u32 secid)
32731da177e4SLinus Torvalds {
32741da177e4SLinus Torvalds 	u32 perm;
32751da177e4SLinus Torvalds 	int rc;
3276f9008e4cSDavid Quigley 	struct task_security_struct *tsec;
32771da177e4SLinus Torvalds 
3278f9008e4cSDavid Quigley 	rc = secondary_ops->task_kill(p, info, sig, secid);
32791da177e4SLinus Torvalds 	if (rc)
32801da177e4SLinus Torvalds 		return rc;
32811da177e4SLinus Torvalds 
3282621d3121SOleg Nesterov 	if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
32831da177e4SLinus Torvalds 		return 0;
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds 	if (!sig)
32861da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
32871da177e4SLinus Torvalds 	else
32881da177e4SLinus Torvalds 		perm = signal_to_av(sig);
3289f9008e4cSDavid Quigley 	tsec = p->security;
3290f9008e4cSDavid Quigley 	if (secid)
3291f9008e4cSDavid Quigley 		rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL);
3292f9008e4cSDavid Quigley 	else
3293f9008e4cSDavid Quigley 		rc = task_has_perm(current, p, perm);
3294f9008e4cSDavid Quigley 	return rc;
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds static int selinux_task_prctl(int option,
32981da177e4SLinus Torvalds 			      unsigned long arg2,
32991da177e4SLinus Torvalds 			      unsigned long arg3,
33001da177e4SLinus Torvalds 			      unsigned long arg4,
33011da177e4SLinus Torvalds 			      unsigned long arg5)
33021da177e4SLinus Torvalds {
33031da177e4SLinus Torvalds 	/* The current prctl operations do not appear to require
33041da177e4SLinus Torvalds 	   any SELinux controls since they merely observe or modify
33051da177e4SLinus Torvalds 	   the state of the current process. */
33061da177e4SLinus Torvalds 	return 0;
33071da177e4SLinus Torvalds }
33081da177e4SLinus Torvalds 
33091da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
33101da177e4SLinus Torvalds {
33118a535140SEric Paris 	return task_has_perm(p, current, PROCESS__SIGCHLD);
33121da177e4SLinus Torvalds }
33131da177e4SLinus Torvalds 
33141da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p)
33151da177e4SLinus Torvalds {
33161da177e4SLinus Torvalds   	struct task_security_struct *tsec;
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 	secondary_ops->task_reparent_to_init(p);
33191da177e4SLinus Torvalds 
33201da177e4SLinus Torvalds 	tsec = p->security;
33211da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
33221da177e4SLinus Torvalds 	tsec->sid = SECINITSID_KERNEL;
33231da177e4SLinus Torvalds 	return;
33241da177e4SLinus Torvalds }
33251da177e4SLinus Torvalds 
33261da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
33271da177e4SLinus Torvalds 				  struct inode *inode)
33281da177e4SLinus Torvalds {
33291da177e4SLinus Torvalds 	struct task_security_struct *tsec = p->security;
33301da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
33311da177e4SLinus Torvalds 
33321da177e4SLinus Torvalds 	isec->sid = tsec->sid;
33331da177e4SLinus Torvalds 	isec->initialized = 1;
33341da177e4SLinus Torvalds 	return;
33351da177e4SLinus Torvalds }
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
333867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
333967f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
33401da177e4SLinus Torvalds {
33411da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
33421da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
33431da177e4SLinus Torvalds 
3344bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
33451da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
33461da177e4SLinus Torvalds 	if (ih == NULL)
33471da177e4SLinus Torvalds 		goto out;
33481da177e4SLinus Torvalds 
33491da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
33501da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
33511da177e4SLinus Torvalds 		goto out;
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds 	ad->u.net.v4info.saddr = ih->saddr;
33541da177e4SLinus Torvalds 	ad->u.net.v4info.daddr = ih->daddr;
33551da177e4SLinus Torvalds 	ret = 0;
33561da177e4SLinus Torvalds 
335767f83cbfSVenkat Yekkirala 	if (proto)
335867f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
335967f83cbfSVenkat Yekkirala 
33601da177e4SLinus Torvalds 	switch (ih->protocol) {
33611da177e4SLinus Torvalds         case IPPROTO_TCP: {
33621da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
33631da177e4SLinus Torvalds 
33641da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
33651da177e4SLinus Torvalds         		break;
33661da177e4SLinus Torvalds 
33671da177e4SLinus Torvalds 		offset += ihlen;
33681da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
33691da177e4SLinus Torvalds 		if (th == NULL)
33701da177e4SLinus Torvalds 			break;
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
33731da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
33741da177e4SLinus Torvalds 		break;
33751da177e4SLinus Torvalds         }
33761da177e4SLinus Torvalds 
33771da177e4SLinus Torvalds         case IPPROTO_UDP: {
33781da177e4SLinus Torvalds         	struct udphdr _udph, *uh;
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
33811da177e4SLinus Torvalds         		break;
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds 		offset += ihlen;
33841da177e4SLinus Torvalds         	uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
33851da177e4SLinus Torvalds 		if (uh == NULL)
33861da177e4SLinus Torvalds 			break;
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds         	ad->u.net.sport = uh->source;
33891da177e4SLinus Torvalds         	ad->u.net.dport = uh->dest;
33901da177e4SLinus Torvalds         	break;
33911da177e4SLinus Torvalds         }
33921da177e4SLinus Torvalds 
33932ee92d46SJames Morris 	case IPPROTO_DCCP: {
33942ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
33952ee92d46SJames Morris 
33962ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
33972ee92d46SJames Morris 			break;
33982ee92d46SJames Morris 
33992ee92d46SJames Morris 		offset += ihlen;
34002ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
34012ee92d46SJames Morris 		if (dh == NULL)
34022ee92d46SJames Morris 			break;
34032ee92d46SJames Morris 
34042ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
34052ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
34062ee92d46SJames Morris 		break;
34072ee92d46SJames Morris         }
34082ee92d46SJames Morris 
34091da177e4SLinus Torvalds         default:
34101da177e4SLinus Torvalds         	break;
34111da177e4SLinus Torvalds         }
34121da177e4SLinus Torvalds out:
34131da177e4SLinus Torvalds 	return ret;
34141da177e4SLinus Torvalds }
34151da177e4SLinus Torvalds 
34161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
341967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
342067f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
34211da177e4SLinus Torvalds {
34221da177e4SLinus Torvalds 	u8 nexthdr;
34231da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
34241da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
34251da177e4SLinus Torvalds 
3426bbe735e4SArnaldo Carvalho de Melo 	offset = skb_network_offset(skb);
34271da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
34281da177e4SLinus Torvalds 	if (ip6 == NULL)
34291da177e4SLinus Torvalds 		goto out;
34301da177e4SLinus Torvalds 
34311da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
34321da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
34331da177e4SLinus Torvalds 	ret = 0;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
34361da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
34370d3d077cSHerbert Xu 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
34381da177e4SLinus Torvalds 	if (offset < 0)
34391da177e4SLinus Torvalds 		goto out;
34401da177e4SLinus Torvalds 
344167f83cbfSVenkat Yekkirala 	if (proto)
344267f83cbfSVenkat Yekkirala 		*proto = nexthdr;
344367f83cbfSVenkat Yekkirala 
34441da177e4SLinus Torvalds 	switch (nexthdr) {
34451da177e4SLinus Torvalds 	case IPPROTO_TCP: {
34461da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
34471da177e4SLinus Torvalds 
34481da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
34491da177e4SLinus Torvalds 		if (th == NULL)
34501da177e4SLinus Torvalds 			break;
34511da177e4SLinus Torvalds 
34521da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
34531da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
34541da177e4SLinus Torvalds 		break;
34551da177e4SLinus Torvalds 	}
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds 	case IPPROTO_UDP: {
34581da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
34611da177e4SLinus Torvalds 		if (uh == NULL)
34621da177e4SLinus Torvalds 			break;
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 		ad->u.net.sport = uh->source;
34651da177e4SLinus Torvalds 		ad->u.net.dport = uh->dest;
34661da177e4SLinus Torvalds 		break;
34671da177e4SLinus Torvalds 	}
34681da177e4SLinus Torvalds 
34692ee92d46SJames Morris 	case IPPROTO_DCCP: {
34702ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
34712ee92d46SJames Morris 
34722ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
34732ee92d46SJames Morris 		if (dh == NULL)
34742ee92d46SJames Morris 			break;
34752ee92d46SJames Morris 
34762ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
34772ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
34782ee92d46SJames Morris 		break;
34792ee92d46SJames Morris         }
34802ee92d46SJames Morris 
34811da177e4SLinus Torvalds 	/* includes fragments */
34821da177e4SLinus Torvalds 	default:
34831da177e4SLinus Torvalds 		break;
34841da177e4SLinus Torvalds 	}
34851da177e4SLinus Torvalds out:
34861da177e4SLinus Torvalds 	return ret;
34871da177e4SLinus Torvalds }
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds #endif /* IPV6 */
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
3492224dfbd8SPaul Moore 			     char **addrp, int src, u8 *proto)
34931da177e4SLinus Torvalds {
34941da177e4SLinus Torvalds 	int ret = 0;
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 	switch (ad->u.net.family) {
34971da177e4SLinus Torvalds 	case PF_INET:
349867f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
34991da177e4SLinus Torvalds 		if (ret || !addrp)
35001da177e4SLinus Torvalds 			break;
35011da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v4info.saddr :
35021da177e4SLinus Torvalds 					&ad->u.net.v4info.daddr);
35031da177e4SLinus Torvalds 		break;
35041da177e4SLinus Torvalds 
35051da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
35061da177e4SLinus Torvalds 	case PF_INET6:
350767f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
35081da177e4SLinus Torvalds 		if (ret || !addrp)
35091da177e4SLinus Torvalds 			break;
35101da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v6info.saddr :
35111da177e4SLinus Torvalds 					&ad->u.net.v6info.daddr);
35121da177e4SLinus Torvalds 		break;
35131da177e4SLinus Torvalds #endif	/* IPV6 */
35141da177e4SLinus Torvalds 	default:
35151da177e4SLinus Torvalds 		break;
35161da177e4SLinus Torvalds 	}
35171da177e4SLinus Torvalds 
351871f1cb05SPaul Moore 	if (unlikely(ret))
351971f1cb05SPaul Moore 		printk(KERN_WARNING
352071f1cb05SPaul Moore 		       "SELinux: failure in selinux_parse_skb(),"
352171f1cb05SPaul Moore 		       " unable to parse packet\n");
352271f1cb05SPaul Moore 
35231da177e4SLinus Torvalds 	return ret;
35241da177e4SLinus Torvalds }
35251da177e4SLinus Torvalds 
35264f6a993fSPaul Moore /**
3527220deb96SPaul Moore  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
35284f6a993fSPaul Moore  * @skb: the packet
352975e22910SPaul Moore  * @family: protocol family
3530220deb96SPaul Moore  * @sid: the packet's peer label SID
35314f6a993fSPaul Moore  *
35324f6a993fSPaul Moore  * Description:
3533220deb96SPaul Moore  * Check the various different forms of network peer labeling and determine
3534220deb96SPaul Moore  * the peer label/SID for the packet; most of the magic actually occurs in
3535220deb96SPaul Moore  * the security server function security_net_peersid_cmp().  The function
3536220deb96SPaul Moore  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3537220deb96SPaul Moore  * or -EACCES if @sid is invalid due to inconsistencies with the different
3538220deb96SPaul Moore  * peer labels.
35394f6a993fSPaul Moore  *
35404f6a993fSPaul Moore  */
3541220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
35424f6a993fSPaul Moore {
354371f1cb05SPaul Moore 	int err;
35444f6a993fSPaul Moore 	u32 xfrm_sid;
35454f6a993fSPaul Moore 	u32 nlbl_sid;
3546220deb96SPaul Moore 	u32 nlbl_type;
35474f6a993fSPaul Moore 
35484f6a993fSPaul Moore 	selinux_skb_xfrm_sid(skb, &xfrm_sid);
35495dbe1eb0SPaul Moore 	selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3550220deb96SPaul Moore 
355171f1cb05SPaul Moore 	err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
355271f1cb05SPaul Moore 	if (unlikely(err)) {
355371f1cb05SPaul Moore 		printk(KERN_WARNING
355471f1cb05SPaul Moore 		       "SELinux: failure in selinux_skb_peerlbl_sid(),"
355571f1cb05SPaul Moore 		       " unable to determine packet's peer label\n");
3556220deb96SPaul Moore 		return -EACCES;
355771f1cb05SPaul Moore 	}
3558220deb96SPaul Moore 
3559220deb96SPaul Moore 	return 0;
35604f6a993fSPaul Moore }
35614f6a993fSPaul Moore 
35621da177e4SLinus Torvalds /* socket security operations */
35631da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock,
35641da177e4SLinus Torvalds 			   u32 perms)
35651da177e4SLinus Torvalds {
35661da177e4SLinus Torvalds 	struct inode_security_struct *isec;
35671da177e4SLinus Torvalds 	struct task_security_struct *tsec;
35681da177e4SLinus Torvalds 	struct avc_audit_data ad;
35691da177e4SLinus Torvalds 	int err = 0;
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds 	tsec = task->security;
35721da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
35731da177e4SLinus Torvalds 
35741da177e4SLinus Torvalds 	if (isec->sid == SECINITSID_KERNEL)
35751da177e4SLinus Torvalds 		goto out;
35761da177e4SLinus Torvalds 
35771da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
35781da177e4SLinus Torvalds 	ad.u.net.sk = sock->sk;
35791da177e4SLinus Torvalds 	err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds out:
35821da177e4SLinus Torvalds 	return err;
35831da177e4SLinus Torvalds }
35841da177e4SLinus Torvalds 
35851da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
35861da177e4SLinus Torvalds 				 int protocol, int kern)
35871da177e4SLinus Torvalds {
35881da177e4SLinus Torvalds 	int err = 0;
35891da177e4SLinus Torvalds 	struct task_security_struct *tsec;
359042c3e03eSEric Paris 	u32 newsid;
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds 	if (kern)
35931da177e4SLinus Torvalds 		goto out;
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 	tsec = current->security;
359642c3e03eSEric Paris 	newsid = tsec->sockcreate_sid ? : tsec->sid;
359742c3e03eSEric Paris 	err = avc_has_perm(tsec->sid, newsid,
35981da177e4SLinus Torvalds 			   socket_type_to_security_class(family, type,
35991da177e4SLinus Torvalds 			   protocol), SOCKET__CREATE, NULL);
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds out:
36021da177e4SLinus Torvalds 	return err;
36031da177e4SLinus Torvalds }
36041da177e4SLinus Torvalds 
36057420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
36061da177e4SLinus Torvalds 				      int type, int protocol, int kern)
36071da177e4SLinus Torvalds {
36087420ed23SVenkat Yekkirala 	int err = 0;
36091da177e4SLinus Torvalds 	struct inode_security_struct *isec;
36101da177e4SLinus Torvalds 	struct task_security_struct *tsec;
3611892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
361242c3e03eSEric Paris 	u32 newsid;
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
36151da177e4SLinus Torvalds 
36161da177e4SLinus Torvalds 	tsec = current->security;
361742c3e03eSEric Paris 	newsid = tsec->sockcreate_sid ? : tsec->sid;
36181da177e4SLinus Torvalds 	isec->sclass = socket_type_to_security_class(family, type, protocol);
361942c3e03eSEric Paris 	isec->sid = kern ? SECINITSID_KERNEL : newsid;
36201da177e4SLinus Torvalds 	isec->initialized = 1;
36211da177e4SLinus Torvalds 
3622892c141eSVenkat Yekkirala 	if (sock->sk) {
3623892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3624892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
3625220deb96SPaul Moore 		sksec->sclass = isec->sclass;
36269f2ad665SPaul Moore 		err = selinux_netlbl_socket_post_create(sock);
3627892c141eSVenkat Yekkirala 	}
3628892c141eSVenkat Yekkirala 
36297420ed23SVenkat Yekkirala 	return err;
36301da177e4SLinus Torvalds }
36311da177e4SLinus Torvalds 
36321da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
36331da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
36341da177e4SLinus Torvalds    permission check between the socket and the port number. */
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
36371da177e4SLinus Torvalds {
36381da177e4SLinus Torvalds 	u16 family;
36391da177e4SLinus Torvalds 	int err;
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__BIND);
36421da177e4SLinus Torvalds 	if (err)
36431da177e4SLinus Torvalds 		goto out;
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds 	/*
36461da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
364713402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
364813402580SJames Morris 	 * check the first address now.
36491da177e4SLinus Torvalds 	 */
36501da177e4SLinus Torvalds 	family = sock->sk->sk_family;
36511da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
36521da177e4SLinus Torvalds 		char *addrp;
36531da177e4SLinus Torvalds 		struct inode_security_struct *isec;
36541da177e4SLinus Torvalds 		struct task_security_struct *tsec;
36551da177e4SLinus Torvalds 		struct avc_audit_data ad;
36561da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
36571da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
36581da177e4SLinus Torvalds 		unsigned short snum;
36591da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
36601da177e4SLinus Torvalds 		u32 sid, node_perm, addrlen;
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds 		tsec = current->security;
36631da177e4SLinus Torvalds 		isec = SOCK_INODE(sock)->i_security;
36641da177e4SLinus Torvalds 
36651da177e4SLinus Torvalds 		if (family == PF_INET) {
36661da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
36671da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
36681da177e4SLinus Torvalds 			addrlen = sizeof(addr4->sin_addr.s_addr);
36691da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
36701da177e4SLinus Torvalds 		} else {
36711da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
36721da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
36731da177e4SLinus Torvalds 			addrlen = sizeof(addr6->sin6_addr.s6_addr);
36741da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
36751da177e4SLinus Torvalds 		}
36761da177e4SLinus Torvalds 
3677227b60f5SStephen Hemminger 		if (snum) {
3678227b60f5SStephen Hemminger 			int low, high;
3679227b60f5SStephen Hemminger 
3680227b60f5SStephen Hemminger 			inet_get_local_port_range(&low, &high);
3681227b60f5SStephen Hemminger 
3682227b60f5SStephen Hemminger 			if (snum < max(PROT_SOCK, low) || snum > high) {
36833e112172SPaul Moore 				err = sel_netport_sid(sk->sk_protocol,
36843e112172SPaul Moore 						      snum, &sid);
36851da177e4SLinus Torvalds 				if (err)
36861da177e4SLinus Torvalds 					goto out;
36871da177e4SLinus Torvalds 				AVC_AUDIT_DATA_INIT(&ad,NET);
36881da177e4SLinus Torvalds 				ad.u.net.sport = htons(snum);
36891da177e4SLinus Torvalds 				ad.u.net.family = family;
36901da177e4SLinus Torvalds 				err = avc_has_perm(isec->sid, sid,
36911da177e4SLinus Torvalds 						   isec->sclass,
36921da177e4SLinus Torvalds 						   SOCKET__NAME_BIND, &ad);
36931da177e4SLinus Torvalds 				if (err)
36941da177e4SLinus Torvalds 					goto out;
36951da177e4SLinus Torvalds 			}
3696227b60f5SStephen Hemminger 		}
36971da177e4SLinus Torvalds 
369813402580SJames Morris 		switch(isec->sclass) {
369913402580SJames Morris 		case SECCLASS_TCP_SOCKET:
37001da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
37011da177e4SLinus Torvalds 			break;
37021da177e4SLinus Torvalds 
370313402580SJames Morris 		case SECCLASS_UDP_SOCKET:
37041da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
37051da177e4SLinus Torvalds 			break;
37061da177e4SLinus Torvalds 
37072ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
37082ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
37092ee92d46SJames Morris 			break;
37102ee92d46SJames Morris 
37111da177e4SLinus Torvalds 		default:
37121da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
37131da177e4SLinus Torvalds 			break;
37141da177e4SLinus Torvalds 		}
37151da177e4SLinus Torvalds 
3716224dfbd8SPaul Moore 		err = sel_netnode_sid(addrp, family, &sid);
37171da177e4SLinus Torvalds 		if (err)
37181da177e4SLinus Torvalds 			goto out;
37191da177e4SLinus Torvalds 
37201da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
37211da177e4SLinus Torvalds 		ad.u.net.sport = htons(snum);
37221da177e4SLinus Torvalds 		ad.u.net.family = family;
37231da177e4SLinus Torvalds 
37241da177e4SLinus Torvalds 		if (family == PF_INET)
37251da177e4SLinus Torvalds 			ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
37261da177e4SLinus Torvalds 		else
37271da177e4SLinus Torvalds 			ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, sid,
37301da177e4SLinus Torvalds 		                   isec->sclass, node_perm, &ad);
37311da177e4SLinus Torvalds 		if (err)
37321da177e4SLinus Torvalds 			goto out;
37331da177e4SLinus Torvalds 	}
37341da177e4SLinus Torvalds out:
37351da177e4SLinus Torvalds 	return err;
37361da177e4SLinus Torvalds }
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
37391da177e4SLinus Torvalds {
37401da177e4SLinus Torvalds 	struct inode_security_struct *isec;
37411da177e4SLinus Torvalds 	int err;
37421da177e4SLinus Torvalds 
37431da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__CONNECT);
37441da177e4SLinus Torvalds 	if (err)
37451da177e4SLinus Torvalds 		return err;
37461da177e4SLinus Torvalds 
37471da177e4SLinus Torvalds 	/*
37482ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
37491da177e4SLinus Torvalds 	 */
37501da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
37512ee92d46SJames Morris 	if (isec->sclass == SECCLASS_TCP_SOCKET ||
37522ee92d46SJames Morris 	    isec->sclass == SECCLASS_DCCP_SOCKET) {
37531da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
37541da177e4SLinus Torvalds 		struct avc_audit_data ad;
37551da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
37561da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
37571da177e4SLinus Torvalds 		unsigned short snum;
37582ee92d46SJames Morris 		u32 sid, perm;
37591da177e4SLinus Torvalds 
37601da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
37611da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3762911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
37631da177e4SLinus Torvalds 				return -EINVAL;
37641da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
37651da177e4SLinus Torvalds 		} else {
37661da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3767911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
37681da177e4SLinus Torvalds 				return -EINVAL;
37691da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
37701da177e4SLinus Torvalds 		}
37711da177e4SLinus Torvalds 
37723e112172SPaul Moore 		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
37731da177e4SLinus Torvalds 		if (err)
37741da177e4SLinus Torvalds 			goto out;
37751da177e4SLinus Torvalds 
37762ee92d46SJames Morris 		perm = (isec->sclass == SECCLASS_TCP_SOCKET) ?
37772ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
37782ee92d46SJames Morris 
37791da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
37801da177e4SLinus Torvalds 		ad.u.net.dport = htons(snum);
37811da177e4SLinus Torvalds 		ad.u.net.family = sk->sk_family;
37822ee92d46SJames Morris 		err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad);
37831da177e4SLinus Torvalds 		if (err)
37841da177e4SLinus Torvalds 			goto out;
37851da177e4SLinus Torvalds 	}
37861da177e4SLinus Torvalds 
37871da177e4SLinus Torvalds out:
37881da177e4SLinus Torvalds 	return err;
37891da177e4SLinus Torvalds }
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
37921da177e4SLinus Torvalds {
37931da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__LISTEN);
37941da177e4SLinus Torvalds }
37951da177e4SLinus Torvalds 
37961da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
37971da177e4SLinus Torvalds {
37981da177e4SLinus Torvalds 	int err;
37991da177e4SLinus Torvalds 	struct inode_security_struct *isec;
38001da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__ACCEPT);
38031da177e4SLinus Torvalds 	if (err)
38041da177e4SLinus Torvalds 		return err;
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
38071da177e4SLinus Torvalds 
38081da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
38091da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
38101da177e4SLinus Torvalds 	newisec->sid = isec->sid;
38111da177e4SLinus Torvalds 	newisec->initialized = 1;
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	return 0;
38141da177e4SLinus Torvalds }
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
38171da177e4SLinus Torvalds  				  int size)
38181da177e4SLinus Torvalds {
38197420ed23SVenkat Yekkirala 	int rc;
38207420ed23SVenkat Yekkirala 
38217420ed23SVenkat Yekkirala 	rc = socket_has_perm(current, sock, SOCKET__WRITE);
38227420ed23SVenkat Yekkirala 	if (rc)
38237420ed23SVenkat Yekkirala 		return rc;
38247420ed23SVenkat Yekkirala 
38257420ed23SVenkat Yekkirala 	return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE);
38261da177e4SLinus Torvalds }
38271da177e4SLinus Torvalds 
38281da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
38291da177e4SLinus Torvalds 				  int size, int flags)
38301da177e4SLinus Torvalds {
38311da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__READ);
38321da177e4SLinus Torvalds }
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
38351da177e4SLinus Torvalds {
38361da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
38371da177e4SLinus Torvalds }
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
38401da177e4SLinus Torvalds {
38411da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
38421da177e4SLinus Torvalds }
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname)
38451da177e4SLinus Torvalds {
3846f8687afeSPaul Moore 	int err;
3847f8687afeSPaul Moore 
3848f8687afeSPaul Moore 	err = socket_has_perm(current, sock, SOCKET__SETOPT);
3849f8687afeSPaul Moore 	if (err)
3850f8687afeSPaul Moore 		return err;
3851f8687afeSPaul Moore 
3852f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
38531da177e4SLinus Torvalds }
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
38561da177e4SLinus Torvalds 				     int optname)
38571da177e4SLinus Torvalds {
38581da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETOPT);
38591da177e4SLinus Torvalds }
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
38621da177e4SLinus Torvalds {
38631da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
38641da177e4SLinus Torvalds }
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock,
38671da177e4SLinus Torvalds 					      struct socket *other,
38681da177e4SLinus Torvalds 					      struct sock *newsk)
38691da177e4SLinus Torvalds {
38701da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
38711da177e4SLinus Torvalds 	struct inode_security_struct *isec;
38721da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
38731da177e4SLinus Torvalds 	struct avc_audit_data ad;
38741da177e4SLinus Torvalds 	int err;
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds 	err = secondary_ops->unix_stream_connect(sock, other, newsk);
38771da177e4SLinus Torvalds 	if (err)
38781da177e4SLinus Torvalds 		return err;
38791da177e4SLinus Torvalds 
38801da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
38811da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
38841da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
38851da177e4SLinus Torvalds 
38861da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
38871da177e4SLinus Torvalds 			   isec->sclass,
38881da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
38891da177e4SLinus Torvalds 	if (err)
38901da177e4SLinus Torvalds 		return err;
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds 	/* connecting socket */
38931da177e4SLinus Torvalds 	ssec = sock->sk->sk_security;
38941da177e4SLinus Torvalds 	ssec->peer_sid = other_isec->sid;
38951da177e4SLinus Torvalds 
38961da177e4SLinus Torvalds 	/* server child socket */
38971da177e4SLinus Torvalds 	ssec = newsk->sk_security;
38981da177e4SLinus Torvalds 	ssec->peer_sid = isec->sid;
38994237c75cSVenkat Yekkirala 	err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid);
39001da177e4SLinus Torvalds 
39014237c75cSVenkat Yekkirala 	return err;
39021da177e4SLinus Torvalds }
39031da177e4SLinus Torvalds 
39041da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
39051da177e4SLinus Torvalds 					struct socket *other)
39061da177e4SLinus Torvalds {
39071da177e4SLinus Torvalds 	struct inode_security_struct *isec;
39081da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
39091da177e4SLinus Torvalds 	struct avc_audit_data ad;
39101da177e4SLinus Torvalds 	int err;
39111da177e4SLinus Torvalds 
39121da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
39131da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
39141da177e4SLinus Torvalds 
39151da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
39161da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
39171da177e4SLinus Torvalds 
39181da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
39191da177e4SLinus Torvalds 			   isec->sclass, SOCKET__SENDTO, &ad);
39201da177e4SLinus Torvalds 	if (err)
39211da177e4SLinus Torvalds 		return err;
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds 	return 0;
39241da177e4SLinus Torvalds }
39251da177e4SLinus Torvalds 
3926effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
3927effad8dfSPaul Moore 				    u32 peer_sid,
3928effad8dfSPaul Moore 				    struct avc_audit_data *ad)
3929effad8dfSPaul Moore {
3930effad8dfSPaul Moore 	int err;
3931effad8dfSPaul Moore 	u32 if_sid;
3932effad8dfSPaul Moore 	u32 node_sid;
3933effad8dfSPaul Moore 
3934effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
3935effad8dfSPaul Moore 	if (err)
3936effad8dfSPaul Moore 		return err;
3937effad8dfSPaul Moore 	err = avc_has_perm(peer_sid, if_sid,
3938effad8dfSPaul Moore 			   SECCLASS_NETIF, NETIF__INGRESS, ad);
3939effad8dfSPaul Moore 	if (err)
3940effad8dfSPaul Moore 		return err;
3941effad8dfSPaul Moore 
3942effad8dfSPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
3943effad8dfSPaul Moore 	if (err)
3944effad8dfSPaul Moore 		return err;
3945effad8dfSPaul Moore 	return avc_has_perm(peer_sid, node_sid,
3946effad8dfSPaul Moore 			    SECCLASS_NODE, NODE__RECVFROM, ad);
3947effad8dfSPaul Moore }
3948effad8dfSPaul Moore 
3949220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
3950220deb96SPaul Moore 						struct sk_buff *skb,
3951224dfbd8SPaul Moore 						struct avc_audit_data *ad,
3952220deb96SPaul Moore 						u16 family,
3953220deb96SPaul Moore 						char *addrp)
39541da177e4SLinus Torvalds {
3955220deb96SPaul Moore 	int err;
3956220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
3957220deb96SPaul Moore 	u16 sk_class;
3958220deb96SPaul Moore 	u32 netif_perm, node_perm, recv_perm;
3959220deb96SPaul Moore 	u32 port_sid, node_sid, if_sid, sk_sid;
39604237c75cSVenkat Yekkirala 
3961220deb96SPaul Moore 	sk_sid = sksec->sid;
3962220deb96SPaul Moore 	sk_class = sksec->sclass;
39631da177e4SLinus Torvalds 
3964220deb96SPaul Moore 	switch (sk_class) {
39651da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
39661da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_RECV;
39671da177e4SLinus Torvalds 		node_perm = NODE__UDP_RECV;
39681da177e4SLinus Torvalds 		recv_perm = UDP_SOCKET__RECV_MSG;
39691da177e4SLinus Torvalds 		break;
39701da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
39711da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_RECV;
39721da177e4SLinus Torvalds 		node_perm = NODE__TCP_RECV;
39731da177e4SLinus Torvalds 		recv_perm = TCP_SOCKET__RECV_MSG;
39741da177e4SLinus Torvalds 		break;
39752ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
39762ee92d46SJames Morris 		netif_perm = NETIF__DCCP_RECV;
39772ee92d46SJames Morris 		node_perm = NODE__DCCP_RECV;
39782ee92d46SJames Morris 		recv_perm = DCCP_SOCKET__RECV_MSG;
39792ee92d46SJames Morris 		break;
39801da177e4SLinus Torvalds 	default:
39811da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_RECV;
39821da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_RECV;
3983220deb96SPaul Moore 		recv_perm = 0;
39841da177e4SLinus Torvalds 		break;
39851da177e4SLinus Torvalds 	}
39861da177e4SLinus Torvalds 
3987220deb96SPaul Moore 	err = sel_netif_sid(skb->iif, &if_sid);
39881da177e4SLinus Torvalds 	if (err)
3989220deb96SPaul Moore 		return err;
3990220deb96SPaul Moore 	err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
3991220deb96SPaul Moore 	if (err)
3992220deb96SPaul Moore 		return err;
39931da177e4SLinus Torvalds 
3994224dfbd8SPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
39951da177e4SLinus Torvalds 	if (err)
3996220deb96SPaul Moore 		return err;
3997220deb96SPaul Moore 	err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
39981da177e4SLinus Torvalds 	if (err)
3999220deb96SPaul Moore 		return err;
40001da177e4SLinus Torvalds 
4001220deb96SPaul Moore 	if (!recv_perm)
4002220deb96SPaul Moore 		return 0;
40033e112172SPaul Moore 	err = sel_netport_sid(sk->sk_protocol,
40043e112172SPaul Moore 			      ntohs(ad->u.net.sport), &port_sid);
400571f1cb05SPaul Moore 	if (unlikely(err)) {
400671f1cb05SPaul Moore 		printk(KERN_WARNING
400771f1cb05SPaul Moore 		       "SELinux: failure in"
400871f1cb05SPaul Moore 		       " selinux_sock_rcv_skb_iptables_compat(),"
400971f1cb05SPaul Moore 		       " network port label not found\n");
4010220deb96SPaul Moore 		return err;
401171f1cb05SPaul Moore 	}
4012220deb96SPaul Moore 	return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad);
40131da177e4SLinus Torvalds }
4014d28d1e08STrent Jaeger 
4015220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4016220deb96SPaul Moore 				       struct avc_audit_data *ad,
4017220deb96SPaul Moore 				       u16 family, char *addrp)
4018220deb96SPaul Moore {
4019220deb96SPaul Moore 	int err;
4020220deb96SPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4021220deb96SPaul Moore 	u32 peer_sid;
4022220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
4023220deb96SPaul Moore 
4024220deb96SPaul Moore 	if (selinux_compat_net)
4025220deb96SPaul Moore 		err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad,
4026220deb96SPaul Moore 							   family, addrp);
4027220deb96SPaul Moore 	else
4028220deb96SPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4029220deb96SPaul Moore 				   PACKET__RECV, ad);
4030220deb96SPaul Moore 	if (err)
4031220deb96SPaul Moore 		return err;
4032220deb96SPaul Moore 
4033220deb96SPaul Moore 	if (selinux_policycap_netpeer) {
4034220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4035220deb96SPaul Moore 		if (err)
4036220deb96SPaul Moore 			return err;
4037220deb96SPaul Moore 		err = avc_has_perm(sk_sid, peer_sid,
4038220deb96SPaul Moore 				   SECCLASS_PEER, PEER__RECV, ad);
4039220deb96SPaul Moore 	} else {
4040220deb96SPaul Moore 		err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad);
4041220deb96SPaul Moore 		if (err)
4042220deb96SPaul Moore 			return err;
4043220deb96SPaul Moore 		err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad);
4044220deb96SPaul Moore 	}
4045220deb96SPaul Moore 
40464e5ab4cbSJames Morris 	return err;
40474e5ab4cbSJames Morris }
4048d28d1e08STrent Jaeger 
40494e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
40504e5ab4cbSJames Morris {
4051220deb96SPaul Moore 	int err;
40524237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
4053220deb96SPaul Moore 	u16 family = sk->sk_family;
4054220deb96SPaul Moore 	u32 sk_sid = sksec->sid;
4055220deb96SPaul Moore 	struct avc_audit_data ad;
4056220deb96SPaul Moore 	char *addrp;
40574e5ab4cbSJames Morris 
40584e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
4059220deb96SPaul Moore 		return 0;
40604e5ab4cbSJames Morris 
40614e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
406287fcd70dSAl Viro 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
40634e5ab4cbSJames Morris 		family = PF_INET;
40644e5ab4cbSJames Morris 
40654e5ab4cbSJames Morris 	AVC_AUDIT_DATA_INIT(&ad, NET);
4066da5645a2SPaul Moore 	ad.u.net.netif = skb->iif;
40674e5ab4cbSJames Morris 	ad.u.net.family = family;
4068224dfbd8SPaul Moore 	err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
40694e5ab4cbSJames Morris 	if (err)
4070220deb96SPaul Moore 		return err;
40714e5ab4cbSJames Morris 
4072220deb96SPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4073220deb96SPaul Moore 	 * to the selinux_sock_rcv_skb_compat() function to deal with the
4074220deb96SPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4075220deb96SPaul Moore 	 * as fast and as clean as possible. */
4076220deb96SPaul Moore 	if (selinux_compat_net || !selinux_policycap_netpeer)
4077220deb96SPaul Moore 		return selinux_sock_rcv_skb_compat(sk, skb, &ad,
4078220deb96SPaul Moore 						   family, addrp);
4079220deb96SPaul Moore 
4080d621d35eSPaul Moore 	if (netlbl_enabled() || selinux_xfrm_enabled()) {
4081d621d35eSPaul Moore 		u32 peer_sid;
4082220deb96SPaul Moore 
4083220deb96SPaul Moore 		err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4084220deb96SPaul Moore 		if (err)
4085220deb96SPaul Moore 			return err;
4086effad8dfSPaul Moore 		err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family,
4087effad8dfSPaul Moore 					       peer_sid, &ad);
4088effad8dfSPaul Moore 		if (err)
4089effad8dfSPaul Moore 			return err;
4090d621d35eSPaul Moore 		err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4091d621d35eSPaul Moore 				   PEER__RECV, &ad);
4092d621d35eSPaul Moore 	}
4093d621d35eSPaul Moore 
4094effad8dfSPaul Moore 	if (selinux_secmark_enabled()) {
4095effad8dfSPaul Moore 		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4096effad8dfSPaul Moore 				   PACKET__RECV, &ad);
4097effad8dfSPaul Moore 		if (err)
4098effad8dfSPaul Moore 			return err;
4099effad8dfSPaul Moore 	}
4100effad8dfSPaul Moore 
4101d621d35eSPaul Moore 	return err;
41021da177e4SLinus Torvalds }
41031da177e4SLinus Torvalds 
41042c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
41051da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
41061da177e4SLinus Torvalds {
41071da177e4SLinus Torvalds 	int err = 0;
41081da177e4SLinus Torvalds 	char *scontext;
41091da177e4SLinus Torvalds 	u32 scontext_len;
41101da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
41111da177e4SLinus Torvalds 	struct inode_security_struct *isec;
41123de4bab5SPaul Moore 	u32 peer_sid = SECSID_NULL;
41131da177e4SLinus Torvalds 
41141da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
41152c7946a7SCatherine Zhang 
41163de4bab5SPaul Moore 	if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
41173de4bab5SPaul Moore 	    isec->sclass == SECCLASS_TCP_SOCKET) {
41186b877699SVenkat Yekkirala 		ssec = sock->sk->sk_security;
41196b877699SVenkat Yekkirala 		peer_sid = ssec->peer_sid;
41206b877699SVenkat Yekkirala 	}
41212c7946a7SCatherine Zhang 	if (peer_sid == SECSID_NULL) {
41222c7946a7SCatherine Zhang 		err = -ENOPROTOOPT;
41232c7946a7SCatherine Zhang 		goto out;
41242c7946a7SCatherine Zhang 	}
41251da177e4SLinus Torvalds 
41262c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds 	if (err)
41291da177e4SLinus Torvalds 		goto out;
41301da177e4SLinus Torvalds 
41311da177e4SLinus Torvalds 	if (scontext_len > len) {
41321da177e4SLinus Torvalds 		err = -ERANGE;
41331da177e4SLinus Torvalds 		goto out_len;
41341da177e4SLinus Torvalds 	}
41351da177e4SLinus Torvalds 
41361da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
41371da177e4SLinus Torvalds 		err = -EFAULT;
41381da177e4SLinus Torvalds 
41391da177e4SLinus Torvalds out_len:
41401da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
41411da177e4SLinus Torvalds 		err = -EFAULT;
41421da177e4SLinus Torvalds 
41431da177e4SLinus Torvalds 	kfree(scontext);
41441da177e4SLinus Torvalds out:
41451da177e4SLinus Torvalds 	return err;
41461da177e4SLinus Torvalds }
41471da177e4SLinus Torvalds 
4148dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
41492c7946a7SCatherine Zhang {
4150dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
415175e22910SPaul Moore 	u16 family;
4152877ce7c1SCatherine Zhang 
415375e22910SPaul Moore 	if (sock)
415475e22910SPaul Moore 		family = sock->sk->sk_family;
415575e22910SPaul Moore 	else if (skb && skb->sk)
415675e22910SPaul Moore 		family = skb->sk->sk_family;
415775e22910SPaul Moore 	else
415875e22910SPaul Moore 		goto out;
415975e22910SPaul Moore 
416075e22910SPaul Moore 	if (sock && family == PF_UNIX)
4161713a04aeSAhmed S. Darwish 		selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
41623de4bab5SPaul Moore 	else if (skb)
4163220deb96SPaul Moore 		selinux_skb_peerlbl_sid(skb, family, &peer_secid);
41642c7946a7SCatherine Zhang 
416575e22910SPaul Moore out:
4166dc49c1f9SCatherine Zhang 	*secid = peer_secid;
416775e22910SPaul Moore 	if (peer_secid == SECSID_NULL)
416875e22910SPaul Moore 		return -EINVAL;
416975e22910SPaul Moore 	return 0;
41702c7946a7SCatherine Zhang }
41712c7946a7SCatherine Zhang 
41727d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
41731da177e4SLinus Torvalds {
41741da177e4SLinus Torvalds 	return sk_alloc_security(sk, family, priority);
41751da177e4SLinus Torvalds }
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
41781da177e4SLinus Torvalds {
41791da177e4SLinus Torvalds 	sk_free_security(sk);
41801da177e4SLinus Torvalds }
41811da177e4SLinus Torvalds 
4182892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4183892c141eSVenkat Yekkirala {
4184892c141eSVenkat Yekkirala 	struct sk_security_struct *ssec = sk->sk_security;
4185892c141eSVenkat Yekkirala 	struct sk_security_struct *newssec = newsk->sk_security;
4186892c141eSVenkat Yekkirala 
4187892c141eSVenkat Yekkirala 	newssec->sid = ssec->sid;
4188892c141eSVenkat Yekkirala 	newssec->peer_sid = ssec->peer_sid;
4189220deb96SPaul Moore 	newssec->sclass = ssec->sclass;
419099f59ed0SPaul Moore 
4191f74af6e8SPaul Moore 	selinux_netlbl_sk_security_reset(newssec, newsk->sk_family);
4192892c141eSVenkat Yekkirala }
4193892c141eSVenkat Yekkirala 
4194beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4195d28d1e08STrent Jaeger {
4196d28d1e08STrent Jaeger 	if (!sk)
4197beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
4198892c141eSVenkat Yekkirala 	else {
4199892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
4200d28d1e08STrent Jaeger 
4201beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
4202892c141eSVenkat Yekkirala 	}
4203d28d1e08STrent Jaeger }
4204d28d1e08STrent Jaeger 
42059a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent)
42064237c75cSVenkat Yekkirala {
42074237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
42084237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
42094237c75cSVenkat Yekkirala 
42102148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
42112148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
42124237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
4213220deb96SPaul Moore 	sksec->sclass = isec->sclass;
42147420ed23SVenkat Yekkirala 
42157420ed23SVenkat Yekkirala 	selinux_netlbl_sock_graft(sk, parent);
42164237c75cSVenkat Yekkirala }
42174237c75cSVenkat Yekkirala 
42189a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
42194237c75cSVenkat Yekkirala 				     struct request_sock *req)
42204237c75cSVenkat Yekkirala {
42214237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
42224237c75cSVenkat Yekkirala 	int err;
42237420ed23SVenkat Yekkirala 	u32 newsid;
42244237c75cSVenkat Yekkirala 	u32 peersid;
42254237c75cSVenkat Yekkirala 
4226220deb96SPaul Moore 	err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid);
4227220deb96SPaul Moore 	if (err)
4228220deb96SPaul Moore 		return err;
4229a51c64f1SVenkat Yekkirala 	if (peersid == SECSID_NULL) {
4230a51c64f1SVenkat Yekkirala 		req->secid = sksec->sid;
42313de4bab5SPaul Moore 		req->peer_secid = SECSID_NULL;
4232a51c64f1SVenkat Yekkirala 		return 0;
4233a51c64f1SVenkat Yekkirala 	}
4234a51c64f1SVenkat Yekkirala 
42354237c75cSVenkat Yekkirala 	err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
42364237c75cSVenkat Yekkirala 	if (err)
42374237c75cSVenkat Yekkirala 		return err;
42384237c75cSVenkat Yekkirala 
42394237c75cSVenkat Yekkirala 	req->secid = newsid;
42406b877699SVenkat Yekkirala 	req->peer_secid = peersid;
42414237c75cSVenkat Yekkirala 	return 0;
42424237c75cSVenkat Yekkirala }
42434237c75cSVenkat Yekkirala 
42449a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
42459a673e56SAdrian Bunk 				   const struct request_sock *req)
42464237c75cSVenkat Yekkirala {
42474237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
42484237c75cSVenkat Yekkirala 
42494237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
42506b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
42514237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
42524237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
42534237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
42544237c75cSVenkat Yekkirala 	   time it will have been created and available. */
425599f59ed0SPaul Moore 
42569f2ad665SPaul Moore 	/* We don't need to take any sort of lock here as we are the only
42579f2ad665SPaul Moore 	 * thread with access to newsksec */
42589f2ad665SPaul Moore 	selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family);
42594237c75cSVenkat Yekkirala }
42604237c75cSVenkat Yekkirala 
42616b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk,
42626b877699SVenkat Yekkirala 				struct sk_buff *skb)
42636b877699SVenkat Yekkirala {
42646b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
42656b877699SVenkat Yekkirala 
4266220deb96SPaul Moore 	selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid);
42676b877699SVenkat Yekkirala }
42686b877699SVenkat Yekkirala 
42699a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
42709a673e56SAdrian Bunk 				      struct flowi *fl)
42714237c75cSVenkat Yekkirala {
42724237c75cSVenkat Yekkirala 	fl->secid = req->secid;
42734237c75cSVenkat Yekkirala }
42744237c75cSVenkat Yekkirala 
42751da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
42761da177e4SLinus Torvalds {
42771da177e4SLinus Torvalds 	int err = 0;
42781da177e4SLinus Torvalds 	u32 perm;
42791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
42801da177e4SLinus Torvalds 	struct socket *sock = sk->sk_socket;
42811da177e4SLinus Torvalds 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds 	if (skb->len < NLMSG_SPACE(0)) {
42841da177e4SLinus Torvalds 		err = -EINVAL;
42851da177e4SLinus Torvalds 		goto out;
42861da177e4SLinus Torvalds 	}
4287b529ccf2SArnaldo Carvalho de Melo 	nlh = nlmsg_hdr(skb);
42881da177e4SLinus Torvalds 
42891da177e4SLinus Torvalds 	err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
42901da177e4SLinus Torvalds 	if (err) {
42911da177e4SLinus Torvalds 		if (err == -EINVAL) {
42929ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
42931da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
42941da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
42951da177e4SLinus Torvalds 				  nlh->nlmsg_type, isec->sclass);
42961da177e4SLinus Torvalds 			if (!selinux_enforcing)
42971da177e4SLinus Torvalds 				err = 0;
42981da177e4SLinus Torvalds 		}
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds 		/* Ignore */
43011da177e4SLinus Torvalds 		if (err == -ENOENT)
43021da177e4SLinus Torvalds 			err = 0;
43031da177e4SLinus Torvalds 		goto out;
43041da177e4SLinus Torvalds 	}
43051da177e4SLinus Torvalds 
43061da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, perm);
43071da177e4SLinus Torvalds out:
43081da177e4SLinus Torvalds 	return err;
43091da177e4SLinus Torvalds }
43101da177e4SLinus Torvalds 
43111da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
43121da177e4SLinus Torvalds 
4313effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4314effad8dfSPaul Moore 				       u16 family)
43151da177e4SLinus Torvalds {
4316effad8dfSPaul Moore 	char *addrp;
4317effad8dfSPaul Moore 	u32 peer_sid;
4318effad8dfSPaul Moore 	struct avc_audit_data ad;
4319effad8dfSPaul Moore 	u8 secmark_active;
4320effad8dfSPaul Moore 	u8 peerlbl_active;
43214237c75cSVenkat Yekkirala 
4322effad8dfSPaul Moore 	if (!selinux_policycap_netpeer)
4323effad8dfSPaul Moore 		return NF_ACCEPT;
43244237c75cSVenkat Yekkirala 
4325effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4326effad8dfSPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4327effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4328effad8dfSPaul Moore 		return NF_ACCEPT;
43294237c75cSVenkat Yekkirala 
4330effad8dfSPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4331effad8dfSPaul Moore 	ad.u.net.netif = ifindex;
4332effad8dfSPaul Moore 	ad.u.net.family = family;
4333effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4334effad8dfSPaul Moore 		return NF_DROP;
43351da177e4SLinus Torvalds 
4336effad8dfSPaul Moore 	if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4337effad8dfSPaul Moore 		return NF_DROP;
43381da177e4SLinus Torvalds 
4339effad8dfSPaul Moore 	if (peerlbl_active)
4340effad8dfSPaul Moore 		if (selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4341effad8dfSPaul Moore 					     peer_sid, &ad) != 0)
4342effad8dfSPaul Moore 			return NF_DROP;
4343effad8dfSPaul Moore 
4344effad8dfSPaul Moore 	if (secmark_active)
4345effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4346effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4347effad8dfSPaul Moore 			return NF_DROP;
4348effad8dfSPaul Moore 
4349effad8dfSPaul Moore 	return NF_ACCEPT;
4350effad8dfSPaul Moore }
4351effad8dfSPaul Moore 
4352effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4353effad8dfSPaul Moore 					 struct sk_buff *skb,
4354effad8dfSPaul Moore 					 const struct net_device *in,
4355effad8dfSPaul Moore 					 const struct net_device *out,
4356effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4357effad8dfSPaul Moore {
4358effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET);
4359effad8dfSPaul Moore }
4360effad8dfSPaul Moore 
4361effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4362effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4363effad8dfSPaul Moore 					 struct sk_buff *skb,
4364effad8dfSPaul Moore 					 const struct net_device *in,
4365effad8dfSPaul Moore 					 const struct net_device *out,
4366effad8dfSPaul Moore 					 int (*okfn)(struct sk_buff *))
4367effad8dfSPaul Moore {
4368effad8dfSPaul Moore 	return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4369effad8dfSPaul Moore }
4370effad8dfSPaul Moore #endif	/* IPV6 */
4371effad8dfSPaul Moore 
4372effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk,
4373effad8dfSPaul Moore 						int ifindex,
4374effad8dfSPaul Moore 						struct avc_audit_data *ad,
4375effad8dfSPaul Moore 						u16 family, char *addrp)
4376effad8dfSPaul Moore {
4377effad8dfSPaul Moore 	int err;
4378effad8dfSPaul Moore 	struct sk_security_struct *sksec = sk->sk_security;
4379effad8dfSPaul Moore 	u16 sk_class;
4380effad8dfSPaul Moore 	u32 netif_perm, node_perm, send_perm;
4381effad8dfSPaul Moore 	u32 port_sid, node_sid, if_sid, sk_sid;
4382effad8dfSPaul Moore 
4383effad8dfSPaul Moore 	sk_sid = sksec->sid;
4384effad8dfSPaul Moore 	sk_class = sksec->sclass;
4385effad8dfSPaul Moore 
4386effad8dfSPaul Moore 	switch (sk_class) {
43871da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
43881da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_SEND;
43891da177e4SLinus Torvalds 		node_perm = NODE__UDP_SEND;
43901da177e4SLinus Torvalds 		send_perm = UDP_SOCKET__SEND_MSG;
43911da177e4SLinus Torvalds 		break;
43921da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
43931da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_SEND;
43941da177e4SLinus Torvalds 		node_perm = NODE__TCP_SEND;
43951da177e4SLinus Torvalds 		send_perm = TCP_SOCKET__SEND_MSG;
43961da177e4SLinus Torvalds 		break;
43972ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
43982ee92d46SJames Morris 		netif_perm = NETIF__DCCP_SEND;
43992ee92d46SJames Morris 		node_perm = NODE__DCCP_SEND;
44002ee92d46SJames Morris 		send_perm = DCCP_SOCKET__SEND_MSG;
44012ee92d46SJames Morris 		break;
44021da177e4SLinus Torvalds 	default:
44031da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_SEND;
44041da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_SEND;
4405effad8dfSPaul Moore 		send_perm = 0;
44061da177e4SLinus Torvalds 		break;
44071da177e4SLinus Torvalds 	}
44081da177e4SLinus Torvalds 
4409effad8dfSPaul Moore 	err = sel_netif_sid(ifindex, &if_sid);
44104e5ab4cbSJames Morris 	if (err)
4411effad8dfSPaul Moore 		return err;
4412effad8dfSPaul Moore 	err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
4413effad8dfSPaul Moore 		return err;
44141da177e4SLinus Torvalds 
4415224dfbd8SPaul Moore 	err = sel_netnode_sid(addrp, family, &node_sid);
44164e5ab4cbSJames Morris 	if (err)
4417effad8dfSPaul Moore 		return err;
4418effad8dfSPaul Moore 	err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
44194e5ab4cbSJames Morris 	if (err)
4420effad8dfSPaul Moore 		return err;
44211da177e4SLinus Torvalds 
4422effad8dfSPaul Moore 	if (send_perm != 0)
4423effad8dfSPaul Moore 		return 0;
44241da177e4SLinus Torvalds 
44253e112172SPaul Moore 	err = sel_netport_sid(sk->sk_protocol,
44263e112172SPaul Moore 			      ntohs(ad->u.net.dport), &port_sid);
442771f1cb05SPaul Moore 	if (unlikely(err)) {
442871f1cb05SPaul Moore 		printk(KERN_WARNING
442971f1cb05SPaul Moore 		       "SELinux: failure in"
443071f1cb05SPaul Moore 		       " selinux_ip_postroute_iptables_compat(),"
443171f1cb05SPaul Moore 		       " network port label not found\n");
44324e5ab4cbSJames Morris 		return err;
443371f1cb05SPaul Moore 	}
4434effad8dfSPaul Moore 	return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad);
44351da177e4SLinus Torvalds }
44361da177e4SLinus Torvalds 
4437effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4438effad8dfSPaul Moore 						int ifindex,
4439effad8dfSPaul Moore 						struct avc_audit_data *ad,
4440effad8dfSPaul Moore 						u16 family,
4441effad8dfSPaul Moore 						char *addrp,
4442effad8dfSPaul Moore 						u8 proto)
44434e5ab4cbSJames Morris {
4444effad8dfSPaul Moore 	struct sock *sk = skb->sk;
44454237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
44464e5ab4cbSJames Morris 
4447effad8dfSPaul Moore 	if (sk == NULL)
4448effad8dfSPaul Moore 		return NF_ACCEPT;
44494237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
44504e5ab4cbSJames Morris 
4451effad8dfSPaul Moore 	if (selinux_compat_net) {
4452effad8dfSPaul Moore 		if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex,
4453effad8dfSPaul Moore 							 ad, family, addrp))
4454effad8dfSPaul Moore 			return NF_DROP;
4455effad8dfSPaul Moore 	} else {
4456effad8dfSPaul Moore 		if (avc_has_perm(sksec->sid, skb->secmark,
4457effad8dfSPaul Moore 				 SECCLASS_PACKET, PACKET__SEND, ad))
4458effad8dfSPaul Moore 			return NF_DROP;
44591da177e4SLinus Torvalds 	}
44601da177e4SLinus Torvalds 
4461effad8dfSPaul Moore 	if (selinux_policycap_netpeer)
4462effad8dfSPaul Moore 		if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto))
4463effad8dfSPaul Moore 			return NF_DROP;
4464effad8dfSPaul Moore 
4465effad8dfSPaul Moore 	return NF_ACCEPT;
4466effad8dfSPaul Moore }
4467effad8dfSPaul Moore 
4468effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4469effad8dfSPaul Moore 					 u16 family)
4470effad8dfSPaul Moore {
4471effad8dfSPaul Moore 	u32 secmark_perm;
4472effad8dfSPaul Moore 	u32 peer_sid;
4473effad8dfSPaul Moore 	struct sock *sk;
4474effad8dfSPaul Moore 	struct avc_audit_data ad;
4475effad8dfSPaul Moore 	char *addrp;
4476effad8dfSPaul Moore 	u8 proto;
4477effad8dfSPaul Moore 	u8 secmark_active;
4478effad8dfSPaul Moore 	u8 peerlbl_active;
4479effad8dfSPaul Moore 
4480effad8dfSPaul Moore 	AVC_AUDIT_DATA_INIT(&ad, NET);
4481effad8dfSPaul Moore 	ad.u.net.netif = ifindex;
4482effad8dfSPaul Moore 	ad.u.net.family = family;
4483effad8dfSPaul Moore 	if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4484effad8dfSPaul Moore 		return NF_DROP;
4485effad8dfSPaul Moore 
4486effad8dfSPaul Moore 	/* If any sort of compatibility mode is enabled then handoff processing
4487effad8dfSPaul Moore 	 * to the selinux_ip_postroute_compat() function to deal with the
4488effad8dfSPaul Moore 	 * special handling.  We do this in an attempt to keep this function
4489effad8dfSPaul Moore 	 * as fast and as clean as possible. */
4490effad8dfSPaul Moore 	if (selinux_compat_net || !selinux_policycap_netpeer)
4491effad8dfSPaul Moore 		return selinux_ip_postroute_compat(skb, ifindex, &ad,
4492effad8dfSPaul Moore 						   family, addrp, proto);
4493effad8dfSPaul Moore 
4494effad8dfSPaul Moore 	/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4495effad8dfSPaul Moore 	 * packet transformation so allow the packet to pass without any checks
4496effad8dfSPaul Moore 	 * since we'll have another chance to perform access control checks
4497effad8dfSPaul Moore 	 * when the packet is on it's final way out.
4498effad8dfSPaul Moore 	 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4499effad8dfSPaul Moore 	 *       is NULL, in this case go ahead and apply access control. */
4500effad8dfSPaul Moore 	if (skb->dst != NULL && skb->dst->xfrm != NULL)
4501effad8dfSPaul Moore 		return NF_ACCEPT;
4502effad8dfSPaul Moore 
4503effad8dfSPaul Moore 	secmark_active = selinux_secmark_enabled();
4504effad8dfSPaul Moore 	peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4505effad8dfSPaul Moore 	if (!secmark_active && !peerlbl_active)
4506effad8dfSPaul Moore 		return NF_ACCEPT;
4507effad8dfSPaul Moore 
4508effad8dfSPaul Moore 	/* if the packet is locally generated (skb->sk != NULL) then use the
4509effad8dfSPaul Moore 	 * socket's label as the peer label, otherwise the packet is being
4510effad8dfSPaul Moore 	 * forwarded through this system and we need to fetch the peer label
4511effad8dfSPaul Moore 	 * directly from the packet */
4512effad8dfSPaul Moore 	sk = skb->sk;
4513effad8dfSPaul Moore 	if (sk) {
4514effad8dfSPaul Moore 		struct sk_security_struct *sksec = sk->sk_security;
4515effad8dfSPaul Moore 		peer_sid = sksec->sid;
4516effad8dfSPaul Moore 		secmark_perm = PACKET__SEND;
4517effad8dfSPaul Moore 	} else {
4518effad8dfSPaul Moore 		if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4519effad8dfSPaul Moore 				return NF_DROP;
4520effad8dfSPaul Moore 		secmark_perm = PACKET__FORWARD_OUT;
4521effad8dfSPaul Moore 	}
4522effad8dfSPaul Moore 
4523effad8dfSPaul Moore 	if (secmark_active)
4524effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, skb->secmark,
4525effad8dfSPaul Moore 				 SECCLASS_PACKET, secmark_perm, &ad))
4526effad8dfSPaul Moore 			return NF_DROP;
4527effad8dfSPaul Moore 
4528effad8dfSPaul Moore 	if (peerlbl_active) {
4529effad8dfSPaul Moore 		u32 if_sid;
4530effad8dfSPaul Moore 		u32 node_sid;
4531effad8dfSPaul Moore 
4532effad8dfSPaul Moore 		if (sel_netif_sid(ifindex, &if_sid))
4533effad8dfSPaul Moore 			return NF_DROP;
4534effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, if_sid,
4535effad8dfSPaul Moore 				 SECCLASS_NETIF, NETIF__EGRESS, &ad))
4536effad8dfSPaul Moore 			return NF_DROP;
4537effad8dfSPaul Moore 
4538effad8dfSPaul Moore 		if (sel_netnode_sid(addrp, family, &node_sid))
4539effad8dfSPaul Moore 			return NF_DROP;
4540effad8dfSPaul Moore 		if (avc_has_perm(peer_sid, node_sid,
4541effad8dfSPaul Moore 				 SECCLASS_NODE, NODE__SENDTO, &ad))
4542effad8dfSPaul Moore 			return NF_DROP;
4543effad8dfSPaul Moore 	}
4544effad8dfSPaul Moore 
4545effad8dfSPaul Moore 	return NF_ACCEPT;
4546effad8dfSPaul Moore }
4547effad8dfSPaul Moore 
4548effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4549a224be76SDavid S. Miller 					   struct sk_buff *skb,
45501da177e4SLinus Torvalds 					   const struct net_device *in,
45511da177e4SLinus Torvalds 					   const struct net_device *out,
45521da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
45531da177e4SLinus Torvalds {
4554effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET);
45551da177e4SLinus Torvalds }
45561da177e4SLinus Torvalds 
45571da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4558effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4559a224be76SDavid S. Miller 					   struct sk_buff *skb,
45601da177e4SLinus Torvalds 					   const struct net_device *in,
45611da177e4SLinus Torvalds 					   const struct net_device *out,
45621da177e4SLinus Torvalds 					   int (*okfn)(struct sk_buff *))
45631da177e4SLinus Torvalds {
4564effad8dfSPaul Moore 	return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
45651da177e4SLinus Torvalds }
45661da177e4SLinus Torvalds #endif	/* IPV6 */
45671da177e4SLinus Torvalds 
45681da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
45691da177e4SLinus Torvalds 
45701da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
45711da177e4SLinus Torvalds {
45721da177e4SLinus Torvalds 	int err;
45731da177e4SLinus Torvalds 
45741da177e4SLinus Torvalds 	err = secondary_ops->netlink_send(sk, skb);
45751da177e4SLinus Torvalds 	if (err)
45761da177e4SLinus Torvalds 		return err;
45771da177e4SLinus Torvalds 
45781da177e4SLinus Torvalds 	if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
45791da177e4SLinus Torvalds 		err = selinux_nlmsg_perm(sk, skb);
45801da177e4SLinus Torvalds 
45811da177e4SLinus Torvalds 	return err;
45821da177e4SLinus Torvalds }
45831da177e4SLinus Torvalds 
4584c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability)
45851da177e4SLinus Torvalds {
4586c7bdb545SDarrel Goeddel 	int err;
4587c7bdb545SDarrel Goeddel 	struct avc_audit_data ad;
4588c7bdb545SDarrel Goeddel 
4589c7bdb545SDarrel Goeddel 	err = secondary_ops->netlink_recv(skb, capability);
4590c7bdb545SDarrel Goeddel 	if (err)
4591c7bdb545SDarrel Goeddel 		return err;
4592c7bdb545SDarrel Goeddel 
4593c7bdb545SDarrel Goeddel 	AVC_AUDIT_DATA_INIT(&ad, CAP);
4594c7bdb545SDarrel Goeddel 	ad.u.cap = capability;
4595c7bdb545SDarrel Goeddel 
4596c7bdb545SDarrel Goeddel 	return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid,
4597c7bdb545SDarrel Goeddel 	                    SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad);
45981da177e4SLinus Torvalds }
45991da177e4SLinus Torvalds 
46001da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
46011da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
46021da177e4SLinus Torvalds 			      u16 sclass)
46031da177e4SLinus Torvalds {
46041da177e4SLinus Torvalds 	struct task_security_struct *tsec = task->security;
46051da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
46061da177e4SLinus Torvalds 
460789d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
46081da177e4SLinus Torvalds 	if (!isec)
46091da177e4SLinus Torvalds 		return -ENOMEM;
46101da177e4SLinus Torvalds 
46111da177e4SLinus Torvalds 	isec->sclass = sclass;
46121da177e4SLinus Torvalds 	isec->sid = tsec->sid;
46131da177e4SLinus Torvalds 	perm->security = isec;
46141da177e4SLinus Torvalds 
46151da177e4SLinus Torvalds 	return 0;
46161da177e4SLinus Torvalds }
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
46191da177e4SLinus Torvalds {
46201da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
46211da177e4SLinus Torvalds 	perm->security = NULL;
46221da177e4SLinus Torvalds 	kfree(isec);
46231da177e4SLinus Torvalds }
46241da177e4SLinus Torvalds 
46251da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
46261da177e4SLinus Torvalds {
46271da177e4SLinus Torvalds 	struct msg_security_struct *msec;
46281da177e4SLinus Torvalds 
462989d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
46301da177e4SLinus Torvalds 	if (!msec)
46311da177e4SLinus Torvalds 		return -ENOMEM;
46321da177e4SLinus Torvalds 
46331da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
46341da177e4SLinus Torvalds 	msg->security = msec;
46351da177e4SLinus Torvalds 
46361da177e4SLinus Torvalds 	return 0;
46371da177e4SLinus Torvalds }
46381da177e4SLinus Torvalds 
46391da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
46401da177e4SLinus Torvalds {
46411da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
46421da177e4SLinus Torvalds 
46431da177e4SLinus Torvalds 	msg->security = NULL;
46441da177e4SLinus Torvalds 	kfree(msec);
46451da177e4SLinus Torvalds }
46461da177e4SLinus Torvalds 
46471da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
46486af963f1SStephen Smalley 			u32 perms)
46491da177e4SLinus Torvalds {
46501da177e4SLinus Torvalds 	struct task_security_struct *tsec;
46511da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
46521da177e4SLinus Torvalds 	struct avc_audit_data ad;
46531da177e4SLinus Torvalds 
46541da177e4SLinus Torvalds 	tsec = current->security;
46551da177e4SLinus Torvalds 	isec = ipc_perms->security;
46561da177e4SLinus Torvalds 
46571da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
46581da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
46591da177e4SLinus Torvalds 
46606af963f1SStephen Smalley 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
46611da177e4SLinus Torvalds }
46621da177e4SLinus Torvalds 
46631da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
46641da177e4SLinus Torvalds {
46651da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
46661da177e4SLinus Torvalds }
46671da177e4SLinus Torvalds 
46681da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
46691da177e4SLinus Torvalds {
46701da177e4SLinus Torvalds 	msg_msg_free_security(msg);
46711da177e4SLinus Torvalds }
46721da177e4SLinus Torvalds 
46731da177e4SLinus Torvalds /* message queue security operations */
46741da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
46751da177e4SLinus Torvalds {
46761da177e4SLinus Torvalds 	struct task_security_struct *tsec;
46771da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
46781da177e4SLinus Torvalds 	struct avc_audit_data ad;
46791da177e4SLinus Torvalds 	int rc;
46801da177e4SLinus Torvalds 
46811da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
46821da177e4SLinus Torvalds 	if (rc)
46831da177e4SLinus Torvalds 		return rc;
46841da177e4SLinus Torvalds 
46851da177e4SLinus Torvalds 	tsec = current->security;
46861da177e4SLinus Torvalds 	isec = msq->q_perm.security;
46871da177e4SLinus Torvalds 
46881da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
46891da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
46901da177e4SLinus Torvalds 
46911da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
46921da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
46931da177e4SLinus Torvalds 	if (rc) {
46941da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
46951da177e4SLinus Torvalds 		return rc;
46961da177e4SLinus Torvalds 	}
46971da177e4SLinus Torvalds 	return 0;
46981da177e4SLinus Torvalds }
46991da177e4SLinus Torvalds 
47001da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
47011da177e4SLinus Torvalds {
47021da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
47031da177e4SLinus Torvalds }
47041da177e4SLinus Torvalds 
47051da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
47061da177e4SLinus Torvalds {
47071da177e4SLinus Torvalds 	struct task_security_struct *tsec;
47081da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
47091da177e4SLinus Torvalds 	struct avc_audit_data ad;
47101da177e4SLinus Torvalds 
47111da177e4SLinus Torvalds 	tsec = current->security;
47121da177e4SLinus Torvalds 	isec = msq->q_perm.security;
47131da177e4SLinus Torvalds 
47141da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
47151da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
47161da177e4SLinus Torvalds 
47171da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
47181da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
47191da177e4SLinus Torvalds }
47201da177e4SLinus Torvalds 
47211da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
47221da177e4SLinus Torvalds {
47231da177e4SLinus Torvalds 	int err;
47241da177e4SLinus Torvalds 	int perms;
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds 	switch(cmd) {
47271da177e4SLinus Torvalds 	case IPC_INFO:
47281da177e4SLinus Torvalds 	case MSG_INFO:
47291da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
47301da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
47311da177e4SLinus Torvalds 	case IPC_STAT:
47321da177e4SLinus Torvalds 	case MSG_STAT:
47331da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
47341da177e4SLinus Torvalds 		break;
47351da177e4SLinus Torvalds 	case IPC_SET:
47361da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
47371da177e4SLinus Torvalds 		break;
47381da177e4SLinus Torvalds 	case IPC_RMID:
47391da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
47401da177e4SLinus Torvalds 		break;
47411da177e4SLinus Torvalds 	default:
47421da177e4SLinus Torvalds 		return 0;
47431da177e4SLinus Torvalds 	}
47441da177e4SLinus Torvalds 
47456af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
47461da177e4SLinus Torvalds 	return err;
47471da177e4SLinus Torvalds }
47481da177e4SLinus Torvalds 
47491da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
47501da177e4SLinus Torvalds {
47511da177e4SLinus Torvalds 	struct task_security_struct *tsec;
47521da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
47531da177e4SLinus Torvalds 	struct msg_security_struct *msec;
47541da177e4SLinus Torvalds 	struct avc_audit_data ad;
47551da177e4SLinus Torvalds 	int rc;
47561da177e4SLinus Torvalds 
47571da177e4SLinus Torvalds 	tsec = current->security;
47581da177e4SLinus Torvalds 	isec = msq->q_perm.security;
47591da177e4SLinus Torvalds 	msec = msg->security;
47601da177e4SLinus Torvalds 
47611da177e4SLinus Torvalds 	/*
47621da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
47631da177e4SLinus Torvalds 	 */
47641da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
47651da177e4SLinus Torvalds 		/*
47661da177e4SLinus Torvalds 		 * Compute new sid based on current process and
47671da177e4SLinus Torvalds 		 * message queue this message will be stored in
47681da177e4SLinus Torvalds 		 */
47691da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid,
47701da177e4SLinus Torvalds 					     isec->sid,
47711da177e4SLinus Torvalds 					     SECCLASS_MSG,
47721da177e4SLinus Torvalds 					     &msec->sid);
47731da177e4SLinus Torvalds 		if (rc)
47741da177e4SLinus Torvalds 			return rc;
47751da177e4SLinus Torvalds 	}
47761da177e4SLinus Torvalds 
47771da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
47781da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
47791da177e4SLinus Torvalds 
47801da177e4SLinus Torvalds 	/* Can this process write to the queue? */
47811da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
47821da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
47831da177e4SLinus Torvalds 	if (!rc)
47841da177e4SLinus Torvalds 		/* Can this process send the message */
47851da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
47861da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__SEND, &ad);
47871da177e4SLinus Torvalds 	if (!rc)
47881da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
47891da177e4SLinus Torvalds 		rc = avc_has_perm(msec->sid, isec->sid,
47901da177e4SLinus Torvalds 				  SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad);
47911da177e4SLinus Torvalds 
47921da177e4SLinus Torvalds 	return rc;
47931da177e4SLinus Torvalds }
47941da177e4SLinus Torvalds 
47951da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
47961da177e4SLinus Torvalds 				    struct task_struct *target,
47971da177e4SLinus Torvalds 				    long type, int mode)
47981da177e4SLinus Torvalds {
47991da177e4SLinus Torvalds 	struct task_security_struct *tsec;
48001da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48011da177e4SLinus Torvalds 	struct msg_security_struct *msec;
48021da177e4SLinus Torvalds 	struct avc_audit_data ad;
48031da177e4SLinus Torvalds 	int rc;
48041da177e4SLinus Torvalds 
48051da177e4SLinus Torvalds 	tsec = target->security;
48061da177e4SLinus Torvalds 	isec = msq->q_perm.security;
48071da177e4SLinus Torvalds 	msec = msg->security;
48081da177e4SLinus Torvalds 
48091da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48101da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
48111da177e4SLinus Torvalds 
48121da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid,
48131da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
48141da177e4SLinus Torvalds 	if (!rc)
48151da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
48161da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
48171da177e4SLinus Torvalds 	return rc;
48181da177e4SLinus Torvalds }
48191da177e4SLinus Torvalds 
48201da177e4SLinus Torvalds /* Shared Memory security operations */
48211da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
48221da177e4SLinus Torvalds {
48231da177e4SLinus Torvalds 	struct task_security_struct *tsec;
48241da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48251da177e4SLinus Torvalds 	struct avc_audit_data ad;
48261da177e4SLinus Torvalds 	int rc;
48271da177e4SLinus Torvalds 
48281da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
48291da177e4SLinus Torvalds 	if (rc)
48301da177e4SLinus Torvalds 		return rc;
48311da177e4SLinus Torvalds 
48321da177e4SLinus Torvalds 	tsec = current->security;
48331da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
48341da177e4SLinus Torvalds 
48351da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48361da177e4SLinus Torvalds  	ad.u.ipc_id = shp->shm_perm.key;
48371da177e4SLinus Torvalds 
48381da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
48391da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
48401da177e4SLinus Torvalds 	if (rc) {
48411da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
48421da177e4SLinus Torvalds 		return rc;
48431da177e4SLinus Torvalds 	}
48441da177e4SLinus Torvalds 	return 0;
48451da177e4SLinus Torvalds }
48461da177e4SLinus Torvalds 
48471da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
48481da177e4SLinus Torvalds {
48491da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
48501da177e4SLinus Torvalds }
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
48531da177e4SLinus Torvalds {
48541da177e4SLinus Torvalds 	struct task_security_struct *tsec;
48551da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
48561da177e4SLinus Torvalds 	struct avc_audit_data ad;
48571da177e4SLinus Torvalds 
48581da177e4SLinus Torvalds 	tsec = current->security;
48591da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
48601da177e4SLinus Torvalds 
48611da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
48621da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
48631da177e4SLinus Torvalds 
48641da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
48651da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
48661da177e4SLinus Torvalds }
48671da177e4SLinus Torvalds 
48681da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
48691da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
48701da177e4SLinus Torvalds {
48711da177e4SLinus Torvalds 	int perms;
48721da177e4SLinus Torvalds 	int err;
48731da177e4SLinus Torvalds 
48741da177e4SLinus Torvalds 	switch(cmd) {
48751da177e4SLinus Torvalds 	case IPC_INFO:
48761da177e4SLinus Torvalds 	case SHM_INFO:
48771da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
48781da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
48791da177e4SLinus Torvalds 	case IPC_STAT:
48801da177e4SLinus Torvalds 	case SHM_STAT:
48811da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
48821da177e4SLinus Torvalds 		break;
48831da177e4SLinus Torvalds 	case IPC_SET:
48841da177e4SLinus Torvalds 		perms = SHM__SETATTR;
48851da177e4SLinus Torvalds 		break;
48861da177e4SLinus Torvalds 	case SHM_LOCK:
48871da177e4SLinus Torvalds 	case SHM_UNLOCK:
48881da177e4SLinus Torvalds 		perms = SHM__LOCK;
48891da177e4SLinus Torvalds 		break;
48901da177e4SLinus Torvalds 	case IPC_RMID:
48911da177e4SLinus Torvalds 		perms = SHM__DESTROY;
48921da177e4SLinus Torvalds 		break;
48931da177e4SLinus Torvalds 	default:
48941da177e4SLinus Torvalds 		return 0;
48951da177e4SLinus Torvalds 	}
48961da177e4SLinus Torvalds 
48976af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
48981da177e4SLinus Torvalds 	return err;
48991da177e4SLinus Torvalds }
49001da177e4SLinus Torvalds 
49011da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
49021da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
49031da177e4SLinus Torvalds {
49041da177e4SLinus Torvalds 	u32 perms;
49051da177e4SLinus Torvalds 	int rc;
49061da177e4SLinus Torvalds 
49071da177e4SLinus Torvalds 	rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg);
49081da177e4SLinus Torvalds 	if (rc)
49091da177e4SLinus Torvalds 		return rc;
49101da177e4SLinus Torvalds 
49111da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
49121da177e4SLinus Torvalds 		perms = SHM__READ;
49131da177e4SLinus Torvalds 	else
49141da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
49151da177e4SLinus Torvalds 
49166af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
49171da177e4SLinus Torvalds }
49181da177e4SLinus Torvalds 
49191da177e4SLinus Torvalds /* Semaphore security operations */
49201da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
49211da177e4SLinus Torvalds {
49221da177e4SLinus Torvalds 	struct task_security_struct *tsec;
49231da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49241da177e4SLinus Torvalds 	struct avc_audit_data ad;
49251da177e4SLinus Torvalds 	int rc;
49261da177e4SLinus Torvalds 
49271da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
49281da177e4SLinus Torvalds 	if (rc)
49291da177e4SLinus Torvalds 		return rc;
49301da177e4SLinus Torvalds 
49311da177e4SLinus Torvalds 	tsec = current->security;
49321da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
49331da177e4SLinus Torvalds 
49341da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
49351da177e4SLinus Torvalds  	ad.u.ipc_id = sma->sem_perm.key;
49361da177e4SLinus Torvalds 
49371da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
49381da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
49391da177e4SLinus Torvalds 	if (rc) {
49401da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
49411da177e4SLinus Torvalds 		return rc;
49421da177e4SLinus Torvalds 	}
49431da177e4SLinus Torvalds 	return 0;
49441da177e4SLinus Torvalds }
49451da177e4SLinus Torvalds 
49461da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
49471da177e4SLinus Torvalds {
49481da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
49491da177e4SLinus Torvalds }
49501da177e4SLinus Torvalds 
49511da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
49521da177e4SLinus Torvalds {
49531da177e4SLinus Torvalds 	struct task_security_struct *tsec;
49541da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
49551da177e4SLinus Torvalds 	struct avc_audit_data ad;
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds 	tsec = current->security;
49581da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
49591da177e4SLinus Torvalds 
49601da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
49611da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
49621da177e4SLinus Torvalds 
49631da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
49641da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
49651da177e4SLinus Torvalds }
49661da177e4SLinus Torvalds 
49671da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
49681da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
49691da177e4SLinus Torvalds {
49701da177e4SLinus Torvalds 	int err;
49711da177e4SLinus Torvalds 	u32 perms;
49721da177e4SLinus Torvalds 
49731da177e4SLinus Torvalds 	switch(cmd) {
49741da177e4SLinus Torvalds 	case IPC_INFO:
49751da177e4SLinus Torvalds 	case SEM_INFO:
49761da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
49771da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
49781da177e4SLinus Torvalds 	case GETPID:
49791da177e4SLinus Torvalds 	case GETNCNT:
49801da177e4SLinus Torvalds 	case GETZCNT:
49811da177e4SLinus Torvalds 		perms = SEM__GETATTR;
49821da177e4SLinus Torvalds 		break;
49831da177e4SLinus Torvalds 	case GETVAL:
49841da177e4SLinus Torvalds 	case GETALL:
49851da177e4SLinus Torvalds 		perms = SEM__READ;
49861da177e4SLinus Torvalds 		break;
49871da177e4SLinus Torvalds 	case SETVAL:
49881da177e4SLinus Torvalds 	case SETALL:
49891da177e4SLinus Torvalds 		perms = SEM__WRITE;
49901da177e4SLinus Torvalds 		break;
49911da177e4SLinus Torvalds 	case IPC_RMID:
49921da177e4SLinus Torvalds 		perms = SEM__DESTROY;
49931da177e4SLinus Torvalds 		break;
49941da177e4SLinus Torvalds 	case IPC_SET:
49951da177e4SLinus Torvalds 		perms = SEM__SETATTR;
49961da177e4SLinus Torvalds 		break;
49971da177e4SLinus Torvalds 	case IPC_STAT:
49981da177e4SLinus Torvalds 	case SEM_STAT:
49991da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
50001da177e4SLinus Torvalds 		break;
50011da177e4SLinus Torvalds 	default:
50021da177e4SLinus Torvalds 		return 0;
50031da177e4SLinus Torvalds 	}
50041da177e4SLinus Torvalds 
50056af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
50061da177e4SLinus Torvalds 	return err;
50071da177e4SLinus Torvalds }
50081da177e4SLinus Torvalds 
50091da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
50101da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
50111da177e4SLinus Torvalds {
50121da177e4SLinus Torvalds 	u32 perms;
50131da177e4SLinus Torvalds 
50141da177e4SLinus Torvalds 	if (alter)
50151da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
50161da177e4SLinus Torvalds 	else
50171da177e4SLinus Torvalds 		perms = SEM__READ;
50181da177e4SLinus Torvalds 
50196af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
50201da177e4SLinus Torvalds }
50211da177e4SLinus Torvalds 
50221da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
50231da177e4SLinus Torvalds {
50241da177e4SLinus Torvalds 	u32 av = 0;
50251da177e4SLinus Torvalds 
50261da177e4SLinus Torvalds 	av = 0;
50271da177e4SLinus Torvalds 	if (flag & S_IRUGO)
50281da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
50291da177e4SLinus Torvalds 	if (flag & S_IWUGO)
50301da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
50311da177e4SLinus Torvalds 
50321da177e4SLinus Torvalds 	if (av == 0)
50331da177e4SLinus Torvalds 		return 0;
50341da177e4SLinus Torvalds 
50356af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
50361da177e4SLinus Torvalds }
50371da177e4SLinus Torvalds 
5038713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5039713a04aeSAhmed S. Darwish {
5040713a04aeSAhmed S. Darwish 	struct ipc_security_struct *isec = ipcp->security;
5041713a04aeSAhmed S. Darwish 	*secid = isec->sid;
5042713a04aeSAhmed S. Darwish }
5043713a04aeSAhmed S. Darwish 
50441da177e4SLinus Torvalds /* module stacking operations */
50451da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops)
50461da177e4SLinus Torvalds {
50471da177e4SLinus Torvalds 	if (secondary_ops != original_ops) {
5048fadcdb45SEric Paris 		printk(KERN_ERR "%s:  There is already a secondary security "
5049dd6f953aSHarvey Harrison 		       "module registered.\n", __func__);
50501da177e4SLinus Torvalds 		return -EINVAL;
50511da177e4SLinus Torvalds  	}
50521da177e4SLinus Torvalds 
50531da177e4SLinus Torvalds 	secondary_ops = ops;
50541da177e4SLinus Torvalds 
50551da177e4SLinus Torvalds 	printk(KERN_INFO "%s:  Registering secondary module %s\n",
5056dd6f953aSHarvey Harrison 	       __func__,
50571da177e4SLinus Torvalds 	       name);
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds 	return 0;
50601da177e4SLinus Torvalds }
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode)
50631da177e4SLinus Torvalds {
50641da177e4SLinus Torvalds 	if (inode)
50651da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
50661da177e4SLinus Torvalds }
50671da177e4SLinus Torvalds 
50681da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
506904ff9708SAl Viro 			       char *name, char **value)
50701da177e4SLinus Torvalds {
50711da177e4SLinus Torvalds 	struct task_security_struct *tsec;
50728c8570fbSDustin Kirkland 	u32 sid;
50731da177e4SLinus Torvalds 	int error;
507404ff9708SAl Viro 	unsigned len;
50751da177e4SLinus Torvalds 
50761da177e4SLinus Torvalds 	if (current != p) {
50771da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__GETATTR);
50781da177e4SLinus Torvalds 		if (error)
50791da177e4SLinus Torvalds 			return error;
50801da177e4SLinus Torvalds 	}
50811da177e4SLinus Torvalds 
50821da177e4SLinus Torvalds 	tsec = p->security;
50831da177e4SLinus Torvalds 
50841da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
50851da177e4SLinus Torvalds 		sid = tsec->sid;
50861da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
50871da177e4SLinus Torvalds 		sid = tsec->osid;
50881da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
50891da177e4SLinus Torvalds 		sid = tsec->exec_sid;
50901da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
50911da177e4SLinus Torvalds 		sid = tsec->create_sid;
50924eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
50934eb582cfSMichael LeMay 		sid = tsec->keycreate_sid;
509442c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
509542c3e03eSEric Paris 		sid = tsec->sockcreate_sid;
50961da177e4SLinus Torvalds 	else
50971da177e4SLinus Torvalds 		return -EINVAL;
50981da177e4SLinus Torvalds 
50991da177e4SLinus Torvalds 	if (!sid)
51001da177e4SLinus Torvalds 		return 0;
51011da177e4SLinus Torvalds 
510204ff9708SAl Viro 	error = security_sid_to_context(sid, value, &len);
510304ff9708SAl Viro 	if (error)
510404ff9708SAl Viro 		return error;
510504ff9708SAl Viro 	return len;
51061da177e4SLinus Torvalds }
51071da177e4SLinus Torvalds 
51081da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
51091da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
51101da177e4SLinus Torvalds {
51111da177e4SLinus Torvalds 	struct task_security_struct *tsec;
51120356357cSRoland McGrath 	struct task_struct *tracer;
51131da177e4SLinus Torvalds 	u32 sid = 0;
51141da177e4SLinus Torvalds 	int error;
51151da177e4SLinus Torvalds 	char *str = value;
51161da177e4SLinus Torvalds 
51171da177e4SLinus Torvalds 	if (current != p) {
51181da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
51191da177e4SLinus Torvalds 		   security attributes. */
51201da177e4SLinus Torvalds 		return -EACCES;
51211da177e4SLinus Torvalds 	}
51221da177e4SLinus Torvalds 
51231da177e4SLinus Torvalds 	/*
51241da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
51251da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
51261da177e4SLinus Torvalds 	 * above restriction is ever removed.
51271da177e4SLinus Torvalds 	 */
51281da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
51291da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETEXEC);
51301da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
51311da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETFSCREATE);
51324eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
51334eb582cfSMichael LeMay 		error = task_has_perm(current, p, PROCESS__SETKEYCREATE);
513442c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
513542c3e03eSEric Paris 		error = task_has_perm(current, p, PROCESS__SETSOCKCREATE);
51361da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
51371da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETCURRENT);
51381da177e4SLinus Torvalds 	else
51391da177e4SLinus Torvalds 		error = -EINVAL;
51401da177e4SLinus Torvalds 	if (error)
51411da177e4SLinus Torvalds 		return error;
51421da177e4SLinus Torvalds 
51431da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
51441da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
51451da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
51461da177e4SLinus Torvalds 			str[size-1] = 0;
51471da177e4SLinus Torvalds 			size--;
51481da177e4SLinus Torvalds 		}
51491da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
51501da177e4SLinus Torvalds 		if (error)
51511da177e4SLinus Torvalds 			return error;
51521da177e4SLinus Torvalds 	}
51531da177e4SLinus Torvalds 
51541da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
51551da177e4SLinus Torvalds 	   performed during the actual operation (execve,
51561da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
51571da177e4SLinus Torvalds 	   operation.  See selinux_bprm_set_security for the execve
51581da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
51591da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
51601da177e4SLinus Torvalds 	tsec = p->security;
51611da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
51621da177e4SLinus Torvalds 		tsec->exec_sid = sid;
51631da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
51641da177e4SLinus Torvalds 		tsec->create_sid = sid;
51654eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate")) {
51664eb582cfSMichael LeMay 		error = may_create_key(sid, p);
51674eb582cfSMichael LeMay 		if (error)
51684eb582cfSMichael LeMay 			return error;
51694eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
517042c3e03eSEric Paris 	} else if (!strcmp(name, "sockcreate"))
517142c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
517242c3e03eSEric Paris 	else if (!strcmp(name, "current")) {
51731da177e4SLinus Torvalds 		struct av_decision avd;
51741da177e4SLinus Torvalds 
51751da177e4SLinus Torvalds 		if (sid == 0)
51761da177e4SLinus Torvalds 			return -EINVAL;
51771da177e4SLinus Torvalds 
51781da177e4SLinus Torvalds 		/* Only allow single threaded processes to change context */
51791da177e4SLinus Torvalds 		if (atomic_read(&p->mm->mm_users) != 1) {
51801da177e4SLinus Torvalds 			struct task_struct *g, *t;
51811da177e4SLinus Torvalds 			struct mm_struct *mm = p->mm;
51821da177e4SLinus Torvalds 			read_lock(&tasklist_lock);
51831da177e4SLinus Torvalds 			do_each_thread(g, t)
51841da177e4SLinus Torvalds 				if (t->mm == mm && t != p) {
51851da177e4SLinus Torvalds 					read_unlock(&tasklist_lock);
51861da177e4SLinus Torvalds 					return -EPERM;
51871da177e4SLinus Torvalds 				}
51881da177e4SLinus Torvalds 			while_each_thread(g, t);
51891da177e4SLinus Torvalds 			read_unlock(&tasklist_lock);
51901da177e4SLinus Torvalds                 }
51911da177e4SLinus Torvalds 
51921da177e4SLinus Torvalds 		/* Check permissions for the transition. */
51931da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
51941da177e4SLinus Torvalds 		                     PROCESS__DYNTRANSITION, NULL);
51951da177e4SLinus Torvalds 		if (error)
51961da177e4SLinus Torvalds 			return error;
51971da177e4SLinus Torvalds 
51981da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
51991da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
52001da177e4SLinus Torvalds 		task_lock(p);
52010356357cSRoland McGrath 		rcu_read_lock();
52020356357cSRoland McGrath 		tracer = task_tracer_task(p);
52030356357cSRoland McGrath 		if (tracer != NULL) {
52040356357cSRoland McGrath 			struct task_security_struct *ptsec = tracer->security;
52050356357cSRoland McGrath 			u32 ptsid = ptsec->sid;
52060356357cSRoland McGrath 			rcu_read_unlock();
52070356357cSRoland McGrath 			error = avc_has_perm_noaudit(ptsid, sid,
52081da177e4SLinus Torvalds 						     SECCLASS_PROCESS,
52092c3c05dbSStephen Smalley 						     PROCESS__PTRACE, 0, &avd);
52101da177e4SLinus Torvalds 			if (!error)
52111da177e4SLinus Torvalds 				tsec->sid = sid;
52121da177e4SLinus Torvalds 			task_unlock(p);
52130356357cSRoland McGrath 			avc_audit(ptsid, sid, SECCLASS_PROCESS,
52141da177e4SLinus Torvalds 				  PROCESS__PTRACE, &avd, error, NULL);
52151da177e4SLinus Torvalds 			if (error)
52161da177e4SLinus Torvalds 				return error;
52171da177e4SLinus Torvalds 		} else {
52180356357cSRoland McGrath 			rcu_read_unlock();
52191da177e4SLinus Torvalds 			tsec->sid = sid;
52201da177e4SLinus Torvalds 			task_unlock(p);
52211da177e4SLinus Torvalds 		}
52221da177e4SLinus Torvalds 	}
52231da177e4SLinus Torvalds 	else
52241da177e4SLinus Torvalds 		return -EINVAL;
52251da177e4SLinus Torvalds 
52261da177e4SLinus Torvalds 	return size;
52271da177e4SLinus Torvalds }
52281da177e4SLinus Torvalds 
5229dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5230dc49c1f9SCatherine Zhang {
5231dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
5232dc49c1f9SCatherine Zhang }
5233dc49c1f9SCatherine Zhang 
523463cb3449SDavid Howells static int selinux_secctx_to_secid(char *secdata, u32 seclen, u32 *secid)
523563cb3449SDavid Howells {
523663cb3449SDavid Howells 	return security_context_to_sid(secdata, seclen, secid);
523763cb3449SDavid Howells }
523863cb3449SDavid Howells 
5239dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
5240dc49c1f9SCatherine Zhang {
5241dc49c1f9SCatherine Zhang 	kfree(secdata);
5242dc49c1f9SCatherine Zhang }
5243dc49c1f9SCatherine Zhang 
5244d720024eSMichael LeMay #ifdef CONFIG_KEYS
5245d720024eSMichael LeMay 
52467e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk,
52477e047ef5SDavid Howells 			     unsigned long flags)
5248d720024eSMichael LeMay {
5249d720024eSMichael LeMay 	struct task_security_struct *tsec = tsk->security;
5250d720024eSMichael LeMay 	struct key_security_struct *ksec;
5251d720024eSMichael LeMay 
5252d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5253d720024eSMichael LeMay 	if (!ksec)
5254d720024eSMichael LeMay 		return -ENOMEM;
5255d720024eSMichael LeMay 
52564eb582cfSMichael LeMay 	if (tsec->keycreate_sid)
52574eb582cfSMichael LeMay 		ksec->sid = tsec->keycreate_sid;
52584eb582cfSMichael LeMay 	else
5259d720024eSMichael LeMay 		ksec->sid = tsec->sid;
5260d720024eSMichael LeMay 	k->security = ksec;
5261d720024eSMichael LeMay 
5262d720024eSMichael LeMay 	return 0;
5263d720024eSMichael LeMay }
5264d720024eSMichael LeMay 
5265d720024eSMichael LeMay static void selinux_key_free(struct key *k)
5266d720024eSMichael LeMay {
5267d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
5268d720024eSMichael LeMay 
5269d720024eSMichael LeMay 	k->security = NULL;
5270d720024eSMichael LeMay 	kfree(ksec);
5271d720024eSMichael LeMay }
5272d720024eSMichael LeMay 
5273d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
5274d720024eSMichael LeMay 			    struct task_struct *ctx,
5275d720024eSMichael LeMay 			    key_perm_t perm)
5276d720024eSMichael LeMay {
5277d720024eSMichael LeMay 	struct key *key;
5278d720024eSMichael LeMay 	struct task_security_struct *tsec;
5279d720024eSMichael LeMay 	struct key_security_struct *ksec;
5280d720024eSMichael LeMay 
5281d720024eSMichael LeMay 	key = key_ref_to_ptr(key_ref);
5282d720024eSMichael LeMay 
5283d720024eSMichael LeMay 	tsec = ctx->security;
5284d720024eSMichael LeMay 	ksec = key->security;
5285d720024eSMichael LeMay 
5286d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
5287d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
5288d720024eSMichael LeMay 	   appear to be created. */
5289d720024eSMichael LeMay 	if (perm == 0)
5290d720024eSMichael LeMay 		return 0;
5291d720024eSMichael LeMay 
5292d720024eSMichael LeMay 	return avc_has_perm(tsec->sid, ksec->sid,
5293d720024eSMichael LeMay 			    SECCLASS_KEY, perm, NULL);
5294d720024eSMichael LeMay }
5295d720024eSMichael LeMay 
5296d720024eSMichael LeMay #endif
5297d720024eSMichael LeMay 
52981da177e4SLinus Torvalds static struct security_operations selinux_ops = {
5299076c54c5SAhmed S. Darwish 	.name =				"selinux",
5300076c54c5SAhmed S. Darwish 
53011da177e4SLinus Torvalds 	.ptrace =			selinux_ptrace,
53021da177e4SLinus Torvalds 	.capget =			selinux_capget,
53031da177e4SLinus Torvalds 	.capset_check =			selinux_capset_check,
53041da177e4SLinus Torvalds 	.capset_set =			selinux_capset_set,
53051da177e4SLinus Torvalds 	.sysctl =			selinux_sysctl,
53061da177e4SLinus Torvalds 	.capable =			selinux_capable,
53071da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
53081da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
53091da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
53101da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
53111da177e4SLinus Torvalds 
53121da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
53131da177e4SLinus Torvalds         .netlink_recv =			selinux_netlink_recv,
53141da177e4SLinus Torvalds 
53151da177e4SLinus Torvalds 	.bprm_alloc_security =		selinux_bprm_alloc_security,
53161da177e4SLinus Torvalds 	.bprm_free_security =		selinux_bprm_free_security,
53171da177e4SLinus Torvalds 	.bprm_apply_creds =		selinux_bprm_apply_creds,
53181da177e4SLinus Torvalds 	.bprm_post_apply_creds =	selinux_bprm_post_apply_creds,
53191da177e4SLinus Torvalds 	.bprm_set_security =		selinux_bprm_set_security,
53201da177e4SLinus Torvalds 	.bprm_check_security =		selinux_bprm_check_security,
53211da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
53221da177e4SLinus Torvalds 
53231da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
53241da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
53251da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
53261da177e4SLinus Torvalds 	.sb_kern_mount =	        selinux_sb_kern_mount,
53271da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
53281da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
53291da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
5330c9180a57SEric Paris 	.sb_get_mnt_opts =		selinux_get_mnt_opts,
5331c9180a57SEric Paris 	.sb_set_mnt_opts =		selinux_set_mnt_opts,
5332c9180a57SEric Paris 	.sb_clone_mnt_opts = 		selinux_sb_clone_mnt_opts,
5333e0007529SEric Paris 	.sb_parse_opts_str = 		selinux_parse_opts_str,
5334e0007529SEric Paris 
53351da177e4SLinus Torvalds 
53361da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
53371da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
53385e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
53391da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
53401da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
53411da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
53421da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
53431da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
53441da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
53451da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
53461da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
53471da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
53481da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
53491da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
53501da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
53511da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
53521da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
53531da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
53541da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
53551da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
53561da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
53571da177e4SLinus Torvalds 	.inode_getsecurity =            selinux_inode_getsecurity,
53581da177e4SLinus Torvalds 	.inode_setsecurity =            selinux_inode_setsecurity,
53591da177e4SLinus Torvalds 	.inode_listsecurity =           selinux_inode_listsecurity,
5360b5376771SSerge E. Hallyn 	.inode_need_killpriv =		selinux_inode_need_killpriv,
5361b5376771SSerge E. Hallyn 	.inode_killpriv =		selinux_inode_killpriv,
5362713a04aeSAhmed S. Darwish 	.inode_getsecid =               selinux_inode_getsecid,
53631da177e4SLinus Torvalds 
53641da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
53651da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
53661da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
53671da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
53681da177e4SLinus Torvalds 	.file_mmap =			selinux_file_mmap,
53691da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
53701da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
53711da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
53721da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
53731da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
53741da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
53751da177e4SLinus Torvalds 
5376788e7dd4SYuichi Nakamura 	.dentry_open =                  selinux_dentry_open,
5377788e7dd4SYuichi Nakamura 
53781da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
53791da177e4SLinus Torvalds 	.task_alloc_security =		selinux_task_alloc_security,
53801da177e4SLinus Torvalds 	.task_free_security =		selinux_task_free_security,
53811da177e4SLinus Torvalds 	.task_setuid =			selinux_task_setuid,
53821da177e4SLinus Torvalds 	.task_post_setuid =		selinux_task_post_setuid,
53831da177e4SLinus Torvalds 	.task_setgid =			selinux_task_setgid,
53841da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
53851da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
53861da177e4SLinus Torvalds 	.task_getsid =		        selinux_task_getsid,
5387f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
53881da177e4SLinus Torvalds 	.task_setgroups =		selinux_task_setgroups,
53891da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
539003e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
5391a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
53921da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
53931da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
53941da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
539535601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
53961da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
53971da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
53981da177e4SLinus Torvalds 	.task_prctl =			selinux_task_prctl,
53991da177e4SLinus Torvalds 	.task_reparent_to_init =	selinux_task_reparent_to_init,
54001da177e4SLinus Torvalds 	.task_to_inode =                selinux_task_to_inode,
54011da177e4SLinus Torvalds 
54021da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
5403713a04aeSAhmed S. Darwish 	.ipc_getsecid =                 selinux_ipc_getsecid,
54041da177e4SLinus Torvalds 
54051da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
54061da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
54071da177e4SLinus Torvalds 
54081da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
54091da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
54101da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
54111da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
54121da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
54131da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
54141da177e4SLinus Torvalds 
54151da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
54161da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
54171da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
54181da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
54191da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
54201da177e4SLinus Torvalds 
54211da177e4SLinus Torvalds 	.sem_alloc_security = 		selinux_sem_alloc_security,
54221da177e4SLinus Torvalds 	.sem_free_security =  		selinux_sem_free_security,
54231da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
54241da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
54251da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
54261da177e4SLinus Torvalds 
54271da177e4SLinus Torvalds 	.register_security =		selinux_register_security,
54281da177e4SLinus Torvalds 
54291da177e4SLinus Torvalds 	.d_instantiate =                selinux_d_instantiate,
54301da177e4SLinus Torvalds 
54311da177e4SLinus Torvalds 	.getprocattr =                  selinux_getprocattr,
54321da177e4SLinus Torvalds 	.setprocattr =                  selinux_setprocattr,
54331da177e4SLinus Torvalds 
5434dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
543563cb3449SDavid Howells 	.secctx_to_secid =		selinux_secctx_to_secid,
5436dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
5437dc49c1f9SCatherine Zhang 
54381da177e4SLinus Torvalds         .unix_stream_connect =		selinux_socket_unix_stream_connect,
54391da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
54401da177e4SLinus Torvalds 
54411da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
54421da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
54431da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
54441da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
54451da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
54461da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
54471da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
54481da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
54491da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
54501da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
54511da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
54521da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
54531da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
54541da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
54552c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
54562c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
54571da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
54581da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
5459892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
5460beb8d13bSVenkat Yekkirala 	.sk_getsecid = 			selinux_sk_getsecid,
54614237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
54624237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
54634237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
54646b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
54654237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
5466d28d1e08STrent Jaeger 
5467d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
5468d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
5469d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
5470d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
5471c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
5472d28d1e08STrent Jaeger 	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
5473d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
5474c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
5475d28d1e08STrent Jaeger 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
5476e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
5477e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
54781da177e4SLinus Torvalds #endif
5479d720024eSMichael LeMay 
5480d720024eSMichael LeMay #ifdef CONFIG_KEYS
5481d720024eSMichael LeMay 	.key_alloc =                    selinux_key_alloc,
5482d720024eSMichael LeMay 	.key_free =                     selinux_key_free,
5483d720024eSMichael LeMay 	.key_permission =               selinux_key_permission,
5484d720024eSMichael LeMay #endif
54859d57a7f9SAhmed S. Darwish 
54869d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT
54879d57a7f9SAhmed S. Darwish 	.audit_rule_init =		selinux_audit_rule_init,
54889d57a7f9SAhmed S. Darwish 	.audit_rule_known =		selinux_audit_rule_known,
54899d57a7f9SAhmed S. Darwish 	.audit_rule_match =		selinux_audit_rule_match,
54909d57a7f9SAhmed S. Darwish 	.audit_rule_free =		selinux_audit_rule_free,
54919d57a7f9SAhmed S. Darwish #endif
54921da177e4SLinus Torvalds };
54931da177e4SLinus Torvalds 
54941da177e4SLinus Torvalds static __init int selinux_init(void)
54951da177e4SLinus Torvalds {
54961da177e4SLinus Torvalds 	struct task_security_struct *tsec;
54971da177e4SLinus Torvalds 
5498076c54c5SAhmed S. Darwish 	if (!security_module_enable(&selinux_ops)) {
5499076c54c5SAhmed S. Darwish 		selinux_enabled = 0;
5500076c54c5SAhmed S. Darwish 		return 0;
5501076c54c5SAhmed S. Darwish 	}
5502076c54c5SAhmed S. Darwish 
55031da177e4SLinus Torvalds 	if (!selinux_enabled) {
55041da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
55051da177e4SLinus Torvalds 		return 0;
55061da177e4SLinus Torvalds 	}
55071da177e4SLinus Torvalds 
55081da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
55091da177e4SLinus Torvalds 
55101da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
55111da177e4SLinus Torvalds 	if (task_alloc_security(current))
55121da177e4SLinus Torvalds 		panic("SELinux:  Failed to initialize initial task.\n");
55131da177e4SLinus Torvalds 	tsec = current->security;
55141da177e4SLinus Torvalds 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
55151da177e4SLinus Torvalds 
55167cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
55177cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
551820c2df83SPaul Mundt 					    0, SLAB_PANIC, NULL);
55191da177e4SLinus Torvalds 	avc_init();
55201da177e4SLinus Torvalds 
55211da177e4SLinus Torvalds 	original_ops = secondary_ops = security_ops;
55221da177e4SLinus Torvalds 	if (!secondary_ops)
55231da177e4SLinus Torvalds 		panic ("SELinux: No initial security operations\n");
55241da177e4SLinus Torvalds 	if (register_security (&selinux_ops))
55251da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
55261da177e4SLinus Torvalds 
55271da177e4SLinus Torvalds 	if (selinux_enforcing) {
5528fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
55291da177e4SLinus Torvalds 	} else {
5530fadcdb45SEric Paris 		printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
55311da177e4SLinus Torvalds 	}
5532d720024eSMichael LeMay 
5533d720024eSMichael LeMay #ifdef CONFIG_KEYS
5534d720024eSMichael LeMay 	/* Add security information to initial keyrings */
55354eb582cfSMichael LeMay 	selinux_key_alloc(&root_user_keyring, current,
55367e047ef5SDavid Howells 			  KEY_ALLOC_NOT_IN_QUOTA);
55374eb582cfSMichael LeMay 	selinux_key_alloc(&root_session_keyring, current,
55387e047ef5SDavid Howells 			  KEY_ALLOC_NOT_IN_QUOTA);
5539d720024eSMichael LeMay #endif
5540d720024eSMichael LeMay 
55411da177e4SLinus Torvalds 	return 0;
55421da177e4SLinus Torvalds }
55431da177e4SLinus Torvalds 
55441da177e4SLinus Torvalds void selinux_complete_init(void)
55451da177e4SLinus Torvalds {
5546fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
55471da177e4SLinus Torvalds 
55481da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
5549fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
5550ba0c19edSStephen Smalley 	spin_lock(&sb_lock);
55511da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
55521da177e4SLinus Torvalds next_sb:
55531da177e4SLinus Torvalds 	if (!list_empty(&superblock_security_head)) {
55541da177e4SLinus Torvalds 		struct superblock_security_struct *sbsec =
55551da177e4SLinus Torvalds 				list_entry(superblock_security_head.next,
55561da177e4SLinus Torvalds 				           struct superblock_security_struct,
55571da177e4SLinus Torvalds 				           list);
55581da177e4SLinus Torvalds 		struct super_block *sb = sbsec->sb;
55591da177e4SLinus Torvalds 		sb->s_count++;
55601da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
5561ba0c19edSStephen Smalley 		spin_unlock(&sb_lock);
55621da177e4SLinus Torvalds 		down_read(&sb->s_umount);
55631da177e4SLinus Torvalds 		if (sb->s_root)
55641da177e4SLinus Torvalds 			superblock_doinit(sb, NULL);
55651da177e4SLinus Torvalds 		drop_super(sb);
5566ba0c19edSStephen Smalley 		spin_lock(&sb_lock);
55671da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
55681da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
55691da177e4SLinus Torvalds 		goto next_sb;
55701da177e4SLinus Torvalds 	}
55711da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
5572ba0c19edSStephen Smalley 	spin_unlock(&sb_lock);
55731da177e4SLinus Torvalds }
55741da177e4SLinus Torvalds 
55751da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
55761da177e4SLinus Torvalds    all processes and objects when they are created. */
55771da177e4SLinus Torvalds security_initcall(selinux_init);
55781da177e4SLinus Torvalds 
5579c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
55801da177e4SLinus Torvalds 
5581effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = {
5582effad8dfSPaul Moore 	{
5583effad8dfSPaul Moore 		.hook =		selinux_ipv4_postroute,
55841da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
55851da177e4SLinus Torvalds 		.pf =		PF_INET,
55866e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
55871da177e4SLinus Torvalds 		.priority =	NF_IP_PRI_SELINUX_LAST,
5588effad8dfSPaul Moore 	},
5589effad8dfSPaul Moore 	{
5590effad8dfSPaul Moore 		.hook =		selinux_ipv4_forward,
5591effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5592effad8dfSPaul Moore 		.pf =		PF_INET,
5593effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5594effad8dfSPaul Moore 		.priority =	NF_IP_PRI_SELINUX_FIRST,
5595effad8dfSPaul Moore 	}
55961da177e4SLinus Torvalds };
55971da177e4SLinus Torvalds 
55981da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
55991da177e4SLinus Torvalds 
5600effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = {
5601effad8dfSPaul Moore 	{
5602effad8dfSPaul Moore 		.hook =		selinux_ipv6_postroute,
56031da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
56041da177e4SLinus Torvalds 		.pf =		PF_INET6,
56056e23ae2aSPatrick McHardy 		.hooknum =	NF_INET_POST_ROUTING,
56061da177e4SLinus Torvalds 		.priority =	NF_IP6_PRI_SELINUX_LAST,
5607effad8dfSPaul Moore 	},
5608effad8dfSPaul Moore 	{
5609effad8dfSPaul Moore 		.hook =		selinux_ipv6_forward,
5610effad8dfSPaul Moore 		.owner =	THIS_MODULE,
5611effad8dfSPaul Moore 		.pf =		PF_INET6,
5612effad8dfSPaul Moore 		.hooknum =	NF_INET_FORWARD,
5613effad8dfSPaul Moore 		.priority =	NF_IP6_PRI_SELINUX_FIRST,
5614effad8dfSPaul Moore 	}
56151da177e4SLinus Torvalds };
56161da177e4SLinus Torvalds 
56171da177e4SLinus Torvalds #endif	/* IPV6 */
56181da177e4SLinus Torvalds 
56191da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
56201da177e4SLinus Torvalds {
56211da177e4SLinus Torvalds 	int err = 0;
5622effad8dfSPaul Moore 	u32 iter;
56231da177e4SLinus Torvalds 
56241da177e4SLinus Torvalds 	if (!selinux_enabled)
56251da177e4SLinus Torvalds 		goto out;
56261da177e4SLinus Torvalds 
5627fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
56281da177e4SLinus Torvalds 
5629effad8dfSPaul Moore 	for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) {
5630effad8dfSPaul Moore 		err = nf_register_hook(&selinux_ipv4_ops[iter]);
56311da177e4SLinus Torvalds 		if (err)
5632effad8dfSPaul Moore 			panic("SELinux: nf_register_hook for IPv4: error %d\n",
5633effad8dfSPaul Moore 			      err);
5634effad8dfSPaul Moore 	}
56351da177e4SLinus Torvalds 
56361da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5637effad8dfSPaul Moore 	for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) {
5638effad8dfSPaul Moore 		err = nf_register_hook(&selinux_ipv6_ops[iter]);
56391da177e4SLinus Torvalds 		if (err)
5640effad8dfSPaul Moore 			panic("SELinux: nf_register_hook for IPv6: error %d\n",
5641effad8dfSPaul Moore 			      err);
5642effad8dfSPaul Moore 	}
56431da177e4SLinus Torvalds #endif	/* IPV6 */
5644d28d1e08STrent Jaeger 
56451da177e4SLinus Torvalds out:
56461da177e4SLinus Torvalds 	return err;
56471da177e4SLinus Torvalds }
56481da177e4SLinus Torvalds 
56491da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
56501da177e4SLinus Torvalds 
56511da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
56521da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
56531da177e4SLinus Torvalds {
5654effad8dfSPaul Moore 	u32 iter;
5655effad8dfSPaul Moore 
5656fadcdb45SEric Paris 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
56571da177e4SLinus Torvalds 
5658effad8dfSPaul Moore 	for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++)
5659effad8dfSPaul Moore 		nf_unregister_hook(&selinux_ipv4_ops[iter]);
56601da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5661effad8dfSPaul Moore 	for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++)
5662effad8dfSPaul Moore 		nf_unregister_hook(&selinux_ipv6_ops[iter]);
56631da177e4SLinus Torvalds #endif	/* IPV6 */
56641da177e4SLinus Torvalds }
56651da177e4SLinus Torvalds #endif
56661da177e4SLinus Torvalds 
5667c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
56681da177e4SLinus Torvalds 
56691da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
56701da177e4SLinus Torvalds #define selinux_nf_ip_exit()
56711da177e4SLinus Torvalds #endif
56721da177e4SLinus Torvalds 
5673c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
56741da177e4SLinus Torvalds 
56751da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
56761da177e4SLinus Torvalds int selinux_disable(void)
56771da177e4SLinus Torvalds {
56781da177e4SLinus Torvalds 	extern void exit_sel_fs(void);
56791da177e4SLinus Torvalds 	static int selinux_disabled = 0;
56801da177e4SLinus Torvalds 
56811da177e4SLinus Torvalds 	if (ss_initialized) {
56821da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
56831da177e4SLinus Torvalds 		return -EINVAL;
56841da177e4SLinus Torvalds 	}
56851da177e4SLinus Torvalds 
56861da177e4SLinus Torvalds 	if (selinux_disabled) {
56871da177e4SLinus Torvalds 		/* Only do this once. */
56881da177e4SLinus Torvalds 		return -EINVAL;
56891da177e4SLinus Torvalds 	}
56901da177e4SLinus Torvalds 
56911da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
56921da177e4SLinus Torvalds 
56931da177e4SLinus Torvalds 	selinux_disabled = 1;
569430d55280SStephen Smalley 	selinux_enabled = 0;
56951da177e4SLinus Torvalds 
56961da177e4SLinus Torvalds 	/* Reset security_ops to the secondary module, dummy or capability. */
56971da177e4SLinus Torvalds 	security_ops = secondary_ops;
56981da177e4SLinus Torvalds 
56991da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
57001da177e4SLinus Torvalds 	selinux_nf_ip_exit();
57011da177e4SLinus Torvalds 
57021da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
57031da177e4SLinus Torvalds 	exit_sel_fs();
57041da177e4SLinus Torvalds 
57051da177e4SLinus Torvalds 	return 0;
57061da177e4SLinus Torvalds }
57071da177e4SLinus Torvalds #endif
5708