xref: /openbmc/linux/security/selinux/hooks.c (revision 2ee92d46c6cabedd50edf6f273fa8cf84f707618)
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>
157420ed23SVenkat Yekkirala  *  Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
167420ed23SVenkat Yekkirala  *                     Paul Moore, <paul.moore@hp.com>
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
191da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
201da177e4SLinus Torvalds  *      as published by the Free Software Foundation.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/ptrace.h>
271da177e4SLinus Torvalds #include <linux/errno.h>
281da177e4SLinus Torvalds #include <linux/sched.h>
291da177e4SLinus Torvalds #include <linux/security.h>
301da177e4SLinus Torvalds #include <linux/xattr.h>
311da177e4SLinus Torvalds #include <linux/capability.h>
321da177e4SLinus Torvalds #include <linux/unistd.h>
331da177e4SLinus Torvalds #include <linux/mm.h>
341da177e4SLinus Torvalds #include <linux/mman.h>
351da177e4SLinus Torvalds #include <linux/slab.h>
361da177e4SLinus Torvalds #include <linux/pagemap.h>
371da177e4SLinus Torvalds #include <linux/swap.h>
381da177e4SLinus Torvalds #include <linux/smp_lock.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>
511da177e4SLinus Torvalds #include <net/ip.h>		/* for sysctl_local_port_range[] */
521da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
531da177e4SLinus Torvalds #include <asm/uaccess.h>
541da177e4SLinus Torvalds #include <asm/ioctls.h>
551da177e4SLinus Torvalds #include <linux/bitops.h>
561da177e4SLinus Torvalds #include <linux/interrupt.h>
571da177e4SLinus Torvalds #include <linux/netdevice.h>	/* for network interface checks */
581da177e4SLinus Torvalds #include <linux/netlink.h>
591da177e4SLinus Torvalds #include <linux/tcp.h>
601da177e4SLinus Torvalds #include <linux/udp.h>
61*2ee92d46SJames Morris #include <linux/dccp.h>
621da177e4SLinus Torvalds #include <linux/quota.h>
631da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
641da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
651da177e4SLinus Torvalds #include <linux/parser.h>
661da177e4SLinus Torvalds #include <linux/nfs_mount.h>
671da177e4SLinus Torvalds #include <net/ipv6.h>
681da177e4SLinus Torvalds #include <linux/hugetlb.h>
691da177e4SLinus Torvalds #include <linux/personality.h>
701da177e4SLinus Torvalds #include <linux/sysctl.h>
711da177e4SLinus Torvalds #include <linux/audit.h>
726931dfc9SEric Paris #include <linux/string.h>
73877ce7c1SCatherine Zhang #include <linux/selinux.h>
7423970741SEric Paris #include <linux/mutex.h>
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #include "avc.h"
771da177e4SLinus Torvalds #include "objsec.h"
781da177e4SLinus Torvalds #include "netif.h"
79d28d1e08STrent Jaeger #include "xfrm.h"
807420ed23SVenkat Yekkirala #include "selinux_netlabel.h"
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux"
831da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds extern unsigned int policydb_loaded_version;
861da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
874e5ab4cbSJames Morris extern int selinux_compat_net;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
901da177e4SLinus Torvalds int selinux_enforcing = 0;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	selinux_enforcing = simple_strtol(str,NULL,0);
951da177e4SLinus Torvalds 	return 1;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
981da177e4SLinus Torvalds #endif
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1011da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	selinux_enabled = simple_strtol(str, NULL, 0);
1061da177e4SLinus Torvalds 	return 1;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
10930d55280SStephen Smalley #else
11030d55280SStephen Smalley int selinux_enabled = 1;
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* Original (dummy) security module. */
1141da177e4SLinus Torvalds static struct security_operations *original_ops = NULL;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /* Minimal support for a secondary security module,
1171da177e4SLinus Torvalds    just to allow the use of the dummy or capability modules.
1181da177e4SLinus Torvalds    The owlsm module can alternatively be used as a secondary
1191da177e4SLinus Torvalds    module as long as CONFIG_OWLSM_FD is not enabled. */
1201da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized
1231da177e4SLinus Torvalds    before the policy was loaded. */
1241da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head);
1251da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock);
1261da177e4SLinus Torvalds 
1277cae7e26SJames Morris static kmem_cache_t *sel_inode_cache;
1287cae7e26SJames Morris 
1298c8570fbSDustin Kirkland /* Return security context for a given sid or just the context
1308c8570fbSDustin Kirkland    length if the buffer is null or length is 0 */
1318c8570fbSDustin Kirkland static int selinux_getsecurity(u32 sid, void *buffer, size_t size)
1328c8570fbSDustin Kirkland {
1338c8570fbSDustin Kirkland 	char *context;
1348c8570fbSDustin Kirkland 	unsigned len;
1358c8570fbSDustin Kirkland 	int rc;
1368c8570fbSDustin Kirkland 
1378c8570fbSDustin Kirkland 	rc = security_sid_to_context(sid, &context, &len);
1388c8570fbSDustin Kirkland 	if (rc)
1398c8570fbSDustin Kirkland 		return rc;
1408c8570fbSDustin Kirkland 
1418c8570fbSDustin Kirkland 	if (!buffer || !size)
1428c8570fbSDustin Kirkland 		goto getsecurity_exit;
1438c8570fbSDustin Kirkland 
1448c8570fbSDustin Kirkland 	if (size < len) {
1458c8570fbSDustin Kirkland 		len = -ERANGE;
1468c8570fbSDustin Kirkland 		goto getsecurity_exit;
1478c8570fbSDustin Kirkland 	}
1488c8570fbSDustin Kirkland 	memcpy(buffer, context, len);
1498c8570fbSDustin Kirkland 
1508c8570fbSDustin Kirkland getsecurity_exit:
1518c8570fbSDustin Kirkland 	kfree(context);
1528c8570fbSDustin Kirkland 	return len;
1538c8570fbSDustin Kirkland }
1548c8570fbSDustin Kirkland 
1551da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1601da177e4SLinus Torvalds 
16189d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1621da177e4SLinus Torvalds 	if (!tsec)
1631da177e4SLinus Torvalds 		return -ENOMEM;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	tsec->task = task;
1661da177e4SLinus Torvalds 	tsec->osid = tsec->sid = tsec->ptrace_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 
1847cae7e26SJames Morris 	isec = kmem_cache_alloc(sel_inode_cache, SLAB_KERNEL);
1851da177e4SLinus Torvalds 	if (!isec)
1861da177e4SLinus Torvalds 		return -ENOMEM;
1871da177e4SLinus Torvalds 
1887cae7e26SJames Morris 	memset(isec, 0, sizeof(*isec));
18923970741SEric Paris 	mutex_init(&isec->lock);
1901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
1911da177e4SLinus Torvalds 	isec->inode = inode;
1921da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
1931da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
1941da177e4SLinus Torvalds 	isec->task_sid = tsec->sid;
1951da177e4SLinus Torvalds 	inode->i_security = isec;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	return 0;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
2031da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2061da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2071da177e4SLinus Torvalds 		list_del_init(&isec->list);
2081da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	inode->i_security = NULL;
2117cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
2171da177e4SLinus Torvalds 	struct file_security_struct *fsec;
2181da177e4SLinus Torvalds 
21926d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2201da177e4SLinus Torvalds 	if (!fsec)
2211da177e4SLinus Torvalds 		return -ENOMEM;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	fsec->file = file;
2241da177e4SLinus Torvalds 	fsec->sid = tsec->sid;
2251da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
2261da177e4SLinus Torvalds 	file->f_security = fsec;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	return 0;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static void file_free_security(struct file *file)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2341da177e4SLinus Torvalds 	file->f_security = NULL;
2351da177e4SLinus Torvalds 	kfree(fsec);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2411da177e4SLinus Torvalds 
24289d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2431da177e4SLinus Torvalds 	if (!sbsec)
2441da177e4SLinus Torvalds 		return -ENOMEM;
2451da177e4SLinus Torvalds 
246bc7e982bSEric Paris 	mutex_init(&sbsec->lock);
2471da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->list);
2481da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2491da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2501da177e4SLinus Torvalds 	sbsec->sb = sb;
2511da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2521da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
253c312feb2SEric Paris 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2541da177e4SLinus Torvalds 	sb->s_security = sbsec;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	return 0;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
2641da177e4SLinus Torvalds 	if (!list_empty(&sbsec->list))
2651da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
2661da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	sb->s_security = NULL;
2691da177e4SLinus Torvalds 	kfree(sbsec);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2727d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
2751da177e4SLinus Torvalds 
27689d155efSJames Morris 	ssec = kzalloc(sizeof(*ssec), priority);
2771da177e4SLinus Torvalds 	if (!ssec)
2781da177e4SLinus Torvalds 		return -ENOMEM;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	ssec->sk = sk;
2811da177e4SLinus Torvalds 	ssec->peer_sid = SECINITSID_UNLABELED;
282892c141eSVenkat Yekkirala 	ssec->sid = SECINITSID_UNLABELED;
2831da177e4SLinus Torvalds 	sk->sk_security = ssec;
2841da177e4SLinus Torvalds 
28599f59ed0SPaul Moore 	selinux_netlbl_sk_security_init(ssec, family);
28699f59ed0SPaul Moore 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void sk_free_security(struct sock *sk)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct sk_security_struct *ssec = sk->sk_security;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	sk->sk_security = NULL;
2951da177e4SLinus Torvalds 	kfree(ssec);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds /* The security server must be initialized before
2991da177e4SLinus Torvalds    any labeling or access decisions can be provided. */
3001da177e4SLinus Torvalds extern int ss_initialized;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds static char *labeling_behaviors[6] = {
3051da177e4SLinus Torvalds 	"uses xattr",
3061da177e4SLinus Torvalds 	"uses transition SIDs",
3071da177e4SLinus Torvalds 	"uses task SIDs",
3081da177e4SLinus Torvalds 	"uses genfs_contexts",
3091da177e4SLinus Torvalds 	"not configured for labeling",
3101da177e4SLinus Torvalds 	"uses mountpoint labeling",
3111da177e4SLinus Torvalds };
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds enum {
3211da177e4SLinus Torvalds 	Opt_context = 1,
3221da177e4SLinus Torvalds 	Opt_fscontext = 2,
3231da177e4SLinus Torvalds 	Opt_defcontext = 4,
3240808925eSEric Paris 	Opt_rootcontext = 8,
3251da177e4SLinus Torvalds };
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static match_table_t tokens = {
3281da177e4SLinus Torvalds 	{Opt_context, "context=%s"},
3291da177e4SLinus Torvalds 	{Opt_fscontext, "fscontext=%s"},
3301da177e4SLinus Torvalds 	{Opt_defcontext, "defcontext=%s"},
3310808925eSEric Paris 	{Opt_rootcontext, "rootcontext=%s"},
3321da177e4SLinus Torvalds };
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3351da177e4SLinus Torvalds 
336c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid,
337c312feb2SEric Paris 			struct superblock_security_struct *sbsec,
338c312feb2SEric Paris 			struct task_security_struct *tsec)
339c312feb2SEric Paris {
340c312feb2SEric Paris 	int rc;
341c312feb2SEric Paris 
342c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
343c312feb2SEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
344c312feb2SEric Paris 	if (rc)
345c312feb2SEric Paris 		return rc;
346c312feb2SEric Paris 
347c312feb2SEric Paris 	rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
348c312feb2SEric Paris 			  FILESYSTEM__RELABELTO, NULL);
349c312feb2SEric Paris 	return rc;
350c312feb2SEric Paris }
351c312feb2SEric Paris 
3520808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid,
3530808925eSEric Paris 			struct superblock_security_struct *sbsec,
3540808925eSEric Paris 			struct task_security_struct *tsec)
3550808925eSEric Paris {
3560808925eSEric Paris 	int rc;
3570808925eSEric Paris 	rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
3580808925eSEric Paris 			  FILESYSTEM__RELABELFROM, NULL);
3590808925eSEric Paris 	if (rc)
3600808925eSEric Paris 		return rc;
3610808925eSEric Paris 
3620808925eSEric Paris 	rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
3630808925eSEric Paris 			  FILESYSTEM__ASSOCIATE, NULL);
3640808925eSEric Paris 	return rc;
3650808925eSEric Paris }
3660808925eSEric Paris 
3671da177e4SLinus Torvalds static int try_context_mount(struct super_block *sb, void *data)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	char *context = NULL, *defcontext = NULL;
3700808925eSEric Paris 	char *fscontext = NULL, *rootcontext = NULL;
3711da177e4SLinus Torvalds 	const char *name;
3721da177e4SLinus Torvalds 	u32 sid;
3731da177e4SLinus Torvalds 	int alloc = 0, rc = 0, seen = 0;
3741da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
3751da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	if (!data)
3781da177e4SLinus Torvalds 		goto out;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	name = sb->s_type->name;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) {
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		/* NFS we understand. */
3851da177e4SLinus Torvalds 		if (!strcmp(name, "nfs")) {
3861da177e4SLinus Torvalds 			struct nfs_mount_data *d = data;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 			if (d->version <  NFS_MOUNT_VERSION)
3891da177e4SLinus Torvalds 				goto out;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 			if (d->context[0]) {
3921da177e4SLinus Torvalds 				context = d->context;
3931da177e4SLinus Torvalds 				seen |= Opt_context;
3941da177e4SLinus Torvalds 			}
3951da177e4SLinus Torvalds 		} else
3961da177e4SLinus Torvalds 			goto out;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	} else {
3991da177e4SLinus Torvalds 		/* Standard string-based options. */
4001da177e4SLinus Torvalds 		char *p, *options = data;
4011da177e4SLinus Torvalds 
4023528a953SCory Olmo 		while ((p = strsep(&options, "|")) != NULL) {
4031da177e4SLinus Torvalds 			int token;
4041da177e4SLinus Torvalds 			substring_t args[MAX_OPT_ARGS];
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 			if (!*p)
4071da177e4SLinus Torvalds 				continue;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 			token = match_token(p, tokens, args);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 			switch (token) {
4121da177e4SLinus Torvalds 			case Opt_context:
413c312feb2SEric Paris 				if (seen & (Opt_context|Opt_defcontext)) {
4141da177e4SLinus Torvalds 					rc = -EINVAL;
4151da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
4161da177e4SLinus Torvalds 					goto out_free;
4171da177e4SLinus Torvalds 				}
4181da177e4SLinus Torvalds 				context = match_strdup(&args[0]);
4191da177e4SLinus Torvalds 				if (!context) {
4201da177e4SLinus Torvalds 					rc = -ENOMEM;
4211da177e4SLinus Torvalds 					goto out_free;
4221da177e4SLinus Torvalds 				}
4231da177e4SLinus Torvalds 				if (!alloc)
4241da177e4SLinus Torvalds 					alloc = 1;
4251da177e4SLinus Torvalds 				seen |= Opt_context;
4261da177e4SLinus Torvalds 				break;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 			case Opt_fscontext:
429c312feb2SEric Paris 				if (seen & Opt_fscontext) {
4301da177e4SLinus Torvalds 					rc = -EINVAL;
4311da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
4321da177e4SLinus Torvalds 					goto out_free;
4331da177e4SLinus Torvalds 				}
434c312feb2SEric Paris 				fscontext = match_strdup(&args[0]);
435c312feb2SEric Paris 				if (!fscontext) {
4361da177e4SLinus Torvalds 					rc = -ENOMEM;
4371da177e4SLinus Torvalds 					goto out_free;
4381da177e4SLinus Torvalds 				}
4391da177e4SLinus Torvalds 				if (!alloc)
4401da177e4SLinus Torvalds 					alloc = 1;
4411da177e4SLinus Torvalds 				seen |= Opt_fscontext;
4421da177e4SLinus Torvalds 				break;
4431da177e4SLinus Torvalds 
4440808925eSEric Paris 			case Opt_rootcontext:
4450808925eSEric Paris 				if (seen & Opt_rootcontext) {
4460808925eSEric Paris 					rc = -EINVAL;
4470808925eSEric Paris 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
4480808925eSEric Paris 					goto out_free;
4490808925eSEric Paris 				}
4500808925eSEric Paris 				rootcontext = match_strdup(&args[0]);
4510808925eSEric Paris 				if (!rootcontext) {
4520808925eSEric Paris 					rc = -ENOMEM;
4530808925eSEric Paris 					goto out_free;
4540808925eSEric Paris 				}
4550808925eSEric Paris 				if (!alloc)
4560808925eSEric Paris 					alloc = 1;
4570808925eSEric Paris 				seen |= Opt_rootcontext;
4580808925eSEric Paris 				break;
4590808925eSEric Paris 
4601da177e4SLinus Torvalds 			case Opt_defcontext:
4611da177e4SLinus Torvalds 				if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
4621da177e4SLinus Torvalds 					rc = -EINVAL;
4631da177e4SLinus Torvalds 					printk(KERN_WARNING "SELinux:  "
4641da177e4SLinus Torvalds 					       "defcontext option is invalid "
4651da177e4SLinus Torvalds 					       "for this filesystem type\n");
4661da177e4SLinus Torvalds 					goto out_free;
4671da177e4SLinus Torvalds 				}
4681da177e4SLinus Torvalds 				if (seen & (Opt_context|Opt_defcontext)) {
4691da177e4SLinus Torvalds 					rc = -EINVAL;
4701da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
4711da177e4SLinus Torvalds 					goto out_free;
4721da177e4SLinus Torvalds 				}
4731da177e4SLinus Torvalds 				defcontext = match_strdup(&args[0]);
4741da177e4SLinus Torvalds 				if (!defcontext) {
4751da177e4SLinus Torvalds 					rc = -ENOMEM;
4761da177e4SLinus Torvalds 					goto out_free;
4771da177e4SLinus Torvalds 				}
4781da177e4SLinus Torvalds 				if (!alloc)
4791da177e4SLinus Torvalds 					alloc = 1;
4801da177e4SLinus Torvalds 				seen |= Opt_defcontext;
4811da177e4SLinus Torvalds 				break;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 			default:
4841da177e4SLinus Torvalds 				rc = -EINVAL;
4851da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux:  unknown mount "
4861da177e4SLinus Torvalds 				       "option\n");
4871da177e4SLinus Torvalds 				goto out_free;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 			}
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	if (!seen)
4941da177e4SLinus Torvalds 		goto out;
4951da177e4SLinus Torvalds 
496c312feb2SEric Paris 	/* sets the context of the superblock for the fs being mounted. */
497c312feb2SEric Paris 	if (fscontext) {
498c312feb2SEric Paris 		rc = security_context_to_sid(fscontext, strlen(fscontext), &sid);
499c312feb2SEric Paris 		if (rc) {
500c312feb2SEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
501c312feb2SEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
502c312feb2SEric Paris 			       fscontext, sb->s_id, name, rc);
503c312feb2SEric Paris 			goto out_free;
504c312feb2SEric Paris 		}
505c312feb2SEric Paris 
506c312feb2SEric Paris 		rc = may_context_mount_sb_relabel(sid, sbsec, tsec);
507c312feb2SEric Paris 		if (rc)
508c312feb2SEric Paris 			goto out_free;
509c312feb2SEric Paris 
510c312feb2SEric Paris 		sbsec->sid = sid;
511c312feb2SEric Paris 	}
512c312feb2SEric Paris 
513c312feb2SEric Paris 	/*
514c312feb2SEric Paris 	 * Switch to using mount point labeling behavior.
515c312feb2SEric Paris 	 * sets the label used on all file below the mountpoint, and will set
516c312feb2SEric Paris 	 * the superblock context if not already set.
517c312feb2SEric Paris 	 */
5181da177e4SLinus Torvalds 	if (context) {
5191da177e4SLinus Torvalds 		rc = security_context_to_sid(context, strlen(context), &sid);
5201da177e4SLinus Torvalds 		if (rc) {
5211da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: security_context_to_sid"
5221da177e4SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
5231da177e4SLinus Torvalds 			       context, sb->s_id, name, rc);
5241da177e4SLinus Torvalds 			goto out_free;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 
527b04ea3ceSEric Paris 		if (!fscontext) {
528c312feb2SEric Paris 			rc = may_context_mount_sb_relabel(sid, sbsec, tsec);
5291da177e4SLinus Torvalds 			if (rc)
5301da177e4SLinus Torvalds 				goto out_free;
5311da177e4SLinus Torvalds 			sbsec->sid = sid;
532b04ea3ceSEric Paris 		} else {
533b04ea3ceSEric Paris 			rc = may_context_mount_inode_relabel(sid, sbsec, tsec);
534b04ea3ceSEric Paris 			if (rc)
535b04ea3ceSEric Paris 				goto out_free;
536b04ea3ceSEric Paris 		}
537c312feb2SEric Paris 		sbsec->mntpoint_sid = sid;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 		sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5420808925eSEric Paris 	if (rootcontext) {
5430808925eSEric Paris 		struct inode *inode = sb->s_root->d_inode;
5440808925eSEric Paris 		struct inode_security_struct *isec = inode->i_security;
5450808925eSEric Paris 		rc = security_context_to_sid(rootcontext, strlen(rootcontext), &sid);
5460808925eSEric Paris 		if (rc) {
5470808925eSEric Paris 			printk(KERN_WARNING "SELinux: security_context_to_sid"
5480808925eSEric Paris 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
5490808925eSEric Paris 			       rootcontext, sb->s_id, name, rc);
5500808925eSEric Paris 			goto out_free;
5510808925eSEric Paris 		}
5520808925eSEric Paris 
5530808925eSEric Paris 		rc = may_context_mount_inode_relabel(sid, sbsec, tsec);
5540808925eSEric Paris 		if (rc)
5550808925eSEric Paris 			goto out_free;
5560808925eSEric Paris 
5570808925eSEric Paris 		isec->sid = sid;
5580808925eSEric Paris 		isec->initialized = 1;
5590808925eSEric Paris 	}
5600808925eSEric Paris 
5611da177e4SLinus Torvalds 	if (defcontext) {
5621da177e4SLinus Torvalds 		rc = security_context_to_sid(defcontext, strlen(defcontext), &sid);
5631da177e4SLinus Torvalds 		if (rc) {
5641da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: security_context_to_sid"
5651da177e4SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
5661da177e4SLinus Torvalds 			       defcontext, sb->s_id, name, rc);
5671da177e4SLinus Torvalds 			goto out_free;
5681da177e4SLinus Torvalds 		}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 		if (sid == sbsec->def_sid)
5711da177e4SLinus Torvalds 			goto out_free;
5721da177e4SLinus Torvalds 
5730808925eSEric Paris 		rc = may_context_mount_inode_relabel(sid, sbsec, tsec);
5741da177e4SLinus Torvalds 		if (rc)
5751da177e4SLinus Torvalds 			goto out_free;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 		sbsec->def_sid = sid;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds out_free:
5811da177e4SLinus Torvalds 	if (alloc) {
5821da177e4SLinus Torvalds 		kfree(context);
5831da177e4SLinus Torvalds 		kfree(defcontext);
584c312feb2SEric Paris 		kfree(fscontext);
5850808925eSEric Paris 		kfree(rootcontext);
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds out:
5881da177e4SLinus Torvalds 	return rc;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds static int superblock_doinit(struct super_block *sb, void *data)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5941da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
5951da177e4SLinus Torvalds 	struct inode *inode = root->d_inode;
5961da177e4SLinus Torvalds 	int rc = 0;
5971da177e4SLinus Torvalds 
598bc7e982bSEric Paris 	mutex_lock(&sbsec->lock);
5991da177e4SLinus Torvalds 	if (sbsec->initialized)
6001da177e4SLinus Torvalds 		goto out;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (!ss_initialized) {
6031da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
6041da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
6051da177e4SLinus Torvalds 		   server is ready to handle calls. */
6061da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
6071da177e4SLinus Torvalds 		if (list_empty(&sbsec->list))
6081da177e4SLinus Torvalds 			list_add(&sbsec->list, &superblock_security_head);
6091da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
6101da177e4SLinus Torvalds 		goto out;
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	/* Determine the labeling behavior to use for this filesystem type. */
6141da177e4SLinus Torvalds 	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
6151da177e4SLinus Torvalds 	if (rc) {
6161da177e4SLinus Torvalds 		printk(KERN_WARNING "%s:  security_fs_use(%s) returned %d\n",
6171da177e4SLinus Torvalds 		       __FUNCTION__, sb->s_type->name, rc);
6181da177e4SLinus Torvalds 		goto out;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	rc = try_context_mount(sb, data);
6221da177e4SLinus Torvalds 	if (rc)
6231da177e4SLinus Torvalds 		goto out;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
6261da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
6271da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
6281da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
6291da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
6301da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
6311da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
6321da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
6331da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
6341da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
6351da177e4SLinus Torvalds 			goto out;
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
6381da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
6391da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
6401da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
6411da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
6421da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
6431da177e4SLinus Torvalds 			else
6441da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
6451da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
6461da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
6471da177e4SLinus Torvalds 			goto out;
6481da177e4SLinus Torvalds 		}
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (strcmp(sb->s_type->name, "proc") == 0)
6521da177e4SLinus Torvalds 		sbsec->proc = 1;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	sbsec->initialized = 1;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) {
6571da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux: initialized (dev %s, type %s), unknown behavior\n",
6581da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 	else {
6611da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux: initialized (dev %s, type %s), %s\n",
6621da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
6631da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* Initialize the root inode. */
6671da177e4SLinus Torvalds 	rc = inode_doinit_with_dentry(sb->s_root->d_inode, sb->s_root);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
6701da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
6711da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
6721da177e4SLinus Torvalds 	   populates itself. */
6731da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
6741da177e4SLinus Torvalds next_inode:
6751da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
6761da177e4SLinus Torvalds 		struct inode_security_struct *isec =
6771da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
6781da177e4SLinus Torvalds 				           struct inode_security_struct, list);
6791da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
6801da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
6811da177e4SLinus Torvalds 		inode = igrab(inode);
6821da177e4SLinus Torvalds 		if (inode) {
6831da177e4SLinus Torvalds 			if (!IS_PRIVATE (inode))
6841da177e4SLinus Torvalds 				inode_doinit(inode);
6851da177e4SLinus Torvalds 			iput(inode);
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
6881da177e4SLinus Torvalds 		list_del_init(&isec->list);
6891da177e4SLinus Torvalds 		goto next_inode;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
6921da177e4SLinus Torvalds out:
693bc7e982bSEric Paris 	mutex_unlock(&sbsec->lock);
6941da177e4SLinus Torvalds 	return rc;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
7001da177e4SLinus Torvalds 	case S_IFSOCK:
7011da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
7021da177e4SLinus Torvalds 	case S_IFLNK:
7031da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
7041da177e4SLinus Torvalds 	case S_IFREG:
7051da177e4SLinus Torvalds 		return SECCLASS_FILE;
7061da177e4SLinus Torvalds 	case S_IFBLK:
7071da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
7081da177e4SLinus Torvalds 	case S_IFDIR:
7091da177e4SLinus Torvalds 		return SECCLASS_DIR;
7101da177e4SLinus Torvalds 	case S_IFCHR:
7111da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
7121da177e4SLinus Torvalds 	case S_IFIFO:
7131da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	}
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	return SECCLASS_FILE;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
72013402580SJames Morris static inline int default_protocol_stream(int protocol)
72113402580SJames Morris {
72213402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
72313402580SJames Morris }
72413402580SJames Morris 
72513402580SJames Morris static inline int default_protocol_dgram(int protocol)
72613402580SJames Morris {
72713402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
72813402580SJames Morris }
72913402580SJames Morris 
7301da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	switch (family) {
7331da177e4SLinus Torvalds 	case PF_UNIX:
7341da177e4SLinus Torvalds 		switch (type) {
7351da177e4SLinus Torvalds 		case SOCK_STREAM:
7361da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
7371da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
7381da177e4SLinus Torvalds 		case SOCK_DGRAM:
7391da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	case PF_INET:
7431da177e4SLinus Torvalds 	case PF_INET6:
7441da177e4SLinus Torvalds 		switch (type) {
7451da177e4SLinus Torvalds 		case SOCK_STREAM:
74613402580SJames Morris 			if (default_protocol_stream(protocol))
7471da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
74813402580SJames Morris 			else
74913402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
7501da177e4SLinus Torvalds 		case SOCK_DGRAM:
75113402580SJames Morris 			if (default_protocol_dgram(protocol))
7521da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
75313402580SJames Morris 			else
75413402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
755*2ee92d46SJames Morris 		case SOCK_DCCP:
756*2ee92d46SJames Morris 			return SECCLASS_DCCP_SOCKET;
75713402580SJames Morris 		default:
7581da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 		break;
7611da177e4SLinus Torvalds 	case PF_NETLINK:
7621da177e4SLinus Torvalds 		switch (protocol) {
7631da177e4SLinus Torvalds 		case NETLINK_ROUTE:
7641da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
7651da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
7661da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
767216efaaaSJames Morris 		case NETLINK_INET_DIAG:
7681da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
7691da177e4SLinus Torvalds 		case NETLINK_NFLOG:
7701da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
7711da177e4SLinus Torvalds 		case NETLINK_XFRM:
7721da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
7731da177e4SLinus Torvalds 		case NETLINK_SELINUX:
7741da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
7751da177e4SLinus Torvalds 		case NETLINK_AUDIT:
7761da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
7771da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
7781da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
7791da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
7801da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
7810c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
7820c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
7831da177e4SLinus Torvalds 		default:
7841da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
7851da177e4SLinus Torvalds 		}
7861da177e4SLinus Torvalds 	case PF_PACKET:
7871da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
7881da177e4SLinus Torvalds 	case PF_KEY:
7891da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
7903e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
7913e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
7981da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
7991da177e4SLinus Torvalds 				u16 tclass,
8001da177e4SLinus Torvalds 				u32 *sid)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	int buflen, rc;
8031da177e4SLinus Torvalds 	char *buffer, *path, *end;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	buffer = (char*)__get_free_page(GFP_KERNEL);
8061da177e4SLinus Torvalds 	if (!buffer)
8071da177e4SLinus Torvalds 		return -ENOMEM;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	buflen = PAGE_SIZE;
8101da177e4SLinus Torvalds 	end = buffer+buflen;
8111da177e4SLinus Torvalds 	*--end = '\0';
8121da177e4SLinus Torvalds 	buflen--;
8131da177e4SLinus Torvalds 	path = end-1;
8141da177e4SLinus Torvalds 	*path = '/';
8151da177e4SLinus Torvalds 	while (de && de != de->parent) {
8161da177e4SLinus Torvalds 		buflen -= de->namelen + 1;
8171da177e4SLinus Torvalds 		if (buflen < 0)
8181da177e4SLinus Torvalds 			break;
8191da177e4SLinus Torvalds 		end -= de->namelen;
8201da177e4SLinus Torvalds 		memcpy(end, de->name, de->namelen);
8211da177e4SLinus Torvalds 		*--end = '/';
8221da177e4SLinus Torvalds 		path = end;
8231da177e4SLinus Torvalds 		de = de->parent;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	rc = security_genfs_sid("proc", path, tclass, sid);
8261da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
8271da177e4SLinus Torvalds 	return rc;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds #else
8301da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
8311da177e4SLinus Torvalds 				u16 tclass,
8321da177e4SLinus Torvalds 				u32 *sid)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	return -EINVAL;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds #endif
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
8391da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
8421da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
8431da177e4SLinus Torvalds 	u32 sid;
8441da177e4SLinus Torvalds 	struct dentry *dentry;
8451da177e4SLinus Torvalds #define INITCONTEXTLEN 255
8461da177e4SLinus Torvalds 	char *context = NULL;
8471da177e4SLinus Torvalds 	unsigned len = 0;
8481da177e4SLinus Torvalds 	int rc = 0;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	if (isec->initialized)
8511da177e4SLinus Torvalds 		goto out;
8521da177e4SLinus Torvalds 
85323970741SEric Paris 	mutex_lock(&isec->lock);
8541da177e4SLinus Torvalds 	if (isec->initialized)
85523970741SEric Paris 		goto out_unlock;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
8581da177e4SLinus Torvalds 	if (!sbsec->initialized) {
8591da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
8601da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
8611da177e4SLinus Torvalds 		   server is ready to handle calls. */
8621da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
8631da177e4SLinus Torvalds 		if (list_empty(&isec->list))
8641da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
8651da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
86623970741SEric Paris 		goto out_unlock;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	switch (sbsec->behavior) {
8701da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
8711da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
8721da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
8731da177e4SLinus Torvalds 			break;
8741da177e4SLinus Torvalds 		}
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
8771da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
8781da177e4SLinus Torvalds 		if (opt_dentry) {
8791da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
8801da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
8811da177e4SLinus Torvalds 		} else {
8821da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
8831da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 		if (!dentry) {
8861da177e4SLinus Torvalds 			printk(KERN_WARNING "%s:  no dentry for dev=%s "
8871da177e4SLinus Torvalds 			       "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id,
8881da177e4SLinus Torvalds 			       inode->i_ino);
88923970741SEric Paris 			goto out_unlock;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
8931da177e4SLinus Torvalds 		context = kmalloc(len, GFP_KERNEL);
8941da177e4SLinus Torvalds 		if (!context) {
8951da177e4SLinus Torvalds 			rc = -ENOMEM;
8961da177e4SLinus Torvalds 			dput(dentry);
89723970741SEric Paris 			goto out_unlock;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
9001da177e4SLinus Torvalds 					   context, len);
9011da177e4SLinus Torvalds 		if (rc == -ERANGE) {
9021da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
9031da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
9041da177e4SLinus Torvalds 						   NULL, 0);
9051da177e4SLinus Torvalds 			if (rc < 0) {
9061da177e4SLinus Torvalds 				dput(dentry);
90723970741SEric Paris 				goto out_unlock;
9081da177e4SLinus Torvalds 			}
9091da177e4SLinus Torvalds 			kfree(context);
9101da177e4SLinus Torvalds 			len = rc;
9111da177e4SLinus Torvalds 			context = kmalloc(len, GFP_KERNEL);
9121da177e4SLinus Torvalds 			if (!context) {
9131da177e4SLinus Torvalds 				rc = -ENOMEM;
9141da177e4SLinus Torvalds 				dput(dentry);
91523970741SEric Paris 				goto out_unlock;
9161da177e4SLinus Torvalds 			}
9171da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
9181da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
9191da177e4SLinus Torvalds 						   context, len);
9201da177e4SLinus Torvalds 		}
9211da177e4SLinus Torvalds 		dput(dentry);
9221da177e4SLinus Torvalds 		if (rc < 0) {
9231da177e4SLinus Torvalds 			if (rc != -ENODATA) {
9241da177e4SLinus Torvalds 				printk(KERN_WARNING "%s:  getxattr returned "
9251da177e4SLinus Torvalds 				       "%d for dev=%s ino=%ld\n", __FUNCTION__,
9261da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
9271da177e4SLinus Torvalds 				kfree(context);
92823970741SEric Paris 				goto out_unlock;
9291da177e4SLinus Torvalds 			}
9301da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
9311da177e4SLinus Torvalds 			sid = sbsec->def_sid;
9321da177e4SLinus Torvalds 			rc = 0;
9331da177e4SLinus Torvalds 		} else {
934f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
935f5c1d5b2SJames Morris 			                                     sbsec->def_sid);
9361da177e4SLinus Torvalds 			if (rc) {
9371da177e4SLinus Torvalds 				printk(KERN_WARNING "%s:  context_to_sid(%s) "
9381da177e4SLinus Torvalds 				       "returned %d for dev=%s ino=%ld\n",
9391da177e4SLinus Torvalds 				       __FUNCTION__, context, -rc,
9401da177e4SLinus Torvalds 				       inode->i_sb->s_id, inode->i_ino);
9411da177e4SLinus Torvalds 				kfree(context);
9421da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
9431da177e4SLinus Torvalds 				rc = 0;
9441da177e4SLinus Torvalds 				break;
9451da177e4SLinus Torvalds 			}
9461da177e4SLinus Torvalds 		}
9471da177e4SLinus Torvalds 		kfree(context);
9481da177e4SLinus Torvalds 		isec->sid = sid;
9491da177e4SLinus Torvalds 		break;
9501da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
9511da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
9521da177e4SLinus Torvalds 		break;
9531da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
9541da177e4SLinus Torvalds 		/* Default to the fs SID. */
9551da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
9581da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
9591da177e4SLinus Torvalds 		rc = security_transition_sid(isec->task_sid,
9601da177e4SLinus Torvalds 					     sbsec->sid,
9611da177e4SLinus Torvalds 					     isec->sclass,
9621da177e4SLinus Torvalds 					     &sid);
9631da177e4SLinus Torvalds 		if (rc)
96423970741SEric Paris 			goto out_unlock;
9651da177e4SLinus Torvalds 		isec->sid = sid;
9661da177e4SLinus Torvalds 		break;
967c312feb2SEric Paris 	case SECURITY_FS_USE_MNTPOINT:
968c312feb2SEric Paris 		isec->sid = sbsec->mntpoint_sid;
969c312feb2SEric Paris 		break;
9701da177e4SLinus Torvalds 	default:
971c312feb2SEric Paris 		/* Default to the fs superblock SID. */
9721da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 		if (sbsec->proc) {
9751da177e4SLinus Torvalds 			struct proc_inode *proci = PROC_I(inode);
9761da177e4SLinus Torvalds 			if (proci->pde) {
9771da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
9781da177e4SLinus Torvalds 				rc = selinux_proc_get_sid(proci->pde,
9791da177e4SLinus Torvalds 							  isec->sclass,
9801da177e4SLinus Torvalds 							  &sid);
9811da177e4SLinus Torvalds 				if (rc)
98223970741SEric Paris 					goto out_unlock;
9831da177e4SLinus Torvalds 				isec->sid = sid;
9841da177e4SLinus Torvalds 			}
9851da177e4SLinus Torvalds 		}
9861da177e4SLinus Torvalds 		break;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	isec->initialized = 1;
9901da177e4SLinus Torvalds 
99123970741SEric Paris out_unlock:
99223970741SEric Paris 	mutex_unlock(&isec->lock);
9931da177e4SLinus Torvalds out:
9941da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
9951da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
9961da177e4SLinus Torvalds 	return rc;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
10001da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	u32 perm = 0;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	switch (sig) {
10051da177e4SLinus Torvalds 	case SIGCHLD:
10061da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
10071da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
10081da177e4SLinus Torvalds 		break;
10091da177e4SLinus Torvalds 	case SIGKILL:
10101da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
10111da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
10121da177e4SLinus Torvalds 		break;
10131da177e4SLinus Torvalds 	case SIGSTOP:
10141da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
10151da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
10161da177e4SLinus Torvalds 		break;
10171da177e4SLinus Torvalds 	default:
10181da177e4SLinus Torvalds 		/* All other signals. */
10191da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
10201da177e4SLinus Torvalds 		break;
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	return perm;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks,
10271da177e4SLinus Torvalds    fork check, ptrace check, etc. */
10281da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1,
10291da177e4SLinus Torvalds 			 struct task_struct *tsk2,
10301da177e4SLinus Torvalds 			 u32 perms)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	tsec1 = tsk1->security;
10351da177e4SLinus Torvalds 	tsec2 = tsk2->security;
10361da177e4SLinus Torvalds 	return avc_has_perm(tsec1->sid, tsec2->sid,
10371da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perms, NULL);
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
10411da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk,
10421da177e4SLinus Torvalds 			       int cap)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	struct task_security_struct *tsec;
10451da177e4SLinus Torvalds 	struct avc_audit_data ad;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	tsec = tsk->security;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,CAP);
10501da177e4SLinus Torvalds 	ad.tsk = tsk;
10511da177e4SLinus Torvalds 	ad.u.cap = cap;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, tsec->sid,
10541da177e4SLinus Torvalds 			    SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
10581da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
10591da177e4SLinus Torvalds 			   u32 perms)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	struct task_security_struct *tsec;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	tsec = tsk->security;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, SECINITSID_KERNEL,
10661da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
10701da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
10711da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
10721da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk,
10731da177e4SLinus Torvalds 			  struct inode *inode,
10741da177e4SLinus Torvalds 			  u32 perms,
10751da177e4SLinus Torvalds 			  struct avc_audit_data *adp)
10761da177e4SLinus Torvalds {
10771da177e4SLinus Torvalds 	struct task_security_struct *tsec;
10781da177e4SLinus Torvalds 	struct inode_security_struct *isec;
10791da177e4SLinus Torvalds 	struct avc_audit_data ad;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	tsec = tsk->security;
10821da177e4SLinus Torvalds 	isec = inode->i_security;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	if (!adp) {
10851da177e4SLinus Torvalds 		adp = &ad;
10861da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, FS);
10871da177e4SLinus Torvalds 		ad.u.fs.inode = inode;
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp);
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
10941da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
10951da177e4SLinus Torvalds    pathname if needed. */
10961da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk,
10971da177e4SLinus Torvalds 				  struct vfsmount *mnt,
10981da177e4SLinus Torvalds 				  struct dentry *dentry,
10991da177e4SLinus Torvalds 				  u32 av)
11001da177e4SLinus Torvalds {
11011da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11021da177e4SLinus Torvalds 	struct avc_audit_data ad;
11031da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
11041da177e4SLinus Torvalds 	ad.u.fs.mnt = mnt;
11051da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
11061da177e4SLinus Torvalds 	return inode_has_perm(tsk, inode, av, &ad);
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
11101da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
11111da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
11121da177e4SLinus Torvalds    check a particular permission to the file.
11131da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
11141da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
11151da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
11161da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
1117858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk,
11181da177e4SLinus Torvalds 				struct file *file,
11191da177e4SLinus Torvalds 				u32 av)
11201da177e4SLinus Torvalds {
11211da177e4SLinus Torvalds 	struct task_security_struct *tsec = tsk->security;
11221da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
11231da177e4SLinus Torvalds 	struct vfsmount *mnt = file->f_vfsmnt;
11241da177e4SLinus Torvalds 	struct dentry *dentry = file->f_dentry;
11251da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11261da177e4SLinus Torvalds 	struct avc_audit_data ad;
11271da177e4SLinus Torvalds 	int rc;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
11301da177e4SLinus Torvalds 	ad.u.fs.mnt = mnt;
11311da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	if (tsec->sid != fsec->sid) {
11341da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, fsec->sid,
11351da177e4SLinus Torvalds 				  SECCLASS_FD,
11361da177e4SLinus Torvalds 				  FD__USE,
11371da177e4SLinus Torvalds 				  &ad);
11381da177e4SLinus Torvalds 		if (rc)
11391da177e4SLinus Torvalds 			return rc;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
11431da177e4SLinus Torvalds 	if (av)
11441da177e4SLinus Torvalds 		return inode_has_perm(tsk, inode, av, &ad);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	return 0;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds /* Check whether a task can create a file. */
11501da177e4SLinus Torvalds static int may_create(struct inode *dir,
11511da177e4SLinus Torvalds 		      struct dentry *dentry,
11521da177e4SLinus Torvalds 		      u16 tclass)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	struct task_security_struct *tsec;
11551da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
11561da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
11571da177e4SLinus Torvalds 	u32 newsid;
11581da177e4SLinus Torvalds 	struct avc_audit_data ad;
11591da177e4SLinus Torvalds 	int rc;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	tsec = current->security;
11621da177e4SLinus Torvalds 	dsec = dir->i_security;
11631da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
11661da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR,
11691da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
11701da177e4SLinus Torvalds 			  &ad);
11711da177e4SLinus Torvalds 	if (rc)
11721da177e4SLinus Torvalds 		return rc;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
11751da177e4SLinus Torvalds 		newsid = tsec->create_sid;
11761da177e4SLinus Torvalds 	} else {
11771da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, dsec->sid, tclass,
11781da177e4SLinus Torvalds 					     &newsid);
11791da177e4SLinus Torvalds 		if (rc)
11801da177e4SLinus Torvalds 			return rc;
11811da177e4SLinus Torvalds 	}
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad);
11841da177e4SLinus Torvalds 	if (rc)
11851da177e4SLinus Torvalds 		return rc;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
11881da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
11891da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
11924eb582cfSMichael LeMay /* Check whether a task can create a key. */
11934eb582cfSMichael LeMay static int may_create_key(u32 ksid,
11944eb582cfSMichael LeMay 			  struct task_struct *ctx)
11954eb582cfSMichael LeMay {
11964eb582cfSMichael LeMay 	struct task_security_struct *tsec;
11974eb582cfSMichael LeMay 
11984eb582cfSMichael LeMay 	tsec = ctx->security;
11994eb582cfSMichael LeMay 
12004eb582cfSMichael LeMay 	return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
12014eb582cfSMichael LeMay }
12024eb582cfSMichael LeMay 
12031da177e4SLinus Torvalds #define MAY_LINK   0
12041da177e4SLinus Torvalds #define MAY_UNLINK 1
12051da177e4SLinus Torvalds #define MAY_RMDIR  2
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
12081da177e4SLinus Torvalds static int may_link(struct inode *dir,
12091da177e4SLinus Torvalds 		    struct dentry *dentry,
12101da177e4SLinus Torvalds 		    int kind)
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct task_security_struct *tsec;
12141da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
12151da177e4SLinus Torvalds 	struct avc_audit_data ad;
12161da177e4SLinus Torvalds 	u32 av;
12171da177e4SLinus Torvalds 	int rc;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	tsec = current->security;
12201da177e4SLinus Torvalds 	dsec = dir->i_security;
12211da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
12241da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	av = DIR__SEARCH;
12271da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
12281da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad);
12291da177e4SLinus Torvalds 	if (rc)
12301da177e4SLinus Torvalds 		return rc;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	switch (kind) {
12331da177e4SLinus Torvalds 	case MAY_LINK:
12341da177e4SLinus Torvalds 		av = FILE__LINK;
12351da177e4SLinus Torvalds 		break;
12361da177e4SLinus Torvalds 	case MAY_UNLINK:
12371da177e4SLinus Torvalds 		av = FILE__UNLINK;
12381da177e4SLinus Torvalds 		break;
12391da177e4SLinus Torvalds 	case MAY_RMDIR:
12401da177e4SLinus Torvalds 		av = DIR__RMDIR;
12411da177e4SLinus Torvalds 		break;
12421da177e4SLinus Torvalds 	default:
12431da177e4SLinus Torvalds 		printk(KERN_WARNING "may_link:  unrecognized kind %d\n", kind);
12441da177e4SLinus Torvalds 		return 0;
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad);
12481da177e4SLinus Torvalds 	return rc;
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
12521da177e4SLinus Torvalds 			     struct dentry *old_dentry,
12531da177e4SLinus Torvalds 			     struct inode *new_dir,
12541da177e4SLinus Torvalds 			     struct dentry *new_dentry)
12551da177e4SLinus Torvalds {
12561da177e4SLinus Torvalds 	struct task_security_struct *tsec;
12571da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
12581da177e4SLinus Torvalds 	struct avc_audit_data ad;
12591da177e4SLinus Torvalds 	u32 av;
12601da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
12611da177e4SLinus Torvalds 	int rc;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	tsec = current->security;
12641da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
12651da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
12661da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
12671da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	ad.u.fs.dentry = old_dentry;
12721da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR,
12731da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
12741da177e4SLinus Torvalds 	if (rc)
12751da177e4SLinus Torvalds 		return rc;
12761da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_isec->sid,
12771da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
12781da177e4SLinus Torvalds 	if (rc)
12791da177e4SLinus Torvalds 		return rc;
12801da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
12811da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, old_isec->sid,
12821da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
12831da177e4SLinus Torvalds 		if (rc)
12841da177e4SLinus Torvalds 			return rc;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	ad.u.fs.dentry = new_dentry;
12881da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
12891da177e4SLinus Torvalds 	if (new_dentry->d_inode)
12901da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
12911da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
12921da177e4SLinus Torvalds 	if (rc)
12931da177e4SLinus Torvalds 		return rc;
12941da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
12951da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
12961da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
12971da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, new_isec->sid,
12981da177e4SLinus Torvalds 				  new_isec->sclass,
12991da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
13001da177e4SLinus Torvalds 		if (rc)
13011da177e4SLinus Torvalds 			return rc;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return 0;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
13081da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk,
13091da177e4SLinus Torvalds 			       struct super_block *sb,
13101da177e4SLinus Torvalds 			       u32 perms,
13111da177e4SLinus Torvalds 			       struct avc_audit_data *ad)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	struct task_security_struct *tsec;
13141da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	tsec = tsk->security;
13171da177e4SLinus Torvalds 	sbsec = sb->s_security;
13181da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
13191da177e4SLinus Torvalds 			    perms, ad);
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
13231da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
13241da177e4SLinus Torvalds {
13251da177e4SLinus Torvalds 	u32 av = 0;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	if ((mode & S_IFMT) != S_IFDIR) {
13281da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
13291da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
13301da177e4SLinus Torvalds 		if (mask & MAY_READ)
13311da177e4SLinus Torvalds 			av |= FILE__READ;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
13341da177e4SLinus Torvalds 			av |= FILE__APPEND;
13351da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
13361da177e4SLinus Torvalds 			av |= FILE__WRITE;
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	} else {
13391da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
13401da177e4SLinus Torvalds 			av |= DIR__SEARCH;
13411da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
13421da177e4SLinus Torvalds 			av |= DIR__WRITE;
13431da177e4SLinus Torvalds 		if (mask & MAY_READ)
13441da177e4SLinus Torvalds 			av |= DIR__READ;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	return av;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
13511da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	u32 av = 0;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
13561da177e4SLinus Torvalds 		av |= FILE__READ;
13571da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
13581da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
13591da177e4SLinus Torvalds 			av |= FILE__APPEND;
13601da177e4SLinus Torvalds 		else
13611da177e4SLinus Torvalds 			av |= FILE__WRITE;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	return av;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds /* Hook functions begin here. */
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	struct task_security_struct *psec = parent->security;
13721da177e4SLinus Torvalds 	struct task_security_struct *csec = child->security;
13731da177e4SLinus Torvalds 	int rc;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	rc = secondary_ops->ptrace(parent,child);
13761da177e4SLinus Torvalds 	if (rc)
13771da177e4SLinus Torvalds 		return rc;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	rc = task_has_perm(parent, child, PROCESS__PTRACE);
13801da177e4SLinus Torvalds 	/* Save the SID of the tracing process for later use in apply_creds. */
1381341c2d80SStephen Smalley 	if (!(child->ptrace & PT_PTRACED) && !rc)
13821da177e4SLinus Torvalds 		csec->ptrace_sid = psec->sid;
13831da177e4SLinus Torvalds 	return rc;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
13871da177e4SLinus Torvalds                           kernel_cap_t *inheritable, kernel_cap_t *permitted)
13881da177e4SLinus Torvalds {
13891da177e4SLinus Torvalds 	int error;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	error = task_has_perm(current, target, PROCESS__GETCAP);
13921da177e4SLinus Torvalds 	if (error)
13931da177e4SLinus Torvalds 		return error;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	return secondary_ops->capget(target, effective, inheritable, permitted);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective,
13991da177e4SLinus Torvalds                                 kernel_cap_t *inheritable, kernel_cap_t *permitted)
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds 	int error;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	error = secondary_ops->capset_check(target, effective, inheritable, permitted);
14041da177e4SLinus Torvalds 	if (error)
14051da177e4SLinus Torvalds 		return error;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	return task_has_perm(current, target, PROCESS__SETCAP);
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective,
14111da177e4SLinus Torvalds                                kernel_cap_t *inheritable, kernel_cap_t *permitted)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	secondary_ops->capset_set(target, effective, inheritable, permitted);
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap)
14171da177e4SLinus Torvalds {
14181da177e4SLinus Torvalds 	int rc;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	rc = secondary_ops->capable(tsk, cap);
14211da177e4SLinus Torvalds 	if (rc)
14221da177e4SLinus Torvalds 		return rc;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	return task_has_capability(tsk,cap);
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	int error = 0;
14301da177e4SLinus Torvalds 	u32 av;
14311da177e4SLinus Torvalds 	struct task_security_struct *tsec;
14321da177e4SLinus Torvalds 	u32 tsid;
14331da177e4SLinus Torvalds 	int rc;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	rc = secondary_ops->sysctl(table, op);
14361da177e4SLinus Torvalds 	if (rc)
14371da177e4SLinus Torvalds 		return rc;
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	tsec = current->security;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	rc = selinux_proc_get_sid(table->de, (op == 001) ?
14421da177e4SLinus Torvalds 	                          SECCLASS_DIR : SECCLASS_FILE, &tsid);
14431da177e4SLinus Torvalds 	if (rc) {
14441da177e4SLinus Torvalds 		/* Default to the well-defined sysctl SID. */
14451da177e4SLinus Torvalds 		tsid = SECINITSID_SYSCTL;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	/* The op values are "defined" in sysctl.c, thereby creating
14491da177e4SLinus Torvalds 	 * a bad coupling between this module and sysctl.c */
14501da177e4SLinus Torvalds 	if(op == 001) {
14511da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, tsid,
14521da177e4SLinus Torvalds 				     SECCLASS_DIR, DIR__SEARCH, NULL);
14531da177e4SLinus Torvalds 	} else {
14541da177e4SLinus Torvalds 		av = 0;
14551da177e4SLinus Torvalds 		if (op & 004)
14561da177e4SLinus Torvalds 			av |= FILE__READ;
14571da177e4SLinus Torvalds 		if (op & 002)
14581da177e4SLinus Torvalds 			av |= FILE__WRITE;
14591da177e4SLinus Torvalds 		if (av)
14601da177e4SLinus Torvalds 			error = avc_has_perm(tsec->sid, tsid,
14611da177e4SLinus Torvalds 					     SECCLASS_FILE, av, NULL);
14621da177e4SLinus Torvalds         }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	return error;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	int rc = 0;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	if (!sb)
14721da177e4SLinus Torvalds 		return 0;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	switch (cmds) {
14751da177e4SLinus Torvalds 		case Q_SYNC:
14761da177e4SLinus Torvalds 		case Q_QUOTAON:
14771da177e4SLinus Torvalds 		case Q_QUOTAOFF:
14781da177e4SLinus Torvalds 	        case Q_SETINFO:
14791da177e4SLinus Torvalds 		case Q_SETQUOTA:
14801da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
14811da177e4SLinus Torvalds 						 sb,
14821da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAMOD, NULL);
14831da177e4SLinus Torvalds 			break;
14841da177e4SLinus Torvalds 	        case Q_GETFMT:
14851da177e4SLinus Torvalds 	        case Q_GETINFO:
14861da177e4SLinus Torvalds 		case Q_GETQUOTA:
14871da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
14881da177e4SLinus Torvalds 						 sb,
14891da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAGET, NULL);
14901da177e4SLinus Torvalds 			break;
14911da177e4SLinus Torvalds 		default:
14921da177e4SLinus Torvalds 			rc = 0;  /* let the kernel handle invalid cmds */
14931da177e4SLinus Torvalds 			break;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 	return rc;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds static int selinux_syslog(int type)
15041da177e4SLinus Torvalds {
15051da177e4SLinus Torvalds 	int rc;
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	rc = secondary_ops->syslog(type);
15081da177e4SLinus Torvalds 	if (rc)
15091da177e4SLinus Torvalds 		return rc;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	switch (type) {
15121da177e4SLinus Torvalds 		case 3:         /* Read last kernel messages */
15131da177e4SLinus Torvalds 		case 10:        /* Return size of the log buffer */
15141da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_READ);
15151da177e4SLinus Torvalds 			break;
15161da177e4SLinus Torvalds 		case 6:         /* Disable logging to console */
15171da177e4SLinus Torvalds 		case 7:         /* Enable logging to console */
15181da177e4SLinus Torvalds 		case 8:		/* Set level of messages printed to console */
15191da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
15201da177e4SLinus Torvalds 			break;
15211da177e4SLinus Torvalds 		case 0:         /* Close log */
15221da177e4SLinus Torvalds 		case 1:         /* Open log */
15231da177e4SLinus Torvalds 		case 2:         /* Read from log */
15241da177e4SLinus Torvalds 		case 4:         /* Read/clear last kernel messages */
15251da177e4SLinus Torvalds 		case 5:         /* Clear ring buffer */
15261da177e4SLinus Torvalds 		default:
15271da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
15281da177e4SLinus Torvalds 			break;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 	return rc;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds /*
15341da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
15351da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
15361da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
15371da177e4SLinus Torvalds  *
15381da177e4SLinus Torvalds  * Note that secondary_ops->capable and task_has_perm_noaudit return 0
15391da177e4SLinus Torvalds  * if the capability is granted, but __vm_enough_memory requires 1 if
15401da177e4SLinus Torvalds  * the capability is granted.
15411da177e4SLinus Torvalds  *
15421da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
15431da177e4SLinus Torvalds  * processes that allocate mappings.
15441da177e4SLinus Torvalds  */
15451da177e4SLinus Torvalds static int selinux_vm_enough_memory(long pages)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
15481da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	rc = secondary_ops->capable(current, CAP_SYS_ADMIN);
15511da177e4SLinus Torvalds 	if (rc == 0)
15521da177e4SLinus Torvalds 		rc = avc_has_perm_noaudit(tsec->sid, tsec->sid,
15531da177e4SLinus Torvalds 					SECCLASS_CAPABILITY,
15541da177e4SLinus Torvalds 					CAP_TO_MASK(CAP_SYS_ADMIN),
15551da177e4SLinus Torvalds 					NULL);
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (rc == 0)
15581da177e4SLinus Torvalds 		cap_sys_admin = 1;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	return __vm_enough_memory(pages, cap_sys_admin);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds /* binprm security operations */
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
15681da177e4SLinus Torvalds 
156989d155efSJames Morris 	bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL);
15701da177e4SLinus Torvalds 	if (!bsec)
15711da177e4SLinus Torvalds 		return -ENOMEM;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	bsec->bprm = bprm;
15741da177e4SLinus Torvalds 	bsec->sid = SECINITSID_UNLABELED;
15751da177e4SLinus Torvalds 	bsec->set = 0;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	bprm->security = bsec;
15781da177e4SLinus Torvalds 	return 0;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm)
15821da177e4SLinus Torvalds {
15831da177e4SLinus Torvalds 	struct task_security_struct *tsec;
15841da177e4SLinus Torvalds 	struct inode *inode = bprm->file->f_dentry->d_inode;
15851da177e4SLinus Torvalds 	struct inode_security_struct *isec;
15861da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
15871da177e4SLinus Torvalds 	u32 newsid;
15881da177e4SLinus Torvalds 	struct avc_audit_data ad;
15891da177e4SLinus Torvalds 	int rc;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	rc = secondary_ops->bprm_set_security(bprm);
15921da177e4SLinus Torvalds 	if (rc)
15931da177e4SLinus Torvalds 		return rc;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	bsec = bprm->security;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	if (bsec->set)
15981da177e4SLinus Torvalds 		return 0;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	tsec = current->security;
16011da177e4SLinus Torvalds 	isec = inode->i_security;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	/* Default to the current task SID. */
16041da177e4SLinus Torvalds 	bsec->sid = tsec->sid;
16051da177e4SLinus Torvalds 
160628eba5bfSMichael LeMay 	/* Reset fs, key, and sock SIDs on execve. */
16071da177e4SLinus Torvalds 	tsec->create_sid = 0;
160828eba5bfSMichael LeMay 	tsec->keycreate_sid = 0;
160942c3e03eSEric Paris 	tsec->sockcreate_sid = 0;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	if (tsec->exec_sid) {
16121da177e4SLinus Torvalds 		newsid = tsec->exec_sid;
16131da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
16141da177e4SLinus Torvalds 		tsec->exec_sid = 0;
16151da177e4SLinus Torvalds 	} else {
16161da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
16171da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, isec->sid,
16181da177e4SLinus Torvalds 		                             SECCLASS_PROCESS, &newsid);
16191da177e4SLinus Torvalds 		if (rc)
16201da177e4SLinus Torvalds 			return rc;
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
16241da177e4SLinus Torvalds 	ad.u.fs.mnt = bprm->file->f_vfsmnt;
16251da177e4SLinus Torvalds 	ad.u.fs.dentry = bprm->file->f_dentry;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
16281da177e4SLinus Torvalds 		newsid = tsec->sid;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds         if (tsec->sid == newsid) {
16311da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, isec->sid,
16321da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
16331da177e4SLinus Torvalds 		if (rc)
16341da177e4SLinus Torvalds 			return rc;
16351da177e4SLinus Torvalds 	} else {
16361da177e4SLinus Torvalds 		/* Check permissions for the transition. */
16371da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, newsid,
16381da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
16391da177e4SLinus Torvalds 		if (rc)
16401da177e4SLinus Torvalds 			return rc;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 		rc = avc_has_perm(newsid, isec->sid,
16431da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
16441da177e4SLinus Torvalds 		if (rc)
16451da177e4SLinus Torvalds 			return rc;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		/* Clear any possibly unsafe personality bits on exec: */
16481da177e4SLinus Torvalds 		current->personality &= ~PER_CLEAR_ON_SETID;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 		/* Set the security field to the new SID. */
16511da177e4SLinus Torvalds 		bsec->sid = newsid;
16521da177e4SLinus Torvalds 	}
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	bsec->set = 1;
16551da177e4SLinus Torvalds 	return 0;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	return secondary_ops->bprm_check_security(bprm);
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm)
16651da177e4SLinus Torvalds {
16661da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
16671da177e4SLinus Torvalds 	int atsecure = 0;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	if (tsec->osid != tsec->sid) {
16701da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
16711da177e4SLinus Torvalds 		   the noatsecure permission is granted between
16721da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
16731da177e4SLinus Torvalds 		atsecure = avc_has_perm(tsec->osid, tsec->sid,
16741da177e4SLinus Torvalds 					 SECCLASS_PROCESS,
16751da177e4SLinus Torvalds 					 PROCESS__NOATSECURE, NULL);
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	return (atsecure || secondary_ops->bprm_secureexec(bprm));
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm)
16821da177e4SLinus Torvalds {
16839a5f04bfSJesper Juhl 	kfree(bprm->security);
16841da177e4SLinus Torvalds 	bprm->security = NULL;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount;
16881da177e4SLinus Torvalds extern struct dentry *selinux_null;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
16911da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	struct avc_audit_data ad;
16941da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
1695b20c8122SStephen Smalley 	struct tty_struct *tty;
1696badf1662SDipankar Sarma 	struct fdtable *fdt;
16971da177e4SLinus Torvalds 	long j = -1;
16981da177e4SLinus Torvalds 
1699b20c8122SStephen Smalley 	mutex_lock(&tty_mutex);
1700b20c8122SStephen Smalley 	tty = current->signal->tty;
17011da177e4SLinus Torvalds 	if (tty) {
17021da177e4SLinus Torvalds 		file_list_lock();
17032f512016SEric Dumazet 		file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);
17041da177e4SLinus Torvalds 		if (file) {
17051da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
17061da177e4SLinus Torvalds 			   Use inode_has_perm on the tty inode directly rather
17071da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
17081da177e4SLinus Torvalds 			   file may belong to another process and we are only
17091da177e4SLinus Torvalds 			   interested in the inode-based check here. */
17101da177e4SLinus Torvalds 			struct inode *inode = file->f_dentry->d_inode;
17111da177e4SLinus Torvalds 			if (inode_has_perm(current, inode,
17121da177e4SLinus Torvalds 					   FILE__READ | FILE__WRITE, NULL)) {
17131da177e4SLinus Torvalds 				/* Reset controlling tty. */
17141da177e4SLinus Torvalds 				current->signal->tty = NULL;
17151da177e4SLinus Torvalds 				current->signal->tty_old_pgrp = 0;
17161da177e4SLinus Torvalds 			}
17171da177e4SLinus Torvalds 		}
17181da177e4SLinus Torvalds 		file_list_unlock();
17191da177e4SLinus Torvalds 	}
1720b20c8122SStephen Smalley 	mutex_unlock(&tty_mutex);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	spin_lock(&files->file_lock);
17271da177e4SLinus Torvalds 	for (;;) {
17281da177e4SLinus Torvalds 		unsigned long set, i;
17291da177e4SLinus Torvalds 		int fd;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 		j++;
17321da177e4SLinus Torvalds 		i = j * __NFDBITS;
1733badf1662SDipankar Sarma 		fdt = files_fdtable(files);
1734badf1662SDipankar Sarma 		if (i >= fdt->max_fds || i >= fdt->max_fdset)
17351da177e4SLinus Torvalds 			break;
1736badf1662SDipankar Sarma 		set = fdt->open_fds->fds_bits[j];
17371da177e4SLinus Torvalds 		if (!set)
17381da177e4SLinus Torvalds 			continue;
17391da177e4SLinus Torvalds 		spin_unlock(&files->file_lock);
17401da177e4SLinus Torvalds 		for ( ; set ; i++,set >>= 1) {
17411da177e4SLinus Torvalds 			if (set & 1) {
17421da177e4SLinus Torvalds 				file = fget(i);
17431da177e4SLinus Torvalds 				if (!file)
17441da177e4SLinus Torvalds 					continue;
17451da177e4SLinus Torvalds 				if (file_has_perm(current,
17461da177e4SLinus Torvalds 						  file,
17471da177e4SLinus Torvalds 						  file_to_av(file))) {
17481da177e4SLinus Torvalds 					sys_close(i);
17491da177e4SLinus Torvalds 					fd = get_unused_fd();
17501da177e4SLinus Torvalds 					if (fd != i) {
17511da177e4SLinus Torvalds 						if (fd >= 0)
17521da177e4SLinus Torvalds 							put_unused_fd(fd);
17531da177e4SLinus Torvalds 						fput(file);
17541da177e4SLinus Torvalds 						continue;
17551da177e4SLinus Torvalds 					}
17561da177e4SLinus Torvalds 					if (devnull) {
1757095975daSNick Piggin 						get_file(devnull);
17581da177e4SLinus Torvalds 					} else {
17591da177e4SLinus Torvalds 						devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR);
1760fc5d81e6SAkinobu Mita 						if (IS_ERR(devnull)) {
1761fc5d81e6SAkinobu Mita 							devnull = NULL;
17621da177e4SLinus Torvalds 							put_unused_fd(fd);
17631da177e4SLinus Torvalds 							fput(file);
17641da177e4SLinus Torvalds 							continue;
17651da177e4SLinus Torvalds 						}
17661da177e4SLinus Torvalds 					}
17671da177e4SLinus Torvalds 					fd_install(fd, devnull);
17681da177e4SLinus Torvalds 				}
17691da177e4SLinus Torvalds 				fput(file);
17701da177e4SLinus Torvalds 			}
17711da177e4SLinus Torvalds 		}
17721da177e4SLinus Torvalds 		spin_lock(&files->file_lock);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 	spin_unlock(&files->file_lock);
17761da177e4SLinus Torvalds }
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
17791da177e4SLinus Torvalds {
17801da177e4SLinus Torvalds 	struct task_security_struct *tsec;
17811da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
17821da177e4SLinus Torvalds 	u32 sid;
17831da177e4SLinus Torvalds 	int rc;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	secondary_ops->bprm_apply_creds(bprm, unsafe);
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	tsec = current->security;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	bsec = bprm->security;
17901da177e4SLinus Torvalds 	sid = bsec->sid;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
17931da177e4SLinus Torvalds 	bsec->unsafe = 0;
17941da177e4SLinus Torvalds 	if (tsec->sid != sid) {
17951da177e4SLinus Torvalds 		/* Check for shared state.  If not ok, leave SID
17961da177e4SLinus Torvalds 		   unchanged and kill. */
17971da177e4SLinus Torvalds 		if (unsafe & LSM_UNSAFE_SHARE) {
17981da177e4SLinus Torvalds 			rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
17991da177e4SLinus Torvalds 					PROCESS__SHARE, NULL);
18001da177e4SLinus Torvalds 			if (rc) {
18011da177e4SLinus Torvalds 				bsec->unsafe = 1;
18021da177e4SLinus Torvalds 				return;
18031da177e4SLinus Torvalds 			}
18041da177e4SLinus Torvalds 		}
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
18071da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and kill. */
18081da177e4SLinus Torvalds 		if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
18091da177e4SLinus Torvalds 			rc = avc_has_perm(tsec->ptrace_sid, sid,
18101da177e4SLinus Torvalds 					  SECCLASS_PROCESS, PROCESS__PTRACE,
18111da177e4SLinus Torvalds 					  NULL);
18121da177e4SLinus Torvalds 			if (rc) {
18131da177e4SLinus Torvalds 				bsec->unsafe = 1;
18141da177e4SLinus Torvalds 				return;
18151da177e4SLinus Torvalds 			}
18161da177e4SLinus Torvalds 		}
18171da177e4SLinus Torvalds 		tsec->sid = sid;
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds /*
18221da177e4SLinus Torvalds  * called after apply_creds without the task lock held
18231da177e4SLinus Torvalds  */
18241da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm)
18251da177e4SLinus Torvalds {
18261da177e4SLinus Torvalds 	struct task_security_struct *tsec;
18271da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
18281da177e4SLinus Torvalds 	struct itimerval itimer;
18291da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
18301da177e4SLinus Torvalds 	int rc, i;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	tsec = current->security;
18331da177e4SLinus Torvalds 	bsec = bprm->security;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (bsec->unsafe) {
18361da177e4SLinus Torvalds 		force_sig_specific(SIGKILL, current);
18371da177e4SLinus Torvalds 		return;
18381da177e4SLinus Torvalds 	}
18391da177e4SLinus Torvalds 	if (tsec->osid == tsec->sid)
18401da177e4SLinus Torvalds 		return;
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
18431da177e4SLinus Torvalds 	flush_unauthorized_files(current->files);
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	/* Check whether the new SID can inherit signal state
18461da177e4SLinus Torvalds 	   from the old SID.  If not, clear itimers to avoid
18471da177e4SLinus Torvalds 	   subsequent signal generation and flush and unblock
18481da177e4SLinus Torvalds 	   signals. This must occur _after_ the task SID has
18491da177e4SLinus Torvalds 	  been updated so that any kill done after the flush
18501da177e4SLinus Torvalds 	  will be checked against the new SID. */
18511da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
18521da177e4SLinus Torvalds 			  PROCESS__SIGINH, NULL);
18531da177e4SLinus Torvalds 	if (rc) {
18541da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
18551da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
18561da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
18571da177e4SLinus Torvalds 		flush_signals(current);
18581da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
18591da177e4SLinus Torvalds 		flush_signal_handlers(current, 1);
18601da177e4SLinus Torvalds 		sigemptyset(&current->blocked);
18611da177e4SLinus Torvalds 		recalc_sigpending();
18621da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
18631da177e4SLinus Torvalds 	}
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	/* Check whether the new SID can inherit resource limits
18661da177e4SLinus Torvalds 	   from the old SID.  If not, reset all soft limits to
18671da177e4SLinus Torvalds 	   the lower of the current task's hard limit and the init
18681da177e4SLinus Torvalds 	   task's soft limit.  Note that the setting of hard limits
18691da177e4SLinus Torvalds 	   (even to lower them) can be controlled by the setrlimit
18701da177e4SLinus Torvalds 	   check. The inclusion of the init task's soft limit into
18711da177e4SLinus Torvalds 	   the computation is to avoid resetting soft limits higher
18721da177e4SLinus Torvalds 	   than the default soft limit for cases where the default
18731da177e4SLinus Torvalds 	   is lower than the hard limit, e.g. RLIMIT_CORE or
18741da177e4SLinus Torvalds 	   RLIMIT_STACK.*/
18751da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
18761da177e4SLinus Torvalds 			  PROCESS__RLIMITINH, NULL);
18771da177e4SLinus Torvalds 	if (rc) {
18781da177e4SLinus Torvalds 		for (i = 0; i < RLIM_NLIMITS; i++) {
18791da177e4SLinus Torvalds 			rlim = current->signal->rlim + i;
18801da177e4SLinus Torvalds 			initrlim = init_task.signal->rlim+i;
18811da177e4SLinus Torvalds 			rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur);
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 		if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
18841da177e4SLinus Torvalds 			/*
18851da177e4SLinus Torvalds 			 * This will cause RLIMIT_CPU calculations
18861da177e4SLinus Torvalds 			 * to be refigured.
18871da177e4SLinus Torvalds 			 */
18881da177e4SLinus Torvalds 			current->it_prof_expires = jiffies_to_cputime(1);
18891da177e4SLinus Torvalds 		}
18901da177e4SLinus Torvalds 	}
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	/* Wake up the parent if it is waiting so that it can
18931da177e4SLinus Torvalds 	   recheck wait permission to the new task SID. */
18941da177e4SLinus Torvalds 	wake_up_interruptible(&current->parent->signal->wait_chldexit);
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds /* superblock security operations */
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
19001da177e4SLinus Torvalds {
19011da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
19051da177e4SLinus Torvalds {
19061da177e4SLinus Torvalds 	superblock_free_security(sb);
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	if (plen > olen)
19121da177e4SLinus Torvalds 		return 0;
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
19181da177e4SLinus Torvalds {
19191da177e4SLinus Torvalds 	return (match_prefix("context=", sizeof("context=")-1, option, len) ||
19201da177e4SLinus Torvalds 	        match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) ||
19210808925eSEric Paris 	        match_prefix("defcontext=", sizeof("defcontext=")-1, option, len) ||
19220808925eSEric Paris 		match_prefix("rootcontext=", sizeof("rootcontext=")-1, option, len));
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	if (!*first) {
19281da177e4SLinus Torvalds 		**to = ',';
19291da177e4SLinus Torvalds 		*to += 1;
19303528a953SCory Olmo 	} else
19311da177e4SLinus Torvalds 		*first = 0;
19321da177e4SLinus Torvalds 	memcpy(*to, from, len);
19331da177e4SLinus Torvalds 	*to += len;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
19363528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first,
19373528a953SCory Olmo 		                       int len)
19383528a953SCory Olmo {
19393528a953SCory Olmo 	int current_size = 0;
19403528a953SCory Olmo 
19413528a953SCory Olmo 	if (!*first) {
19423528a953SCory Olmo 		**to = '|';
19433528a953SCory Olmo 		*to += 1;
19443528a953SCory Olmo 	}
19453528a953SCory Olmo 	else
19463528a953SCory Olmo 		*first = 0;
19473528a953SCory Olmo 
19483528a953SCory Olmo 	while (current_size < len) {
19493528a953SCory Olmo 		if (*from != '"') {
19503528a953SCory Olmo 			**to = *from;
19513528a953SCory Olmo 			*to += 1;
19523528a953SCory Olmo 		}
19533528a953SCory Olmo 		from += 1;
19543528a953SCory Olmo 		current_size += 1;
19553528a953SCory Olmo 	}
19563528a953SCory Olmo }
19573528a953SCory Olmo 
19581da177e4SLinus Torvalds static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy)
19591da177e4SLinus Torvalds {
19601da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
19611da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
19621da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
19633528a953SCory Olmo 	int open_quote = 0;
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	in_curr = orig;
19661da177e4SLinus Torvalds 	sec_curr = copy;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	/* Binary mount data: just copy */
19691da177e4SLinus Torvalds 	if (type->fs_flags & FS_BINARY_MOUNTDATA) {
19701da177e4SLinus Torvalds 		copy_page(sec_curr, in_curr);
19711da177e4SLinus Torvalds 		goto out;
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
19751da177e4SLinus Torvalds 	if (!nosec) {
19761da177e4SLinus Torvalds 		rc = -ENOMEM;
19771da177e4SLinus Torvalds 		goto out;
19781da177e4SLinus Torvalds 	}
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	nosec_save = nosec;
19811da177e4SLinus Torvalds 	fnosec = fsec = 1;
19821da177e4SLinus Torvalds 	in_save = in_end = orig;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	do {
19853528a953SCory Olmo 		if (*in_end == '"')
19863528a953SCory Olmo 			open_quote = !open_quote;
19873528a953SCory Olmo 		if ((*in_end == ',' && open_quote == 0) ||
19883528a953SCory Olmo 				*in_end == '\0') {
19891da177e4SLinus Torvalds 			int len = in_end - in_curr;
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
19923528a953SCory Olmo 				take_selinux_option(&sec_curr, in_curr, &fsec, len);
19931da177e4SLinus Torvalds 			else
19941da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 			in_curr = in_end + 1;
19971da177e4SLinus Torvalds 		}
19981da177e4SLinus Torvalds 	} while (*in_end++);
19991da177e4SLinus Torvalds 
20006931dfc9SEric Paris 	strcpy(in_save, nosec_save);
2001da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
20021da177e4SLinus Torvalds out:
20031da177e4SLinus Torvalds 	return rc;
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	struct avc_audit_data ad;
20091da177e4SLinus Torvalds 	int rc;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
20121da177e4SLinus Torvalds 	if (rc)
20131da177e4SLinus Torvalds 		return rc;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
20161da177e4SLinus Torvalds 	ad.u.fs.dentry = sb->s_root;
20171da177e4SLinus Torvalds 	return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad);
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
2020726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry)
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	struct avc_audit_data ad;
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
2025726c3342SDavid Howells 	ad.u.fs.dentry = dentry->d_sb->s_root;
2026726c3342SDavid Howells 	return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds static int selinux_mount(char * dev_name,
20301da177e4SLinus Torvalds                          struct nameidata *nd,
20311da177e4SLinus Torvalds                          char * type,
20321da177e4SLinus Torvalds                          unsigned long flags,
20331da177e4SLinus Torvalds                          void * data)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	int rc;
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data);
20381da177e4SLinus Torvalds 	if (rc)
20391da177e4SLinus Torvalds 		return rc;
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
20421da177e4SLinus Torvalds 		return superblock_has_perm(current, nd->mnt->mnt_sb,
20431da177e4SLinus Torvalds 		                           FILESYSTEM__REMOUNT, NULL);
20441da177e4SLinus Torvalds 	else
20451da177e4SLinus Torvalds 		return dentry_has_perm(current, nd->mnt, nd->dentry,
20461da177e4SLinus Torvalds 		                       FILE__MOUNTON);
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	int rc;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	rc = secondary_ops->sb_umount(mnt, flags);
20541da177e4SLinus Torvalds 	if (rc)
20551da177e4SLinus Torvalds 		return rc;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	return superblock_has_perm(current,mnt->mnt_sb,
20581da177e4SLinus Torvalds 	                           FILESYSTEM__UNMOUNT,NULL);
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds /* inode security operations */
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	return inode_alloc_security(inode);
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	inode_free_security(inode);
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
20735e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
20745e41ff9eSStephen Smalley 				       char **name, void **value,
20755e41ff9eSStephen Smalley 				       size_t *len)
20765e41ff9eSStephen Smalley {
20775e41ff9eSStephen Smalley 	struct task_security_struct *tsec;
20785e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
20795e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
2080570bc1c2SStephen Smalley 	u32 newsid, clen;
20815e41ff9eSStephen Smalley 	int rc;
2082570bc1c2SStephen Smalley 	char *namep = NULL, *context;
20835e41ff9eSStephen Smalley 
20845e41ff9eSStephen Smalley 	tsec = current->security;
20855e41ff9eSStephen Smalley 	dsec = dir->i_security;
20865e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
20875e41ff9eSStephen Smalley 
20885e41ff9eSStephen Smalley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
20895e41ff9eSStephen Smalley 		newsid = tsec->create_sid;
20905e41ff9eSStephen Smalley 	} else {
20915e41ff9eSStephen Smalley 		rc = security_transition_sid(tsec->sid, dsec->sid,
20925e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
20935e41ff9eSStephen Smalley 					     &newsid);
20945e41ff9eSStephen Smalley 		if (rc) {
20955e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
20965e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
20975e41ff9eSStephen Smalley 			       "ino=%ld)\n",
20985e41ff9eSStephen Smalley 			       __FUNCTION__,
20995e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
21005e41ff9eSStephen Smalley 			return rc;
21015e41ff9eSStephen Smalley 		}
21025e41ff9eSStephen Smalley 	}
21035e41ff9eSStephen Smalley 
2104296fddf7SEric Paris 	/* Possibly defer initialization to selinux_complete_init. */
2105296fddf7SEric Paris 	if (sbsec->initialized) {
2106296fddf7SEric Paris 		struct inode_security_struct *isec = inode->i_security;
2107296fddf7SEric Paris 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
2108296fddf7SEric Paris 		isec->sid = newsid;
2109296fddf7SEric Paris 		isec->initialized = 1;
2110296fddf7SEric Paris 	}
21115e41ff9eSStephen Smalley 
21128aad3875SStephen Smalley 	if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
211325a74f3bSStephen Smalley 		return -EOPNOTSUPP;
211425a74f3bSStephen Smalley 
2115570bc1c2SStephen Smalley 	if (name) {
21165e41ff9eSStephen Smalley 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL);
21175e41ff9eSStephen Smalley 		if (!namep)
21185e41ff9eSStephen Smalley 			return -ENOMEM;
21195e41ff9eSStephen Smalley 		*name = namep;
2120570bc1c2SStephen Smalley 	}
21215e41ff9eSStephen Smalley 
2122570bc1c2SStephen Smalley 	if (value && len) {
2123570bc1c2SStephen Smalley 		rc = security_sid_to_context(newsid, &context, &clen);
21245e41ff9eSStephen Smalley 		if (rc) {
21255e41ff9eSStephen Smalley 			kfree(namep);
21265e41ff9eSStephen Smalley 			return rc;
21275e41ff9eSStephen Smalley 		}
21285e41ff9eSStephen Smalley 		*value = context;
2129570bc1c2SStephen Smalley 		*len = clen;
2130570bc1c2SStephen Smalley 	}
21315e41ff9eSStephen Smalley 
21325e41ff9eSStephen Smalley 	return 0;
21335e41ff9eSStephen Smalley }
21345e41ff9eSStephen Smalley 
21351da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
21361da177e4SLinus Torvalds {
21371da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
21381da177e4SLinus Torvalds }
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds 	int rc;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	rc = secondary_ops->inode_link(old_dentry,dir,new_dentry);
21451da177e4SLinus Torvalds 	if (rc)
21461da177e4SLinus Torvalds 		return rc;
21471da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
21511da177e4SLinus Torvalds {
21521da177e4SLinus Torvalds 	int rc;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	rc = secondary_ops->inode_unlink(dir, dentry);
21551da177e4SLinus Torvalds 	if (rc)
21561da177e4SLinus Torvalds 		return rc;
21571da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
21611da177e4SLinus Torvalds {
21621da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
21761da177e4SLinus Torvalds {
21771da177e4SLinus Torvalds 	int rc;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	rc = secondary_ops->inode_mknod(dir, dentry, mode, dev);
21801da177e4SLinus Torvalds 	if (rc)
21811da177e4SLinus Torvalds 		return rc;
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
21871da177e4SLinus Torvalds                                 struct inode *new_inode, struct dentry *new_dentry)
21881da177e4SLinus Torvalds {
21891da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
21931da177e4SLinus Torvalds {
21941da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
21981da177e4SLinus Torvalds {
21991da177e4SLinus Torvalds 	int rc;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	rc = secondary_ops->inode_follow_link(dentry,nameidata);
22021da177e4SLinus Torvalds 	if (rc)
22031da177e4SLinus Torvalds 		return rc;
22041da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask,
22081da177e4SLinus Torvalds 				    struct nameidata *nd)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	int rc;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	rc = secondary_ops->inode_permission(inode, mask, nd);
22131da177e4SLinus Torvalds 	if (rc)
22141da177e4SLinus Torvalds 		return rc;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	if (!mask) {
22171da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
22181da177e4SLinus Torvalds 		return 0;
22191da177e4SLinus Torvalds 	}
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	return inode_has_perm(current, inode,
22221da177e4SLinus Torvalds 			       file_mask_to_av(inode->i_mode, mask), NULL);
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
22261da177e4SLinus Torvalds {
22271da177e4SLinus Torvalds 	int rc;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	rc = secondary_ops->inode_setattr(dentry, iattr);
22301da177e4SLinus Torvalds 	if (rc)
22311da177e4SLinus Torvalds 		return rc;
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	if (iattr->ia_valid & ATTR_FORCE)
22341da177e4SLinus Torvalds 		return 0;
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
22371da177e4SLinus Torvalds 			       ATTR_ATIME_SET | ATTR_MTIME_SET))
22381da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__WRITE);
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	return dentry_has_perm(current, mnt, dentry, FILE__GETATTR);
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags)
22491da177e4SLinus Torvalds {
22501da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
22511da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
22521da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
22531da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
22541da177e4SLinus Torvalds 	struct avc_audit_data ad;
22551da177e4SLinus Torvalds 	u32 newsid;
22561da177e4SLinus Torvalds 	int rc = 0;
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
22591da177e4SLinus Torvalds 		if (!strncmp(name, XATTR_SECURITY_PREFIX,
22601da177e4SLinus Torvalds 			     sizeof XATTR_SECURITY_PREFIX - 1) &&
22611da177e4SLinus Torvalds 		    !capable(CAP_SYS_ADMIN)) {
22621da177e4SLinus Torvalds 			/* A different attribute in the security namespace.
22631da177e4SLinus Torvalds 			   Restrict to administrator. */
22641da177e4SLinus Torvalds 			return -EPERM;
22651da177e4SLinus Torvalds 		}
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 		/* Not an attribute we recognize, so just check the
22681da177e4SLinus Torvalds 		   ordinary setattr permission. */
22691da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
22731da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
22741da177e4SLinus Torvalds 		return -EOPNOTSUPP;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
22771da177e4SLinus Torvalds 		return -EPERM;
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
22801da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass,
22831da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
22841da177e4SLinus Torvalds 	if (rc)
22851da177e4SLinus Torvalds 		return rc;
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
22881da177e4SLinus Torvalds 	if (rc)
22891da177e4SLinus Torvalds 		return rc;
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, isec->sclass,
22921da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
22931da177e4SLinus Torvalds 	if (rc)
22941da177e4SLinus Torvalds 		return rc;
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	rc = security_validate_transition(isec->sid, newsid, tsec->sid,
22971da177e4SLinus Torvalds 	                                  isec->sclass);
22981da177e4SLinus Torvalds 	if (rc)
22991da177e4SLinus Torvalds 		return rc;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	return avc_has_perm(newsid,
23021da177e4SLinus Torvalds 			    sbsec->sid,
23031da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
23041da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
23051da177e4SLinus Torvalds 			    &ad);
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name,
23091da177e4SLinus Torvalds                                         void *value, size_t size, int flags)
23101da177e4SLinus Torvalds {
23111da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
23121da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
23131da177e4SLinus Torvalds 	u32 newsid;
23141da177e4SLinus Torvalds 	int rc;
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
23171da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
23181da177e4SLinus Torvalds 		return;
23191da177e4SLinus Torvalds 	}
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
23221da177e4SLinus Torvalds 	if (rc) {
23231da177e4SLinus Torvalds 		printk(KERN_WARNING "%s:  unable to obtain SID for context "
23241da177e4SLinus Torvalds 		       "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc);
23251da177e4SLinus Torvalds 		return;
23261da177e4SLinus Torvalds 	}
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	isec->sid = newsid;
23291da177e4SLinus Torvalds 	return;
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name)
23331da177e4SLinus Torvalds {
23341da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry)
23381da177e4SLinus Torvalds {
23391da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
23451da177e4SLinus Torvalds 		if (!strncmp(name, XATTR_SECURITY_PREFIX,
23461da177e4SLinus Torvalds 			     sizeof XATTR_SECURITY_PREFIX - 1) &&
23471da177e4SLinus Torvalds 		    !capable(CAP_SYS_ADMIN)) {
23481da177e4SLinus Torvalds 			/* A different attribute in the security namespace.
23491da177e4SLinus Torvalds 			   Restrict to administrator. */
23501da177e4SLinus Torvalds 			return -EPERM;
23511da177e4SLinus Torvalds 		}
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 		/* Not an attribute we recognize, so just check the
23541da177e4SLinus Torvalds 		   ordinary setattr permission. Might want a separate
23551da177e4SLinus Torvalds 		   permission for removexattr. */
23561da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
23571da177e4SLinus Torvalds 	}
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
23601da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
23611da177e4SLinus Torvalds 	return -EACCES;
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
23648c8570fbSDustin Kirkland static const char *selinux_inode_xattr_getsuffix(void)
23658c8570fbSDustin Kirkland {
23668c8570fbSDustin Kirkland       return XATTR_SELINUX_SUFFIX;
23678c8570fbSDustin Kirkland }
23688c8570fbSDustin Kirkland 
2369d381d8a9SJames Morris /*
2370d381d8a9SJames Morris  * Copy the in-core inode security context value to the user.  If the
2371d381d8a9SJames Morris  * getxattr() prior to this succeeded, check to see if we need to
2372d381d8a9SJames Morris  * canonicalize the value to be finally returned to the user.
2373d381d8a9SJames Morris  *
2374d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2375d381d8a9SJames Morris  */
23767306a0b9SDustin Kirkland static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
23791da177e4SLinus Torvalds 
23808c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
23818c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
23821da177e4SLinus Torvalds 
23838c8570fbSDustin Kirkland 	return selinux_getsecurity(isec->sid, buffer, size);
23841da177e4SLinus Torvalds }
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
23871da177e4SLinus Torvalds                                      const void *value, size_t size, int flags)
23881da177e4SLinus Torvalds {
23891da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
23901da177e4SLinus Torvalds 	u32 newsid;
23911da177e4SLinus Torvalds 	int rc;
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
23941da177e4SLinus Torvalds 		return -EOPNOTSUPP;
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	if (!value || !size)
23971da177e4SLinus Torvalds 		return -EACCES;
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	rc = security_context_to_sid((void*)value, size, &newsid);
24001da177e4SLinus Torvalds 	if (rc)
24011da177e4SLinus Torvalds 		return rc;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 	isec->sid = newsid;
24041da177e4SLinus Torvalds 	return 0;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
24081da177e4SLinus Torvalds {
24091da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
24101da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
24111da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
24121da177e4SLinus Torvalds 	return len;
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds /* file security operations */
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds static int selinux_file_permission(struct file *file, int mask)
24181da177e4SLinus Torvalds {
24197420ed23SVenkat Yekkirala 	int rc;
24201da177e4SLinus Torvalds 	struct inode *inode = file->f_dentry->d_inode;
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	if (!mask) {
24231da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
24241da177e4SLinus Torvalds 		return 0;
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
24281da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
24291da177e4SLinus Torvalds 		mask |= MAY_APPEND;
24301da177e4SLinus Torvalds 
24317420ed23SVenkat Yekkirala 	rc = file_has_perm(current, file,
24321da177e4SLinus Torvalds 			   file_mask_to_av(inode->i_mode, mask));
24337420ed23SVenkat Yekkirala 	if (rc)
24347420ed23SVenkat Yekkirala 		return rc;
24357420ed23SVenkat Yekkirala 
24367420ed23SVenkat Yekkirala 	return selinux_netlbl_inode_permission(inode, mask);
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
24401da177e4SLinus Torvalds {
24411da177e4SLinus Torvalds 	return file_alloc_security(file);
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
24451da177e4SLinus Torvalds {
24461da177e4SLinus Torvalds 	file_free_security(file);
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
24501da177e4SLinus Torvalds 			      unsigned long arg)
24511da177e4SLinus Torvalds {
24521da177e4SLinus Torvalds 	int error = 0;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	switch (cmd) {
24551da177e4SLinus Torvalds 		case FIONREAD:
24561da177e4SLinus Torvalds 		/* fall through */
24571da177e4SLinus Torvalds 		case FIBMAP:
24581da177e4SLinus Torvalds 		/* fall through */
24591da177e4SLinus Torvalds 		case FIGETBSZ:
24601da177e4SLinus Torvalds 		/* fall through */
24611da177e4SLinus Torvalds 		case EXT2_IOC_GETFLAGS:
24621da177e4SLinus Torvalds 		/* fall through */
24631da177e4SLinus Torvalds 		case EXT2_IOC_GETVERSION:
24641da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__GETATTR);
24651da177e4SLinus Torvalds 			break;
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 		case EXT2_IOC_SETFLAGS:
24681da177e4SLinus Torvalds 		/* fall through */
24691da177e4SLinus Torvalds 		case EXT2_IOC_SETVERSION:
24701da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__SETATTR);
24711da177e4SLinus Torvalds 			break;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 		/* sys_ioctl() checks */
24741da177e4SLinus Torvalds 		case FIONBIO:
24751da177e4SLinus Torvalds 		/* fall through */
24761da177e4SLinus Torvalds 		case FIOASYNC:
24771da177e4SLinus Torvalds 			error = file_has_perm(current, file, 0);
24781da177e4SLinus Torvalds 			break;
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	        case KDSKBENT:
24811da177e4SLinus Torvalds 	        case KDSKBSENT:
24821da177e4SLinus Torvalds 			error = task_has_capability(current,CAP_SYS_TTY_CONFIG);
24831da177e4SLinus Torvalds 			break;
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 		/* default case assumes that the command will go
24861da177e4SLinus Torvalds 		 * to the file's ioctl() function.
24871da177e4SLinus Torvalds 		 */
24881da177e4SLinus Torvalds 		default:
24891da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__IOCTL);
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 	}
24921da177e4SLinus Torvalds 	return error;
24931da177e4SLinus Torvalds }
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
24961da177e4SLinus Torvalds {
24971da177e4SLinus Torvalds #ifndef CONFIG_PPC32
24981da177e4SLinus Torvalds 	if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
24991da177e4SLinus Torvalds 		/*
25001da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
25011da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
25021da177e4SLinus Torvalds 		 * This has an additional check.
25031da177e4SLinus Torvalds 		 */
25041da177e4SLinus Torvalds 		int rc = task_has_perm(current, current, PROCESS__EXECMEM);
25051da177e4SLinus Torvalds 		if (rc)
25061da177e4SLinus Torvalds 			return rc;
25071da177e4SLinus Torvalds 	}
25081da177e4SLinus Torvalds #endif
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	if (file) {
25111da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
25121da177e4SLinus Torvalds 		u32 av = FILE__READ;
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
25151da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
25161da177e4SLinus Torvalds 			av |= FILE__WRITE;
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
25191da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		return file_has_perm(current, file, av);
25221da177e4SLinus Torvalds 	}
25231da177e4SLinus Torvalds 	return 0;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot,
25271da177e4SLinus Torvalds 			     unsigned long prot, unsigned long flags)
25281da177e4SLinus Torvalds {
25291da177e4SLinus Torvalds 	int rc;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	rc = secondary_ops->file_mmap(file, reqprot, prot, flags);
25321da177e4SLinus Torvalds 	if (rc)
25331da177e4SLinus Torvalds 		return rc;
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	if (selinux_checkreqprot)
25361da177e4SLinus Torvalds 		prot = reqprot;
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
25391da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
25431da177e4SLinus Torvalds 				 unsigned long reqprot,
25441da177e4SLinus Torvalds 				 unsigned long prot)
25451da177e4SLinus Torvalds {
25461da177e4SLinus Torvalds 	int rc;
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	rc = secondary_ops->file_mprotect(vma, reqprot, prot);
25491da177e4SLinus Torvalds 	if (rc)
25501da177e4SLinus Torvalds 		return rc;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	if (selinux_checkreqprot)
25531da177e4SLinus Torvalds 		prot = reqprot;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds #ifndef CONFIG_PPC32
2556db4c9641SStephen Smalley 	if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
2557db4c9641SStephen Smalley 		rc = 0;
2558db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
2559db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
2560db4c9641SStephen Smalley 			rc = task_has_perm(current, current,
2561db4c9641SStephen Smalley 					   PROCESS__EXECHEAP);
2562db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
25636b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
25646b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
25656b992197SLorenzo Hernandez García-Hierro 			rc = task_has_perm(current, current, PROCESS__EXECSTACK);
2566db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
2567db4c9641SStephen Smalley 			/*
2568db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
2569db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
2570db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
2571db4c9641SStephen Smalley 			 * modified content.  This typically should only
2572db4c9641SStephen Smalley 			 * occur for text relocations.
2573db4c9641SStephen Smalley 			 */
2574db4c9641SStephen Smalley 			rc = file_has_perm(current, vma->vm_file,
2575db4c9641SStephen Smalley 					   FILE__EXECMOD);
2576db4c9641SStephen Smalley 		}
25776b992197SLorenzo Hernandez García-Hierro 		if (rc)
25786b992197SLorenzo Hernandez García-Hierro 			return rc;
25796b992197SLorenzo Hernandez García-Hierro 	}
25801da177e4SLinus Torvalds #endif
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
25831da177e4SLinus Torvalds }
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
25861da177e4SLinus Torvalds {
25871da177e4SLinus Torvalds 	return file_has_perm(current, file, FILE__LOCK);
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
25911da177e4SLinus Torvalds 			      unsigned long arg)
25921da177e4SLinus Torvalds {
25931da177e4SLinus Torvalds 	int err = 0;
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	switch (cmd) {
25961da177e4SLinus Torvalds 	        case F_SETFL:
25971da177e4SLinus Torvalds 			if (!file->f_dentry || !file->f_dentry->d_inode) {
25981da177e4SLinus Torvalds 				err = -EINVAL;
25991da177e4SLinus Torvalds 				break;
26001da177e4SLinus Torvalds 			}
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 			if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
26031da177e4SLinus Torvalds 				err = file_has_perm(current, file,FILE__WRITE);
26041da177e4SLinus Torvalds 				break;
26051da177e4SLinus Torvalds 			}
26061da177e4SLinus Torvalds 			/* fall through */
26071da177e4SLinus Torvalds 	        case F_SETOWN:
26081da177e4SLinus Torvalds 	        case F_SETSIG:
26091da177e4SLinus Torvalds 	        case F_GETFL:
26101da177e4SLinus Torvalds 	        case F_GETOWN:
26111da177e4SLinus Torvalds 	        case F_GETSIG:
26121da177e4SLinus Torvalds 			/* Just check FD__USE permission */
26131da177e4SLinus Torvalds 			err = file_has_perm(current, file, 0);
26141da177e4SLinus Torvalds 			break;
26151da177e4SLinus Torvalds 		case F_GETLK:
26161da177e4SLinus Torvalds 		case F_SETLK:
26171da177e4SLinus Torvalds 	        case F_SETLKW:
26181da177e4SLinus Torvalds #if BITS_PER_LONG == 32
26191da177e4SLinus Torvalds 	        case F_GETLK64:
26201da177e4SLinus Torvalds 		case F_SETLK64:
26211da177e4SLinus Torvalds 	        case F_SETLKW64:
26221da177e4SLinus Torvalds #endif
26231da177e4SLinus Torvalds 			if (!file->f_dentry || !file->f_dentry->d_inode) {
26241da177e4SLinus Torvalds 				err = -EINVAL;
26251da177e4SLinus Torvalds 				break;
26261da177e4SLinus Torvalds 			}
26271da177e4SLinus Torvalds 			err = file_has_perm(current, file, FILE__LOCK);
26281da177e4SLinus Torvalds 			break;
26291da177e4SLinus Torvalds 	}
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	return err;
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
26351da177e4SLinus Torvalds {
26361da177e4SLinus Torvalds 	struct task_security_struct *tsec;
26371da177e4SLinus Torvalds 	struct file_security_struct *fsec;
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds 	tsec = current->security;
26401da177e4SLinus Torvalds 	fsec = file->f_security;
26411da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	return 0;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
26471da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds         struct file *file;
26501da177e4SLinus Torvalds 	u32 perm;
26511da177e4SLinus Torvalds 	struct task_security_struct *tsec;
26521da177e4SLinus Torvalds 	struct file_security_struct *fsec;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
26551da177e4SLinus Torvalds         file = (struct file *)((long)fown - offsetof(struct file,f_owner));
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 	tsec = tsk->security;
26581da177e4SLinus Torvalds 	fsec = file->f_security;
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	if (!signum)
26611da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
26621da177e4SLinus Torvalds 	else
26631da177e4SLinus Torvalds 		perm = signal_to_av(signum);
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	return avc_has_perm(fsec->fown_sid, tsec->sid,
26661da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
26701da177e4SLinus Torvalds {
26711da177e4SLinus Torvalds 	return file_has_perm(current, file, file_to_av(file));
26721da177e4SLinus Torvalds }
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds /* task security operations */
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	int rc;
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds 	rc = secondary_ops->task_create(clone_flags);
26811da177e4SLinus Torvalds 	if (rc)
26821da177e4SLinus Torvalds 		return rc;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	return task_has_perm(current, current, PROCESS__FORK);
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk)
26881da177e4SLinus Torvalds {
26891da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
26901da177e4SLinus Torvalds 	int rc;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	tsec1 = current->security;
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	rc = task_alloc_security(tsk);
26951da177e4SLinus Torvalds 	if (rc)
26961da177e4SLinus Torvalds 		return rc;
26971da177e4SLinus Torvalds 	tsec2 = tsk->security;
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	tsec2->osid = tsec1->osid;
27001da177e4SLinus Torvalds 	tsec2->sid = tsec1->sid;
27011da177e4SLinus Torvalds 
270228eba5bfSMichael LeMay 	/* Retain the exec, fs, key, and sock SIDs across fork */
27031da177e4SLinus Torvalds 	tsec2->exec_sid = tsec1->exec_sid;
27041da177e4SLinus Torvalds 	tsec2->create_sid = tsec1->create_sid;
270528eba5bfSMichael LeMay 	tsec2->keycreate_sid = tsec1->keycreate_sid;
270642c3e03eSEric Paris 	tsec2->sockcreate_sid = tsec1->sockcreate_sid;
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	/* Retain ptracer SID across fork, if any.
27091da177e4SLinus Torvalds 	   This will be reset by the ptrace hook upon any
27101da177e4SLinus Torvalds 	   subsequent ptrace_attach operations. */
27111da177e4SLinus Torvalds 	tsec2->ptrace_sid = tsec1->ptrace_sid;
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 	return 0;
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	task_free_security(tsk);
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
27221da177e4SLinus Torvalds {
27231da177e4SLinus Torvalds 	/* Since setuid only affects the current process, and
27241da177e4SLinus Torvalds 	   since the SELinux controls are not based on the Linux
27251da177e4SLinus Torvalds 	   identity attributes, SELinux does not need to control
27261da177e4SLinus Torvalds 	   this operation.  However, SELinux does control the use
27271da177e4SLinus Torvalds 	   of the CAP_SETUID and CAP_SETGID capabilities using the
27281da177e4SLinus Torvalds 	   capable hook. */
27291da177e4SLinus Torvalds 	return 0;
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
27331da177e4SLinus Torvalds {
27341da177e4SLinus Torvalds 	return secondary_ops->task_post_setuid(id0,id1,id2,flags);
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
27381da177e4SLinus Torvalds {
27391da177e4SLinus Torvalds 	/* See the comment for setuid above. */
27401da177e4SLinus Torvalds 	return 0;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
27441da177e4SLinus Torvalds {
27451da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETPGID);
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
27491da177e4SLinus Torvalds {
27501da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETPGID);
27511da177e4SLinus Torvalds }
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
27541da177e4SLinus Torvalds {
27551da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSESSION);
27561da177e4SLinus Torvalds }
27571da177e4SLinus Torvalds 
2758f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
2759f9008e4cSDavid Quigley {
2760f9008e4cSDavid Quigley 	selinux_get_task_sid(p, secid);
2761f9008e4cSDavid Quigley }
2762f9008e4cSDavid Quigley 
27631da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	/* See the comment for setuid above. */
27661da177e4SLinus Torvalds 	return 0;
27671da177e4SLinus Torvalds }
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
27701da177e4SLinus Torvalds {
27711da177e4SLinus Torvalds 	int rc;
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 	rc = secondary_ops->task_setnice(p, nice);
27741da177e4SLinus Torvalds 	if (rc)
27751da177e4SLinus Torvalds 		return rc;
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	return task_has_perm(current,p, PROCESS__SETSCHED);
27781da177e4SLinus Torvalds }
27791da177e4SLinus Torvalds 
278003e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio)
278103e68060SJames Morris {
278203e68060SJames Morris 	return task_has_perm(current, p, PROCESS__SETSCHED);
278303e68060SJames Morris }
278403e68060SJames Morris 
2785a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p)
2786a1836a42SDavid Quigley {
2787a1836a42SDavid Quigley 	return task_has_perm(current, p, PROCESS__GETSCHED);
2788a1836a42SDavid Quigley }
2789a1836a42SDavid Quigley 
27901da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
27911da177e4SLinus Torvalds {
27921da177e4SLinus Torvalds 	struct rlimit *old_rlim = current->signal->rlim + resource;
27931da177e4SLinus Torvalds 	int rc;
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	rc = secondary_ops->task_setrlimit(resource, new_rlim);
27961da177e4SLinus Torvalds 	if (rc)
27971da177e4SLinus Torvalds 		return rc;
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
28001da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
28011da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
28021da177e4SLinus Torvalds 	   upon context transitions. See selinux_bprm_apply_creds. */
28031da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
28041da177e4SLinus Torvalds 		return task_has_perm(current, current, PROCESS__SETRLIMIT);
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	return 0;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
28101da177e4SLinus Torvalds {
28111da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETSCHED);
28121da177e4SLinus Torvalds }
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
28151da177e4SLinus Torvalds {
28161da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSCHED);
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds 
281935601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p)
282035601547SDavid Quigley {
282135601547SDavid Quigley 	return task_has_perm(current, p, PROCESS__SETSCHED);
282235601547SDavid Quigley }
282335601547SDavid Quigley 
2824f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
2825f9008e4cSDavid Quigley 				int sig, u32 secid)
28261da177e4SLinus Torvalds {
28271da177e4SLinus Torvalds 	u32 perm;
28281da177e4SLinus Torvalds 	int rc;
2829f9008e4cSDavid Quigley 	struct task_security_struct *tsec;
28301da177e4SLinus Torvalds 
2831f9008e4cSDavid Quigley 	rc = secondary_ops->task_kill(p, info, sig, secid);
28321da177e4SLinus Torvalds 	if (rc)
28331da177e4SLinus Torvalds 		return rc;
28341da177e4SLinus Torvalds 
2835621d3121SOleg Nesterov 	if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
28361da177e4SLinus Torvalds 		return 0;
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 	if (!sig)
28391da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
28401da177e4SLinus Torvalds 	else
28411da177e4SLinus Torvalds 		perm = signal_to_av(sig);
2842f9008e4cSDavid Quigley 	tsec = p->security;
2843f9008e4cSDavid Quigley 	if (secid)
2844f9008e4cSDavid Quigley 		rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL);
2845f9008e4cSDavid Quigley 	else
2846f9008e4cSDavid Quigley 		rc = task_has_perm(current, p, perm);
2847f9008e4cSDavid Quigley 	return rc;
28481da177e4SLinus Torvalds }
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds static int selinux_task_prctl(int option,
28511da177e4SLinus Torvalds 			      unsigned long arg2,
28521da177e4SLinus Torvalds 			      unsigned long arg3,
28531da177e4SLinus Torvalds 			      unsigned long arg4,
28541da177e4SLinus Torvalds 			      unsigned long arg5)
28551da177e4SLinus Torvalds {
28561da177e4SLinus Torvalds 	/* The current prctl operations do not appear to require
28571da177e4SLinus Torvalds 	   any SELinux controls since they merely observe or modify
28581da177e4SLinus Torvalds 	   the state of the current process. */
28591da177e4SLinus Torvalds 	return 0;
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
28631da177e4SLinus Torvalds {
28641da177e4SLinus Torvalds 	u32 perm;
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds 	perm = signal_to_av(p->exit_signal);
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	return task_has_perm(p, current, perm);
28691da177e4SLinus Torvalds }
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p)
28721da177e4SLinus Torvalds {
28731da177e4SLinus Torvalds   	struct task_security_struct *tsec;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	secondary_ops->task_reparent_to_init(p);
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds 	tsec = p->security;
28781da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
28791da177e4SLinus Torvalds 	tsec->sid = SECINITSID_KERNEL;
28801da177e4SLinus Torvalds 	return;
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
28841da177e4SLinus Torvalds 				  struct inode *inode)
28851da177e4SLinus Torvalds {
28861da177e4SLinus Torvalds 	struct task_security_struct *tsec = p->security;
28871da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	isec->sid = tsec->sid;
28901da177e4SLinus Torvalds 	isec->initialized = 1;
28911da177e4SLinus Torvalds 	return;
28921da177e4SLinus Torvalds }
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
289567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb,
289667f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
28971da177e4SLinus Torvalds {
28981da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
28991da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	offset = skb->nh.raw - skb->data;
29021da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
29031da177e4SLinus Torvalds 	if (ih == NULL)
29041da177e4SLinus Torvalds 		goto out;
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
29071da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
29081da177e4SLinus Torvalds 		goto out;
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	ad->u.net.v4info.saddr = ih->saddr;
29111da177e4SLinus Torvalds 	ad->u.net.v4info.daddr = ih->daddr;
29121da177e4SLinus Torvalds 	ret = 0;
29131da177e4SLinus Torvalds 
291467f83cbfSVenkat Yekkirala 	if (proto)
291567f83cbfSVenkat Yekkirala 		*proto = ih->protocol;
291667f83cbfSVenkat Yekkirala 
29171da177e4SLinus Torvalds 	switch (ih->protocol) {
29181da177e4SLinus Torvalds         case IPPROTO_TCP: {
29191da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
29221da177e4SLinus Torvalds         		break;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 		offset += ihlen;
29251da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
29261da177e4SLinus Torvalds 		if (th == NULL)
29271da177e4SLinus Torvalds 			break;
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
29301da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
29311da177e4SLinus Torvalds 		break;
29321da177e4SLinus Torvalds         }
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds         case IPPROTO_UDP: {
29351da177e4SLinus Torvalds         	struct udphdr _udph, *uh;
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
29381da177e4SLinus Torvalds         		break;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 		offset += ihlen;
29411da177e4SLinus Torvalds         	uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
29421da177e4SLinus Torvalds 		if (uh == NULL)
29431da177e4SLinus Torvalds 			break;
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds         	ad->u.net.sport = uh->source;
29461da177e4SLinus Torvalds         	ad->u.net.dport = uh->dest;
29471da177e4SLinus Torvalds         	break;
29481da177e4SLinus Torvalds         }
29491da177e4SLinus Torvalds 
2950*2ee92d46SJames Morris 	case IPPROTO_DCCP: {
2951*2ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
2952*2ee92d46SJames Morris 
2953*2ee92d46SJames Morris 		if (ntohs(ih->frag_off) & IP_OFFSET)
2954*2ee92d46SJames Morris 			break;
2955*2ee92d46SJames Morris 
2956*2ee92d46SJames Morris 		offset += ihlen;
2957*2ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
2958*2ee92d46SJames Morris 		if (dh == NULL)
2959*2ee92d46SJames Morris 			break;
2960*2ee92d46SJames Morris 
2961*2ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
2962*2ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
2963*2ee92d46SJames Morris 		break;
2964*2ee92d46SJames Morris         }
2965*2ee92d46SJames Morris 
29661da177e4SLinus Torvalds         default:
29671da177e4SLinus Torvalds         	break;
29681da177e4SLinus Torvalds         }
29691da177e4SLinus Torvalds out:
29701da177e4SLinus Torvalds 	return ret;
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
297667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb,
297767f83cbfSVenkat Yekkirala 			struct avc_audit_data *ad, u8 *proto)
29781da177e4SLinus Torvalds {
29791da177e4SLinus Torvalds 	u8 nexthdr;
29801da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
29811da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
29821da177e4SLinus Torvalds 
29831da177e4SLinus Torvalds 	offset = skb->nh.raw - skb->data;
29841da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
29851da177e4SLinus Torvalds 	if (ip6 == NULL)
29861da177e4SLinus Torvalds 		goto out;
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
29891da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
29901da177e4SLinus Torvalds 	ret = 0;
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
29931da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
29940d3d077cSHerbert Xu 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
29951da177e4SLinus Torvalds 	if (offset < 0)
29961da177e4SLinus Torvalds 		goto out;
29971da177e4SLinus Torvalds 
299867f83cbfSVenkat Yekkirala 	if (proto)
299967f83cbfSVenkat Yekkirala 		*proto = nexthdr;
300067f83cbfSVenkat Yekkirala 
30011da177e4SLinus Torvalds 	switch (nexthdr) {
30021da177e4SLinus Torvalds 	case IPPROTO_TCP: {
30031da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
30061da177e4SLinus Torvalds 		if (th == NULL)
30071da177e4SLinus Torvalds 			break;
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
30101da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
30111da177e4SLinus Torvalds 		break;
30121da177e4SLinus Torvalds 	}
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 	case IPPROTO_UDP: {
30151da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
30181da177e4SLinus Torvalds 		if (uh == NULL)
30191da177e4SLinus Torvalds 			break;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 		ad->u.net.sport = uh->source;
30221da177e4SLinus Torvalds 		ad->u.net.dport = uh->dest;
30231da177e4SLinus Torvalds 		break;
30241da177e4SLinus Torvalds 	}
30251da177e4SLinus Torvalds 
3026*2ee92d46SJames Morris 	case IPPROTO_DCCP: {
3027*2ee92d46SJames Morris 		struct dccp_hdr _dccph, *dh;
3028*2ee92d46SJames Morris 
3029*2ee92d46SJames Morris 		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3030*2ee92d46SJames Morris 		if (dh == NULL)
3031*2ee92d46SJames Morris 			break;
3032*2ee92d46SJames Morris 
3033*2ee92d46SJames Morris 		ad->u.net.sport = dh->dccph_sport;
3034*2ee92d46SJames Morris 		ad->u.net.dport = dh->dccph_dport;
3035*2ee92d46SJames Morris 		break;
3036*2ee92d46SJames Morris         }
3037*2ee92d46SJames Morris 
30381da177e4SLinus Torvalds 	/* includes fragments */
30391da177e4SLinus Torvalds 	default:
30401da177e4SLinus Torvalds 		break;
30411da177e4SLinus Torvalds 	}
30421da177e4SLinus Torvalds out:
30431da177e4SLinus Torvalds 	return ret;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds #endif /* IPV6 */
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
304967f83cbfSVenkat Yekkirala 			     char **addrp, int *len, int src, u8 *proto)
30501da177e4SLinus Torvalds {
30511da177e4SLinus Torvalds 	int ret = 0;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	switch (ad->u.net.family) {
30541da177e4SLinus Torvalds 	case PF_INET:
305567f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv4(skb, ad, proto);
30561da177e4SLinus Torvalds 		if (ret || !addrp)
30571da177e4SLinus Torvalds 			break;
30581da177e4SLinus Torvalds 		*len = 4;
30591da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v4info.saddr :
30601da177e4SLinus Torvalds 					&ad->u.net.v4info.daddr);
30611da177e4SLinus Torvalds 		break;
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
30641da177e4SLinus Torvalds 	case PF_INET6:
306567f83cbfSVenkat Yekkirala 		ret = selinux_parse_skb_ipv6(skb, ad, proto);
30661da177e4SLinus Torvalds 		if (ret || !addrp)
30671da177e4SLinus Torvalds 			break;
30681da177e4SLinus Torvalds 		*len = 16;
30691da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v6info.saddr :
30701da177e4SLinus Torvalds 					&ad->u.net.v6info.daddr);
30711da177e4SLinus Torvalds 		break;
30721da177e4SLinus Torvalds #endif	/* IPV6 */
30731da177e4SLinus Torvalds 	default:
30741da177e4SLinus Torvalds 		break;
30751da177e4SLinus Torvalds 	}
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 	return ret;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds /* socket security operations */
30811da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock,
30821da177e4SLinus Torvalds 			   u32 perms)
30831da177e4SLinus Torvalds {
30841da177e4SLinus Torvalds 	struct inode_security_struct *isec;
30851da177e4SLinus Torvalds 	struct task_security_struct *tsec;
30861da177e4SLinus Torvalds 	struct avc_audit_data ad;
30871da177e4SLinus Torvalds 	int err = 0;
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 	tsec = task->security;
30901da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	if (isec->sid == SECINITSID_KERNEL)
30931da177e4SLinus Torvalds 		goto out;
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
30961da177e4SLinus Torvalds 	ad.u.net.sk = sock->sk;
30971da177e4SLinus Torvalds 	err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds out:
31001da177e4SLinus Torvalds 	return err;
31011da177e4SLinus Torvalds }
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
31041da177e4SLinus Torvalds 				 int protocol, int kern)
31051da177e4SLinus Torvalds {
31061da177e4SLinus Torvalds 	int err = 0;
31071da177e4SLinus Torvalds 	struct task_security_struct *tsec;
310842c3e03eSEric Paris 	u32 newsid;
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds 	if (kern)
31111da177e4SLinus Torvalds 		goto out;
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	tsec = current->security;
311442c3e03eSEric Paris 	newsid = tsec->sockcreate_sid ? : tsec->sid;
311542c3e03eSEric Paris 	err = avc_has_perm(tsec->sid, newsid,
31161da177e4SLinus Torvalds 			   socket_type_to_security_class(family, type,
31171da177e4SLinus Torvalds 			   protocol), SOCKET__CREATE, NULL);
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds out:
31201da177e4SLinus Torvalds 	return err;
31211da177e4SLinus Torvalds }
31221da177e4SLinus Torvalds 
31237420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family,
31241da177e4SLinus Torvalds 				      int type, int protocol, int kern)
31251da177e4SLinus Torvalds {
31267420ed23SVenkat Yekkirala 	int err = 0;
31271da177e4SLinus Torvalds 	struct inode_security_struct *isec;
31281da177e4SLinus Torvalds 	struct task_security_struct *tsec;
3129892c141eSVenkat Yekkirala 	struct sk_security_struct *sksec;
313042c3e03eSEric Paris 	u32 newsid;
31311da177e4SLinus Torvalds 
31321da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 	tsec = current->security;
313542c3e03eSEric Paris 	newsid = tsec->sockcreate_sid ? : tsec->sid;
31361da177e4SLinus Torvalds 	isec->sclass = socket_type_to_security_class(family, type, protocol);
313742c3e03eSEric Paris 	isec->sid = kern ? SECINITSID_KERNEL : newsid;
31381da177e4SLinus Torvalds 	isec->initialized = 1;
31391da177e4SLinus Torvalds 
3140892c141eSVenkat Yekkirala 	if (sock->sk) {
3141892c141eSVenkat Yekkirala 		sksec = sock->sk->sk_security;
3142892c141eSVenkat Yekkirala 		sksec->sid = isec->sid;
31437420ed23SVenkat Yekkirala 		err = selinux_netlbl_socket_post_create(sock,
31447420ed23SVenkat Yekkirala 							family,
31457420ed23SVenkat Yekkirala 							isec->sid);
3146892c141eSVenkat Yekkirala 	}
3147892c141eSVenkat Yekkirala 
31487420ed23SVenkat Yekkirala 	return err;
31491da177e4SLinus Torvalds }
31501da177e4SLinus Torvalds 
31511da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
31521da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
31531da177e4SLinus Torvalds    permission check between the socket and the port number. */
31541da177e4SLinus Torvalds #define ip_local_port_range_0 sysctl_local_port_range[0]
31551da177e4SLinus Torvalds #define ip_local_port_range_1 sysctl_local_port_range[1]
31561da177e4SLinus Torvalds 
31571da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
31581da177e4SLinus Torvalds {
31591da177e4SLinus Torvalds 	u16 family;
31601da177e4SLinus Torvalds 	int err;
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__BIND);
31631da177e4SLinus Torvalds 	if (err)
31641da177e4SLinus Torvalds 		goto out;
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds 	/*
31671da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
316813402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
316913402580SJames Morris 	 * check the first address now.
31701da177e4SLinus Torvalds 	 */
31711da177e4SLinus Torvalds 	family = sock->sk->sk_family;
31721da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
31731da177e4SLinus Torvalds 		char *addrp;
31741da177e4SLinus Torvalds 		struct inode_security_struct *isec;
31751da177e4SLinus Torvalds 		struct task_security_struct *tsec;
31761da177e4SLinus Torvalds 		struct avc_audit_data ad;
31771da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
31781da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
31791da177e4SLinus Torvalds 		unsigned short snum;
31801da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
31811da177e4SLinus Torvalds 		u32 sid, node_perm, addrlen;
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds 		tsec = current->security;
31841da177e4SLinus Torvalds 		isec = SOCK_INODE(sock)->i_security;
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds 		if (family == PF_INET) {
31871da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
31881da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
31891da177e4SLinus Torvalds 			addrlen = sizeof(addr4->sin_addr.s_addr);
31901da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
31911da177e4SLinus Torvalds 		} else {
31921da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
31931da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
31941da177e4SLinus Torvalds 			addrlen = sizeof(addr6->sin6_addr.s6_addr);
31951da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
31961da177e4SLinus Torvalds 		}
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 		if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) ||
31991da177e4SLinus Torvalds 			   snum > ip_local_port_range_1)) {
32001da177e4SLinus Torvalds 			err = security_port_sid(sk->sk_family, sk->sk_type,
32011da177e4SLinus Torvalds 						sk->sk_protocol, snum, &sid);
32021da177e4SLinus Torvalds 			if (err)
32031da177e4SLinus Torvalds 				goto out;
32041da177e4SLinus Torvalds 			AVC_AUDIT_DATA_INIT(&ad,NET);
32051da177e4SLinus Torvalds 			ad.u.net.sport = htons(snum);
32061da177e4SLinus Torvalds 			ad.u.net.family = family;
32071da177e4SLinus Torvalds 			err = avc_has_perm(isec->sid, sid,
32081da177e4SLinus Torvalds 					   isec->sclass,
32091da177e4SLinus Torvalds 					   SOCKET__NAME_BIND, &ad);
32101da177e4SLinus Torvalds 			if (err)
32111da177e4SLinus Torvalds 				goto out;
32121da177e4SLinus Torvalds 		}
32131da177e4SLinus Torvalds 
321413402580SJames Morris 		switch(isec->sclass) {
321513402580SJames Morris 		case SECCLASS_TCP_SOCKET:
32161da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
32171da177e4SLinus Torvalds 			break;
32181da177e4SLinus Torvalds 
321913402580SJames Morris 		case SECCLASS_UDP_SOCKET:
32201da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
32211da177e4SLinus Torvalds 			break;
32221da177e4SLinus Torvalds 
3223*2ee92d46SJames Morris 		case SECCLASS_DCCP_SOCKET:
3224*2ee92d46SJames Morris 			node_perm = DCCP_SOCKET__NODE_BIND;
3225*2ee92d46SJames Morris 			break;
3226*2ee92d46SJames Morris 
32271da177e4SLinus Torvalds 		default:
32281da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
32291da177e4SLinus Torvalds 			break;
32301da177e4SLinus Torvalds 		}
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 		err = security_node_sid(family, addrp, addrlen, &sid);
32331da177e4SLinus Torvalds 		if (err)
32341da177e4SLinus Torvalds 			goto out;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
32371da177e4SLinus Torvalds 		ad.u.net.sport = htons(snum);
32381da177e4SLinus Torvalds 		ad.u.net.family = family;
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 		if (family == PF_INET)
32411da177e4SLinus Torvalds 			ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
32421da177e4SLinus Torvalds 		else
32431da177e4SLinus Torvalds 			ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, sid,
32461da177e4SLinus Torvalds 		                   isec->sclass, node_perm, &ad);
32471da177e4SLinus Torvalds 		if (err)
32481da177e4SLinus Torvalds 			goto out;
32491da177e4SLinus Torvalds 	}
32501da177e4SLinus Torvalds out:
32511da177e4SLinus Torvalds 	return err;
32521da177e4SLinus Torvalds }
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
32551da177e4SLinus Torvalds {
32561da177e4SLinus Torvalds 	struct inode_security_struct *isec;
32571da177e4SLinus Torvalds 	int err;
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__CONNECT);
32601da177e4SLinus Torvalds 	if (err)
32611da177e4SLinus Torvalds 		return err;
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	/*
3264*2ee92d46SJames Morris 	 * If a TCP or DCCP socket, check name_connect permission for the port.
32651da177e4SLinus Torvalds 	 */
32661da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
3267*2ee92d46SJames Morris 	if (isec->sclass == SECCLASS_TCP_SOCKET ||
3268*2ee92d46SJames Morris 	    isec->sclass == SECCLASS_DCCP_SOCKET) {
32691da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
32701da177e4SLinus Torvalds 		struct avc_audit_data ad;
32711da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
32721da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
32731da177e4SLinus Torvalds 		unsigned short snum;
3274*2ee92d46SJames Morris 		u32 sid, perm;
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
32771da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3278911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
32791da177e4SLinus Torvalds 				return -EINVAL;
32801da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
32811da177e4SLinus Torvalds 		} else {
32821da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3283911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
32841da177e4SLinus Torvalds 				return -EINVAL;
32851da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
32861da177e4SLinus Torvalds 		}
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family, sk->sk_type,
32891da177e4SLinus Torvalds 					sk->sk_protocol, snum, &sid);
32901da177e4SLinus Torvalds 		if (err)
32911da177e4SLinus Torvalds 			goto out;
32921da177e4SLinus Torvalds 
3293*2ee92d46SJames Morris 		perm = (isec->sclass == SECCLASS_TCP_SOCKET) ?
3294*2ee92d46SJames Morris 		       TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3295*2ee92d46SJames Morris 
32961da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
32971da177e4SLinus Torvalds 		ad.u.net.dport = htons(snum);
32981da177e4SLinus Torvalds 		ad.u.net.family = sk->sk_family;
3299*2ee92d46SJames Morris 		err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad);
33001da177e4SLinus Torvalds 		if (err)
33011da177e4SLinus Torvalds 			goto out;
33021da177e4SLinus Torvalds 	}
33031da177e4SLinus Torvalds 
33041da177e4SLinus Torvalds out:
33051da177e4SLinus Torvalds 	return err;
33061da177e4SLinus Torvalds }
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
33091da177e4SLinus Torvalds {
33101da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__LISTEN);
33111da177e4SLinus Torvalds }
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
33141da177e4SLinus Torvalds {
33151da177e4SLinus Torvalds 	int err;
33161da177e4SLinus Torvalds 	struct inode_security_struct *isec;
33171da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
33181da177e4SLinus Torvalds 
33191da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__ACCEPT);
33201da177e4SLinus Torvalds 	if (err)
33211da177e4SLinus Torvalds 		return err;
33221da177e4SLinus Torvalds 
33231da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
33261da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
33271da177e4SLinus Torvalds 	newisec->sid = isec->sid;
33281da177e4SLinus Torvalds 	newisec->initialized = 1;
33291da177e4SLinus Torvalds 
33301da177e4SLinus Torvalds 	return 0;
33311da177e4SLinus Torvalds }
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
33341da177e4SLinus Torvalds  				  int size)
33351da177e4SLinus Torvalds {
33367420ed23SVenkat Yekkirala 	int rc;
33377420ed23SVenkat Yekkirala 
33387420ed23SVenkat Yekkirala 	rc = socket_has_perm(current, sock, SOCKET__WRITE);
33397420ed23SVenkat Yekkirala 	if (rc)
33407420ed23SVenkat Yekkirala 		return rc;
33417420ed23SVenkat Yekkirala 
33427420ed23SVenkat Yekkirala 	return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE);
33431da177e4SLinus Torvalds }
33441da177e4SLinus Torvalds 
33451da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
33461da177e4SLinus Torvalds 				  int size, int flags)
33471da177e4SLinus Torvalds {
33481da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__READ);
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
33521da177e4SLinus Torvalds {
33531da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
33541da177e4SLinus Torvalds }
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
33571da177e4SLinus Torvalds {
33581da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
33591da177e4SLinus Torvalds }
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname)
33621da177e4SLinus Torvalds {
3363f8687afeSPaul Moore 	int err;
3364f8687afeSPaul Moore 
3365f8687afeSPaul Moore 	err = socket_has_perm(current, sock, SOCKET__SETOPT);
3366f8687afeSPaul Moore 	if (err)
3367f8687afeSPaul Moore 		return err;
3368f8687afeSPaul Moore 
3369f8687afeSPaul Moore 	return selinux_netlbl_socket_setsockopt(sock, level, optname);
33701da177e4SLinus Torvalds }
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
33731da177e4SLinus Torvalds 				     int optname)
33741da177e4SLinus Torvalds {
33751da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETOPT);
33761da177e4SLinus Torvalds }
33771da177e4SLinus Torvalds 
33781da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
33791da177e4SLinus Torvalds {
33801da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
33811da177e4SLinus Torvalds }
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock,
33841da177e4SLinus Torvalds 					      struct socket *other,
33851da177e4SLinus Torvalds 					      struct sock *newsk)
33861da177e4SLinus Torvalds {
33871da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
33881da177e4SLinus Torvalds 	struct inode_security_struct *isec;
33891da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
33901da177e4SLinus Torvalds 	struct avc_audit_data ad;
33911da177e4SLinus Torvalds 	int err;
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds 	err = secondary_ops->unix_stream_connect(sock, other, newsk);
33941da177e4SLinus Torvalds 	if (err)
33951da177e4SLinus Torvalds 		return err;
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
33981da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
34011da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
34041da177e4SLinus Torvalds 			   isec->sclass,
34051da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
34061da177e4SLinus Torvalds 	if (err)
34071da177e4SLinus Torvalds 		return err;
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds 	/* connecting socket */
34101da177e4SLinus Torvalds 	ssec = sock->sk->sk_security;
34111da177e4SLinus Torvalds 	ssec->peer_sid = other_isec->sid;
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds 	/* server child socket */
34141da177e4SLinus Torvalds 	ssec = newsk->sk_security;
34151da177e4SLinus Torvalds 	ssec->peer_sid = isec->sid;
34164237c75cSVenkat Yekkirala 	err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid);
34171da177e4SLinus Torvalds 
34184237c75cSVenkat Yekkirala 	return err;
34191da177e4SLinus Torvalds }
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
34221da177e4SLinus Torvalds 					struct socket *other)
34231da177e4SLinus Torvalds {
34241da177e4SLinus Torvalds 	struct inode_security_struct *isec;
34251da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
34261da177e4SLinus Torvalds 	struct avc_audit_data ad;
34271da177e4SLinus Torvalds 	int err;
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
34301da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
34311da177e4SLinus Torvalds 
34321da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
34331da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
34361da177e4SLinus Torvalds 			   isec->sclass, SOCKET__SENDTO, &ad);
34371da177e4SLinus Torvalds 	if (err)
34381da177e4SLinus Torvalds 		return err;
34391da177e4SLinus Torvalds 
34401da177e4SLinus Torvalds 	return 0;
34411da177e4SLinus Torvalds }
34421da177e4SLinus Torvalds 
34434e5ab4cbSJames Morris static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
34444237c75cSVenkat Yekkirala 		struct avc_audit_data *ad, u16 family, char *addrp, int len)
34451da177e4SLinus Torvalds {
34464e5ab4cbSJames Morris 	int err = 0;
34471da177e4SLinus Torvalds 	u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0;
34484237c75cSVenkat Yekkirala 	struct socket *sock;
34494237c75cSVenkat Yekkirala 	u16 sock_class = 0;
34504237c75cSVenkat Yekkirala 	u32 sock_sid = 0;
34514237c75cSVenkat Yekkirala 
34524237c75cSVenkat Yekkirala  	read_lock_bh(&sk->sk_callback_lock);
34534237c75cSVenkat Yekkirala  	sock = sk->sk_socket;
34544237c75cSVenkat Yekkirala  	if (sock) {
34554237c75cSVenkat Yekkirala  		struct inode *inode;
34564237c75cSVenkat Yekkirala  		inode = SOCK_INODE(sock);
34574237c75cSVenkat Yekkirala  		if (inode) {
34584237c75cSVenkat Yekkirala  			struct inode_security_struct *isec;
34594237c75cSVenkat Yekkirala  			isec = inode->i_security;
34604237c75cSVenkat Yekkirala  			sock_sid = isec->sid;
34614237c75cSVenkat Yekkirala  			sock_class = isec->sclass;
34624237c75cSVenkat Yekkirala  		}
34634237c75cSVenkat Yekkirala  	}
34644237c75cSVenkat Yekkirala  	read_unlock_bh(&sk->sk_callback_lock);
34654237c75cSVenkat Yekkirala  	if (!sock_sid)
34664237c75cSVenkat Yekkirala   		goto out;
34671da177e4SLinus Torvalds 
34684e5ab4cbSJames Morris 	if (!skb->dev)
34691da177e4SLinus Torvalds 		goto out;
34701da177e4SLinus Torvalds 
34714e5ab4cbSJames Morris 	err = sel_netif_sids(skb->dev, &if_sid, NULL);
34721da177e4SLinus Torvalds 	if (err)
34731da177e4SLinus Torvalds 		goto out;
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 	switch (sock_class) {
34761da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
34771da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_RECV;
34781da177e4SLinus Torvalds 		node_perm = NODE__UDP_RECV;
34791da177e4SLinus Torvalds 		recv_perm = UDP_SOCKET__RECV_MSG;
34801da177e4SLinus Torvalds 		break;
34811da177e4SLinus Torvalds 
34821da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
34831da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_RECV;
34841da177e4SLinus Torvalds 		node_perm = NODE__TCP_RECV;
34851da177e4SLinus Torvalds 		recv_perm = TCP_SOCKET__RECV_MSG;
34861da177e4SLinus Torvalds 		break;
34871da177e4SLinus Torvalds 
3488*2ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
3489*2ee92d46SJames Morris 		netif_perm = NETIF__DCCP_RECV;
3490*2ee92d46SJames Morris 		node_perm = NODE__DCCP_RECV;
3491*2ee92d46SJames Morris 		recv_perm = DCCP_SOCKET__RECV_MSG;
3492*2ee92d46SJames Morris 		break;
3493*2ee92d46SJames Morris 
34941da177e4SLinus Torvalds 	default:
34951da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_RECV;
34961da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_RECV;
34971da177e4SLinus Torvalds 		break;
34981da177e4SLinus Torvalds 	}
34991da177e4SLinus Torvalds 
35004e5ab4cbSJames Morris 	err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
35011da177e4SLinus Torvalds 	if (err)
35021da177e4SLinus Torvalds 		goto out;
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 	err = security_node_sid(family, addrp, len, &node_sid);
35051da177e4SLinus Torvalds 	if (err)
35061da177e4SLinus Torvalds 		goto out;
35071da177e4SLinus Torvalds 
35084e5ab4cbSJames Morris 	err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, ad);
35091da177e4SLinus Torvalds 	if (err)
35101da177e4SLinus Torvalds 		goto out;
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds 	if (recv_perm) {
35131da177e4SLinus Torvalds 		u32 port_sid;
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family, sk->sk_type,
35164e5ab4cbSJames Morris 		                        sk->sk_protocol, ntohs(ad->u.net.sport),
35171da177e4SLinus Torvalds 		                        &port_sid);
35181da177e4SLinus Torvalds 		if (err)
35191da177e4SLinus Torvalds 			goto out;
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 		err = avc_has_perm(sock_sid, port_sid,
35224e5ab4cbSJames Morris 				   sock_class, recv_perm, ad);
35231da177e4SLinus Torvalds 	}
3524d28d1e08STrent Jaeger 
35254e5ab4cbSJames Morris out:
35264e5ab4cbSJames Morris 	return err;
35274e5ab4cbSJames Morris }
3528d28d1e08STrent Jaeger 
35294e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
35304e5ab4cbSJames Morris {
35314e5ab4cbSJames Morris 	u16 family;
35324e5ab4cbSJames Morris 	char *addrp;
35334e5ab4cbSJames Morris 	int len, err = 0;
35344e5ab4cbSJames Morris 	struct avc_audit_data ad;
35354237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
35364e5ab4cbSJames Morris 
35374e5ab4cbSJames Morris 	family = sk->sk_family;
35384e5ab4cbSJames Morris 	if (family != PF_INET && family != PF_INET6)
35394e5ab4cbSJames Morris 		goto out;
35404e5ab4cbSJames Morris 
35414e5ab4cbSJames Morris 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
35424e5ab4cbSJames Morris 	if (family == PF_INET6 && skb->protocol == ntohs(ETH_P_IP))
35434e5ab4cbSJames Morris 		family = PF_INET;
35444e5ab4cbSJames Morris 
35454e5ab4cbSJames Morris 	AVC_AUDIT_DATA_INIT(&ad, NET);
35464e5ab4cbSJames Morris 	ad.u.net.netif = skb->dev ? skb->dev->name : "[unknown]";
35474e5ab4cbSJames Morris 	ad.u.net.family = family;
35484e5ab4cbSJames Morris 
354967f83cbfSVenkat Yekkirala 	err = selinux_parse_skb(skb, &ad, &addrp, &len, 1, NULL);
35504e5ab4cbSJames Morris 	if (err)
35514e5ab4cbSJames Morris 		goto out;
35524e5ab4cbSJames Morris 
35534e5ab4cbSJames Morris 	if (selinux_compat_net)
35544237c75cSVenkat Yekkirala 		err = selinux_sock_rcv_skb_compat(sk, skb, &ad, family,
35554e5ab4cbSJames Morris 						  addrp, len);
35564e5ab4cbSJames Morris 	else
35574237c75cSVenkat Yekkirala 		err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET,
35584e5ab4cbSJames Morris 				   PACKET__RECV, &ad);
35594e5ab4cbSJames Morris 	if (err)
35604e5ab4cbSJames Morris 		goto out;
35614e5ab4cbSJames Morris 
35627420ed23SVenkat Yekkirala 	err = selinux_netlbl_sock_rcv_skb(sksec, skb, &ad);
35637420ed23SVenkat Yekkirala 	if (err)
35647420ed23SVenkat Yekkirala 		goto out;
35657420ed23SVenkat Yekkirala 
35664237c75cSVenkat Yekkirala 	err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
35671da177e4SLinus Torvalds out:
35681da177e4SLinus Torvalds 	return err;
35691da177e4SLinus Torvalds }
35701da177e4SLinus Torvalds 
35712c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
35721da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
35731da177e4SLinus Torvalds {
35741da177e4SLinus Torvalds 	int err = 0;
35751da177e4SLinus Torvalds 	char *scontext;
35761da177e4SLinus Torvalds 	u32 scontext_len;
35771da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
35781da177e4SLinus Torvalds 	struct inode_security_struct *isec;
35792c7946a7SCatherine Zhang 	u32 peer_sid = 0;
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
35822c7946a7SCatherine Zhang 
35832c7946a7SCatherine Zhang 	/* if UNIX_STREAM check peer_sid, if TCP check dst for labelled sa */
35842c7946a7SCatherine Zhang 	if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET) {
35852c7946a7SCatherine Zhang 		ssec = sock->sk->sk_security;
35862c7946a7SCatherine Zhang 		peer_sid = ssec->peer_sid;
35872c7946a7SCatherine Zhang 	}
35882c7946a7SCatherine Zhang 	else if (isec->sclass == SECCLASS_TCP_SOCKET) {
35897420ed23SVenkat Yekkirala 		peer_sid = selinux_netlbl_socket_getpeersec_stream(sock);
35906b877699SVenkat Yekkirala 		if (peer_sid == SECSID_NULL) {
35916b877699SVenkat Yekkirala 			ssec = sock->sk->sk_security;
35926b877699SVenkat Yekkirala 			peer_sid = ssec->peer_sid;
35936b877699SVenkat Yekkirala 		}
35942c7946a7SCatherine Zhang 		if (peer_sid == SECSID_NULL) {
35952c7946a7SCatherine Zhang 			err = -ENOPROTOOPT;
35962c7946a7SCatherine Zhang 			goto out;
35972c7946a7SCatherine Zhang 		}
35982c7946a7SCatherine Zhang 	}
35992c7946a7SCatherine Zhang 	else {
36001da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
36011da177e4SLinus Torvalds 		goto out;
36021da177e4SLinus Torvalds 	}
36031da177e4SLinus Torvalds 
36042c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 	if (err)
36071da177e4SLinus Torvalds 		goto out;
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	if (scontext_len > len) {
36101da177e4SLinus Torvalds 		err = -ERANGE;
36111da177e4SLinus Torvalds 		goto out_len;
36121da177e4SLinus Torvalds 	}
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
36151da177e4SLinus Torvalds 		err = -EFAULT;
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds out_len:
36181da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
36191da177e4SLinus Torvalds 		err = -EFAULT;
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds 	kfree(scontext);
36221da177e4SLinus Torvalds out:
36231da177e4SLinus Torvalds 	return err;
36241da177e4SLinus Torvalds }
36251da177e4SLinus Torvalds 
3626dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
36272c7946a7SCatherine Zhang {
3628dc49c1f9SCatherine Zhang 	u32 peer_secid = SECSID_NULL;
36292c7946a7SCatherine Zhang 	int err = 0;
3630877ce7c1SCatherine Zhang 
3631dc49c1f9SCatherine Zhang 	if (sock && (sock->sk->sk_family == PF_UNIX))
3632dc49c1f9SCatherine Zhang 		selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid);
36337420ed23SVenkat Yekkirala 	else if (skb) {
36347420ed23SVenkat Yekkirala 		peer_secid = selinux_netlbl_socket_getpeersec_dgram(skb);
36357420ed23SVenkat Yekkirala 		if (peer_secid == SECSID_NULL)
3636dc49c1f9SCatherine Zhang 			peer_secid = selinux_socket_getpeer_dgram(skb);
36377420ed23SVenkat Yekkirala 	}
36382c7946a7SCatherine Zhang 
3639dc49c1f9SCatherine Zhang 	if (peer_secid == SECSID_NULL)
3640dc49c1f9SCatherine Zhang 		err = -EINVAL;
3641dc49c1f9SCatherine Zhang 	*secid = peer_secid;
36422c7946a7SCatherine Zhang 
36432c7946a7SCatherine Zhang 	return err;
36442c7946a7SCatherine Zhang }
36452c7946a7SCatherine Zhang 
36467d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
36471da177e4SLinus Torvalds {
36481da177e4SLinus Torvalds 	return sk_alloc_security(sk, family, priority);
36491da177e4SLinus Torvalds }
36501da177e4SLinus Torvalds 
36511da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
36521da177e4SLinus Torvalds {
36531da177e4SLinus Torvalds 	sk_free_security(sk);
36541da177e4SLinus Torvalds }
36551da177e4SLinus Torvalds 
3656892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
3657892c141eSVenkat Yekkirala {
3658892c141eSVenkat Yekkirala 	struct sk_security_struct *ssec = sk->sk_security;
3659892c141eSVenkat Yekkirala 	struct sk_security_struct *newssec = newsk->sk_security;
3660892c141eSVenkat Yekkirala 
3661892c141eSVenkat Yekkirala 	newssec->sid = ssec->sid;
3662892c141eSVenkat Yekkirala 	newssec->peer_sid = ssec->peer_sid;
366399f59ed0SPaul Moore 
366499f59ed0SPaul Moore 	selinux_netlbl_sk_clone_security(ssec, newssec);
3665892c141eSVenkat Yekkirala }
3666892c141eSVenkat Yekkirala 
3667beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
3668d28d1e08STrent Jaeger {
3669d28d1e08STrent Jaeger 	if (!sk)
3670beb8d13bSVenkat Yekkirala 		*secid = SECINITSID_ANY_SOCKET;
3671892c141eSVenkat Yekkirala 	else {
3672892c141eSVenkat Yekkirala 		struct sk_security_struct *sksec = sk->sk_security;
3673d28d1e08STrent Jaeger 
3674beb8d13bSVenkat Yekkirala 		*secid = sksec->sid;
3675892c141eSVenkat Yekkirala 	}
3676d28d1e08STrent Jaeger }
3677d28d1e08STrent Jaeger 
36789a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent)
36794237c75cSVenkat Yekkirala {
36804237c75cSVenkat Yekkirala 	struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
36814237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
36824237c75cSVenkat Yekkirala 
36832148ccc4SDavid Woodhouse 	if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
36842148ccc4SDavid Woodhouse 	    sk->sk_family == PF_UNIX)
36854237c75cSVenkat Yekkirala 		isec->sid = sksec->sid;
36867420ed23SVenkat Yekkirala 
36877420ed23SVenkat Yekkirala 	selinux_netlbl_sock_graft(sk, parent);
36884237c75cSVenkat Yekkirala }
36894237c75cSVenkat Yekkirala 
36909a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
36914237c75cSVenkat Yekkirala 				     struct request_sock *req)
36924237c75cSVenkat Yekkirala {
36934237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
36944237c75cSVenkat Yekkirala 	int err;
36957420ed23SVenkat Yekkirala 	u32 newsid;
36964237c75cSVenkat Yekkirala 	u32 peersid;
36974237c75cSVenkat Yekkirala 
36987420ed23SVenkat Yekkirala 	newsid = selinux_netlbl_inet_conn_request(skb, sksec->sid);
36997420ed23SVenkat Yekkirala 	if (newsid != SECSID_NULL) {
37007420ed23SVenkat Yekkirala 		req->secid = newsid;
37017420ed23SVenkat Yekkirala 		return 0;
37027420ed23SVenkat Yekkirala 	}
37037420ed23SVenkat Yekkirala 
37046b877699SVenkat Yekkirala 	selinux_skb_xfrm_sid(skb, &peersid);
37054237c75cSVenkat Yekkirala 
3706a51c64f1SVenkat Yekkirala 	if (peersid == SECSID_NULL) {
3707a51c64f1SVenkat Yekkirala 		req->secid = sksec->sid;
37086b877699SVenkat Yekkirala 		req->peer_secid = 0;
3709a51c64f1SVenkat Yekkirala 		return 0;
3710a51c64f1SVenkat Yekkirala 	}
3711a51c64f1SVenkat Yekkirala 
37124237c75cSVenkat Yekkirala 	err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
37134237c75cSVenkat Yekkirala 	if (err)
37144237c75cSVenkat Yekkirala 		return err;
37154237c75cSVenkat Yekkirala 
37164237c75cSVenkat Yekkirala 	req->secid = newsid;
37176b877699SVenkat Yekkirala 	req->peer_secid = peersid;
37184237c75cSVenkat Yekkirala 	return 0;
37194237c75cSVenkat Yekkirala }
37204237c75cSVenkat Yekkirala 
37219a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk,
37229a673e56SAdrian Bunk 				   const struct request_sock *req)
37234237c75cSVenkat Yekkirala {
37244237c75cSVenkat Yekkirala 	struct sk_security_struct *newsksec = newsk->sk_security;
37254237c75cSVenkat Yekkirala 
37264237c75cSVenkat Yekkirala 	newsksec->sid = req->secid;
37276b877699SVenkat Yekkirala 	newsksec->peer_sid = req->peer_secid;
37284237c75cSVenkat Yekkirala 	/* NOTE: Ideally, we should also get the isec->sid for the
37294237c75cSVenkat Yekkirala 	   new socket in sync, but we don't have the isec available yet.
37304237c75cSVenkat Yekkirala 	   So we will wait until sock_graft to do it, by which
37314237c75cSVenkat Yekkirala 	   time it will have been created and available. */
373299f59ed0SPaul Moore 
373399f59ed0SPaul Moore 	selinux_netlbl_sk_security_init(newsksec, req->rsk_ops->family);
37344237c75cSVenkat Yekkirala }
37354237c75cSVenkat Yekkirala 
37366b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk,
37376b877699SVenkat Yekkirala 				struct sk_buff *skb)
37386b877699SVenkat Yekkirala {
37396b877699SVenkat Yekkirala 	struct sk_security_struct *sksec = sk->sk_security;
37406b877699SVenkat Yekkirala 
37416b877699SVenkat Yekkirala 	selinux_skb_xfrm_sid(skb, &sksec->peer_sid);
37426b877699SVenkat Yekkirala }
37436b877699SVenkat Yekkirala 
37449a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req,
37459a673e56SAdrian Bunk 				      struct flowi *fl)
37464237c75cSVenkat Yekkirala {
37474237c75cSVenkat Yekkirala 	fl->secid = req->secid;
37484237c75cSVenkat Yekkirala }
37494237c75cSVenkat Yekkirala 
37501da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
37511da177e4SLinus Torvalds {
37521da177e4SLinus Torvalds 	int err = 0;
37531da177e4SLinus Torvalds 	u32 perm;
37541da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
37551da177e4SLinus Torvalds 	struct socket *sock = sk->sk_socket;
37561da177e4SLinus Torvalds 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
37571da177e4SLinus Torvalds 
37581da177e4SLinus Torvalds 	if (skb->len < NLMSG_SPACE(0)) {
37591da177e4SLinus Torvalds 		err = -EINVAL;
37601da177e4SLinus Torvalds 		goto out;
37611da177e4SLinus Torvalds 	}
37621da177e4SLinus Torvalds 	nlh = (struct nlmsghdr *)skb->data;
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 	err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
37651da177e4SLinus Torvalds 	if (err) {
37661da177e4SLinus Torvalds 		if (err == -EINVAL) {
37679ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
37681da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
37691da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
37701da177e4SLinus Torvalds 				  nlh->nlmsg_type, isec->sclass);
37711da177e4SLinus Torvalds 			if (!selinux_enforcing)
37721da177e4SLinus Torvalds 				err = 0;
37731da177e4SLinus Torvalds 		}
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds 		/* Ignore */
37761da177e4SLinus Torvalds 		if (err == -ENOENT)
37771da177e4SLinus Torvalds 			err = 0;
37781da177e4SLinus Torvalds 		goto out;
37791da177e4SLinus Torvalds 	}
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, perm);
37821da177e4SLinus Torvalds out:
37831da177e4SLinus Torvalds 	return err;
37841da177e4SLinus Torvalds }
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
37871da177e4SLinus Torvalds 
37884e5ab4cbSJames Morris static int selinux_ip_postroute_last_compat(struct sock *sk, struct net_device *dev,
37894e5ab4cbSJames Morris 					    struct avc_audit_data *ad,
37904e5ab4cbSJames Morris 					    u16 family, char *addrp, int len)
37911da177e4SLinus Torvalds {
37924237c75cSVenkat Yekkirala 	int err = 0;
37931da177e4SLinus Torvalds 	u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0;
37944237c75cSVenkat Yekkirala 	struct socket *sock;
37954237c75cSVenkat Yekkirala 	struct inode *inode;
37964237c75cSVenkat Yekkirala 	struct inode_security_struct *isec;
37974237c75cSVenkat Yekkirala 
37984237c75cSVenkat Yekkirala 	sock = sk->sk_socket;
37994237c75cSVenkat Yekkirala 	if (!sock)
38004237c75cSVenkat Yekkirala 		goto out;
38014237c75cSVenkat Yekkirala 
38024237c75cSVenkat Yekkirala 	inode = SOCK_INODE(sock);
38034237c75cSVenkat Yekkirala 	if (!inode)
38044237c75cSVenkat Yekkirala 		goto out;
38054237c75cSVenkat Yekkirala 
38064237c75cSVenkat Yekkirala 	isec = inode->i_security;
38071da177e4SLinus Torvalds 
38081da177e4SLinus Torvalds 	err = sel_netif_sids(dev, &if_sid, NULL);
38091da177e4SLinus Torvalds 	if (err)
38101da177e4SLinus Torvalds 		goto out;
38111da177e4SLinus Torvalds 
38121da177e4SLinus Torvalds 	switch (isec->sclass) {
38131da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
38141da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_SEND;
38151da177e4SLinus Torvalds 		node_perm = NODE__UDP_SEND;
38161da177e4SLinus Torvalds 		send_perm = UDP_SOCKET__SEND_MSG;
38171da177e4SLinus Torvalds 		break;
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
38201da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_SEND;
38211da177e4SLinus Torvalds 		node_perm = NODE__TCP_SEND;
38221da177e4SLinus Torvalds 		send_perm = TCP_SOCKET__SEND_MSG;
38231da177e4SLinus Torvalds 		break;
38241da177e4SLinus Torvalds 
3825*2ee92d46SJames Morris 	case SECCLASS_DCCP_SOCKET:
3826*2ee92d46SJames Morris 		netif_perm = NETIF__DCCP_SEND;
3827*2ee92d46SJames Morris 		node_perm = NODE__DCCP_SEND;
3828*2ee92d46SJames Morris 		send_perm = DCCP_SOCKET__SEND_MSG;
3829*2ee92d46SJames Morris 		break;
3830*2ee92d46SJames Morris 
38311da177e4SLinus Torvalds 	default:
38321da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_SEND;
38331da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_SEND;
38341da177e4SLinus Torvalds 		break;
38351da177e4SLinus Torvalds 	}
38361da177e4SLinus Torvalds 
38374e5ab4cbSJames Morris 	err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
38384e5ab4cbSJames Morris 	if (err)
38391da177e4SLinus Torvalds 		goto out;
38401da177e4SLinus Torvalds 
38414e5ab4cbSJames Morris 	err = security_node_sid(family, addrp, len, &node_sid);
38424e5ab4cbSJames Morris 	if (err)
38431da177e4SLinus Torvalds 		goto out;
38441da177e4SLinus Torvalds 
38454e5ab4cbSJames Morris 	err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE, node_perm, ad);
38464e5ab4cbSJames Morris 	if (err)
38471da177e4SLinus Torvalds 		goto out;
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	if (send_perm) {
38501da177e4SLinus Torvalds 		u32 port_sid;
38511da177e4SLinus Torvalds 
38521da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family,
38531da177e4SLinus Torvalds 		                        sk->sk_type,
38541da177e4SLinus Torvalds 		                        sk->sk_protocol,
38554e5ab4cbSJames Morris 		                        ntohs(ad->u.net.dport),
38564e5ab4cbSJames Morris 		                        &port_sid);
38574e5ab4cbSJames Morris 		if (err)
38581da177e4SLinus Torvalds 			goto out;
38591da177e4SLinus Torvalds 
38601da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, port_sid, isec->sclass,
38614e5ab4cbSJames Morris 				   send_perm, ad);
38624e5ab4cbSJames Morris 	}
38634e5ab4cbSJames Morris out:
38644e5ab4cbSJames Morris 	return err;
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
38674e5ab4cbSJames Morris static unsigned int selinux_ip_postroute_last(unsigned int hooknum,
38684e5ab4cbSJames Morris                                               struct sk_buff **pskb,
38694e5ab4cbSJames Morris                                               const struct net_device *in,
38704e5ab4cbSJames Morris                                               const struct net_device *out,
38714e5ab4cbSJames Morris                                               int (*okfn)(struct sk_buff *),
38724e5ab4cbSJames Morris                                               u16 family)
38734e5ab4cbSJames Morris {
38744e5ab4cbSJames Morris 	char *addrp;
38754e5ab4cbSJames Morris 	int len, err = 0;
38764e5ab4cbSJames Morris 	struct sock *sk;
38774e5ab4cbSJames Morris 	struct sk_buff *skb = *pskb;
38784e5ab4cbSJames Morris 	struct avc_audit_data ad;
38794e5ab4cbSJames Morris 	struct net_device *dev = (struct net_device *)out;
38804237c75cSVenkat Yekkirala 	struct sk_security_struct *sksec;
388167f83cbfSVenkat Yekkirala 	u8 proto;
38824e5ab4cbSJames Morris 
38834e5ab4cbSJames Morris 	sk = skb->sk;
38844e5ab4cbSJames Morris 	if (!sk)
38854e5ab4cbSJames Morris 		goto out;
38864e5ab4cbSJames Morris 
38874237c75cSVenkat Yekkirala 	sksec = sk->sk_security;
38884e5ab4cbSJames Morris 
38894e5ab4cbSJames Morris 	AVC_AUDIT_DATA_INIT(&ad, NET);
38904e5ab4cbSJames Morris 	ad.u.net.netif = dev->name;
38914e5ab4cbSJames Morris 	ad.u.net.family = family;
38924e5ab4cbSJames Morris 
389367f83cbfSVenkat Yekkirala 	err = selinux_parse_skb(skb, &ad, &addrp, &len, 0, &proto);
38944e5ab4cbSJames Morris 	if (err)
38954e5ab4cbSJames Morris 		goto out;
38964e5ab4cbSJames Morris 
38974e5ab4cbSJames Morris 	if (selinux_compat_net)
38984237c75cSVenkat Yekkirala 		err = selinux_ip_postroute_last_compat(sk, dev, &ad,
38994e5ab4cbSJames Morris 						       family, addrp, len);
39004e5ab4cbSJames Morris 	else
39014237c75cSVenkat Yekkirala 		err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET,
39024e5ab4cbSJames Morris 				   PACKET__SEND, &ad);
39034e5ab4cbSJames Morris 
39044e5ab4cbSJames Morris 	if (err)
3905d28d1e08STrent Jaeger 		goto out;
3906d28d1e08STrent Jaeger 
390767f83cbfSVenkat Yekkirala 	err = selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto);
39081da177e4SLinus Torvalds out:
39094e5ab4cbSJames Morris 	return err ? NF_DROP : NF_ACCEPT;
39101da177e4SLinus Torvalds }
39111da177e4SLinus Torvalds 
39121da177e4SLinus Torvalds static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum,
39131da177e4SLinus Torvalds 						struct sk_buff **pskb,
39141da177e4SLinus Torvalds 						const struct net_device *in,
39151da177e4SLinus Torvalds 						const struct net_device *out,
39161da177e4SLinus Torvalds 						int (*okfn)(struct sk_buff *))
39171da177e4SLinus Torvalds {
39181da177e4SLinus Torvalds 	return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET);
39191da177e4SLinus Torvalds }
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum,
39241da177e4SLinus Torvalds 						struct sk_buff **pskb,
39251da177e4SLinus Torvalds 						const struct net_device *in,
39261da177e4SLinus Torvalds 						const struct net_device *out,
39271da177e4SLinus Torvalds 						int (*okfn)(struct sk_buff *))
39281da177e4SLinus Torvalds {
39291da177e4SLinus Torvalds 	return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6);
39301da177e4SLinus Torvalds }
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds #endif	/* IPV6 */
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
39351da177e4SLinus Torvalds 
39361da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
39371da177e4SLinus Torvalds {
39381da177e4SLinus Torvalds 	int err;
39391da177e4SLinus Torvalds 
39401da177e4SLinus Torvalds 	err = secondary_ops->netlink_send(sk, skb);
39411da177e4SLinus Torvalds 	if (err)
39421da177e4SLinus Torvalds 		return err;
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 	if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
39451da177e4SLinus Torvalds 		err = selinux_nlmsg_perm(sk, skb);
39461da177e4SLinus Torvalds 
39471da177e4SLinus Torvalds 	return err;
39481da177e4SLinus Torvalds }
39491da177e4SLinus Torvalds 
3950c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability)
39511da177e4SLinus Torvalds {
3952c7bdb545SDarrel Goeddel 	int err;
3953c7bdb545SDarrel Goeddel 	struct avc_audit_data ad;
3954c7bdb545SDarrel Goeddel 
3955c7bdb545SDarrel Goeddel 	err = secondary_ops->netlink_recv(skb, capability);
3956c7bdb545SDarrel Goeddel 	if (err)
3957c7bdb545SDarrel Goeddel 		return err;
3958c7bdb545SDarrel Goeddel 
3959c7bdb545SDarrel Goeddel 	AVC_AUDIT_DATA_INIT(&ad, CAP);
3960c7bdb545SDarrel Goeddel 	ad.u.cap = capability;
3961c7bdb545SDarrel Goeddel 
3962c7bdb545SDarrel Goeddel 	return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid,
3963c7bdb545SDarrel Goeddel 	                    SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad);
39641da177e4SLinus Torvalds }
39651da177e4SLinus Torvalds 
39661da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
39671da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
39681da177e4SLinus Torvalds 			      u16 sclass)
39691da177e4SLinus Torvalds {
39701da177e4SLinus Torvalds 	struct task_security_struct *tsec = task->security;
39711da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
39721da177e4SLinus Torvalds 
397389d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
39741da177e4SLinus Torvalds 	if (!isec)
39751da177e4SLinus Torvalds 		return -ENOMEM;
39761da177e4SLinus Torvalds 
39771da177e4SLinus Torvalds 	isec->sclass = sclass;
39781da177e4SLinus Torvalds 	isec->ipc_perm = perm;
39791da177e4SLinus Torvalds 	isec->sid = tsec->sid;
39801da177e4SLinus Torvalds 	perm->security = isec;
39811da177e4SLinus Torvalds 
39821da177e4SLinus Torvalds 	return 0;
39831da177e4SLinus Torvalds }
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
39861da177e4SLinus Torvalds {
39871da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
39881da177e4SLinus Torvalds 	perm->security = NULL;
39891da177e4SLinus Torvalds 	kfree(isec);
39901da177e4SLinus Torvalds }
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
39931da177e4SLinus Torvalds {
39941da177e4SLinus Torvalds 	struct msg_security_struct *msec;
39951da177e4SLinus Torvalds 
399689d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
39971da177e4SLinus Torvalds 	if (!msec)
39981da177e4SLinus Torvalds 		return -ENOMEM;
39991da177e4SLinus Torvalds 
40001da177e4SLinus Torvalds 	msec->msg = msg;
40011da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
40021da177e4SLinus Torvalds 	msg->security = msec;
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds 	return 0;
40051da177e4SLinus Torvalds }
40061da177e4SLinus Torvalds 
40071da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
40081da177e4SLinus Torvalds {
40091da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 	msg->security = NULL;
40121da177e4SLinus Torvalds 	kfree(msec);
40131da177e4SLinus Torvalds }
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
40166af963f1SStephen Smalley 			u32 perms)
40171da177e4SLinus Torvalds {
40181da177e4SLinus Torvalds 	struct task_security_struct *tsec;
40191da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
40201da177e4SLinus Torvalds 	struct avc_audit_data ad;
40211da177e4SLinus Torvalds 
40221da177e4SLinus Torvalds 	tsec = current->security;
40231da177e4SLinus Torvalds 	isec = ipc_perms->security;
40241da177e4SLinus Torvalds 
40251da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
40261da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
40271da177e4SLinus Torvalds 
40286af963f1SStephen Smalley 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
40291da177e4SLinus Torvalds }
40301da177e4SLinus Torvalds 
40311da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
40321da177e4SLinus Torvalds {
40331da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
40341da177e4SLinus Torvalds }
40351da177e4SLinus Torvalds 
40361da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
40371da177e4SLinus Torvalds {
40381da177e4SLinus Torvalds 	msg_msg_free_security(msg);
40391da177e4SLinus Torvalds }
40401da177e4SLinus Torvalds 
40411da177e4SLinus Torvalds /* message queue security operations */
40421da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
40431da177e4SLinus Torvalds {
40441da177e4SLinus Torvalds 	struct task_security_struct *tsec;
40451da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
40461da177e4SLinus Torvalds 	struct avc_audit_data ad;
40471da177e4SLinus Torvalds 	int rc;
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
40501da177e4SLinus Torvalds 	if (rc)
40511da177e4SLinus Torvalds 		return rc;
40521da177e4SLinus Torvalds 
40531da177e4SLinus Torvalds 	tsec = current->security;
40541da177e4SLinus Torvalds 	isec = msq->q_perm.security;
40551da177e4SLinus Torvalds 
40561da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
40571da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
40581da177e4SLinus Torvalds 
40591da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
40601da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
40611da177e4SLinus Torvalds 	if (rc) {
40621da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
40631da177e4SLinus Torvalds 		return rc;
40641da177e4SLinus Torvalds 	}
40651da177e4SLinus Torvalds 	return 0;
40661da177e4SLinus Torvalds }
40671da177e4SLinus Torvalds 
40681da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
40691da177e4SLinus Torvalds {
40701da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
40711da177e4SLinus Torvalds }
40721da177e4SLinus Torvalds 
40731da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
40741da177e4SLinus Torvalds {
40751da177e4SLinus Torvalds 	struct task_security_struct *tsec;
40761da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
40771da177e4SLinus Torvalds 	struct avc_audit_data ad;
40781da177e4SLinus Torvalds 
40791da177e4SLinus Torvalds 	tsec = current->security;
40801da177e4SLinus Torvalds 	isec = msq->q_perm.security;
40811da177e4SLinus Torvalds 
40821da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
40831da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
40861da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
40871da177e4SLinus Torvalds }
40881da177e4SLinus Torvalds 
40891da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
40901da177e4SLinus Torvalds {
40911da177e4SLinus Torvalds 	int err;
40921da177e4SLinus Torvalds 	int perms;
40931da177e4SLinus Torvalds 
40941da177e4SLinus Torvalds 	switch(cmd) {
40951da177e4SLinus Torvalds 	case IPC_INFO:
40961da177e4SLinus Torvalds 	case MSG_INFO:
40971da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
40981da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
40991da177e4SLinus Torvalds 	case IPC_STAT:
41001da177e4SLinus Torvalds 	case MSG_STAT:
41011da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
41021da177e4SLinus Torvalds 		break;
41031da177e4SLinus Torvalds 	case IPC_SET:
41041da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
41051da177e4SLinus Torvalds 		break;
41061da177e4SLinus Torvalds 	case IPC_RMID:
41071da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
41081da177e4SLinus Torvalds 		break;
41091da177e4SLinus Torvalds 	default:
41101da177e4SLinus Torvalds 		return 0;
41111da177e4SLinus Torvalds 	}
41121da177e4SLinus Torvalds 
41136af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
41141da177e4SLinus Torvalds 	return err;
41151da177e4SLinus Torvalds }
41161da177e4SLinus Torvalds 
41171da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
41181da177e4SLinus Torvalds {
41191da177e4SLinus Torvalds 	struct task_security_struct *tsec;
41201da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
41211da177e4SLinus Torvalds 	struct msg_security_struct *msec;
41221da177e4SLinus Torvalds 	struct avc_audit_data ad;
41231da177e4SLinus Torvalds 	int rc;
41241da177e4SLinus Torvalds 
41251da177e4SLinus Torvalds 	tsec = current->security;
41261da177e4SLinus Torvalds 	isec = msq->q_perm.security;
41271da177e4SLinus Torvalds 	msec = msg->security;
41281da177e4SLinus Torvalds 
41291da177e4SLinus Torvalds 	/*
41301da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
41311da177e4SLinus Torvalds 	 */
41321da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
41331da177e4SLinus Torvalds 		/*
41341da177e4SLinus Torvalds 		 * Compute new sid based on current process and
41351da177e4SLinus Torvalds 		 * message queue this message will be stored in
41361da177e4SLinus Torvalds 		 */
41371da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid,
41381da177e4SLinus Torvalds 					     isec->sid,
41391da177e4SLinus Torvalds 					     SECCLASS_MSG,
41401da177e4SLinus Torvalds 					     &msec->sid);
41411da177e4SLinus Torvalds 		if (rc)
41421da177e4SLinus Torvalds 			return rc;
41431da177e4SLinus Torvalds 	}
41441da177e4SLinus Torvalds 
41451da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
41461da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
41471da177e4SLinus Torvalds 
41481da177e4SLinus Torvalds 	/* Can this process write to the queue? */
41491da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
41501da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
41511da177e4SLinus Torvalds 	if (!rc)
41521da177e4SLinus Torvalds 		/* Can this process send the message */
41531da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
41541da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__SEND, &ad);
41551da177e4SLinus Torvalds 	if (!rc)
41561da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
41571da177e4SLinus Torvalds 		rc = avc_has_perm(msec->sid, isec->sid,
41581da177e4SLinus Torvalds 				  SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad);
41591da177e4SLinus Torvalds 
41601da177e4SLinus Torvalds 	return rc;
41611da177e4SLinus Torvalds }
41621da177e4SLinus Torvalds 
41631da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
41641da177e4SLinus Torvalds 				    struct task_struct *target,
41651da177e4SLinus Torvalds 				    long type, int mode)
41661da177e4SLinus Torvalds {
41671da177e4SLinus Torvalds 	struct task_security_struct *tsec;
41681da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
41691da177e4SLinus Torvalds 	struct msg_security_struct *msec;
41701da177e4SLinus Torvalds 	struct avc_audit_data ad;
41711da177e4SLinus Torvalds 	int rc;
41721da177e4SLinus Torvalds 
41731da177e4SLinus Torvalds 	tsec = target->security;
41741da177e4SLinus Torvalds 	isec = msq->q_perm.security;
41751da177e4SLinus Torvalds 	msec = msg->security;
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
41781da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid,
41811da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
41821da177e4SLinus Torvalds 	if (!rc)
41831da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
41841da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
41851da177e4SLinus Torvalds 	return rc;
41861da177e4SLinus Torvalds }
41871da177e4SLinus Torvalds 
41881da177e4SLinus Torvalds /* Shared Memory security operations */
41891da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
41901da177e4SLinus Torvalds {
41911da177e4SLinus Torvalds 	struct task_security_struct *tsec;
41921da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
41931da177e4SLinus Torvalds 	struct avc_audit_data ad;
41941da177e4SLinus Torvalds 	int rc;
41951da177e4SLinus Torvalds 
41961da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
41971da177e4SLinus Torvalds 	if (rc)
41981da177e4SLinus Torvalds 		return rc;
41991da177e4SLinus Torvalds 
42001da177e4SLinus Torvalds 	tsec = current->security;
42011da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
42041da177e4SLinus Torvalds  	ad.u.ipc_id = shp->shm_perm.key;
42051da177e4SLinus Torvalds 
42061da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
42071da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
42081da177e4SLinus Torvalds 	if (rc) {
42091da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
42101da177e4SLinus Torvalds 		return rc;
42111da177e4SLinus Torvalds 	}
42121da177e4SLinus Torvalds 	return 0;
42131da177e4SLinus Torvalds }
42141da177e4SLinus Torvalds 
42151da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
42161da177e4SLinus Torvalds {
42171da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
42181da177e4SLinus Torvalds }
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
42211da177e4SLinus Torvalds {
42221da177e4SLinus Torvalds 	struct task_security_struct *tsec;
42231da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
42241da177e4SLinus Torvalds 	struct avc_audit_data ad;
42251da177e4SLinus Torvalds 
42261da177e4SLinus Torvalds 	tsec = current->security;
42271da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
42281da177e4SLinus Torvalds 
42291da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
42301da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
42311da177e4SLinus Torvalds 
42321da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
42331da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
42341da177e4SLinus Torvalds }
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
42371da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
42381da177e4SLinus Torvalds {
42391da177e4SLinus Torvalds 	int perms;
42401da177e4SLinus Torvalds 	int err;
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds 	switch(cmd) {
42431da177e4SLinus Torvalds 	case IPC_INFO:
42441da177e4SLinus Torvalds 	case SHM_INFO:
42451da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
42461da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
42471da177e4SLinus Torvalds 	case IPC_STAT:
42481da177e4SLinus Torvalds 	case SHM_STAT:
42491da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
42501da177e4SLinus Torvalds 		break;
42511da177e4SLinus Torvalds 	case IPC_SET:
42521da177e4SLinus Torvalds 		perms = SHM__SETATTR;
42531da177e4SLinus Torvalds 		break;
42541da177e4SLinus Torvalds 	case SHM_LOCK:
42551da177e4SLinus Torvalds 	case SHM_UNLOCK:
42561da177e4SLinus Torvalds 		perms = SHM__LOCK;
42571da177e4SLinus Torvalds 		break;
42581da177e4SLinus Torvalds 	case IPC_RMID:
42591da177e4SLinus Torvalds 		perms = SHM__DESTROY;
42601da177e4SLinus Torvalds 		break;
42611da177e4SLinus Torvalds 	default:
42621da177e4SLinus Torvalds 		return 0;
42631da177e4SLinus Torvalds 	}
42641da177e4SLinus Torvalds 
42656af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
42661da177e4SLinus Torvalds 	return err;
42671da177e4SLinus Torvalds }
42681da177e4SLinus Torvalds 
42691da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
42701da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
42711da177e4SLinus Torvalds {
42721da177e4SLinus Torvalds 	u32 perms;
42731da177e4SLinus Torvalds 	int rc;
42741da177e4SLinus Torvalds 
42751da177e4SLinus Torvalds 	rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg);
42761da177e4SLinus Torvalds 	if (rc)
42771da177e4SLinus Torvalds 		return rc;
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
42801da177e4SLinus Torvalds 		perms = SHM__READ;
42811da177e4SLinus Torvalds 	else
42821da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
42831da177e4SLinus Torvalds 
42846af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
42851da177e4SLinus Torvalds }
42861da177e4SLinus Torvalds 
42871da177e4SLinus Torvalds /* Semaphore security operations */
42881da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
42891da177e4SLinus Torvalds {
42901da177e4SLinus Torvalds 	struct task_security_struct *tsec;
42911da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
42921da177e4SLinus Torvalds 	struct avc_audit_data ad;
42931da177e4SLinus Torvalds 	int rc;
42941da177e4SLinus Torvalds 
42951da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
42961da177e4SLinus Torvalds 	if (rc)
42971da177e4SLinus Torvalds 		return rc;
42981da177e4SLinus Torvalds 
42991da177e4SLinus Torvalds 	tsec = current->security;
43001da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
43011da177e4SLinus Torvalds 
43021da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
43031da177e4SLinus Torvalds  	ad.u.ipc_id = sma->sem_perm.key;
43041da177e4SLinus Torvalds 
43051da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
43061da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
43071da177e4SLinus Torvalds 	if (rc) {
43081da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
43091da177e4SLinus Torvalds 		return rc;
43101da177e4SLinus Torvalds 	}
43111da177e4SLinus Torvalds 	return 0;
43121da177e4SLinus Torvalds }
43131da177e4SLinus Torvalds 
43141da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
43151da177e4SLinus Torvalds {
43161da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
43171da177e4SLinus Torvalds }
43181da177e4SLinus Torvalds 
43191da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
43201da177e4SLinus Torvalds {
43211da177e4SLinus Torvalds 	struct task_security_struct *tsec;
43221da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
43231da177e4SLinus Torvalds 	struct avc_audit_data ad;
43241da177e4SLinus Torvalds 
43251da177e4SLinus Torvalds 	tsec = current->security;
43261da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
43271da177e4SLinus Torvalds 
43281da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
43291da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
43301da177e4SLinus Torvalds 
43311da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
43321da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
43331da177e4SLinus Torvalds }
43341da177e4SLinus Torvalds 
43351da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
43361da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
43371da177e4SLinus Torvalds {
43381da177e4SLinus Torvalds 	int err;
43391da177e4SLinus Torvalds 	u32 perms;
43401da177e4SLinus Torvalds 
43411da177e4SLinus Torvalds 	switch(cmd) {
43421da177e4SLinus Torvalds 	case IPC_INFO:
43431da177e4SLinus Torvalds 	case SEM_INFO:
43441da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
43451da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
43461da177e4SLinus Torvalds 	case GETPID:
43471da177e4SLinus Torvalds 	case GETNCNT:
43481da177e4SLinus Torvalds 	case GETZCNT:
43491da177e4SLinus Torvalds 		perms = SEM__GETATTR;
43501da177e4SLinus Torvalds 		break;
43511da177e4SLinus Torvalds 	case GETVAL:
43521da177e4SLinus Torvalds 	case GETALL:
43531da177e4SLinus Torvalds 		perms = SEM__READ;
43541da177e4SLinus Torvalds 		break;
43551da177e4SLinus Torvalds 	case SETVAL:
43561da177e4SLinus Torvalds 	case SETALL:
43571da177e4SLinus Torvalds 		perms = SEM__WRITE;
43581da177e4SLinus Torvalds 		break;
43591da177e4SLinus Torvalds 	case IPC_RMID:
43601da177e4SLinus Torvalds 		perms = SEM__DESTROY;
43611da177e4SLinus Torvalds 		break;
43621da177e4SLinus Torvalds 	case IPC_SET:
43631da177e4SLinus Torvalds 		perms = SEM__SETATTR;
43641da177e4SLinus Torvalds 		break;
43651da177e4SLinus Torvalds 	case IPC_STAT:
43661da177e4SLinus Torvalds 	case SEM_STAT:
43671da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
43681da177e4SLinus Torvalds 		break;
43691da177e4SLinus Torvalds 	default:
43701da177e4SLinus Torvalds 		return 0;
43711da177e4SLinus Torvalds 	}
43721da177e4SLinus Torvalds 
43736af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
43741da177e4SLinus Torvalds 	return err;
43751da177e4SLinus Torvalds }
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
43781da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
43791da177e4SLinus Torvalds {
43801da177e4SLinus Torvalds 	u32 perms;
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds 	if (alter)
43831da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
43841da177e4SLinus Torvalds 	else
43851da177e4SLinus Torvalds 		perms = SEM__READ;
43861da177e4SLinus Torvalds 
43876af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
43881da177e4SLinus Torvalds }
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
43911da177e4SLinus Torvalds {
43921da177e4SLinus Torvalds 	u32 av = 0;
43931da177e4SLinus Torvalds 
43941da177e4SLinus Torvalds 	av = 0;
43951da177e4SLinus Torvalds 	if (flag & S_IRUGO)
43961da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
43971da177e4SLinus Torvalds 	if (flag & S_IWUGO)
43981da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
43991da177e4SLinus Torvalds 
44001da177e4SLinus Torvalds 	if (av == 0)
44011da177e4SLinus Torvalds 		return 0;
44021da177e4SLinus Torvalds 
44036af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
44041da177e4SLinus Torvalds }
44051da177e4SLinus Torvalds 
44061da177e4SLinus Torvalds /* module stacking operations */
44071da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops)
44081da177e4SLinus Torvalds {
44091da177e4SLinus Torvalds 	if (secondary_ops != original_ops) {
44101da177e4SLinus Torvalds 		printk(KERN_INFO "%s:  There is already a secondary security "
44111da177e4SLinus Torvalds 		       "module registered.\n", __FUNCTION__);
44121da177e4SLinus Torvalds 		return -EINVAL;
44131da177e4SLinus Torvalds  	}
44141da177e4SLinus Torvalds 
44151da177e4SLinus Torvalds 	secondary_ops = ops;
44161da177e4SLinus Torvalds 
44171da177e4SLinus Torvalds 	printk(KERN_INFO "%s:  Registering secondary module %s\n",
44181da177e4SLinus Torvalds 	       __FUNCTION__,
44191da177e4SLinus Torvalds 	       name);
44201da177e4SLinus Torvalds 
44211da177e4SLinus Torvalds 	return 0;
44221da177e4SLinus Torvalds }
44231da177e4SLinus Torvalds 
44241da177e4SLinus Torvalds static int selinux_unregister_security (const char *name, struct security_operations *ops)
44251da177e4SLinus Torvalds {
44261da177e4SLinus Torvalds 	if (ops != secondary_ops) {
44271da177e4SLinus Torvalds 		printk (KERN_INFO "%s:  trying to unregister a security module "
44281da177e4SLinus Torvalds 		        "that is not registered.\n", __FUNCTION__);
44291da177e4SLinus Torvalds 		return -EINVAL;
44301da177e4SLinus Torvalds 	}
44311da177e4SLinus Torvalds 
44321da177e4SLinus Torvalds 	secondary_ops = original_ops;
44331da177e4SLinus Torvalds 
44341da177e4SLinus Torvalds 	return 0;
44351da177e4SLinus Torvalds }
44361da177e4SLinus Torvalds 
44371da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode)
44381da177e4SLinus Torvalds {
44391da177e4SLinus Torvalds 	if (inode)
44401da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
44411da177e4SLinus Torvalds }
44421da177e4SLinus Torvalds 
44431da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
44441da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
44451da177e4SLinus Torvalds {
44461da177e4SLinus Torvalds 	struct task_security_struct *tsec;
44478c8570fbSDustin Kirkland 	u32 sid;
44481da177e4SLinus Torvalds 	int error;
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds 	if (current != p) {
44511da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__GETATTR);
44521da177e4SLinus Torvalds 		if (error)
44531da177e4SLinus Torvalds 			return error;
44541da177e4SLinus Torvalds 	}
44551da177e4SLinus Torvalds 
44561da177e4SLinus Torvalds 	tsec = p->security;
44571da177e4SLinus Torvalds 
44581da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
44591da177e4SLinus Torvalds 		sid = tsec->sid;
44601da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
44611da177e4SLinus Torvalds 		sid = tsec->osid;
44621da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
44631da177e4SLinus Torvalds 		sid = tsec->exec_sid;
44641da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
44651da177e4SLinus Torvalds 		sid = tsec->create_sid;
44664eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
44674eb582cfSMichael LeMay 		sid = tsec->keycreate_sid;
446842c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
446942c3e03eSEric Paris 		sid = tsec->sockcreate_sid;
44701da177e4SLinus Torvalds 	else
44711da177e4SLinus Torvalds 		return -EINVAL;
44721da177e4SLinus Torvalds 
44731da177e4SLinus Torvalds 	if (!sid)
44741da177e4SLinus Torvalds 		return 0;
44751da177e4SLinus Torvalds 
44768c8570fbSDustin Kirkland 	return selinux_getsecurity(sid, value, size);
44771da177e4SLinus Torvalds }
44781da177e4SLinus Torvalds 
44791da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
44801da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
44811da177e4SLinus Torvalds {
44821da177e4SLinus Torvalds 	struct task_security_struct *tsec;
44831da177e4SLinus Torvalds 	u32 sid = 0;
44841da177e4SLinus Torvalds 	int error;
44851da177e4SLinus Torvalds 	char *str = value;
44861da177e4SLinus Torvalds 
44871da177e4SLinus Torvalds 	if (current != p) {
44881da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
44891da177e4SLinus Torvalds 		   security attributes. */
44901da177e4SLinus Torvalds 		return -EACCES;
44911da177e4SLinus Torvalds 	}
44921da177e4SLinus Torvalds 
44931da177e4SLinus Torvalds 	/*
44941da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
44951da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
44961da177e4SLinus Torvalds 	 * above restriction is ever removed.
44971da177e4SLinus Torvalds 	 */
44981da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
44991da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETEXEC);
45001da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
45011da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETFSCREATE);
45024eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate"))
45034eb582cfSMichael LeMay 		error = task_has_perm(current, p, PROCESS__SETKEYCREATE);
450442c3e03eSEric Paris 	else if (!strcmp(name, "sockcreate"))
450542c3e03eSEric Paris 		error = task_has_perm(current, p, PROCESS__SETSOCKCREATE);
45061da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
45071da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETCURRENT);
45081da177e4SLinus Torvalds 	else
45091da177e4SLinus Torvalds 		error = -EINVAL;
45101da177e4SLinus Torvalds 	if (error)
45111da177e4SLinus Torvalds 		return error;
45121da177e4SLinus Torvalds 
45131da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
45141da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
45151da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
45161da177e4SLinus Torvalds 			str[size-1] = 0;
45171da177e4SLinus Torvalds 			size--;
45181da177e4SLinus Torvalds 		}
45191da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
45201da177e4SLinus Torvalds 		if (error)
45211da177e4SLinus Torvalds 			return error;
45221da177e4SLinus Torvalds 	}
45231da177e4SLinus Torvalds 
45241da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
45251da177e4SLinus Torvalds 	   performed during the actual operation (execve,
45261da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
45271da177e4SLinus Torvalds 	   operation.  See selinux_bprm_set_security for the execve
45281da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
45291da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
45301da177e4SLinus Torvalds 	tsec = p->security;
45311da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
45321da177e4SLinus Torvalds 		tsec->exec_sid = sid;
45331da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
45341da177e4SLinus Torvalds 		tsec->create_sid = sid;
45354eb582cfSMichael LeMay 	else if (!strcmp(name, "keycreate")) {
45364eb582cfSMichael LeMay 		error = may_create_key(sid, p);
45374eb582cfSMichael LeMay 		if (error)
45384eb582cfSMichael LeMay 			return error;
45394eb582cfSMichael LeMay 		tsec->keycreate_sid = sid;
454042c3e03eSEric Paris 	} else if (!strcmp(name, "sockcreate"))
454142c3e03eSEric Paris 		tsec->sockcreate_sid = sid;
454242c3e03eSEric Paris 	else if (!strcmp(name, "current")) {
45431da177e4SLinus Torvalds 		struct av_decision avd;
45441da177e4SLinus Torvalds 
45451da177e4SLinus Torvalds 		if (sid == 0)
45461da177e4SLinus Torvalds 			return -EINVAL;
45471da177e4SLinus Torvalds 
45481da177e4SLinus Torvalds 		/* Only allow single threaded processes to change context */
45491da177e4SLinus Torvalds 		if (atomic_read(&p->mm->mm_users) != 1) {
45501da177e4SLinus Torvalds 			struct task_struct *g, *t;
45511da177e4SLinus Torvalds 			struct mm_struct *mm = p->mm;
45521da177e4SLinus Torvalds 			read_lock(&tasklist_lock);
45531da177e4SLinus Torvalds 			do_each_thread(g, t)
45541da177e4SLinus Torvalds 				if (t->mm == mm && t != p) {
45551da177e4SLinus Torvalds 					read_unlock(&tasklist_lock);
45561da177e4SLinus Torvalds 					return -EPERM;
45571da177e4SLinus Torvalds 				}
45581da177e4SLinus Torvalds 			while_each_thread(g, t);
45591da177e4SLinus Torvalds 			read_unlock(&tasklist_lock);
45601da177e4SLinus Torvalds                 }
45611da177e4SLinus Torvalds 
45621da177e4SLinus Torvalds 		/* Check permissions for the transition. */
45631da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
45641da177e4SLinus Torvalds 		                     PROCESS__DYNTRANSITION, NULL);
45651da177e4SLinus Torvalds 		if (error)
45661da177e4SLinus Torvalds 			return error;
45671da177e4SLinus Torvalds 
45681da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
45691da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
45701da177e4SLinus Torvalds 		task_lock(p);
45711da177e4SLinus Torvalds 		if (p->ptrace & PT_PTRACED) {
45721da177e4SLinus Torvalds 			error = avc_has_perm_noaudit(tsec->ptrace_sid, sid,
45731da177e4SLinus Torvalds 						     SECCLASS_PROCESS,
45741da177e4SLinus Torvalds 						     PROCESS__PTRACE, &avd);
45751da177e4SLinus Torvalds 			if (!error)
45761da177e4SLinus Torvalds 				tsec->sid = sid;
45771da177e4SLinus Torvalds 			task_unlock(p);
45781da177e4SLinus Torvalds 			avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS,
45791da177e4SLinus Torvalds 				  PROCESS__PTRACE, &avd, error, NULL);
45801da177e4SLinus Torvalds 			if (error)
45811da177e4SLinus Torvalds 				return error;
45821da177e4SLinus Torvalds 		} else {
45831da177e4SLinus Torvalds 			tsec->sid = sid;
45841da177e4SLinus Torvalds 			task_unlock(p);
45851da177e4SLinus Torvalds 		}
45861da177e4SLinus Torvalds 	}
45871da177e4SLinus Torvalds 	else
45881da177e4SLinus Torvalds 		return -EINVAL;
45891da177e4SLinus Torvalds 
45901da177e4SLinus Torvalds 	return size;
45911da177e4SLinus Torvalds }
45921da177e4SLinus Torvalds 
4593dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4594dc49c1f9SCatherine Zhang {
4595dc49c1f9SCatherine Zhang 	return security_sid_to_context(secid, secdata, seclen);
4596dc49c1f9SCatherine Zhang }
4597dc49c1f9SCatherine Zhang 
4598dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen)
4599dc49c1f9SCatherine Zhang {
4600dc49c1f9SCatherine Zhang 	if (secdata)
4601dc49c1f9SCatherine Zhang 		kfree(secdata);
4602dc49c1f9SCatherine Zhang }
4603dc49c1f9SCatherine Zhang 
4604d720024eSMichael LeMay #ifdef CONFIG_KEYS
4605d720024eSMichael LeMay 
46067e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk,
46077e047ef5SDavid Howells 			     unsigned long flags)
4608d720024eSMichael LeMay {
4609d720024eSMichael LeMay 	struct task_security_struct *tsec = tsk->security;
4610d720024eSMichael LeMay 	struct key_security_struct *ksec;
4611d720024eSMichael LeMay 
4612d720024eSMichael LeMay 	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
4613d720024eSMichael LeMay 	if (!ksec)
4614d720024eSMichael LeMay 		return -ENOMEM;
4615d720024eSMichael LeMay 
4616d720024eSMichael LeMay 	ksec->obj = k;
46174eb582cfSMichael LeMay 	if (tsec->keycreate_sid)
46184eb582cfSMichael LeMay 		ksec->sid = tsec->keycreate_sid;
46194eb582cfSMichael LeMay 	else
4620d720024eSMichael LeMay 		ksec->sid = tsec->sid;
4621d720024eSMichael LeMay 	k->security = ksec;
4622d720024eSMichael LeMay 
4623d720024eSMichael LeMay 	return 0;
4624d720024eSMichael LeMay }
4625d720024eSMichael LeMay 
4626d720024eSMichael LeMay static void selinux_key_free(struct key *k)
4627d720024eSMichael LeMay {
4628d720024eSMichael LeMay 	struct key_security_struct *ksec = k->security;
4629d720024eSMichael LeMay 
4630d720024eSMichael LeMay 	k->security = NULL;
4631d720024eSMichael LeMay 	kfree(ksec);
4632d720024eSMichael LeMay }
4633d720024eSMichael LeMay 
4634d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref,
4635d720024eSMichael LeMay 			    struct task_struct *ctx,
4636d720024eSMichael LeMay 			    key_perm_t perm)
4637d720024eSMichael LeMay {
4638d720024eSMichael LeMay 	struct key *key;
4639d720024eSMichael LeMay 	struct task_security_struct *tsec;
4640d720024eSMichael LeMay 	struct key_security_struct *ksec;
4641d720024eSMichael LeMay 
4642d720024eSMichael LeMay 	key = key_ref_to_ptr(key_ref);
4643d720024eSMichael LeMay 
4644d720024eSMichael LeMay 	tsec = ctx->security;
4645d720024eSMichael LeMay 	ksec = key->security;
4646d720024eSMichael LeMay 
4647d720024eSMichael LeMay 	/* if no specific permissions are requested, we skip the
4648d720024eSMichael LeMay 	   permission check. No serious, additional covert channels
4649d720024eSMichael LeMay 	   appear to be created. */
4650d720024eSMichael LeMay 	if (perm == 0)
4651d720024eSMichael LeMay 		return 0;
4652d720024eSMichael LeMay 
4653d720024eSMichael LeMay 	return avc_has_perm(tsec->sid, ksec->sid,
4654d720024eSMichael LeMay 			    SECCLASS_KEY, perm, NULL);
4655d720024eSMichael LeMay }
4656d720024eSMichael LeMay 
4657d720024eSMichael LeMay #endif
4658d720024eSMichael LeMay 
46591da177e4SLinus Torvalds static struct security_operations selinux_ops = {
46601da177e4SLinus Torvalds 	.ptrace =			selinux_ptrace,
46611da177e4SLinus Torvalds 	.capget =			selinux_capget,
46621da177e4SLinus Torvalds 	.capset_check =			selinux_capset_check,
46631da177e4SLinus Torvalds 	.capset_set =			selinux_capset_set,
46641da177e4SLinus Torvalds 	.sysctl =			selinux_sysctl,
46651da177e4SLinus Torvalds 	.capable =			selinux_capable,
46661da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
46671da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
46681da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
46691da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
46701da177e4SLinus Torvalds 
46711da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
46721da177e4SLinus Torvalds         .netlink_recv =			selinux_netlink_recv,
46731da177e4SLinus Torvalds 
46741da177e4SLinus Torvalds 	.bprm_alloc_security =		selinux_bprm_alloc_security,
46751da177e4SLinus Torvalds 	.bprm_free_security =		selinux_bprm_free_security,
46761da177e4SLinus Torvalds 	.bprm_apply_creds =		selinux_bprm_apply_creds,
46771da177e4SLinus Torvalds 	.bprm_post_apply_creds =	selinux_bprm_post_apply_creds,
46781da177e4SLinus Torvalds 	.bprm_set_security =		selinux_bprm_set_security,
46791da177e4SLinus Torvalds 	.bprm_check_security =		selinux_bprm_check_security,
46801da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
46811da177e4SLinus Torvalds 
46821da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
46831da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
46841da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
46851da177e4SLinus Torvalds 	.sb_kern_mount =	        selinux_sb_kern_mount,
46861da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
46871da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
46881da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
46891da177e4SLinus Torvalds 
46901da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
46911da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
46925e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
46931da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
46941da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
46951da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
46961da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
46971da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
46981da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
46991da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
47001da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
47011da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
47021da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
47031da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
47041da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
47051da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
47061da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
47071da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
47081da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
47091da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
47101da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
47118c8570fbSDustin Kirkland 	.inode_xattr_getsuffix =        selinux_inode_xattr_getsuffix,
47121da177e4SLinus Torvalds 	.inode_getsecurity =            selinux_inode_getsecurity,
47131da177e4SLinus Torvalds 	.inode_setsecurity =            selinux_inode_setsecurity,
47141da177e4SLinus Torvalds 	.inode_listsecurity =           selinux_inode_listsecurity,
47151da177e4SLinus Torvalds 
47161da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
47171da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
47181da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
47191da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
47201da177e4SLinus Torvalds 	.file_mmap =			selinux_file_mmap,
47211da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
47221da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
47231da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
47241da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
47251da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
47261da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
47271da177e4SLinus Torvalds 
47281da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
47291da177e4SLinus Torvalds 	.task_alloc_security =		selinux_task_alloc_security,
47301da177e4SLinus Torvalds 	.task_free_security =		selinux_task_free_security,
47311da177e4SLinus Torvalds 	.task_setuid =			selinux_task_setuid,
47321da177e4SLinus Torvalds 	.task_post_setuid =		selinux_task_post_setuid,
47331da177e4SLinus Torvalds 	.task_setgid =			selinux_task_setgid,
47341da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
47351da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
47361da177e4SLinus Torvalds 	.task_getsid =		        selinux_task_getsid,
4737f9008e4cSDavid Quigley 	.task_getsecid =		selinux_task_getsecid,
47381da177e4SLinus Torvalds 	.task_setgroups =		selinux_task_setgroups,
47391da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
474003e68060SJames Morris 	.task_setioprio =		selinux_task_setioprio,
4741a1836a42SDavid Quigley 	.task_getioprio =		selinux_task_getioprio,
47421da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
47431da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
47441da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
474535601547SDavid Quigley 	.task_movememory =		selinux_task_movememory,
47461da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
47471da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
47481da177e4SLinus Torvalds 	.task_prctl =			selinux_task_prctl,
47491da177e4SLinus Torvalds 	.task_reparent_to_init =	selinux_task_reparent_to_init,
47501da177e4SLinus Torvalds 	.task_to_inode =                selinux_task_to_inode,
47511da177e4SLinus Torvalds 
47521da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
47531da177e4SLinus Torvalds 
47541da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
47551da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
47561da177e4SLinus Torvalds 
47571da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
47581da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
47591da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
47601da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
47611da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
47621da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
47631da177e4SLinus Torvalds 
47641da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
47651da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
47661da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
47671da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
47681da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
47691da177e4SLinus Torvalds 
47701da177e4SLinus Torvalds 	.sem_alloc_security = 		selinux_sem_alloc_security,
47711da177e4SLinus Torvalds 	.sem_free_security =  		selinux_sem_free_security,
47721da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
47731da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
47741da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
47751da177e4SLinus Torvalds 
47761da177e4SLinus Torvalds 	.register_security =		selinux_register_security,
47771da177e4SLinus Torvalds 	.unregister_security =		selinux_unregister_security,
47781da177e4SLinus Torvalds 
47791da177e4SLinus Torvalds 	.d_instantiate =                selinux_d_instantiate,
47801da177e4SLinus Torvalds 
47811da177e4SLinus Torvalds 	.getprocattr =                  selinux_getprocattr,
47821da177e4SLinus Torvalds 	.setprocattr =                  selinux_setprocattr,
47831da177e4SLinus Torvalds 
4784dc49c1f9SCatherine Zhang 	.secid_to_secctx =		selinux_secid_to_secctx,
4785dc49c1f9SCatherine Zhang 	.release_secctx =		selinux_release_secctx,
4786dc49c1f9SCatherine Zhang 
47871da177e4SLinus Torvalds         .unix_stream_connect =		selinux_socket_unix_stream_connect,
47881da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
47891da177e4SLinus Torvalds 
47901da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
47911da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
47921da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
47931da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
47941da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
47951da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
47961da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
47971da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
47981da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
47991da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
48001da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
48011da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
48021da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
48031da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
48042c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
48052c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
48061da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
48071da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
4808892c141eSVenkat Yekkirala 	.sk_clone_security =		selinux_sk_clone_security,
4809beb8d13bSVenkat Yekkirala 	.sk_getsecid = 			selinux_sk_getsecid,
48104237c75cSVenkat Yekkirala 	.sock_graft =			selinux_sock_graft,
48114237c75cSVenkat Yekkirala 	.inet_conn_request =		selinux_inet_conn_request,
48124237c75cSVenkat Yekkirala 	.inet_csk_clone =		selinux_inet_csk_clone,
48136b877699SVenkat Yekkirala 	.inet_conn_established =	selinux_inet_conn_established,
48144237c75cSVenkat Yekkirala 	.req_classify_flow =		selinux_req_classify_flow,
4815d28d1e08STrent Jaeger 
4816d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
4817d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
4818d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
4819d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
4820c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
4821d28d1e08STrent Jaeger 	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
4822d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
4823c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
4824d28d1e08STrent Jaeger 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
4825e0d1caa7SVenkat Yekkirala 	.xfrm_state_pol_flow_match =	selinux_xfrm_state_pol_flow_match,
4826e0d1caa7SVenkat Yekkirala 	.xfrm_decode_session =		selinux_xfrm_decode_session,
48271da177e4SLinus Torvalds #endif
4828d720024eSMichael LeMay 
4829d720024eSMichael LeMay #ifdef CONFIG_KEYS
4830d720024eSMichael LeMay 	.key_alloc =                    selinux_key_alloc,
4831d720024eSMichael LeMay 	.key_free =                     selinux_key_free,
4832d720024eSMichael LeMay 	.key_permission =               selinux_key_permission,
4833d720024eSMichael LeMay #endif
48341da177e4SLinus Torvalds };
48351da177e4SLinus Torvalds 
48361da177e4SLinus Torvalds static __init int selinux_init(void)
48371da177e4SLinus Torvalds {
48381da177e4SLinus Torvalds 	struct task_security_struct *tsec;
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds 	if (!selinux_enabled) {
48411da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
48421da177e4SLinus Torvalds 		return 0;
48431da177e4SLinus Torvalds 	}
48441da177e4SLinus Torvalds 
48451da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
48461da177e4SLinus Torvalds 
48471da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
48481da177e4SLinus Torvalds 	if (task_alloc_security(current))
48491da177e4SLinus Torvalds 		panic("SELinux:  Failed to initialize initial task.\n");
48501da177e4SLinus Torvalds 	tsec = current->security;
48511da177e4SLinus Torvalds 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
48521da177e4SLinus Torvalds 
48537cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
48547cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
48557cae7e26SJames Morris 					    0, SLAB_PANIC, NULL, NULL);
48561da177e4SLinus Torvalds 	avc_init();
48571da177e4SLinus Torvalds 
48581da177e4SLinus Torvalds 	original_ops = secondary_ops = security_ops;
48591da177e4SLinus Torvalds 	if (!secondary_ops)
48601da177e4SLinus Torvalds 		panic ("SELinux: No initial security operations\n");
48611da177e4SLinus Torvalds 	if (register_security (&selinux_ops))
48621da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
48631da177e4SLinus Torvalds 
48641da177e4SLinus Torvalds 	if (selinux_enforcing) {
48651da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Starting in enforcing mode\n");
48661da177e4SLinus Torvalds 	} else {
48671da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
48681da177e4SLinus Torvalds 	}
4869d720024eSMichael LeMay 
4870d720024eSMichael LeMay #ifdef CONFIG_KEYS
4871d720024eSMichael LeMay 	/* Add security information to initial keyrings */
48724eb582cfSMichael LeMay 	selinux_key_alloc(&root_user_keyring, current,
48737e047ef5SDavid Howells 			  KEY_ALLOC_NOT_IN_QUOTA);
48744eb582cfSMichael LeMay 	selinux_key_alloc(&root_session_keyring, current,
48757e047ef5SDavid Howells 			  KEY_ALLOC_NOT_IN_QUOTA);
4876d720024eSMichael LeMay #endif
4877d720024eSMichael LeMay 
48781da177e4SLinus Torvalds 	return 0;
48791da177e4SLinus Torvalds }
48801da177e4SLinus Torvalds 
48811da177e4SLinus Torvalds void selinux_complete_init(void)
48821da177e4SLinus Torvalds {
48831da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Completing initialization.\n");
48841da177e4SLinus Torvalds 
48851da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
48861da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Setting up existing superblocks.\n");
4887ba0c19edSStephen Smalley 	spin_lock(&sb_lock);
48881da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
48891da177e4SLinus Torvalds next_sb:
48901da177e4SLinus Torvalds 	if (!list_empty(&superblock_security_head)) {
48911da177e4SLinus Torvalds 		struct superblock_security_struct *sbsec =
48921da177e4SLinus Torvalds 				list_entry(superblock_security_head.next,
48931da177e4SLinus Torvalds 				           struct superblock_security_struct,
48941da177e4SLinus Torvalds 				           list);
48951da177e4SLinus Torvalds 		struct super_block *sb = sbsec->sb;
48961da177e4SLinus Torvalds 		sb->s_count++;
48971da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
4898ba0c19edSStephen Smalley 		spin_unlock(&sb_lock);
48991da177e4SLinus Torvalds 		down_read(&sb->s_umount);
49001da177e4SLinus Torvalds 		if (sb->s_root)
49011da177e4SLinus Torvalds 			superblock_doinit(sb, NULL);
49021da177e4SLinus Torvalds 		drop_super(sb);
4903ba0c19edSStephen Smalley 		spin_lock(&sb_lock);
49041da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
49051da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
49061da177e4SLinus Torvalds 		goto next_sb;
49071da177e4SLinus Torvalds 	}
49081da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
4909ba0c19edSStephen Smalley 	spin_unlock(&sb_lock);
49101da177e4SLinus Torvalds }
49111da177e4SLinus Torvalds 
49121da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
49131da177e4SLinus Torvalds    all processes and objects when they are created. */
49141da177e4SLinus Torvalds security_initcall(selinux_init);
49151da177e4SLinus Torvalds 
4916c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
49171da177e4SLinus Torvalds 
49181da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv4_op = {
49191da177e4SLinus Torvalds 	.hook =		selinux_ipv4_postroute_last,
49201da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
49211da177e4SLinus Torvalds 	.pf =		PF_INET,
49221da177e4SLinus Torvalds 	.hooknum =	NF_IP_POST_ROUTING,
49231da177e4SLinus Torvalds 	.priority =	NF_IP_PRI_SELINUX_LAST,
49241da177e4SLinus Torvalds };
49251da177e4SLinus Torvalds 
49261da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
49271da177e4SLinus Torvalds 
49281da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv6_op = {
49291da177e4SLinus Torvalds 	.hook =		selinux_ipv6_postroute_last,
49301da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
49311da177e4SLinus Torvalds 	.pf =		PF_INET6,
49321da177e4SLinus Torvalds 	.hooknum =	NF_IP6_POST_ROUTING,
49331da177e4SLinus Torvalds 	.priority =	NF_IP6_PRI_SELINUX_LAST,
49341da177e4SLinus Torvalds };
49351da177e4SLinus Torvalds 
49361da177e4SLinus Torvalds #endif	/* IPV6 */
49371da177e4SLinus Torvalds 
49381da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
49391da177e4SLinus Torvalds {
49401da177e4SLinus Torvalds 	int err = 0;
49411da177e4SLinus Torvalds 
49421da177e4SLinus Torvalds 	if (!selinux_enabled)
49431da177e4SLinus Torvalds 		goto out;
49441da177e4SLinus Torvalds 
49451da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Registering netfilter hooks\n");
49461da177e4SLinus Torvalds 
49471da177e4SLinus Torvalds 	err = nf_register_hook(&selinux_ipv4_op);
49481da177e4SLinus Torvalds 	if (err)
49491da177e4SLinus Torvalds 		panic("SELinux: nf_register_hook for IPv4: error %d\n", err);
49501da177e4SLinus Torvalds 
49511da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
49521da177e4SLinus Torvalds 
49531da177e4SLinus Torvalds 	err = nf_register_hook(&selinux_ipv6_op);
49541da177e4SLinus Torvalds 	if (err)
49551da177e4SLinus Torvalds 		panic("SELinux: nf_register_hook for IPv6: error %d\n", err);
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds #endif	/* IPV6 */
4958d28d1e08STrent Jaeger 
49591da177e4SLinus Torvalds out:
49601da177e4SLinus Torvalds 	return err;
49611da177e4SLinus Torvalds }
49621da177e4SLinus Torvalds 
49631da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
49641da177e4SLinus Torvalds 
49651da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
49661da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
49671da177e4SLinus Torvalds {
49681da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Unregistering netfilter hooks\n");
49691da177e4SLinus Torvalds 
49701da177e4SLinus Torvalds 	nf_unregister_hook(&selinux_ipv4_op);
49711da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
49721da177e4SLinus Torvalds 	nf_unregister_hook(&selinux_ipv6_op);
49731da177e4SLinus Torvalds #endif	/* IPV6 */
49741da177e4SLinus Torvalds }
49751da177e4SLinus Torvalds #endif
49761da177e4SLinus Torvalds 
4977c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
49781da177e4SLinus Torvalds 
49791da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
49801da177e4SLinus Torvalds #define selinux_nf_ip_exit()
49811da177e4SLinus Torvalds #endif
49821da177e4SLinus Torvalds 
4983c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
49841da177e4SLinus Torvalds 
49851da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
49861da177e4SLinus Torvalds int selinux_disable(void)
49871da177e4SLinus Torvalds {
49881da177e4SLinus Torvalds 	extern void exit_sel_fs(void);
49891da177e4SLinus Torvalds 	static int selinux_disabled = 0;
49901da177e4SLinus Torvalds 
49911da177e4SLinus Torvalds 	if (ss_initialized) {
49921da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
49931da177e4SLinus Torvalds 		return -EINVAL;
49941da177e4SLinus Torvalds 	}
49951da177e4SLinus Torvalds 
49961da177e4SLinus Torvalds 	if (selinux_disabled) {
49971da177e4SLinus Torvalds 		/* Only do this once. */
49981da177e4SLinus Torvalds 		return -EINVAL;
49991da177e4SLinus Torvalds 	}
50001da177e4SLinus Torvalds 
50011da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
50021da177e4SLinus Torvalds 
50031da177e4SLinus Torvalds 	selinux_disabled = 1;
500430d55280SStephen Smalley 	selinux_enabled = 0;
50051da177e4SLinus Torvalds 
50061da177e4SLinus Torvalds 	/* Reset security_ops to the secondary module, dummy or capability. */
50071da177e4SLinus Torvalds 	security_ops = secondary_ops;
50081da177e4SLinus Torvalds 
50091da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
50101da177e4SLinus Torvalds 	selinux_nf_ip_exit();
50111da177e4SLinus Torvalds 
50121da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
50131da177e4SLinus Torvalds 	exit_sel_fs();
50141da177e4SLinus Torvalds 
50151da177e4SLinus Torvalds 	return 0;
50161da177e4SLinus Torvalds }
50171da177e4SLinus Torvalds #endif
50181da177e4SLinus Torvalds 
50191da177e4SLinus Torvalds 
5020